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