001: /*
002: * $Id: NamespaceContextIterator.java,v 1.8 2006/01/27 12:49:52 vj135062 Exp $
003: */
004:
005: /*
006: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
007: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
008: *
009: * This code is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU General Public License version 2 only, as
011: * published by the Free Software Foundation. Sun designates this
012: * particular file as subject to the "Classpath" exception as provided
013: * by Sun in the LICENSE file that accompanied this code.
014: *
015: * This code is distributed in the hope that it will be useful, but WITHOUT
016: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
017: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
018: * version 2 for more details (a copy is included in the LICENSE file that
019: * accompanied this code).
020: *
021: * You should have received a copy of the GNU General Public License version
022: * 2 along with this work; if not, write to the Free Software Foundation,
023: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
024: *
025: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
026: * CA 95054 USA or visit www.sun.com if you need additional information or
027: * have any questions.
028: */
029:
030: /**
031: *
032: * @author SAAJ RI Development Team
033: */package com.sun.xml.internal.messaging.saaj.util;
034:
035: import java.util.Iterator;
036: import java.util.NoSuchElementException;
037:
038: import org.w3c.dom.*;
039:
040: public class NamespaceContextIterator implements Iterator {
041: Node context;
042: NamedNodeMap attributes = null;
043: int attributesLength;
044: int attributeIndex;
045: Attr next = null;
046: Attr last = null;
047: boolean traverseStack = true;
048:
049: public NamespaceContextIterator(Node context) {
050: this .context = context;
051: findContextAttributes();
052: }
053:
054: public NamespaceContextIterator(Node context, boolean traverseStack) {
055: this (context);
056: this .traverseStack = traverseStack;
057: }
058:
059: protected void findContextAttributes() {
060: while (context != null) {
061: int type = context.getNodeType();
062: if (type == Node.ELEMENT_NODE) {
063: attributes = context.getAttributes();
064: attributesLength = attributes.getLength();
065: attributeIndex = 0;
066: return;
067: } else {
068: context = null;
069: }
070: }
071: }
072:
073: protected void findNext() {
074: while (next == null && context != null) {
075: for (; attributeIndex < attributesLength; ++attributeIndex) {
076: Node currentAttribute = attributes.item(attributeIndex);
077: String attributeName = currentAttribute.getNodeName();
078: if (attributeName.startsWith("xmlns")
079: && (attributeName.length() == 5 || attributeName
080: .charAt(5) == ':')) {
081: next = (Attr) currentAttribute;
082: ++attributeIndex;
083: return;
084: }
085: }
086: if (traverseStack) {
087: context = context.getParentNode();
088: findContextAttributes();
089: } else {
090: context = null;
091: }
092: }
093: }
094:
095: public boolean hasNext() {
096: findNext();
097: return next != null;
098: }
099:
100: public Object next() {
101: return getNext();
102: }
103:
104: public Attr nextNamespaceAttr() {
105: return getNext();
106: }
107:
108: protected Attr getNext() {
109: findNext();
110: if (next == null) {
111: throw new NoSuchElementException();
112: }
113: last = next;
114: next = null;
115: return last;
116: }
117:
118: public void remove() {
119: if (last == null) {
120: throw new IllegalStateException();
121: }
122: ((Element) context).removeAttributeNode(last);
123: }
124:
125: }
|