aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * aoqi@0: * @author SAAJ RI Development Team aoqi@0: */ aoqi@0: package com.sun.xml.internal.messaging.saaj.util; aoqi@0: aoqi@0: import java.util.Iterator; aoqi@0: import java.util.NoSuchElementException; aoqi@0: aoqi@0: import org.w3c.dom.*; aoqi@0: aoqi@0: public class NamespaceContextIterator implements Iterator { aoqi@0: Node context; aoqi@0: NamedNodeMap attributes = null; aoqi@0: int attributesLength; aoqi@0: int attributeIndex; aoqi@0: Attr next = null; aoqi@0: Attr last = null; aoqi@0: boolean traverseStack = true; aoqi@0: aoqi@0: public NamespaceContextIterator(Node context) { aoqi@0: this.context = context; aoqi@0: findContextAttributes(); aoqi@0: } aoqi@0: aoqi@0: public NamespaceContextIterator(Node context, boolean traverseStack) { aoqi@0: this(context); aoqi@0: this.traverseStack = traverseStack; aoqi@0: } aoqi@0: aoqi@0: protected void findContextAttributes() { aoqi@0: while (context != null) { aoqi@0: int type = context.getNodeType(); aoqi@0: if (type == Node.ELEMENT_NODE) { aoqi@0: attributes = context.getAttributes(); aoqi@0: attributesLength = attributes.getLength(); aoqi@0: attributeIndex = 0; aoqi@0: return; aoqi@0: } else { aoqi@0: context = null; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected void findNext() { aoqi@0: while (next == null && context != null) { aoqi@0: for (; attributeIndex < attributesLength; ++attributeIndex) { aoqi@0: Node currentAttribute = attributes.item(attributeIndex); aoqi@0: String attributeName = currentAttribute.getNodeName(); aoqi@0: if (attributeName.startsWith("xmlns") aoqi@0: && (attributeName.length() == 5 aoqi@0: || attributeName.charAt(5) == ':')) { aoqi@0: next = (Attr) currentAttribute; aoqi@0: ++attributeIndex; aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: if (traverseStack) { aoqi@0: context = context.getParentNode(); aoqi@0: findContextAttributes(); aoqi@0: } else { aoqi@0: context = null; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public boolean hasNext() { aoqi@0: findNext(); aoqi@0: return next != null; aoqi@0: } aoqi@0: aoqi@0: public Object next() { aoqi@0: return getNext(); aoqi@0: } aoqi@0: aoqi@0: public Attr nextNamespaceAttr() { aoqi@0: return getNext(); aoqi@0: } aoqi@0: aoqi@0: protected Attr getNext() { aoqi@0: findNext(); aoqi@0: if (next == null) { aoqi@0: throw new NoSuchElementException(); aoqi@0: } aoqi@0: last = next; aoqi@0: next = null; aoqi@0: return last; aoqi@0: } aoqi@0: aoqi@0: public void remove() { aoqi@0: if (last == null) { aoqi@0: throw new IllegalStateException(); aoqi@0: } aoqi@0: ((Element) context).removeAttributeNode(last); aoqi@0: } aoqi@0: aoqi@0: }