View Javadoc

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