1 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.QName; 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.Element ; 26 import org.w3c.dom.NamedNodeMap ; 27 import org.w3c.dom.Node ; 28 29 35 public class DOMAttributeIterator implements NodeIterator { 36 private NodePointer parent; 37 private QName name; 38 private List attributes; 39 private int position = 0; 40 41 public DOMAttributeIterator(NodePointer parent, QName name) { 42 this.parent = parent; 43 this.name = name; 44 attributes = new ArrayList (); 45 Node node = (Node ) parent.getNode(); 46 if (node.getNodeType() == Node.ELEMENT_NODE) { 47 String lname = name.getName(); 48 if (!lname.equals("*")) { 49 Attr attr = getAttribute((Element ) node, name); 50 if (attr != null) { 51 attributes.add(attr); 52 } 53 } 54 else { 55 NamedNodeMap map = node.getAttributes(); 56 int count = map.getLength(); 57 for (int i = 0; i < count; i++) { 58 Attr attr = (Attr ) map.item(i); 59 if (testAttr(attr, name)) { 60 attributes.add(attr); 61 } 62 } 63 } 64 } 65 } 66 67 private boolean testAttr(Attr attr, QName testName) { 68 String nodePrefix = DOMNodePointer.getPrefix(attr); 69 String nodeLocalName = DOMNodePointer.getLocalName(attr); 70 71 if (nodePrefix != null && nodePrefix.equals("xmlns")) { 72 return false; 73 } 74 75 if (nodePrefix == null && nodeLocalName.equals("xmlns")) { 76 return false; 77 } 78 79 String testLocalName = name.getName(); 80 if (testLocalName.equals("*") || testLocalName.equals(nodeLocalName)) { 81 String testPrefix = testName.getPrefix(); 82 83 if (equalStrings(testPrefix, nodePrefix)) { 84 return true; 85 } 86 87 String testNS = null; 88 if (testPrefix != null) { 89 testNS = parent.getNamespaceURI(testPrefix); 90 } 91 92 String nodeNS = null; 93 if (nodePrefix != null) { 94 nodeNS = parent.getNamespaceURI(nodePrefix); 95 } 96 return equalStrings(testNS, nodeNS); 97 } 98 return false; 99 } 100 101 private static boolean equalStrings(String s1, String s2) { 102 if (s1 == null && s2 != null) { 103 return false; 104 } 105 if (s1 != null && !s1.equals(s2)) { 106 return false; 107 } 108 return true; 109 } 110 111 private Attr getAttribute(Element element, QName name) { 112 String testPrefix = name.getPrefix(); 113 String testNS = null; 114 115 if (testPrefix != null) { 116 testNS = parent.getNamespaceURI(testPrefix); 117 } 118 119 if (testNS != null) { 120 Attr attr = element.getAttributeNodeNS(testNS, name.getName()); 121 if (attr != null) { 122 return attr; 123 } 124 125 NamedNodeMap nnm = element.getAttributes(); 129 for (int i = 0; i < nnm.getLength(); i++) { 130 attr = (Attr ) nnm.item(i); 131 if (testAttr(attr, name)) { 132 return attr; 133 } 134 } 135 return null; 136 } 137 else { 138 return element.getAttributeNode(name.getName()); 139 } 140 } 141 142 public NodePointer getNodePointer() { 143 if (position == 0) { 144 if (!setPosition(1)) { 145 return null; 146 } 147 position = 0; 148 } 149 int index = position - 1; 150 if (index < 0) { 151 index = 0; 152 } 153 return new DOMAttributePointer(parent, (Attr ) attributes.get(index)); 154 } 155 156 public int getPosition() { 157 return position; 158 } 159 160 public boolean setPosition(int position) { 161 this.position = position; 162 return position >= 1 && position <= attributes.size(); 163 } 164 } | Popular Tags |