001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: NamespaceContextIterator.java,v 1.2 2007/07/16 16:41:25 ofung Exp $
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059:
060: /**
061: *
062: * @author SAAJ RI Development Team
063: */package com.sun.xml.messaging.saaj.util;
064:
065: import java.util.Iterator;
066: import java.util.NoSuchElementException;
067:
068: import org.w3c.dom.*;
069:
070: public class NamespaceContextIterator implements Iterator {
071: Node context;
072: NamedNodeMap attributes = null;
073: int attributesLength;
074: int attributeIndex;
075: Attr next = null;
076: Attr last = null;
077: boolean traverseStack = true;
078:
079: public NamespaceContextIterator(Node context) {
080: this .context = context;
081: findContextAttributes();
082: }
083:
084: public NamespaceContextIterator(Node context, boolean traverseStack) {
085: this (context);
086: this .traverseStack = traverseStack;
087: }
088:
089: protected void findContextAttributes() {
090: while (context != null) {
091: int type = context.getNodeType();
092: if (type == Node.ELEMENT_NODE) {
093: attributes = context.getAttributes();
094: attributesLength = attributes.getLength();
095: attributeIndex = 0;
096: return;
097: } else {
098: context = null;
099: }
100: }
101: }
102:
103: protected void findNext() {
104: while (next == null && context != null) {
105: for (; attributeIndex < attributesLength; ++attributeIndex) {
106: Node currentAttribute = attributes.item(attributeIndex);
107: String attributeName = currentAttribute.getNodeName();
108: if (attributeName.startsWith("xmlns")
109: && (attributeName.length() == 5 || attributeName
110: .charAt(5) == ':')) {
111: next = (Attr) currentAttribute;
112: ++attributeIndex;
113: return;
114: }
115: }
116: if (traverseStack) {
117: context = context.getParentNode();
118: findContextAttributes();
119: } else {
120: context = null;
121: }
122: }
123: }
124:
125: public boolean hasNext() {
126: findNext();
127: return next != null;
128: }
129:
130: public Object next() {
131: return getNext();
132: }
133:
134: public Attr nextNamespaceAttr() {
135: return getNext();
136: }
137:
138: protected Attr getNext() {
139: findNext();
140: if (next == null) {
141: throw new NoSuchElementException();
142: }
143: last = next;
144: next = null;
145: return last;
146: }
147:
148: public void remove() {
149: if (last == null) {
150: throw new IllegalStateException();
151: }
152: ((Element) context).removeAttributeNode(last);
153: }
154:
155: }
|