1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /**
  27 *
  28 * @author SAAJ RI Development Team
  29 */
  30 package com.sun.xml.internal.messaging.saaj.util;
  31 
  32 import java.util.Iterator;
  33 import java.util.NoSuchElementException;
  34 
  35 import org.w3c.dom.*;
  36 
  37 public class NamespaceContextIterator implements Iterator {
  38     Node context;
  39     NamedNodeMap attributes = null;
  40     int attributesLength;
  41     int attributeIndex;
  42     Attr next = null;
  43     Attr last = null;
  44     boolean traverseStack = true;
  45 
  46     public NamespaceContextIterator(Node context) {
  47         this.context = context;
  48         findContextAttributes();
  49     }
  50 
  51     public NamespaceContextIterator(Node context, boolean traverseStack) {
  52         this(context);
  53         this.traverseStack = traverseStack;
  54     }
  55 
  56     protected void findContextAttributes() {
  57         while (context != null) {
  58             int type = context.getNodeType();
  59             if (type == Node.ELEMENT_NODE) {
  60                 attributes = context.getAttributes();
  61                 attributesLength = attributes.getLength();
  62                 attributeIndex = 0;
  63                 return;
  64             } else {
  65                 context = null;
  66             }
  67         }
  68     }
  69 
  70     protected void findNext() {
  71         while (next == null && context != null) {
  72             for (; attributeIndex < attributesLength; ++attributeIndex) {
  73                 Node currentAttribute = attributes.item(attributeIndex);
  74                 String attributeName = currentAttribute.getNodeName();
  75                 if (attributeName.startsWith("xmlns")
  76                     && (attributeName.length() == 5
  77                         || attributeName.charAt(5) == ':')) {
  78                     next = (Attr) currentAttribute;
  79                     ++attributeIndex;
  80                     return;
  81                 }
  82             }
  83             if (traverseStack) {
  84                 context = context.getParentNode();
  85                 findContextAttributes();
  86             } else {
  87                 context = null;
  88             }
  89         }
  90     }
  91 
  92     public boolean hasNext() {
  93         findNext();
  94         return next != null;
  95     }
  96 
  97     public Object next() {
  98         return getNext();
  99     }
 100 
 101     public Attr nextNamespaceAttr() {
 102         return getNext();
 103     }
 104 
 105     protected Attr getNext() {
 106         findNext();
 107         if (next == null) {
 108             throw new NoSuchElementException();
 109         }
 110         last = next;
 111         next = null;
 112         return last;
 113     }
 114 
 115     public void remove() {
 116         if (last == null) {
 117             throw new IllegalStateException();
 118         }
 119         ((Element) context).removeAttributeNode(last);
 120     }
 121 
 122 }