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. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.apache.commons.jxpath.ri.model.NodeIterator;
  20. import org.apache.commons.jxpath.ri.model.NodePointer;
  21. import org.w3c.dom.Attr;
  22. import org.w3c.dom.Document;
  23. import org.w3c.dom.NamedNodeMap;
  24. import org.w3c.dom.Node;
  25. /**
  26. * An iterator of namespaces of a DOM Node.
  27. *
  28. * @author Dmitri Plotnikov
  29. * @version $Revision: 1.9 $ $Date: 2004/04/01 02:55:32 $
  30. */
  31. public class DOMNamespaceIterator implements NodeIterator {
  32. private NodePointer parent;
  33. private List attributes;
  34. private int position = 0;
  35. public DOMNamespaceIterator(NodePointer parent) {
  36. this.parent = parent;
  37. attributes = new ArrayList();
  38. collectNamespaces(attributes, (Node) parent.getNode());
  39. }
  40. private void collectNamespaces(List attributes, Node node) {
  41. Node parent = node.getParentNode();
  42. if (parent != null) {
  43. collectNamespaces(attributes, parent);
  44. }
  45. if (node.getNodeType() == Node.DOCUMENT_NODE) {
  46. node = ((Document) node).getDocumentElement();
  47. }
  48. if (node.getNodeType() == Node.ELEMENT_NODE) {
  49. NamedNodeMap map = node.getAttributes();
  50. int count = map.getLength();
  51. for (int i = 0; i < count; i++) {
  52. Attr attr = (Attr) map.item(i);
  53. String prefix = DOMNodePointer.getPrefix(attr);
  54. String name = DOMNodePointer.getLocalName(attr);
  55. if ((prefix != null && prefix.equals("xmlns"))
  56. || (prefix == null && name.equals("xmlns"))) {
  57. attributes.add(attr);
  58. }
  59. }
  60. }
  61. }
  62. public NodePointer getNodePointer() {
  63. if (position == 0) {
  64. if (!setPosition(1)) {
  65. return null;
  66. }
  67. position = 0;
  68. }
  69. int index = position - 1;
  70. if (index < 0) {
  71. index = 0;
  72. }
  73. String prefix = "";
  74. Attr attr = (Attr) attributes.get(index);
  75. String name = attr.getPrefix();
  76. if (name != null && name.equals("xmlns")) {
  77. prefix = DOMNodePointer.getLocalName(attr);
  78. }
  79. return new NamespacePointer(parent, prefix, attr.getValue());
  80. }
  81. public int getPosition() {
  82. return position;
  83. }
  84. public boolean setPosition(int position) {
  85. this.position = position;
  86. return position >= 1 && position <= attributes.size();
  87. }
  88. }