View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jxpath.ri.model.dom;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.jxpath.ri.model.NodeIterator;
23  import org.apache.commons.jxpath.ri.model.NodePointer;
24  import org.w3c.dom.Attr;
25  import org.w3c.dom.Document;
26  import org.w3c.dom.NamedNodeMap;
27  import org.w3c.dom.Node;
28  
29  /**
30   * An iterator of namespaces of a DOM Node.
31   *
32   * @author Dmitri Plotnikov
33   * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
34   */
35  public class DOMNamespaceIterator implements NodeIterator {
36      private NodePointer parent;
37      private List attributes;
38      private int position = 0;
39  
40      /**
41       * Create a new DOMNamespaceIterator.
42       * @param parent parent pointer
43       */
44      public DOMNamespaceIterator(NodePointer parent) {
45          this.parent = parent;
46          attributes = new ArrayList();
47          collectNamespaces(attributes, (Node) parent.getNode());
48      }
49  
50      /**
51       * Collect namespaces from attribute nodes.
52       * @param attributes attribute list
53       * @param node target node
54       */
55      private void collectNamespaces(List attributes, Node node) {
56          Node parent = node.getParentNode();
57          if (parent != null) {
58              collectNamespaces(attributes, parent);
59          }
60          if (node.getNodeType() == Node.DOCUMENT_NODE) {
61              node = ((Document) node).getDocumentElement();
62          }
63          if (node.getNodeType() == Node.ELEMENT_NODE) {
64              NamedNodeMap map = node.getAttributes();
65              int count = map.getLength();
66              for (int i = 0; i < count; i++) {
67                  Attr attr = (Attr) map.item(i);
68                  String prefix = DOMNodePointer.getPrefix(attr);
69                  String name = DOMNodePointer.getLocalName(attr);
70                  if ((prefix != null && prefix.equals("xmlns"))
71                      || (prefix == null && name.equals("xmlns"))) {
72                      attributes.add(attr);
73                  }
74              }
75          }
76      }
77  
78      public NodePointer getNodePointer() {
79          if (position == 0) {
80              if (!setPosition(1)) {
81                  return null;
82              }
83              position = 0;
84          }
85          int index = position - 1;
86          if (index < 0) {
87              index = 0;
88          }
89          String prefix = "";
90          Attr attr = (Attr) attributes.get(index);
91          String name = attr.getPrefix();
92          if (name != null && name.equals("xmlns")) {
93              prefix = DOMNodePointer.getLocalName(attr);
94          }
95          return new NamespacePointer(parent, prefix, attr.getValue());
96      }
97  
98      public int getPosition() {
99          return position;
100     }
101 
102     public boolean setPosition(int position) {
103         this.position = position;
104         return position >= 1 && position <= attributes.size();
105     }
106 }