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.jdom;
  17. import java.util.ArrayList;
  18. import java.util.HashSet;
  19. import java.util.List;
  20. import java.util.Set;
  21. import org.apache.commons.jxpath.ri.model.NodeIterator;
  22. import org.apache.commons.jxpath.ri.model.NodePointer;
  23. import org.jdom.Document;
  24. import org.jdom.Element;
  25. import org.jdom.Namespace;
  26. /**
  27. * An iterator of namespaces of a DOM Node.
  28. *
  29. * @author Dmitri Plotnikov
  30. * @version $Revision: 1.9 $ $Date: 2004/04/01 02:55:31 $
  31. */
  32. public class JDOMNamespaceIterator implements NodeIterator {
  33. private NodePointer parent;
  34. private List namespaces;
  35. private Set prefixes;
  36. private int position = 0;
  37. public JDOMNamespaceIterator(NodePointer parent) {
  38. this.parent = parent;
  39. Object node = parent.getNode();
  40. if (node instanceof Document) {
  41. node = ((Document)node).getRootElement();
  42. }
  43. if (node instanceof Element) {
  44. namespaces = new ArrayList();
  45. prefixes = new HashSet();
  46. collectNamespaces((Element) node);
  47. }
  48. }
  49. private void collectNamespaces(Element element) {
  50. Namespace ns = element.getNamespace();
  51. if (ns != null && !prefixes.contains(ns.getPrefix())) {
  52. namespaces.add(ns);
  53. prefixes.add(ns.getPrefix());
  54. }
  55. List others = element.getAdditionalNamespaces();
  56. for (int i = 0; i < others.size(); i++) {
  57. ns = (Namespace) others.get(i);
  58. if (ns != null && !prefixes.contains(ns.getPrefix())) {
  59. namespaces.add(ns);
  60. prefixes.add(ns.getPrefix());
  61. }
  62. }
  63. Object parent = element.getParent();
  64. if (parent instanceof Element) {
  65. collectNamespaces((Element)parent);
  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. Namespace ns = (Namespace) namespaces.get(index);
  80. return new JDOMNamespacePointer(parent, ns.getPrefix(), ns.getURI());
  81. }
  82. public int getPosition() {
  83. return position;
  84. }
  85. public boolean setPosition(int position) {
  86. if (namespaces == null) {
  87. return false;
  88. }
  89. this.position = position;
  90. return position >= 1 && position <= namespaces.size();
  91. }
  92. }