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.Collections;
  19. import java.util.List;
  20. import org.apache.commons.jxpath.ri.QName;
  21. import org.apache.commons.jxpath.ri.model.NodeIterator;
  22. import org.apache.commons.jxpath.ri.model.NodePointer;
  23. import org.jdom.Attribute;
  24. import org.jdom.Element;
  25. import org.jdom.Namespace;
  26. /**
  27. * An iterator of attributes of a DOM Node.
  28. *
  29. * @author Dmitri Plotnikov
  30. * @version $Revision: 1.6 $ $Date: 2004/02/29 14:17:40 $
  31. */
  32. public class JDOMAttributeIterator implements NodeIterator {
  33. private NodePointer parent;
  34. private QName name;
  35. private List attributes;
  36. private int position = 0;
  37. public JDOMAttributeIterator(NodePointer parent, QName name) {
  38. this.parent = parent;
  39. this.name = name;
  40. if (parent.getNode() instanceof Element) {
  41. Element element = (Element) parent.getNode();
  42. String prefix = name.getPrefix();
  43. Namespace ns;
  44. if (prefix != null) {
  45. if (prefix.equals("xml")) {
  46. ns = Namespace.XML_NAMESPACE;
  47. }
  48. else {
  49. ns = element.getNamespace(prefix);
  50. if (ns == null) {
  51. // TBD: no attributes
  52. attributes = Collections.EMPTY_LIST;
  53. return;
  54. }
  55. }
  56. }
  57. else {
  58. ns = Namespace.NO_NAMESPACE;
  59. }
  60. String lname = name.getName();
  61. if (!lname.equals("*")) {
  62. attributes = new ArrayList();
  63. if (ns != null) {
  64. Attribute attr = element.getAttribute(lname, ns);
  65. if (attr != null) {
  66. attributes.add(attr);
  67. }
  68. }
  69. }
  70. else {
  71. attributes = new ArrayList();
  72. List allAttributes = element.getAttributes();
  73. for (int i = 0; i < allAttributes.size(); i++) {
  74. Attribute attr = (Attribute) allAttributes.get(i);
  75. if (attr.getNamespace().equals(ns)) {
  76. attributes.add(attr);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. /*
  83. private boolean testAttr(Attr attr, QName testName) {
  84. String nodePrefix = DOMNodePointer.getPrefix(attr);
  85. String nodeLocalName = DOMNodePointer.getLocalName(attr);
  86. if (nodePrefix != null && nodePrefix.equals("xmlns")) {
  87. return false;
  88. }
  89. if (nodePrefix == null && nodeLocalName.equals("xmlns")) {
  90. return false;
  91. }
  92. String testLocalName = name.getName();
  93. if (testLocalName.equals("*") || testLocalName.equals(nodeLocalName)) {
  94. String testPrefix = testName.getPrefix();
  95. if (equalStrings(testPrefix, nodePrefix)) {
  96. return true;
  97. }
  98. String testNS = null;
  99. if (testPrefix != null) {
  100. testNS = parent.getNamespaceURI(testPrefix);
  101. }
  102. String nodeNS = null;
  103. if (nodePrefix != null) {
  104. nodeNS = parent.getNamespaceURI(nodePrefix);
  105. }
  106. return equalStrings(testNS, nodeNS);
  107. }
  108. return false;
  109. }
  110. private static boolean equalStrings(String s1, String s2) {
  111. if (s1 == null && s2 != null) {
  112. return false;
  113. }
  114. if (s1 != null && !s1.equals(s2)) {
  115. return false;
  116. }
  117. return true;
  118. }
  119. private Attr getAttribute(Element element, QName name) {
  120. String testPrefix = name.getPrefix();
  121. String testNS = null;
  122. if (testPrefix != null) {
  123. testNS = parent.getNamespaceURI(testPrefix);
  124. }
  125. if (testNS != null) {
  126. Attr attr = element.getAttributeNodeNS(testNS, name.getName());
  127. if (attr == null) {
  128. // This may mean that the parser does not support NS for
  129. // attributes, example - the version of Crimson bundled
  130. // with JDK 1.4.0
  131. NamedNodeMap nnm = element.getAttributes();
  132. for (int i = 0; i < nnm.getLength(); i++) {
  133. attr = (Attr)nnm.item(i);
  134. if (testAttr(attr, name)) {
  135. return attr;
  136. }
  137. }
  138. }
  139. return attr;
  140. }
  141. else {
  142. return element.getAttributeNode(name.getName());
  143. }
  144. }
  145. */
  146. public NodePointer getNodePointer() {
  147. if (position == 0) {
  148. if (!setPosition(1)) {
  149. return null;
  150. }
  151. position = 0;
  152. }
  153. int index = position - 1;
  154. if (index < 0) {
  155. index = 0;
  156. }
  157. return new JDOMAttributePointer(
  158. parent,
  159. (Attribute) attributes.get(index));
  160. }
  161. public int getPosition() {
  162. return position;
  163. }
  164. public boolean setPosition(int position) {
  165. if (attributes == null) {
  166. return false;
  167. }
  168. this.position = position;
  169. return position >= 1 && position <= attributes.size();
  170. }
  171. }