| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.instruct.CopyOf; |
| 12 |
import net.sf.saxon.expr.parser.*; |
| 13 |
import net.sf.saxon.expr.sort.DocumentSorter; |
| 14 |
import net.sf.saxon.functions.Doc; |
| 15 |
import net.sf.saxon.functions.DocumentFn; |
| 16 |
import net.sf.saxon.functions.KeyFn; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.pattern.*; |
| 19 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.type.*; |
| 22 |
import net.sf.saxon.value.Cardinality; |
| 23 |
import net.sf.saxon.value.IntegerValue; |
| 24 |
import net.sf.saxon.value.SequenceType; |
| 25 |
|
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.List; |
| 28 |
import java.util.Stack; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class SlashExpression extends BinaryExpression |
| 40 |
implements ContextSwitchingExpression { |
| 41 |
|
| 42 |
boolean contextFree; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public SlashExpression(Expression start, Expression step) { |
| 53 |
super(start, Token.SLASH, step); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
protected OperandRole getOperandRole(int arg) { |
| 58 |
return arg==0 ? OperandRole.FOCUS_CONTROLLING_SELECT : OperandRole.FOCUS_CONTROLLED_ACTION; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public Expression getStart() { |
| 67 |
return getLhsExpression(); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public void setStart(Expression start) { |
| 76 |
setLhsExpression(start); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public Expression getStep() { |
| 85 |
return getRhsExpression(); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public void setStep(Expression step) { |
| 94 |
setRhsExpression(step); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public String getExpressionName() { |
| 99 |
return "pathExpression"; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public Expression getSelectExpression() { |
| 109 |
return getStart(); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public Expression getActionExpression() { |
| 119 |
return getStep(); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public final ItemType getItemType() { |
| 130 |
return getStep().getItemType(); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
@Override |
| 142 |
public UType getStaticUType(UType contextItemType) { |
| 143 |
return getStep().getStaticUType(getStart().getStaticUType(contextItemType)); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
@Override |
| 161 |
public IntegerValue[] getIntegerBounds() { |
| 162 |
return getStep().getIntegerBounds(); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 171 |
|
| 172 |
getLhs().typeCheck(visitor, contextInfo); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if (Literal.isEmptySequence(getStart())) { |
| 178 |
return getStart(); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
Configuration config = visitor.getConfiguration(); |
| 184 |
TypeChecker tc = config.getTypeChecker(false); |
| 185 |
RoleDiagnostic role0 = new RoleDiagnostic(RoleDiagnostic.BINARY_EXPR, "/", 0); |
| 186 |
role0.setErrorCode("XPTY0019"); |
| 187 |
setStart(tc.staticTypeCheck(getStart(), SequenceType.NODE_SEQUENCE, role0, visitor)); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
ItemType startType = getStart().getItemType(); |
| 192 |
if (startType == ErrorType.getInstance()) { |
| 193 |
|
| 194 |
return Literal.makeEmptySequence(); |
| 195 |
} |
| 196 |
|
| 197 |
ContextItemStaticInfo cit = config.makeContextItemStaticInfo(startType, false); |
| 198 |
cit.setContextSettingExpression(getStart()); |
| 199 |
getRhs().typeCheck(visitor, cit); |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
Expression e2 = simplifyDescendantPath(visitor.getStaticContext()); |
| 205 |
if (e2 != null) { |
| 206 |
return e2.typeCheck(visitor, contextInfo); |
| 207 |
} |
| 208 |
|
| 209 |
if (getStart() instanceof ContextItemExpression && |
| 210 |
getStep().hasSpecialProperty(StaticProperty.ORDERED_NODESET)) { |
| 211 |
return getStep(); |
| 212 |
} |
| 213 |
|
| 214 |
if (getStep() instanceof ContextItemExpression && |
| 215 |
getStart().hasSpecialProperty(StaticProperty.ORDERED_NODESET)) { |
| 216 |
return getStart(); |
| 217 |
} |
| 218 |
|
| 219 |
if (getStep() instanceof AxisExpression && ((AxisExpression) getStep()).getAxis() == AxisInfo.SELF && |
| 220 |
config.getTypeHierarchy().isSubType(startType, getStep().getItemType())) { |
| 221 |
return getStart(); |
| 222 |
} |
| 223 |
|
| 224 |
return this; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public SlashExpression simplifyDescendantPath(StaticContext env) { |
| 234 |
|
| 235 |
Expression underlyingStep = getStep(); |
| 236 |
while (underlyingStep instanceof FilterExpression) { |
| 237 |
if (((FilterExpression) underlyingStep).isPositional(env.getConfiguration().getTypeHierarchy())) { |
| 238 |
return null; |
| 239 |
} |
| 240 |
underlyingStep = ((FilterExpression) underlyingStep).getSelectExpression(); |
| 241 |
} |
| 242 |
|
| 243 |
if (!(underlyingStep instanceof AxisExpression)) { |
| 244 |
return null; |
| 245 |
} |
| 246 |
|
| 247 |
Expression st = getStart(); |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if (st instanceof AxisExpression) { |
| 252 |
AxisExpression stax = (AxisExpression) st; |
| 253 |
if (stax.getAxis() != AxisInfo.DESCENDANT_OR_SELF) { |
| 254 |
return null; |
| 255 |
} |
| 256 |
ContextItemExpression cie = new ContextItemExpression(); |
| 257 |
ExpressionTool.copyLocationInfo(this, cie); |
| 258 |
st = ExpressionTool.makePathExpression(cie, stax.copy(new RebindingMap())); |
| 259 |
ExpressionTool.copyLocationInfo(this, st); |
| 260 |
} |
| 261 |
|
| 262 |
if (!(st instanceof SlashExpression)) { |
| 263 |
return null; |
| 264 |
} |
| 265 |
|
| 266 |
SlashExpression startPath = (SlashExpression) st; |
| 267 |
if (!(startPath.getStep() instanceof AxisExpression)) { |
| 268 |
return null; |
| 269 |
} |
| 270 |
|
| 271 |
AxisExpression mid = (AxisExpression) startPath.getStep(); |
| 272 |
if (mid.getAxis() != AxisInfo.DESCENDANT_OR_SELF) { |
| 273 |
return null; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
NodeTest test = mid.getNodeTest(); |
| 279 |
if (!(test == null || test instanceof AnyNodeTest)) { |
| 280 |
return null; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
byte underlyingAxis = ((AxisExpression) underlyingStep).getAxis(); |
| 286 |
if (underlyingAxis == AxisInfo.CHILD || |
| 287 |
underlyingAxis == AxisInfo.DESCENDANT || |
| 288 |
underlyingAxis == AxisInfo.DESCENDANT_OR_SELF) { |
| 289 |
byte newAxis = underlyingAxis == AxisInfo.DESCENDANT_OR_SELF ? AxisInfo.DESCENDANT_OR_SELF : AxisInfo.DESCENDANT; |
| 290 |
Expression newStep = |
| 291 |
new AxisExpression(newAxis, |
| 292 |
((AxisExpression) underlyingStep).getNodeTest()); |
| 293 |
ExpressionTool.copyLocationInfo(this, newStep); |
| 294 |
|
| 295 |
underlyingStep = getStep(); |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
Stack<Expression> filters = new Stack<Expression>(); |
| 302 |
while (underlyingStep instanceof FilterExpression) { |
| 303 |
filters.add(((FilterExpression) underlyingStep).getFilter()); |
| 304 |
underlyingStep = ((FilterExpression) underlyingStep).getSelectExpression(); |
| 305 |
} |
| 306 |
while (!filters.isEmpty()) { |
| 307 |
newStep = new FilterExpression(newStep, filters.pop()); |
| 308 |
ExpressionTool.copyLocationInfo(getStep(), newStep); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
Expression newPath = ExpressionTool.makePathExpression(startPath.getStart(), newStep); |
| 317 |
if (!(newPath instanceof SlashExpression)) { |
| 318 |
return null; |
| 319 |
} |
| 320 |
ExpressionTool.copyLocationInfo(this, newPath); |
| 321 |
return (SlashExpression) newPath; |
| 322 |
} |
| 323 |
|
| 324 |
if (underlyingAxis == AxisInfo.ATTRIBUTE) { |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
Expression newStep = |
| 329 |
new AxisExpression(AxisInfo.DESCENDANT_OR_SELF, NodeKindTest.ELEMENT); |
| 330 |
ExpressionTool.copyLocationInfo(this, newStep); |
| 331 |
Expression e2 = ExpressionTool.makePathExpression(startPath.getStart(), newStep); |
| 332 |
Expression e3 = ExpressionTool.makePathExpression(e2, getStep()); |
| 333 |
if (!(e3 instanceof SlashExpression)) { |
| 334 |
return null; |
| 335 |
} |
| 336 |
ExpressionTool.copyLocationInfo(this, e3); |
| 337 |
return (SlashExpression) e3; |
| 338 |
} |
| 339 |
|
| 340 |
return null; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 345 |
Configuration config = visitor.getConfiguration(); |
| 346 |
TypeHierarchy th = config.getTypeHierarchy(); |
| 347 |
Optimizer opt = visitor.obtainOptimizer(); |
| 348 |
|
| 349 |
getLhs().optimize(visitor, contextItemType); |
| 350 |
ContextItemStaticInfo cit = visitor.getConfiguration().makeContextItemStaticInfo(getStart().getItemType(), false); |
| 351 |
cit.setContextSettingExpression(getStart()); |
| 352 |
getRhs().optimize(visitor, cit); |
| 353 |
|
| 354 |
if (Literal.isEmptySequence(getStart()) || Literal.isEmptySequence(getStep())) { |
| 355 |
return Literal.makeEmptySequence(); |
| 356 |
} |
| 357 |
|
| 358 |
if (getStart() instanceof RootExpression && th.isSubType(contextItemType.getItemType(), NodeKindTest.DOCUMENT)) { |
| 359 |
|
| 360 |
return getStep(); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
Expression e2 = simplifyDescendantPath(visitor.getStaticContext()); |
| 366 |
if (e2 != null) { |
| 367 |
return e2.optimize(visitor, contextItemType); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
Expression firstStep = getFirstStep(); |
| 373 |
if (!(firstStep.isCallOn(Doc.class) || firstStep.isCallOn(DocumentFn.class))) { |
| 374 |
|
| 375 |
Expression lastStep = getLastStep(); |
| 376 |
if (lastStep instanceof FilterExpression && !((FilterExpression) lastStep).isPositional(th)) { |
| 377 |
Expression leading = getLeadingSteps(); |
| 378 |
Expression p2 = ExpressionTool.makePathExpression(leading, ((FilterExpression) lastStep).getSelectExpression()); |
| 379 |
Expression f2 = new FilterExpression(p2, ((FilterExpression) lastStep).getFilter()); |
| 380 |
ExpressionTool.copyLocationInfo(this, f2); |
| 381 |
return f2.optimize(visitor, contextItemType); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
if (!visitor.isOptimizeForStreaming()) { |
| 386 |
Expression k = opt.convertPathExpressionToKey(this, visitor); |
| 387 |
if (k != null) { |
| 388 |
return k.typeCheck(visitor, contextItemType).optimize(visitor, contextItemType); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
e2 = tryToMakeSorted(visitor, contextItemType); |
| 396 |
if (e2 != null) { |
| 397 |
return e2; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
if (getStep() instanceof AxisExpression) { |
| 404 |
if (!Cardinality.allowsMany(getStart().getCardinality())) { |
| 405 |
SimpleStepExpression sse = new SimpleStepExpression(getStart(), getStep()); |
| 406 |
ExpressionTool.copyLocationInfo(this, sse); |
| 407 |
sse.setParentExpression(getParentExpression()); |
| 408 |
return sse; |
| 409 |
} else { |
| 410 |
contextFree = true; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
if (getStart() instanceof RootExpression && getStep().isCallOn(KeyFn.class)) { |
| 415 |
|
| 416 |
SystemFunctionCall keyCall = (SystemFunctionCall)getStep(); |
| 417 |
if (keyCall.getArity() == 3 && keyCall.getArg(2) instanceof ContextItemExpression) { |
| 418 |
keyCall.setArg(2, new RootExpression()); |
| 419 |
keyCall.setParentExpression(getParentExpression()); |
| 420 |
ExpressionTool.resetStaticProperties(keyCall); |
| 421 |
return keyCall; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
Expression k = promoteFocusIndependentSubexpressions(visitor, contextItemType); |
| 426 |
if (k != this) { |
| 427 |
return k; |
| 428 |
} |
| 429 |
|
| 430 |
if (visitor.isOptimizeForStreaming()) { |
| 431 |
|
| 432 |
Expression rawStep = ExpressionTool.unfilteredExpression(getStep(), true); |
| 433 |
if (rawStep instanceof CopyOf && ((CopyOf) rawStep).getSelect() instanceof ContextItemExpression) { |
| 434 |
((CopyOf) rawStep).setSelect(getStart()); |
| 435 |
rawStep.resetLocalStaticProperties(); |
| 436 |
getStep().resetLocalStaticProperties(); |
| 437 |
return getStep(); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
return this; |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
public SlashExpression tryToMakeAbsolute() { |
| 455 |
Expression first = getFirstStep(); |
| 456 |
if (first.getItemType().getPrimitiveType() == Type.DOCUMENT) { |
| 457 |
return this; |
| 458 |
} |
| 459 |
if (first instanceof AxisExpression) { |
| 460 |
|
| 461 |
ItemType contextItemType = ((AxisExpression) first).getContextItemType(); |
| 462 |
if (contextItemType != null && contextItemType.getPrimitiveType() == Type.DOCUMENT) { |
| 463 |
RootExpression root = new RootExpression(); |
| 464 |
ExpressionTool.copyLocationInfo(this, root); |
| 465 |
Expression path = ExpressionTool.makePathExpression(root, this.copy(new RebindingMap())); |
| 466 |
if (!(path instanceof SlashExpression)) { |
| 467 |
return null; |
| 468 |
} |
| 469 |
ExpressionTool.copyLocationInfo(this, path); |
| 470 |
return (SlashExpression) path; |
| 471 |
} |
| 472 |
} |
| 473 |
if (first instanceof DocumentSorter && ((DocumentSorter) first).getBaseExpression() instanceof SlashExpression) { |
| 474 |
|
| 475 |
SlashExpression se = (SlashExpression) ((DocumentSorter) first).getBaseExpression(); |
| 476 |
SlashExpression se2 = se.tryToMakeAbsolute(); |
| 477 |
if (se2 != null) { |
| 478 |
if (se2 == se) { |
| 479 |
return this; |
| 480 |
} else { |
| 481 |
Expression rest = getRemainingSteps(); |
| 482 |
DocumentSorter ds = new DocumentSorter(se2); |
| 483 |
return new SlashExpression(ds, rest); |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
return null; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
@Override |
| 499 |
public int getCost() { |
| 500 |
int factor = Cardinality.allowsMany(getLhsExpression().getCardinality()) ? 10 : 1; |
| 501 |
int lh = getLhsExpression().getCost() + 1; |
| 502 |
int rh = getRhsExpression().getCost(); |
| 503 |
double product = (double) lh + factor * (double) rh; |
| 504 |
return product > 1e7 ? 10000000 : Math.max(lh + factor * rh, 10000000); |
| 505 |
} |
| 506 |
|
| 507 |
public Expression tryToMakeSorted(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
Configuration config = visitor.getConfiguration(); |
| 512 |
TypeHierarchy th = config.getTypeHierarchy(); |
| 513 |
Optimizer opt = visitor.obtainOptimizer(); |
| 514 |
Expression s1 = ExpressionTool.unfilteredExpression(getStart(), false); |
| 515 |
if (!(s1 instanceof AxisExpression && ((AxisExpression) s1).getAxis() == AxisInfo.DESCENDANT)) { |
| 516 |
return null; |
| 517 |
} |
| 518 |
Expression s2 = ExpressionTool.unfilteredExpression(getStep(), false); |
| 519 |
if (!(s2 instanceof AxisExpression && ((AxisExpression) s2).getAxis() == AxisInfo.CHILD)) { |
| 520 |
return null; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
Expression x = getStart().copy(new RebindingMap()); |
| 525 |
AxisExpression ax = (AxisExpression) ExpressionTool.unfilteredExpression(x, false); |
| 526 |
ax.setAxis(AxisInfo.PARENT); |
| 527 |
|
| 528 |
Expression y = getStep().copy(new RebindingMap()); |
| 529 |
AxisExpression ay = (AxisExpression) ExpressionTool.unfilteredExpression(y, false); |
| 530 |
ay.setAxis(AxisInfo.DESCENDANT); |
| 531 |
|
| 532 |
Expression k = new FilterExpression(y, x); |
| 533 |
|
| 534 |
if (!th.isSubType(contextItemType.getItemType(), NodeKindTest.DOCUMENT)) { |
| 535 |
k = new SlashExpression(new AxisExpression(AxisInfo.CHILD, NodeKindTest.ELEMENT), k); |
| 536 |
ExpressionTool.copyLocationInfo(this, k); |
| 537 |
opt.trace("Rewrote descendant::X/child::Y as child::*/descendant::Y[parent::X]", k); |
| 538 |
} else { |
| 539 |
ExpressionTool.copyLocationInfo(this, k); |
| 540 |
opt.trace("Rewrote descendant::X/child::Y as descendant::Y[parent::X]", k); |
| 541 |
} |
| 542 |
return k; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
protected Expression promoteFocusIndependentSubexpressions( |
| 559 |
ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
return this; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
@Override |
| 587 |
public Expression unordered(boolean retainAllNodes, boolean forStreaming) throws XPathException { |
| 588 |
setStart(getStart().unordered(retainAllNodes, forStreaming)); |
| 589 |
setStep(getStep().unordered(retainAllNodes, forStreaming)); |
| 590 |
return this; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 605 |
PathMap.PathMapNodeSet target = getStart().addToPathMap(pathMap, pathMapNodeSet); |
| 606 |
return getStep().addToPathMap(pathMap, target); |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
@Override |
| 618 |
public int getImplementationMethod() { |
| 619 |
return ITERATE_METHOD; |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
public Expression copy(RebindingMap rebindings) { |
| 647 |
Expression exp = ExpressionTool.makePathExpression(getStart().copy(rebindings), getStep().copy(rebindings)); |
| 648 |
ExpressionTool.copyLocationInfo(this, exp); |
| 649 |
return exp; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
public int computeSpecialProperties() { |
| 659 |
|
| 660 |
int startProperties = getStart().getSpecialProperties(); |
| 661 |
int stepProperties = getStep().getSpecialProperties(); |
| 662 |
|
| 663 |
if ((stepProperties & StaticProperty.ALL_NODES_NEWLY_CREATED) != 0) { |
| 664 |
|
| 665 |
return StaticProperty.ORDERED_NODESET | StaticProperty.PEER_NODESET | StaticProperty.NO_NODES_NEWLY_CREATED; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
int p = 0; |
| 674 |
if (!Cardinality.allowsMany(getStart().getCardinality())) { |
| 675 |
startProperties |= StaticProperty.ORDERED_NODESET | |
| 676 |
StaticProperty.PEER_NODESET | |
| 677 |
StaticProperty.SINGLE_DOCUMENT_NODESET; |
| 678 |
} |
| 679 |
if (!Cardinality.allowsMany(getStep().getCardinality())) { |
| 680 |
stepProperties |= StaticProperty.ORDERED_NODESET | |
| 681 |
StaticProperty.PEER_NODESET | |
| 682 |
StaticProperty.SINGLE_DOCUMENT_NODESET; |
| 683 |
} |
| 684 |
|
| 685 |
if ((startProperties & stepProperties & StaticProperty.CONTEXT_DOCUMENT_NODESET) != 0) { |
| 686 |
p |= StaticProperty.CONTEXT_DOCUMENT_NODESET; |
| 687 |
} |
| 688 |
if (((startProperties & StaticProperty.SINGLE_DOCUMENT_NODESET) != 0) && |
| 689 |
((stepProperties & StaticProperty.CONTEXT_DOCUMENT_NODESET) != 0)) { |
| 690 |
p |= StaticProperty.SINGLE_DOCUMENT_NODESET; |
| 691 |
} |
| 692 |
if ((startProperties & stepProperties & StaticProperty.PEER_NODESET) != 0) { |
| 693 |
p |= StaticProperty.PEER_NODESET; |
| 694 |
} |
| 695 |
if ((startProperties & stepProperties & StaticProperty.SUBTREE_NODESET) != 0) { |
| 696 |
p |= StaticProperty.SUBTREE_NODESET; |
| 697 |
} |
| 698 |
|
| 699 |
if (testNaturallySorted(startProperties, stepProperties)) { |
| 700 |
p |= StaticProperty.ORDERED_NODESET; |
| 701 |
} |
| 702 |
|
| 703 |
if (testNaturallyReverseSorted()) { |
| 704 |
p |= StaticProperty.REVERSE_DOCUMENT_ORDER; |
| 705 |
} |
| 706 |
|
| 707 |
if ((startProperties & stepProperties & StaticProperty.NO_NODES_NEWLY_CREATED) != 0) { |
| 708 |
p |= StaticProperty.NO_NODES_NEWLY_CREATED; |
| 709 |
} |
| 710 |
|
| 711 |
return p; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
private boolean testNaturallySorted(int startProperties, int stepProperties) { |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
if ((stepProperties & StaticProperty.ORDERED_NODESET) == 0) { |
| 736 |
return false; |
| 737 |
} |
| 738 |
if (Cardinality.allowsMany(getStart().getCardinality())) { |
| 739 |
if ((startProperties & StaticProperty.ORDERED_NODESET) == 0) { |
| 740 |
return false; |
| 741 |
} |
| 742 |
} else { |
| 743 |
|
| 744 |
return true; |
| 745 |
|
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
if ((stepProperties & StaticProperty.ATTRIBUTE_NS_NODESET) != 0) { |
| 755 |
return true; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
if ((stepProperties & StaticProperty.ALL_NODES_NEWLY_CREATED) != 0) { |
| 761 |
return true; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
return ((startProperties & StaticProperty.PEER_NODESET) != 0) && |
| 769 |
((stepProperties & StaticProperty.SUBTREE_NODESET) != 0); |
| 770 |
|
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
private boolean testNaturallyReverseSorted() { |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
if (!Cardinality.allowsMany(getStart().getCardinality()) && |
| 794 |
(getStep() instanceof AxisExpression)) { |
| 795 |
return !AxisInfo.isForwards[((AxisExpression) getStep()).getAxis()]; |
| 796 |
} |
| 797 |
|
| 798 |
return !Cardinality.allowsMany(getStep().getCardinality()) && |
| 799 |
(getStart() instanceof AxisExpression) && |
| 800 |
!AxisInfo.isForwards[((AxisExpression) getStart()).getAxis()]; |
| 801 |
|
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
public int computeCardinality() { |
| 810 |
int c1 = getStart().getCardinality(); |
| 811 |
int c2 = getStep().getCardinality(); |
| 812 |
return Cardinality.multiply(c1, c2); |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
@Override |
| 824 |
public Pattern toPattern(Configuration config) throws XPathException { |
| 825 |
Expression head = getLeadingSteps(); |
| 826 |
Expression tail = getLastStep(); |
| 827 |
if (head instanceof ItemChecker) { |
| 828 |
|
| 829 |
ItemChecker checker = (ItemChecker) head; |
| 830 |
if (checker.getBaseExpression() instanceof ContextItemExpression) { |
| 831 |
return tail.toPattern(config); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
Pattern tailPattern = tail.toPattern(config); |
| 836 |
if (tailPattern instanceof NodeTestPattern) { |
| 837 |
if (tailPattern.getItemType() instanceof ErrorType) { |
| 838 |
return tailPattern; |
| 839 |
} |
| 840 |
} else if (tailPattern instanceof GeneralNodePattern) { |
| 841 |
return new GeneralNodePattern(this, (NodeTest)tailPattern.getItemType()); |
| 842 |
} |
| 843 |
|
| 844 |
byte axis = AxisInfo.PARENT; |
| 845 |
Pattern headPattern = null; |
| 846 |
if (head instanceof SlashExpression) { |
| 847 |
SlashExpression start = (SlashExpression) head; |
| 848 |
if (start.getActionExpression() instanceof AxisExpression) { |
| 849 |
AxisExpression mid = (AxisExpression) start.getActionExpression(); |
| 850 |
if (mid.getAxis() == AxisInfo.DESCENDANT_OR_SELF && |
| 851 |
(mid.getNodeTest() == null || mid.getNodeTest() instanceof AnyNodeTest)) { |
| 852 |
axis = AxisInfo.ANCESTOR; |
| 853 |
headPattern = start.getSelectExpression().toPattern(config); |
| 854 |
} |
| 855 |
} |
| 856 |
} |
| 857 |
if (headPattern == null) { |
| 858 |
axis = PatternMaker.getAxisForPathStep(tail); |
| 859 |
headPattern = head.toPattern(config); |
| 860 |
} |
| 861 |
return new AncestorQualifiedPattern(tailPattern, headPattern, axis); |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
public boolean isContextFree() { |
| 873 |
return contextFree; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
public void setContextFree(boolean free) { |
| 883 |
this.contextFree = free; |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
public boolean equals(Object other) { |
| 891 |
if (!(other instanceof SlashExpression)) { |
| 892 |
return false; |
| 893 |
} |
| 894 |
SlashExpression p = (SlashExpression) other; |
| 895 |
return getStart().isEqual(p.getStart()) && getStep().isEqual(p.getStep()); |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
public int computeHashCode() { |
| 903 |
return "SlashExpression".hashCode() + getStart().hashCode() + getStep().hashCode(); |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
public SequenceIterator<?> iterate(final XPathContext context) throws XPathException { |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
if (contextFree) { |
| 920 |
return new MappingIterator<>( |
| 921 |
getStart().iterate(context), |
| 922 |
(MappingFunction<Item<?>, NodeInfo>) item -> |
| 923 |
((AxisExpression) getRhsExpression()).iterate((NodeInfo)item)); |
| 924 |
} |
| 925 |
|
| 926 |
XPathContext context2 = context.newMinorContext(); |
| 927 |
context2.trackFocus(getStart().iterate(context)); |
| 928 |
return new ContextMappingIterator<Item<?>>(c1 -> getStep().iterate(c1), context2); |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
public void export(ExpressionPresenter destination) throws XPathException { |
| 946 |
destination.startElement("slash", this); |
| 947 |
if (this instanceof SimpleStepExpression) { |
| 948 |
destination.emitAttribute("simple", "1"); |
| 949 |
} else if (isContextFree()) { |
| 950 |
destination.emitAttribute("simple", "2"); |
| 951 |
} |
| 952 |
getStart().export(destination); |
| 953 |
getStep().export(destination); |
| 954 |
destination.endElement(); |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
public String toString() { |
| 966 |
return ExpressionTool.parenthesize(getStart()) + "/" + ExpressionTool.parenthesize(getStep()); |
| 967 |
} |
| 968 |
|
| 969 |
@Override |
| 970 |
public String toShortString() { |
| 971 |
return ExpressionTool.parenthesizeShort(getStart()) + "/" + ExpressionTool.parenthesizeShort(getStep()); |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
public Expression getFirstStep() { |
| 982 |
if (getStart() instanceof SlashExpression) { |
| 983 |
return ((SlashExpression) getStart()).getFirstStep(); |
| 984 |
} else { |
| 985 |
return getStart(); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
public Expression getRemainingSteps() { |
| 1000 |
if (getStart() instanceof SlashExpression) { |
| 1001 |
List<Expression> list = new ArrayList<Expression>(8); |
| 1002 |
gatherSteps(list); |
| 1003 |
Expression rem = rebuildSteps(list.subList(1, list.size())); |
| 1004 |
ExpressionTool.copyLocationInfo(this, rem); |
| 1005 |
return rem; |
| 1006 |
} else { |
| 1007 |
return getStep(); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
private void gatherSteps(List<Expression> list) { |
| 1017 |
if (getStart() instanceof SlashExpression) { |
| 1018 |
((SlashExpression)getStart()).gatherSteps(list); |
| 1019 |
} else { |
| 1020 |
list.add(getStart()); |
| 1021 |
} |
| 1022 |
if (getStep() instanceof SlashExpression) { |
| 1023 |
((SlashExpression) getStep()).gatherSteps(list); |
| 1024 |
} else { |
| 1025 |
list.add(getStep()); |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
private Expression rebuildSteps(List<Expression> list) { |
| 1036 |
if (list.size() == 1) { |
| 1037 |
return list.get(0).copy(new RebindingMap()); |
| 1038 |
} else { |
| 1039 |
return new SlashExpression(list.get(0).copy(new RebindingMap()), rebuildSteps(list.subList(1, list.size()))); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
public Expression getLastStep() { |
| 1050 |
if (getStep() instanceof SlashExpression) { |
| 1051 |
return ((SlashExpression) getStep()).getLastStep(); |
| 1052 |
} else { |
| 1053 |
return getStep(); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
public Expression getLeadingSteps() { |
| 1065 |
if (getStep() instanceof SlashExpression) { |
| 1066 |
List<Expression> list = new ArrayList<Expression>(8); |
| 1067 |
gatherSteps(list); |
| 1068 |
Expression rem = rebuildSteps(list.subList(0, list.size()-1)); |
| 1069 |
ExpressionTool.copyLocationInfo(this, rem); |
| 1070 |
return rem; |
| 1071 |
} else { |
| 1072 |
return getStart(); |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
public boolean isAbsolute() { |
| 1084 |
return getFirstStep().getItemType().getPrimitiveType() == Type.DOCUMENT; |
| 1085 |
} |
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
@Override |
| 1095 |
public String getStreamerName() { |
| 1096 |
return "ForEach"; |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|