1 28 29 package com.caucho.xpath.pattern; 30 31 import com.caucho.xml.CauchoElement; 32 import com.caucho.xml.CauchoNode; 33 import com.caucho.xml.QAttr; 34 import com.caucho.xml.QElement; 35 import com.caucho.xpath.Env; 36 import com.caucho.xpath.ExprEnvironment; 37 import com.caucho.xpath.XPathException; 38 39 import org.w3c.dom.Attr ; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.Node ; 42 43 46 public class FromAttributes extends Axis { 47 public FromAttributes(AbstractPattern parent) 48 { 49 super(parent); 50 } 51 52 60 public boolean match(Node node, ExprEnvironment env) 61 throws XPathException 62 { 63 if (! (node instanceof Attr)) 64 return false; 65 66 if (node instanceof QAttr && 67 ((QAttr) node).getNamespaceURI() == XMLNS) 68 return false; 69 70 return _parent == null || _parent.match(node.getParentNode(), env); 71 } 72 73 79 public int position(Node node, Env env, AbstractPattern pattern) 80 throws XPathException 81 { 82 int count = 1; 83 84 for (node = node.getPreviousSibling(); 85 node != null; 86 node = node.getPreviousSibling()) { 87 if (pattern.match(node, env)) 88 count++; 89 } 90 91 return count; 92 } 93 94 97 public int count(Node node, Env env, AbstractPattern pattern) 98 throws XPathException 99 { 100 int count = 0; 101 102 CauchoElement parent = (CauchoElement) node.getParentNode(); 103 104 for (node = parent.getFirstAttribute(); 105 node != null; 106 node = node.getNextSibling()) { 107 if (pattern.match(node, env)) 108 count++; 109 } 110 111 return count; 112 } 113 114 123 public NodeIterator createNodeIterator(Node node, ExprEnvironment env, 124 AbstractPattern match) 125 throws XPathException 126 { 127 if (node instanceof CauchoNode) { 128 if (_parent == null) 129 return new AttributeIterator(null, this, node, env, match); 130 else if (_parent instanceof FromRoot) { 131 if (node instanceof Document ) 132 return new AttributeIterator(null, this, node, env, match); 133 else 134 return new AttributeIterator(null, this, node.getOwnerDocument(), 135 env, match); 136 } 137 138 NodeIterator parentIter; 139 parentIter = _parent.createNodeIterator(node, env, 140 _parent.copyPosition()); 141 142 return new AttributeIterator(parentIter, this, null, env, match); 143 } 144 145 if (_parent == null) 146 return new AttributeListIterator(null, env, match); 147 else if (_parent instanceof FromRoot) { 148 if (node instanceof Document ) 149 return new AttributeListIterator(null, env, match); 150 else 151 return new AttributeListIterator(null, env, match); 152 } 153 154 NodeIterator parentIter; 155 parentIter = _parent.createNodeIterator(node, env, _parent.copyPosition()); 156 157 return new AttributeListIterator(parentIter, env, match); 158 } 159 160 167 public Node firstNode(Node node, ExprEnvironment env) 168 { 169 if (node instanceof QElement) 170 return ((QElement) node).getFirstAttribute(); 171 else 172 return null; 173 } 174 175 183 public Node nextNode(Node node, Node lastNode) 184 { 185 return node.getNextSibling(); 186 } 187 188 191 public boolean isStrictlyAscending() 192 { 193 if (_parent == null) 194 return true; 195 else 196 return _parent.isStrictlyAscending(); 197 } 198 199 202 public boolean equals(Object b) 203 { 204 if (! (b instanceof FromAttributes)) 205 return false; 206 207 FromAttributes bPattern = (FromAttributes) b; 208 209 return (_parent == bPattern._parent || 210 (_parent != null && _parent.equals(bPattern._parent))); 211 } 212 213 public String toString() 214 { 215 return getPrefix() + "attribute::"; 216 } 217 } 218 | Popular Tags |