001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.ri.model.jdom;
017:
018: import java.util.ArrayList;
019: import java.util.HashSet;
020: import java.util.List;
021: import java.util.Set;
022:
023: import org.apache.commons.jxpath.ri.model.NodeIterator;
024: import org.apache.commons.jxpath.ri.model.NodePointer;
025: import org.jdom.Document;
026: import org.jdom.Element;
027: import org.jdom.Namespace;
028:
029: /**
030: * An iterator of namespaces of a DOM Node.
031: *
032: * @author Dmitri Plotnikov
033: * @version $Revision: 1.9 $ $Date: 2004/04/01 02:55:31 $
034: */
035: public class JDOMNamespaceIterator implements NodeIterator {
036: private NodePointer parent;
037: private List namespaces;
038: private Set prefixes;
039: private int position = 0;
040:
041: public JDOMNamespaceIterator(NodePointer parent) {
042: this .parent = parent;
043: Object node = parent.getNode();
044: if (node instanceof Document) {
045: node = ((Document) node).getRootElement();
046: }
047: if (node instanceof Element) {
048: namespaces = new ArrayList();
049: prefixes = new HashSet();
050: collectNamespaces((Element) node);
051: }
052: }
053:
054: private void collectNamespaces(Element element) {
055: Namespace ns = element.getNamespace();
056: if (ns != null && !prefixes.contains(ns.getPrefix())) {
057: namespaces.add(ns);
058: prefixes.add(ns.getPrefix());
059: }
060: List others = element.getAdditionalNamespaces();
061: for (int i = 0; i < others.size(); i++) {
062: ns = (Namespace) others.get(i);
063: if (ns != null && !prefixes.contains(ns.getPrefix())) {
064: namespaces.add(ns);
065: prefixes.add(ns.getPrefix());
066: }
067: }
068: Object parent = element.getParent();
069: if (parent instanceof Element) {
070: collectNamespaces((Element) parent);
071: }
072: }
073:
074: public NodePointer getNodePointer() {
075: if (position == 0) {
076: if (!setPosition(1)) {
077: return null;
078: }
079: position = 0;
080: }
081: int index = position - 1;
082: if (index < 0) {
083: index = 0;
084: }
085: Namespace ns = (Namespace) namespaces.get(index);
086: return new JDOMNamespacePointer(parent, ns.getPrefix(), ns
087: .getURI());
088: }
089:
090: public int getPosition() {
091: return position;
092: }
093:
094: public boolean setPosition(int position) {
095: if (namespaces == null) {
096: return false;
097: }
098: this .position = position;
099: return position >= 1 && position <= namespaces.size();
100: }
101: }
|