| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
package org.jaxen.expr; |
| 47 |
|
|
| 48 |
|
import java.util.ArrayList; |
| 49 |
|
import java.util.Collections; |
| 50 |
|
import java.util.Iterator; |
| 51 |
|
import java.util.List; |
| 52 |
|
|
| 53 |
|
import org.jaxen.Context; |
| 54 |
|
import org.jaxen.ContextSupport; |
| 55 |
|
import org.jaxen.JaxenException; |
| 56 |
|
import org.jaxen.UnresolvableException; |
| 57 |
|
import org.jaxen.Navigator; |
| 58 |
|
import org.jaxen.expr.iter.IterableAxis; |
| 59 |
|
import org.jaxen.saxpath.Axis; |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
public class DefaultNameStep extends DefaultStep implements NameStep { |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
private String prefix; |
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
private String localName; |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
private boolean matchesAnyName; |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
private boolean hasPrefix; |
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
public DefaultNameStep(IterableAxis axis, |
| 106 |
|
String prefix, |
| 107 |
|
String localName, |
| 108 |
|
PredicateSet predicateSet) { |
| 109 |
13158 |
super(axis, predicateSet); |
| 110 |
|
|
| 111 |
13158 |
this.prefix = prefix; |
| 112 |
13158 |
this.localName = localName; |
| 113 |
13158 |
this.matchesAnyName = "*".equals(localName); |
| 114 |
13158 |
this.hasPrefix = (this.prefix != null && this.prefix.length() > 0); |
| 115 |
13158 |
} |
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
public String getPrefix() { |
| 123 |
276 |
return this.prefix; |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
public String getLocalName() { |
| 132 |
888702 |
return this.localName; |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
public boolean isMatchesAnyName() { |
| 141 |
192 |
return matchesAnyName; |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
public String getText() { |
| 150 |
36 |
StringBuffer buf = new StringBuffer(64); |
| 151 |
36 |
buf.append(getAxisName()).append("::"); |
| 152 |
36 |
if (getPrefix() != null && getPrefix().length() > 0) { |
| 153 |
0 |
buf.append(getPrefix()).append(':'); |
| 154 |
|
} |
| 155 |
36 |
return buf.append(getLocalName()).append(super.getText()).toString(); |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
public List evaluate(Context context) throws JaxenException { |
| 164 |
|
|
| 165 |
25656 |
List contextNodeSet = context.getNodeSet(); |
| 166 |
25656 |
int contextSize = contextNodeSet.size(); |
| 167 |
|
|
| 168 |
25656 |
if (contextSize == 0) { |
| 169 |
126 |
return Collections.EMPTY_LIST; |
| 170 |
|
} |
| 171 |
25530 |
ContextSupport support = context.getContextSupport(); |
| 172 |
25530 |
IterableAxis iterableAxis = getIterableAxis(); |
| 173 |
25530 |
boolean namedAccess = (!matchesAnyName && iterableAxis.supportsNamedAccess(support)); |
| 174 |
|
|
| 175 |
|
|
| 176 |
25530 |
if (contextSize == 1) { |
| 177 |
24036 |
Object contextNode = contextNodeSet.get(0); |
| 178 |
24036 |
if (namedAccess) { |
| 179 |
|
|
| 180 |
9792 |
String uri = null; |
| 181 |
9792 |
if (hasPrefix) { |
| 182 |
228 |
uri = support.translateNamespacePrefixToUri(prefix); |
| 183 |
228 |
if (uri == null) { |
| 184 |
0 |
throw new UnresolvableException("XPath expression uses unbound namespace prefix " + prefix); |
| 185 |
|
} |
| 186 |
|
} |
| 187 |
9792 |
Iterator axisNodeIter = iterableAxis.namedAccessIterator( |
| 188 |
|
contextNode, support, localName, prefix, uri); |
| 189 |
9792 |
if (axisNodeIter == null || axisNodeIter.hasNext() == false) { |
| 190 |
2712 |
return Collections.EMPTY_LIST; |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
7080 |
List newNodeSet = new ArrayList(); |
| 196 |
33414 |
while (axisNodeIter.hasNext()) { |
| 197 |
26334 |
newNodeSet.add(axisNodeIter.next()); |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
|
| 201 |
7080 |
return getPredicateSet().evaluatePredicates(newNodeSet, support); |
| 202 |
|
|
| 203 |
|
} |
| 204 |
|
else { |
| 205 |
|
|
| 206 |
14244 |
Iterator axisNodeIter = iterableAxis.iterator(contextNode, support); |
| 207 |
14244 |
if (axisNodeIter == null || axisNodeIter.hasNext() == false) { |
| 208 |
2808 |
return Collections.EMPTY_LIST; |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
11436 |
List newNodeSet = new ArrayList(contextSize); |
| 214 |
2662866 |
while (axisNodeIter.hasNext()) { |
| 215 |
2651430 |
Object eachAxisNode = axisNodeIter.next(); |
| 216 |
2651430 |
if (matches(eachAxisNode, support)) { |
| 217 |
62454 |
newNodeSet.add(eachAxisNode); |
| 218 |
|
} |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
|
| 222 |
11436 |
return getPredicateSet().evaluatePredicates(newNodeSet, support); |
| 223 |
|
} |
| 224 |
|
} |
| 225 |
|
|
| 226 |
|
|
| 227 |
1494 |
IdentitySet unique = new IdentitySet(); |
| 228 |
1494 |
List interimSet = new ArrayList(contextSize); |
| 229 |
1494 |
List newNodeSet = new ArrayList(contextSize); |
| 230 |
|
|
| 231 |
1494 |
if (namedAccess) { |
| 232 |
498 |
String uri = null; |
| 233 |
498 |
if (hasPrefix) { |
| 234 |
0 |
uri = support.translateNamespacePrefixToUri(prefix); |
| 235 |
0 |
if (uri == null) { |
| 236 |
0 |
throw new UnresolvableException("XPath expression uses unbound namespace prefix " + prefix); |
| 237 |
|
} |
| 238 |
|
} |
| 239 |
26124 |
for (int i = 0; i < contextSize; ++i) { |
| 240 |
25626 |
Object eachContextNode = contextNodeSet.get(i); |
| 241 |
|
|
| 242 |
25626 |
Iterator axisNodeIter = iterableAxis.namedAccessIterator( |
| 243 |
|
eachContextNode, support, localName, prefix, uri); |
| 244 |
25626 |
if (axisNodeIter == null || axisNodeIter.hasNext() == false) { |
| 245 |
24420 |
continue; |
| 246 |
|
} |
| 247 |
|
|
| 248 |
|
|
| 249 |
4392 |
while (axisNodeIter.hasNext()) { |
| 250 |
3186 |
Object eachAxisNode = axisNodeIter.next(); |
| 251 |
3186 |
if (! unique.contains(eachAxisNode)) { |
| 252 |
3186 |
unique.add(eachAxisNode); |
| 253 |
3186 |
interimSet.add(eachAxisNode); |
| 254 |
|
} |
| 255 |
|
} |
| 256 |
|
|
| 257 |
|
|
| 258 |
1206 |
newNodeSet.addAll(getPredicateSet().evaluatePredicates(interimSet, support)); |
| 259 |
1206 |
interimSet.clear(); |
| 260 |
|
} |
| 261 |
|
|
| 262 |
|
} else { |
| 263 |
79314 |
for (int i = 0; i < contextSize; ++i) { |
| 264 |
78330 |
Object eachContextNode = contextNodeSet.get(i); |
| 265 |
|
|
| 266 |
78330 |
Iterator axisNodeIter = axisIterator(eachContextNode, support); |
| 267 |
78330 |
if (axisNodeIter == null || axisNodeIter.hasNext() == false) { |
| 268 |
51264 |
continue; |
| 269 |
|
} |
| 270 |
|
|
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
|
| 275 |
|
|
| 276 |
|
|
| 277 |
|
|
| 278 |
|
|
| 279 |
|
|
| 280 |
107352 |
while (axisNodeIter.hasNext()) { |
| 281 |
80286 |
Object eachAxisNode = axisNodeIter.next(); |
| 282 |
|
|
| 283 |
80286 |
if (matches(eachAxisNode, support)) { |
| 284 |
21294 |
if (! unique.contains(eachAxisNode)) { |
| 285 |
21258 |
unique.add(eachAxisNode); |
| 286 |
21258 |
interimSet.add(eachAxisNode); |
| 287 |
|
} |
| 288 |
|
} |
| 289 |
|
} |
| 290 |
|
|
| 291 |
|
|
| 292 |
27066 |
newNodeSet.addAll(getPredicateSet().evaluatePredicates(interimSet, support)); |
| 293 |
27054 |
interimSet.clear(); |
| 294 |
|
} |
| 295 |
|
} |
| 296 |
|
|
| 297 |
1482 |
return newNodeSet; |
| 298 |
|
} |
| 299 |
|
|
| 300 |
|
|
| 301 |
|
|
| 302 |
|
|
| 303 |
|
|
| 304 |
|
|
| 305 |
|
|
| 306 |
|
|
| 307 |
|
|
| 308 |
|
public boolean matches(Object node, ContextSupport contextSupport) throws JaxenException { |
| 309 |
|
|
| 310 |
2731716 |
Navigator nav = contextSupport.getNavigator(); |
| 311 |
2731716 |
String myUri = null; |
| 312 |
2731716 |
String nodeName = null; |
| 313 |
2731716 |
String nodeUri = null; |
| 314 |
|
|
| 315 |
2731716 |
if (nav.isElement(node)) { |
| 316 |
922362 |
nodeName = nav.getElementName(node); |
| 317 |
922362 |
nodeUri = nav.getElementNamespaceUri(node); |
| 318 |
|
} |
| 319 |
1809354 |
else if (nav.isText(node)) { |
| 320 |
1800018 |
return false; |
| 321 |
|
} |
| 322 |
9336 |
else if (nav.isAttribute(node)) { |
| 323 |
5274 |
if (getAxis() != Axis.ATTRIBUTE) { |
| 324 |
6 |
return false; |
| 325 |
|
} |
| 326 |
5268 |
nodeName = nav.getAttributeName(node); |
| 327 |
5268 |
nodeUri = nav.getAttributeNamespaceUri(node); |
| 328 |
|
|
| 329 |
|
} |
| 330 |
4062 |
else if (nav.isDocument(node)) { |
| 331 |
324 |
return false; |
| 332 |
|
} |
| 333 |
3738 |
else if (nav.isNamespace(node)) { |
| 334 |
2874 |
if (getAxis() != Axis.NAMESPACE) { |
| 335 |
|
|
| 336 |
30 |
return false; |
| 337 |
|
} |
| 338 |
2844 |
nodeName = nav.getNamespacePrefix(node); |
| 339 |
|
} |
| 340 |
|
else { |
| 341 |
864 |
return false; |
| 342 |
|
} |
| 343 |
|
|
| 344 |
930474 |
if (hasPrefix) { |
| 345 |
402 |
myUri = contextSupport.translateNamespacePrefixToUri(this.prefix); |
| 346 |
402 |
if (myUri == null) { |
| 347 |
0 |
throw new UnresolvableException("Cannot resolve namespace prefix '"+this.prefix+"'"); |
| 348 |
|
} |
| 349 |
|
} |
| 350 |
930072 |
else if (matchesAnyName) { |
| 351 |
41826 |
return true; |
| 352 |
|
} |
| 353 |
|
|
| 354 |
|
|
| 355 |
|
|
| 356 |
888648 |
if (hasNamespace(myUri) != hasNamespace(nodeUri)) { |
| 357 |
174 |
return false; |
| 358 |
|
} |
| 359 |
|
|
| 360 |
|
|
| 361 |
|
|
| 362 |
|
|
| 363 |
888474 |
if (matchesAnyName || nodeName.equals(getLocalName())) { |
| 364 |
41934 |
return matchesNamespaceURIs(myUri, nodeUri); |
| 365 |
|
} |
| 366 |
|
|
| 367 |
846540 |
return false; |
| 368 |
|
} |
| 369 |
|
|
| 370 |
|
|
| 371 |
|
|
| 372 |
|
|
| 373 |
|
|
| 374 |
|
|
| 375 |
|
|
| 376 |
|
private boolean hasNamespace(String uri) { |
| 377 |
1777296 |
return (uri != null && uri.length() > 0); |
| 378 |
|
} |
| 379 |
|
|
| 380 |
|
|
| 381 |
|
|
| 382 |
|
|
| 383 |
|
|
| 384 |
|
|
| 385 |
|
|
| 386 |
|
|
| 387 |
|
protected boolean matchesNamespaceURIs(String uri1, String uri2) { |
| 388 |
41934 |
if (uri1 == uri2) { |
| 389 |
21534 |
return true; |
| 390 |
|
} |
| 391 |
20400 |
if (uri1 == null) { |
| 392 |
20388 |
return (uri2.length() == 0); |
| 393 |
|
} |
| 394 |
12 |
if (uri2 == null) { |
| 395 |
0 |
return (uri1.length() == 0); |
| 396 |
|
} |
| 397 |
12 |
return uri1.equals(uri2); |
| 398 |
|
} |
| 399 |
|
|
| 400 |
|
|
| 401 |
|
|
| 402 |
|
|
| 403 |
|
|
| 404 |
|
|
| 405 |
|
public void accept(Visitor visitor) { |
| 406 |
0 |
visitor.visit(this); |
| 407 |
0 |
} |
| 408 |
|
|
| 409 |
|
|
| 410 |
|
|
| 411 |
|
|
| 412 |
|
|
| 413 |
|
|
| 414 |
|
public String toString() { |
| 415 |
12 |
return "[(DefaultNameStep): " + getPrefix() + ":" + getLocalName() + "]"; |
| 416 |
|
} |
| 417 |
|
|
| 418 |
|
} |