| 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 | |
package org.jaxen.expr; |
| 35 | |
|
| 36 | |
import java.util.ArrayList; |
| 37 | |
import java.util.Collections; |
| 38 | |
import java.util.Iterator; |
| 39 | |
import java.util.List; |
| 40 | |
|
| 41 | |
import org.jaxen.Context; |
| 42 | |
import org.jaxen.ContextSupport; |
| 43 | |
import org.jaxen.JaxenException; |
| 44 | |
import org.jaxen.UnresolvableException; |
| 45 | |
import org.jaxen.Navigator; |
| 46 | |
import org.jaxen.expr.iter.IterableAxis; |
| 47 | |
import org.jaxen.saxpath.Axis; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
public class DefaultNameStep extends DefaultStep implements NameStep { |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private static final long serialVersionUID = 428414912247718390L; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
private String prefix; |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
private String localName; |
| 83 | |
|
| 84 | |
|
| 85 | |
private boolean matchesAnyName; |
| 86 | |
|
| 87 | |
|
| 88 | |
private boolean hasPrefix; |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public DefaultNameStep(IterableAxis axis, |
| 99 | |
String prefix, |
| 100 | |
String localName, |
| 101 | |
PredicateSet predicateSet) { |
| 102 | 7548 | super(axis, predicateSet); |
| 103 | |
|
| 104 | 7548 | this.prefix = prefix; |
| 105 | 7548 | this.localName = localName; |
| 106 | 7548 | this.matchesAnyName = "*".equals(localName); |
| 107 | 7548 | this.hasPrefix = (this.prefix != null && this.prefix.length() > 0); |
| 108 | 7548 | } |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public String getPrefix() { |
| 116 | 11898 | return this.prefix; |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public String getLocalName() { |
| 125 | 304822 | return this.localName; |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
public boolean isMatchesAnyName() { |
| 134 | 64 | return matchesAnyName; |
| 135 | |
} |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public String getText() { |
| 143 | 3118 | StringBuffer buf = new StringBuffer(64); |
| 144 | 3118 | buf.append(getAxisName()).append("::"); |
| 145 | 3118 | if (getPrefix() != null && getPrefix().length() > 0) { |
| 146 | 152 | buf.append(getPrefix()).append(':'); |
| 147 | |
} |
| 148 | 3118 | return buf.append(getLocalName()).append(super.getText()).toString(); |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public List evaluate(Context context) throws JaxenException { |
| 157 | |
|
| 158 | 8598 | List contextNodeSet = context.getNodeSet(); |
| 159 | 8598 | int contextSize = contextNodeSet.size(); |
| 160 | |
|
| 161 | 8598 | if (contextSize == 0) { |
| 162 | 44 | return Collections.EMPTY_LIST; |
| 163 | |
} |
| 164 | 8554 | ContextSupport support = context.getContextSupport(); |
| 165 | 8554 | IterableAxis iterableAxis = getIterableAxis(); |
| 166 | 8554 | boolean namedAccess = (!matchesAnyName && iterableAxis.supportsNamedAccess(support)); |
| 167 | |
|
| 168 | |
|
| 169 | 8554 | if (contextSize == 1) { |
| 170 | 8052 | Object contextNode = contextNodeSet.get(0); |
| 171 | 8052 | if (namedAccess) { |
| 172 | |
|
| 173 | 3286 | String uri = null; |
| 174 | 3286 | if (hasPrefix) { |
| 175 | 76 | uri = support.translateNamespacePrefixToUri(prefix); |
| 176 | 76 | if (uri == null) { |
| 177 | 0 | throw new UnresolvableException("XPath expression uses unbound namespace prefix " + prefix); |
| 178 | |
} |
| 179 | |
} |
| 180 | 3286 | Iterator axisNodeIter = iterableAxis.namedAccessIterator( |
| 181 | |
contextNode, support, localName, prefix, uri); |
| 182 | 3286 | if (axisNodeIter == null || !axisNodeIter.hasNext()) { |
| 183 | 910 | return Collections.EMPTY_LIST; |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | 2376 | List newNodeSet = new ArrayList(); |
| 189 | 11174 | while (axisNodeIter.hasNext()) { |
| 190 | 8798 | newNodeSet.add(axisNodeIter.next()); |
| 191 | 8798 | } |
| 192 | |
|
| 193 | |
|
| 194 | 2376 | return getPredicateSet().evaluatePredicates(newNodeSet, support); |
| 195 | |
|
| 196 | |
} |
| 197 | |
else { |
| 198 | |
|
| 199 | 4766 | Iterator axisNodeIter = iterableAxis.iterator(contextNode, support); |
| 200 | 4766 | if (axisNodeIter == null || !axisNodeIter.hasNext()) { |
| 201 | 936 | return Collections.EMPTY_LIST; |
| 202 | |
} |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | 3830 | List newNodeSet = new ArrayList(contextSize); |
| 207 | 887662 | while (axisNodeIter.hasNext()) { |
| 208 | 883834 | Object eachAxisNode = axisNodeIter.next(); |
| 209 | 883834 | if (matches(eachAxisNode, support)) { |
| 210 | 20844 | newNodeSet.add(eachAxisNode); |
| 211 | |
} |
| 212 | 883832 | } |
| 213 | |
|
| 214 | |
|
| 215 | 3828 | return getPredicateSet().evaluatePredicates(newNodeSet, support); |
| 216 | |
} |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | 502 | IdentitySet unique = new IdentitySet(); |
| 221 | 502 | List interimSet = new ArrayList(contextSize); |
| 222 | 502 | List newNodeSet = new ArrayList(contextSize); |
| 223 | |
|
| 224 | 502 | if (namedAccess) { |
| 225 | 168 | String uri = null; |
| 226 | 168 | if (hasPrefix) { |
| 227 | 0 | uri = support.translateNamespacePrefixToUri(prefix); |
| 228 | 0 | if (uri == null) { |
| 229 | 0 | throw new UnresolvableException("XPath expression uses unbound namespace prefix " + prefix); |
| 230 | |
} |
| 231 | |
} |
| 232 | 8722 | for (int i = 0; i < contextSize; ++i) { |
| 233 | 8554 | Object eachContextNode = contextNodeSet.get(i); |
| 234 | |
|
| 235 | 8554 | Iterator axisNodeIter = iterableAxis.namedAccessIterator( |
| 236 | |
eachContextNode, support, localName, prefix, uri); |
| 237 | 8554 | if (axisNodeIter == null || !axisNodeIter.hasNext()) { |
| 238 | 8150 | continue; |
| 239 | |
} |
| 240 | |
|
| 241 | 1468 | while (axisNodeIter.hasNext()) |
| 242 | |
{ |
| 243 | 1064 | Object eachAxisNode = axisNodeIter.next(); |
| 244 | 1064 | interimSet.add(eachAxisNode); |
| 245 | 1064 | } |
| 246 | |
|
| 247 | |
|
| 248 | 404 | List predicateNodes = getPredicateSet().evaluatePredicates(interimSet, support); |
| 249 | |
|
| 250 | |
|
| 251 | 404 | Iterator predicateNodeIter = predicateNodes.iterator(); |
| 252 | 1244 | while (predicateNodeIter.hasNext()) |
| 253 | |
{ |
| 254 | 840 | Object eachPredicateNode = predicateNodeIter.next(); |
| 255 | 840 | if (! unique.contains(eachPredicateNode)) |
| 256 | |
{ |
| 257 | 840 | unique.add(eachPredicateNode); |
| 258 | 840 | newNodeSet.add(eachPredicateNode); |
| 259 | |
} |
| 260 | 840 | } |
| 261 | 404 | interimSet.clear(); |
| 262 | |
} |
| 263 | |
|
| 264 | 168 | } else { |
| 265 | 26450 | for (int i = 0; i < contextSize; ++i) { |
| 266 | 26120 | Object eachContextNode = contextNodeSet.get(i); |
| 267 | |
|
| 268 | 26120 | Iterator axisNodeIter = axisIterator(eachContextNode, support); |
| 269 | 26120 | if (axisNodeIter == null || !axisNodeIter.hasNext()) { |
| 270 | 17090 | continue; |
| 271 | |
} |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | 35812 | while (axisNodeIter.hasNext()) { |
| 283 | 26782 | Object eachAxisNode = axisNodeIter.next(); |
| 284 | |
|
| 285 | 26782 | if (matches(eachAxisNode, support)) { |
| 286 | 7118 | interimSet.add(eachAxisNode); |
| 287 | |
} |
| 288 | 26782 | } |
| 289 | |
|
| 290 | |
|
| 291 | 9030 | List predicateNodes = getPredicateSet().evaluatePredicates(interimSet, support); |
| 292 | |
|
| 293 | |
|
| 294 | 9026 | Iterator predicateNodeIter = predicateNodes.iterator(); |
| 295 | 12476 | while (predicateNodeIter.hasNext()) |
| 296 | |
{ |
| 297 | 3450 | Object eachPredicateNode = predicateNodeIter.next(); |
| 298 | 3450 | if (! unique.contains(eachPredicateNode)) |
| 299 | |
{ |
| 300 | 3432 | unique.add(eachPredicateNode); |
| 301 | 3432 | newNodeSet.add(eachPredicateNode); |
| 302 | |
} |
| 303 | 3450 | } |
| 304 | 9026 | interimSet.clear(); |
| 305 | |
} |
| 306 | |
} |
| 307 | |
|
| 308 | 498 | return newNodeSet; |
| 309 | |
} |
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
public boolean matches(Object node, ContextSupport contextSupport) throws JaxenException { |
| 320 | |
|
| 321 | 910616 | Navigator nav = contextSupport.getNavigator(); |
| 322 | 910616 | String myUri = null; |
| 323 | 910616 | String nodeName = null; |
| 324 | 910616 | String nodeUri = null; |
| 325 | |
|
| 326 | 910616 | if (nav.isElement(node)) { |
| 327 | 307498 | nodeName = nav.getElementName(node); |
| 328 | 307498 | nodeUri = nav.getElementNamespaceUri(node); |
| 329 | 307498 | } |
| 330 | 603118 | else if (nav.isText(node)) { |
| 331 | 600006 | return false; |
| 332 | |
} |
| 333 | 3112 | else if (nav.isAttribute(node)) { |
| 334 | 1758 | if (getAxis() != Axis.ATTRIBUTE) { |
| 335 | 2 | return false; |
| 336 | |
} |
| 337 | 1756 | nodeName = nav.getAttributeName(node); |
| 338 | 1756 | nodeUri = nav.getAttributeNamespaceUri(node); |
| 339 | |
|
| 340 | 1756 | } |
| 341 | 1354 | else if (nav.isDocument(node)) { |
| 342 | 108 | return false; |
| 343 | |
} |
| 344 | 1246 | else if (nav.isNamespace(node)) { |
| 345 | 958 | if (getAxis() != Axis.NAMESPACE) { |
| 346 | |
|
| 347 | 10 | return false; |
| 348 | |
} |
| 349 | 948 | nodeName = nav.getNamespacePrefix(node); |
| 350 | 948 | } |
| 351 | |
else { |
| 352 | 288 | return false; |
| 353 | |
} |
| 354 | |
|
| 355 | 310202 | if (hasPrefix) { |
| 356 | 140 | myUri = contextSupport.translateNamespacePrefixToUri(this.prefix); |
| 357 | 140 | if (myUri == null) { |
| 358 | 2 | throw new UnresolvableException("Cannot resolve namespace prefix '"+this.prefix+"'"); |
| 359 | |
} |
| 360 | |
} |
| 361 | 310062 | else if (matchesAnyName) { |
| 362 | 13946 | return true; |
| 363 | |
} |
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | 296254 | if (hasNamespace(myUri) != hasNamespace(nodeUri)) { |
| 368 | 56 | return false; |
| 369 | |
} |
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | 296198 | if (matchesAnyName || nodeName.equals(getLocalName())) { |
| 375 | 14020 | return matchesNamespaceURIs(myUri, nodeUri); |
| 376 | |
} |
| 377 | |
|
| 378 | 282178 | return false; |
| 379 | |
} |
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
private boolean hasNamespace(String uri) { |
| 388 | 592508 | return (uri != null && uri.length() > 0); |
| 389 | |
} |
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
protected boolean matchesNamespaceURIs(String uri1, String uri2) { |
| 399 | 14020 | if (uri1 == uri2) { |
| 400 | 7218 | return true; |
| 401 | |
} |
| 402 | 6802 | if (uri1 == null) { |
| 403 | 6798 | return (uri2.length() == 0); |
| 404 | |
} |
| 405 | 4 | if (uri2 == null) { |
| 406 | 0 | return (uri1.length() == 0); |
| 407 | |
} |
| 408 | 4 | return uri1.equals(uri2); |
| 409 | |
} |
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
public String toString() { |
| 417 | 6 | String prefix = getPrefix(); |
| 418 | 6 | String qName = "".equals(prefix) ? getLocalName() : getPrefix() + ":" + getLocalName(); |
| 419 | 6 | return "[(DefaultNameStep): " + qName + "]"; |
| 420 | |
} |
| 421 | |
|
| 422 | |
} |