| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
|
| 11 |
import net.sf.saxon.Configuration; |
| 12 |
import net.sf.saxon.expr.instruct.Choose; |
| 13 |
import net.sf.saxon.expr.parser.*; |
| 14 |
import net.sf.saxon.functions.PositionAndLast; |
| 15 |
import net.sf.saxon.functions.SystemFunction; |
| 16 |
import net.sf.saxon.functions.registry.VendorFunctionSetHE; |
| 17 |
import net.sf.saxon.lib.Feature; |
| 18 |
import net.sf.saxon.lib.NamespaceConstant; |
| 19 |
import net.sf.saxon.om.*; |
| 20 |
import net.sf.saxon.pattern.*; |
| 21 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 22 |
import net.sf.saxon.trans.XPathException; |
| 23 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 24 |
import net.sf.saxon.type.*; |
| 25 |
import net.sf.saxon.value.*; |
| 26 |
|
| 27 |
import java.math.BigInteger; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public final class FilterExpression extends BinaryExpression implements ContextSwitchingExpression { |
| 36 |
|
| 37 |
private boolean filterIsPositional; |
| 38 |
|
| 39 |
private boolean filterIsSingletonBoolean; |
| 40 |
private boolean filterIsIndependent; |
| 41 |
|
| 42 |
public static final int FILTERED = 10000; |
| 43 |
|
| 44 |
public final static OperandRole FILTER_PREDICATE = |
| 45 |
new OperandRole(OperandRole.USES_NEW_FOCUS | OperandRole.HIGHER_ORDER, OperandUsage.INSPECTION, SequenceType.ANY_SEQUENCE); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public FilterExpression(Expression base, Expression filter) { |
| 56 |
super(base, Token.LSQB, filter); |
| 57 |
base.setFiltered(true); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
protected OperandRole getOperandRole(int arg) { |
| 62 |
return arg == 0 ? OperandRole.SAME_FOCUS_ACTION : FILTER_PREDICATE; |
| 63 |
} |
| 64 |
|
| 65 |
public Expression getBase() { |
| 66 |
return getLhsExpression(); |
| 67 |
} |
| 68 |
|
| 69 |
public void setBase(Expression base) { |
| 70 |
setLhsExpression(base); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public Expression getFilter() { |
| 80 |
return getRhsExpression(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
public void setFilter(Expression filter) { |
| 85 |
setRhsExpression(filter); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public String getExpressionName() { |
| 97 |
return "filter"; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public ItemType getItemType() { |
| 108 |
|
| 109 |
if (getFilter() instanceof InstanceOfExpression && |
| 110 |
((InstanceOfExpression) getFilter()).getBaseExpression() instanceof ContextItemExpression) { |
| 111 |
return ((InstanceOfExpression) getFilter()).getRequiredItemType(); |
| 112 |
} |
| 113 |
return getBase().getItemType(); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
@Override |
| 125 |
public UType getStaticUType(UType contextItemType) { |
| 126 |
return getBase().getStaticUType(contextItemType); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public Expression getSelectExpression() { |
| 137 |
return getBase(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public boolean isFilterIsPositional() { |
| 145 |
return filterIsPositional; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public Expression getActionExpression() { |
| 155 |
return getFilter(); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public boolean isPositional(TypeHierarchy th) { |
| 168 |
return isPositionalFilter(getFilter(), th); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public boolean isSimpleBooleanFilter() { |
| 179 |
return filterIsSingletonBoolean; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public boolean isIndependentFilter() { |
| 190 |
return filterIsIndependent; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public Expression simplify() throws XPathException { |
| 201 |
|
| 202 |
setBase(getBase().simplify()); |
| 203 |
setFilter(getFilter().simplify()); |
| 204 |
|
| 205 |
|
| 206 |
if (Literal.isEmptySequence(getBase())) { |
| 207 |
return getBase(); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
if (getFilter() instanceof Literal && !(((Literal) getFilter()).getValue() instanceof NumericValue)) { |
| 212 |
try { |
| 213 |
if (getFilter().effectiveBooleanValue(new EarlyEvaluationContext(getConfiguration()))) { |
| 214 |
return getBase(); |
| 215 |
} else { |
| 216 |
return Literal.makeEmptySequence(); |
| 217 |
} |
| 218 |
} catch (XPathException e) { |
| 219 |
e.maybeSetLocation(getLocation()); |
| 220 |
throw e; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
if (getFilter().isCallOn(PositionAndLast.Last.class)) { |
| 227 |
setFilter(new IsLastExpression(true)); |
| 228 |
adoptChildExpression(getFilter()); |
| 229 |
} |
| 230 |
|
| 231 |
return this; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 245 |
Configuration config = visitor.getConfiguration(); |
| 246 |
final TypeHierarchy th = config.getTypeHierarchy(); |
| 247 |
getLhs().typeCheck(visitor, contextInfo); |
| 248 |
getBase().setFiltered(true); |
| 249 |
if (Literal.isEmptySequence(getBase())) { |
| 250 |
return getBase(); |
| 251 |
} |
| 252 |
|
| 253 |
ContextItemStaticInfo baseItemType = config.makeContextItemStaticInfo(getSelectExpression().getItemType(), false); |
| 254 |
baseItemType.setContextSettingExpression(getBase()); |
| 255 |
getRhs().typeCheck(visitor, baseItemType); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
Expression filter2 = ExpressionTool.unsortedIfHomogeneous(getFilter(), visitor.isOptimizeForStreaming()); |
| 260 |
if (filter2 != getFilter()) { |
| 261 |
setFilter(filter2); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
if (Literal.isConstantOne(getFilter())) { |
| 267 |
Expression fie = FirstItemExpression.makeFirstItemExpression(getBase()); |
| 268 |
ExpressionTool.copyLocationInfo(this, fie); |
| 269 |
return fie; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
filterIsPositional = isPositionalFilter(getFilter(), th); |
| 274 |
|
| 275 |
|
| 276 |
filterIsSingletonBoolean = |
| 277 |
getFilter().getCardinality() == StaticProperty.EXACTLY_ONE && |
| 278 |
getFilter().getItemType().equals(BuiltInAtomicType.BOOLEAN); |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
filterIsIndependent = (getFilter().getDependencies() & StaticProperty.DEPENDS_ON_FOCUS) == 0; |
| 283 |
|
| 284 |
ExpressionTool.resetStaticProperties(this); |
| 285 |
return this; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 305 |
final Configuration config = visitor.getConfiguration(); |
| 306 |
final Optimizer opt = visitor.obtainOptimizer(); |
| 307 |
final boolean tracing = config.getBooleanProperty(Feature.TRACE_OPTIMIZER_DECISIONS); |
| 308 |
final TypeHierarchy th = config.getTypeHierarchy(); |
| 309 |
|
| 310 |
getLhs().optimize(visitor, contextItemType); |
| 311 |
getBase().setFiltered(true); |
| 312 |
|
| 313 |
ContextItemStaticInfo baseItemType = config.makeContextItemStaticInfo(getSelectExpression().getItemType(), false); |
| 314 |
baseItemType.setContextSettingExpression(getBase()); |
| 315 |
getRhs().optimize(visitor, baseItemType); |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
Expression filter2 = ExpressionTool.unsortedIfHomogeneous(getFilter(), visitor.isOptimizeForStreaming()); |
| 320 |
if (filter2 != getFilter()) { |
| 321 |
setFilter(filter2); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
if (getFilter() instanceof IsLastExpression && |
| 327 |
((IsLastExpression) getFilter()).getCondition() && |
| 328 |
getBase() instanceof AxisExpression && |
| 329 |
((AxisExpression) getBase()).getAxis() == AxisInfo.CHILD) { |
| 330 |
NodeTest test = ((AxisExpression) getBase()).getNodeTest(); |
| 331 |
AxisExpression fs = new AxisExpression(AxisInfo.FOLLOWING_SIBLING, test); |
| 332 |
setFilter(SystemFunction.makeCall("empty", getRetainedStaticContext(), fs)); |
| 333 |
if (tracing) { |
| 334 |
Optimizer.trace(config, "Replaced [last()] predicate by test for following-sibling", this); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
ItemType filterType = getFilter().getItemType(); |
| 342 |
if (!th.isSubType(filterType, BuiltInAtomicType.BOOLEAN) |
| 343 |
&& th.relationship(filterType, NumericType.getInstance()) == TypeHierarchy.DISJOINT) { |
| 344 |
Expression f = SystemFunction.makeCall("boolean", getRetainedStaticContext(), getFilter()); |
| 345 |
setFilter(f.optimize(visitor, baseItemType)); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
if (getFilter() instanceof Literal && ((Literal) getFilter()).getValue() instanceof BooleanValue) { |
| 350 |
if (((BooleanValue) ((Literal) getFilter()).getValue()).getBooleanValue()) { |
| 351 |
if (tracing) { |
| 352 |
opt.trace("Redundant filter removed", getBase()); |
| 353 |
} |
| 354 |
return getBase(); |
| 355 |
} else { |
| 356 |
Expression result = Literal.makeEmptySequence(); |
| 357 |
ExpressionTool.copyLocationInfo(this, result); |
| 358 |
if (tracing) { |
| 359 |
opt.trace("Filter expression eliminated because predicate is always false", result); |
| 360 |
} |
| 361 |
return result; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
filterIsPositional = isPositionalFilter(getFilter(), th); |
| 367 |
filterIsSingletonBoolean = |
| 368 |
getFilter().getCardinality() == StaticProperty.EXACTLY_ONE && |
| 369 |
getFilter().getItemType().equals(BuiltInAtomicType.BOOLEAN); |
| 370 |
|
| 371 |
|
| 372 |
if (!filterIsPositional && !visitor.isOptimizeForStreaming()) { |
| 373 |
int isIndexable = opt.isIndexableFilter(getFilter()); |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
if (isIndexable != 0) { |
| 378 |
boolean contextIsDoc = contextItemType != null && contextItemType.getItemType() != ErrorType.getInstance() && |
| 379 |
th.isSubType(contextItemType.getItemType(), NodeKindTest.DOCUMENT); |
| 380 |
Expression f = opt.tryIndexedFilter(this, visitor, isIndexable > 0, contextIsDoc); |
| 381 |
if (f != this) { |
| 382 |
return f.typeCheck(visitor, contextItemType).optimize(visitor, contextItemType); |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
if (filterIsPositional && |
| 391 |
getFilter() instanceof BooleanExpression && |
| 392 |
((BooleanExpression) getFilter()).operator == Token.AND) { |
| 393 |
BooleanExpression bf = (BooleanExpression) getFilter(); |
| 394 |
if (isExplicitlyPositional(bf.getLhsExpression()) && |
| 395 |
!isExplicitlyPositional(bf.getRhsExpression())) { |
| 396 |
Expression p0 = forceToBoolean(bf.getLhsExpression()); |
| 397 |
Expression p1 = forceToBoolean(bf.getRhsExpression()); |
| 398 |
FilterExpression f1 = new FilterExpression(getBase(), p0); |
| 399 |
ExpressionTool.copyLocationInfo(this, f1); |
| 400 |
FilterExpression f2 = new FilterExpression(f1, p1); |
| 401 |
ExpressionTool.copyLocationInfo(this, f2); |
| 402 |
if (tracing) { |
| 403 |
opt.trace("Composite filter replaced by nested filter expressions", f2); |
| 404 |
} |
| 405 |
return f2.optimize(visitor, contextItemType); |
| 406 |
} |
| 407 |
if (isExplicitlyPositional(bf.getRhsExpression()) && |
| 408 |
!isExplicitlyPositional(bf.getLhsExpression())) { |
| 409 |
Expression p0 = forceToBoolean(bf.getLhsExpression()); |
| 410 |
Expression p1 = forceToBoolean(bf.getRhsExpression()); |
| 411 |
FilterExpression f1 = new FilterExpression(getBase(), p1); |
| 412 |
ExpressionTool.copyLocationInfo(this, f1); |
| 413 |
FilterExpression f2 = new FilterExpression(f1, p0); |
| 414 |
ExpressionTool.copyLocationInfo(this, f2); |
| 415 |
if (tracing) { |
| 416 |
opt.trace("Composite filter replaced by nested filter expressions", f2); |
| 417 |
} |
| 418 |
return f2.optimize(visitor, contextItemType); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
if (getFilter() instanceof IsLastExpression && |
| 423 |
((IsLastExpression) getFilter()).getCondition()) { |
| 424 |
|
| 425 |
if (getBase() instanceof Literal) { |
| 426 |
setFilter(Literal.makeLiteral(new Int64Value(((Literal) getBase()).getValue().getLength()), this)); |
| 427 |
} else { |
| 428 |
return new LastItemExpression(getBase()); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
Expression subsequence = tryToRewritePositionalFilter(visitor, tracing); |
| 433 |
if (subsequence != null) { |
| 434 |
if (tracing) { |
| 435 |
subsequence.setRetainedStaticContext(getRetainedStaticContext()); |
| 436 |
opt.trace("Rewrote Filter Expression as:", subsequence); |
| 437 |
} |
| 438 |
ExpressionTool.copyLocationInfo(this, subsequence); |
| 439 |
return subsequence.simplify() |
| 440 |
.typeCheck(visitor, contextItemType) |
| 441 |
.optimize(visitor, contextItemType); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
if (!filterIsPositional && getLhsExpression() instanceof FilterExpression && |
| 448 |
!((FilterExpression) getLhsExpression()).isFilterIsPositional()) { |
| 449 |
Expression f2 = opt.reorderPredicates(this, visitor, contextItemType); |
| 450 |
if (f2 != this) { |
| 451 |
return f2; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
final Sequence<?> sequence = tryEarlyEvaluation(visitor); |
| 456 |
if (sequence != null) { |
| 457 |
GroundedValue<?> value = sequence.materialize(); |
| 458 |
return Literal.makeLiteral(value, this); |
| 459 |
} |
| 460 |
|
| 461 |
return this; |
| 462 |
|
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
@Override |
| 474 |
public int getCost() { |
| 475 |
return getLhsExpression().getCost() + 5 * getRhsExpression().getCost(); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
@Override |
| 487 |
public int getImplementationMethod() { |
| 488 |
return ITERATE_METHOD; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
@Override |
| 505 |
public IntegerValue[] getIntegerBounds() { |
| 506 |
return getBase().getIntegerBounds(); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
private Sequence<?> tryEarlyEvaluation(ExpressionVisitor visitor) { |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
try { |
| 515 |
if (getBase() instanceof Literal && |
| 516 |
!ExpressionTool.refersToVariableOrFunction(getFilter()) && |
| 517 |
(getFilter().getDependencies() & ~StaticProperty.DEPENDS_ON_FOCUS) == 0) { |
| 518 |
XPathContext context = visitor.getStaticContext().makeEarlyEvaluationContext(); |
| 519 |
return iterate(context).materialize(); |
| 520 |
} |
| 521 |
} catch (XPathException e) { |
| 522 |
|
| 523 |
|
| 524 |
return null; |
| 525 |
} |
| 526 |
return null; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 540 |
PathMap.PathMapNodeSet target = getBase().addToPathMap(pathMap, pathMapNodeSet); |
| 541 |
getFilter().addToPathMap(pathMap, target); |
| 542 |
return target; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
private static Expression forceToBoolean(Expression in) { |
| 554 |
if (in.getItemType().getPrimitiveType() == StandardNames.XS_BOOLEAN) { |
| 555 |
return in; |
| 556 |
} |
| 557 |
return SystemFunction.makeCall("boolean", in.getRetainedStaticContext(), in); |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
private Expression tryToRewritePositionalFilter(ExpressionVisitor visitor, boolean tracing) throws XPathException { |
| 570 |
Configuration config = visitor.getConfiguration(); |
| 571 |
TypeHierarchy th = config.getTypeHierarchy(); |
| 572 |
if (getFilter() instanceof Literal) { |
| 573 |
GroundedValue val = ((Literal) getFilter()).getValue(); |
| 574 |
if (val instanceof NumericValue) { |
| 575 |
Expression result; |
| 576 |
int lvalue = ((NumericValue)val).asSubscript(); |
| 577 |
if (lvalue != -1) { |
| 578 |
if (lvalue == 1) { |
| 579 |
result = FirstItemExpression.makeFirstItemExpression(getBase()); |
| 580 |
} else { |
| 581 |
result = new SubscriptExpression(getBase(), getFilter()); |
| 582 |
} |
| 583 |
} else { |
| 584 |
result = Literal.makeEmptySequence(); |
| 585 |
} |
| 586 |
if (tracing) { |
| 587 |
Optimizer.trace(config, "Rewriting numeric filter expression with constant subscript", result); |
| 588 |
} |
| 589 |
return result; |
| 590 |
} else { |
| 591 |
Expression result = ExpressionTool.effectiveBooleanValue(val.iterate()) ? getBase() : Literal.makeEmptySequence(); |
| 592 |
if (tracing) { |
| 593 |
Optimizer.trace(config, "Rewriting boolean filter expression with constant subscript", result); |
| 594 |
} |
| 595 |
return result; |
| 596 |
} |
| 597 |
} |
| 598 |
if (th.isSubType(getFilter().getItemType(), NumericType.getInstance()) && |
| 599 |
!Cardinality.allowsMany(getFilter().getCardinality()) && |
| 600 |
(getFilter().getDependencies() & StaticProperty.DEPENDS_ON_FOCUS) == 0) { |
| 601 |
Expression result = new SubscriptExpression(getBase(), getFilter()); |
| 602 |
if (tracing) { |
| 603 |
Optimizer.trace(config, "Rewriting numeric filter expression with focus-independent subscript", result); |
| 604 |
} |
| 605 |
return result; |
| 606 |
} |
| 607 |
if (getFilter() instanceof ComparisonExpression) { |
| 608 |
|
| 609 |
Expression lhs = ((ComparisonExpression) getFilter()).getLhsExpression(); |
| 610 |
Expression rhs = ((ComparisonExpression) getFilter()).getRhsExpression(); |
| 611 |
int operator = ((ComparisonExpression) getFilter()).getSingletonOperator(); |
| 612 |
Expression comparand; |
| 613 |
if (lhs.isCallOn(PositionAndLast.Position.class) |
| 614 |
&& th.isSubType(rhs.getItemType(), NumericType.getInstance())) { |
| 615 |
comparand = rhs; |
| 616 |
} else if (rhs.isCallOn(PositionAndLast.Position.class) |
| 617 |
&& th.isSubType(lhs.getItemType(), NumericType.getInstance())) { |
| 618 |
comparand = lhs; |
| 619 |
operator = Token.inverse(operator); |
| 620 |
} else { |
| 621 |
return null; |
| 622 |
} |
| 623 |
|
| 624 |
if (ExpressionTool.dependsOnFocus(comparand)) { |
| 625 |
return null; |
| 626 |
} |
| 627 |
|
| 628 |
int card = comparand.getCardinality(); |
| 629 |
if (Cardinality.allowsMany(card)) { |
| 630 |
return null; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
if (Cardinality.allowsZero(card)) { |
| 636 |
LetExpression let = new LetExpression(); |
| 637 |
let.setRequiredType(SequenceType.makeSequenceType(comparand.getItemType(), card)); |
| 638 |
let.setVariableQName(new StructuredQName("pp", NamespaceConstant.SAXON, "pp" + let.hashCode())); |
| 639 |
let.setSequence(comparand); |
| 640 |
comparand = new LocalVariableReference(let); |
| 641 |
LocalVariableReference existsArg = new LocalVariableReference(let); |
| 642 |
Expression exists = SystemFunction.makeCall("exists", getRetainedStaticContext(), existsArg); |
| 643 |
Expression rewrite = tryToRewritePositionalFilterSupport(getBase(), comparand, operator, th); |
| 644 |
if (rewrite == null) { |
| 645 |
return this; |
| 646 |
} |
| 647 |
Expression choice = Choose.makeConditional(exists, rewrite); |
| 648 |
let.setAction(choice); |
| 649 |
return let; |
| 650 |
} else { |
| 651 |
return tryToRewritePositionalFilterSupport(getBase(), comparand, operator, th); |
| 652 |
} |
| 653 |
} else if (getFilter() instanceof IntegerRangeTest) { |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
Expression val = ((IntegerRangeTest) getFilter()).getValue(); |
| 658 |
if (!val.isCallOn(PositionAndLast.class)) { |
| 659 |
return null; |
| 660 |
} |
| 661 |
Expression min = ((IntegerRangeTest) getFilter()).getMin(); |
| 662 |
Expression max = ((IntegerRangeTest) getFilter()).getMax(); |
| 663 |
|
| 664 |
if (ExpressionTool.dependsOnFocus(min)) { |
| 665 |
return null; |
| 666 |
} |
| 667 |
if (ExpressionTool.dependsOnFocus(max)) { |
| 668 |
if (max.isCallOn(PositionAndLast.Last.class)) { |
| 669 |
Expression result = SystemFunction.makeCall("subsequence", getRetainedStaticContext(), getBase(), min); |
| 670 |
if (tracing) { |
| 671 |
Optimizer.trace(config, "Rewriting numeric range filter expression using subsequence()", result); |
| 672 |
} |
| 673 |
return result; |
| 674 |
} else { |
| 675 |
return null; |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
LetExpression let = new LetExpression(); |
| 680 |
let.setRequiredType(SequenceType.SINGLE_INTEGER); |
| 681 |
let.setVariableQName(new StructuredQName("nn", NamespaceConstant.SAXON, "nn" + let.hashCode())); |
| 682 |
let.setSequence(min); |
| 683 |
min = new LocalVariableReference(let); |
| 684 |
LocalVariableReference min2 = new LocalVariableReference(let); |
| 685 |
Expression minMinusOne = new ArithmeticExpression( |
| 686 |
min2, Token.MINUS, Literal.makeLiteral(Int64Value.makeIntegerValue(1), this)); |
| 687 |
Expression length = new ArithmeticExpression(max, Token.MINUS, minMinusOne); |
| 688 |
Expression subs = SystemFunction.makeCall("subsequence", getRetainedStaticContext(), getBase(), min, length); |
| 689 |
let.setAction(subs); |
| 690 |
if (tracing) { |
| 691 |
Optimizer.trace(config, "Rewriting numeric range filter expression using subsequence()", subs); |
| 692 |
} |
| 693 |
return let; |
| 694 |
|
| 695 |
} else { |
| 696 |
return null; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
private static Expression tryToRewritePositionalFilterSupport( |
| 701 |
Expression start, Expression comparand, int operator, |
| 702 |
TypeHierarchy th) |
| 703 |
throws XPathException { |
| 704 |
if (th.isSubType(comparand.getItemType(), BuiltInAtomicType.INTEGER)) { |
| 705 |
switch (operator) { |
| 706 |
case Token.FEQ: { |
| 707 |
if (Literal.isConstantOne(comparand)) { |
| 708 |
return FirstItemExpression.makeFirstItemExpression(start); |
| 709 |
} else if (comparand instanceof Literal && ((IntegerValue) ((Literal) comparand).getValue()).asBigInteger().compareTo(BigInteger.ZERO) <= 0) { |
| 710 |
return Literal.makeEmptySequence(); |
| 711 |
} else { |
| 712 |
return new SubscriptExpression(start, comparand); |
| 713 |
} |
| 714 |
} |
| 715 |
case Token.FLT: { |
| 716 |
|
| 717 |
Expression[] args = new Expression[3]; |
| 718 |
args[0] = start; |
| 719 |
args[1] = Literal.makeLiteral(Int64Value.makeIntegerValue(1), start); |
| 720 |
if (Literal.isAtomic(comparand)) { |
| 721 |
long n = ((NumericValue) ((Literal) comparand).getValue()).longValue(); |
| 722 |
args[2] = Literal.makeLiteral(Int64Value.makeIntegerValue(n - 1), start); |
| 723 |
} else { |
| 724 |
ArithmeticExpression decrement = new ArithmeticExpression( |
| 725 |
comparand, Token.MINUS, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start)); |
| 726 |
decrement.setCalculator(Calculator.getCalculator( |
| 727 |
StandardNames.XS_INTEGER, StandardNames.XS_INTEGER, Calculator.MINUS, true)); |
| 728 |
args[2] = decrement; |
| 729 |
} |
| 730 |
return SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), args); |
| 731 |
} |
| 732 |
case Token.FLE: { |
| 733 |
Expression[] args = new Expression[3]; |
| 734 |
args[0] = start; |
| 735 |
args[1] = Literal.makeLiteral(Int64Value.makeIntegerValue(1), start); |
| 736 |
args[2] = comparand; |
| 737 |
return SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), args); |
| 738 |
} |
| 739 |
case Token.FNE: { |
| 740 |
return SystemFunction.makeCall("remove", start.getRetainedStaticContext(), start, comparand); |
| 741 |
} |
| 742 |
case Token.FGT: { |
| 743 |
Expression[] args = new Expression[2]; |
| 744 |
args[0] = start; |
| 745 |
if (Literal.isAtomic(comparand)) { |
| 746 |
long n = ((NumericValue) ((Literal) comparand).getValue()).longValue(); |
| 747 |
args[1] = Literal.makeLiteral(Int64Value.makeIntegerValue(n + 1), start); |
| 748 |
} else { |
| 749 |
args[1] = new ArithmeticExpression( |
| 750 |
comparand, Token.PLUS, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start)); |
| 751 |
} |
| 752 |
return SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), args); |
| 753 |
} |
| 754 |
case Token.FGE: { |
| 755 |
return SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), start, comparand); |
| 756 |
} |
| 757 |
default: |
| 758 |
throw new IllegalArgumentException("operator"); |
| 759 |
} |
| 760 |
|
| 761 |
} else { |
| 762 |
|
| 763 |
switch (operator) { |
| 764 |
case Token.FEQ: { |
| 765 |
return new SubscriptExpression(start, comparand); |
| 766 |
} |
| 767 |
case Token.FLT: { |
| 768 |
|
| 769 |
|
| 770 |
LetExpression let = new LetExpression(); |
| 771 |
let.setRequiredType(SequenceType.makeSequenceType( |
| 772 |
comparand.getItemType(), StaticProperty.ALLOWS_ONE)); |
| 773 |
let.setVariableQName(new StructuredQName("pp", NamespaceConstant.SAXON, "pp" + let.hashCode())); |
| 774 |
let.setSequence(comparand); |
| 775 |
LocalVariableReference isWholeArg = new LocalVariableReference(let); |
| 776 |
LocalVariableReference arithArg = new LocalVariableReference(let); |
| 777 |
LocalVariableReference floorArg = new LocalVariableReference(let); |
| 778 |
Expression isWhole = VendorFunctionSetHE.getInstance().makeFunction("is-whole-number", 1).makeFunctionCall(isWholeArg); |
| 779 |
Expression minusOne = new ArithmeticExpression( |
| 780 |
arithArg, Token.MINUS, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start)); |
| 781 |
Expression floor = SystemFunction.makeCall("floor", start.getRetainedStaticContext(), floorArg); |
| 782 |
Expression choice = Choose.makeConditional(isWhole, minusOne, floor); |
| 783 |
Expression subs = SystemFunction.makeCall( |
| 784 |
"subsequence", start.getRetainedStaticContext(), start, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start), choice); |
| 785 |
let.setAction(subs); |
| 786 |
|
| 787 |
return let; |
| 788 |
} |
| 789 |
case Token.FLE: { |
| 790 |
Expression floor = SystemFunction.makeCall("floor", start.getRetainedStaticContext(), comparand); |
| 791 |
return SystemFunction.makeCall( |
| 792 |
"subsequence", start.getRetainedStaticContext(), start, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start), floor); |
| 793 |
} |
| 794 |
case Token.FNE: { |
| 795 |
|
| 796 |
|
| 797 |
LetExpression let = new LetExpression(); |
| 798 |
ExpressionTool.copyLocationInfo(start, let); |
| 799 |
let.setRequiredType(SequenceType.makeSequenceType( |
| 800 |
comparand.getItemType(), StaticProperty.ALLOWS_ONE)); |
| 801 |
let.setVariableQName(new StructuredQName("pp", NamespaceConstant.SAXON, "pp" + let.hashCode())); |
| 802 |
let.setSequence(comparand); |
| 803 |
LocalVariableReference isWholeArg = new LocalVariableReference(let); |
| 804 |
LocalVariableReference castArg = new LocalVariableReference(let); |
| 805 |
Expression isWhole = VendorFunctionSetHE.getInstance().makeFunction("is-whole-number", 1).makeFunctionCall(isWholeArg); |
| 806 |
ExpressionTool.copyLocationInfo(start, isWhole); |
| 807 |
Expression cast = new CastExpression(castArg, BuiltInAtomicType.INTEGER, false); |
| 808 |
ExpressionTool.copyLocationInfo(start, cast); |
| 809 |
Expression choice = Choose.makeConditional( |
| 810 |
isWhole, cast, Literal.makeLiteral(Int64Value.makeIntegerValue(0), start)); |
| 811 |
Expression rem = SystemFunction.makeCall("remove", start.getRetainedStaticContext(), start, choice); |
| 812 |
let.setAction(rem); |
| 813 |
return let; |
| 814 |
} |
| 815 |
case Token.FGT: { |
| 816 |
|
| 817 |
|
| 818 |
LetExpression let = new LetExpression(); |
| 819 |
let.setRequiredType(SequenceType.makeSequenceType( |
| 820 |
comparand.getItemType(), StaticProperty.ALLOWS_ONE)); |
| 821 |
let.setVariableQName(new StructuredQName("pp", NamespaceConstant.SAXON, "pp" + let.hashCode())); |
| 822 |
let.setSequence(comparand); |
| 823 |
LocalVariableReference isWholeArg = new LocalVariableReference(let); |
| 824 |
LocalVariableReference arithArg = new LocalVariableReference(let); |
| 825 |
LocalVariableReference ceilingArg = new LocalVariableReference(let); |
| 826 |
Expression isWhole = VendorFunctionSetHE.getInstance().makeFunction("is-whole-number", 1).makeFunctionCall(isWholeArg); |
| 827 |
Expression plusOne = new ArithmeticExpression( |
| 828 |
arithArg, Token.PLUS, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start)); |
| 829 |
Expression ceiling = SystemFunction.makeCall("ceiling", start.getRetainedStaticContext(), ceilingArg); |
| 830 |
Expression choice = Choose.makeConditional(isWhole, plusOne, ceiling); |
| 831 |
Expression subs = SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), start, choice); |
| 832 |
let.setAction(subs); |
| 833 |
return let; |
| 834 |
} |
| 835 |
case Token.FGE: { |
| 836 |
|
| 837 |
Expression ceiling = SystemFunction.makeCall("ceiling", start.getRetainedStaticContext(), comparand); |
| 838 |
return SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), start, ceiling); |
| 839 |
} |
| 840 |
default: |
| 841 |
throw new IllegalArgumentException("operator"); |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
@Override |
| 855 |
public Expression unordered(boolean retainAllNodes, boolean forStreaming) throws XPathException { |
| 856 |
if (!filterIsPositional) { |
| 857 |
setBase(getBase().unordered(retainAllNodes, forStreaming)); |
| 858 |
} |
| 859 |
return this; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
private FilterExpression promoteIndependentPredicates(Binding[] bindings, Optimizer opt, TypeHierarchy th) { |
| 874 |
if (!ExpressionTool.dependsOnVariable(getBase(), bindings)) { |
| 875 |
return this; |
| 876 |
} |
| 877 |
if (isPositional(th)) { |
| 878 |
return this; |
| 879 |
} |
| 880 |
if (getBase() instanceof FilterExpression) { |
| 881 |
FilterExpression fe = (FilterExpression) getBase(); |
| 882 |
if (fe.isPositional(th)) { |
| 883 |
return this; |
| 884 |
} |
| 885 |
if (!ExpressionTool.dependsOnVariable(fe.getFilter(), bindings)) { |
| 886 |
return this; |
| 887 |
} |
| 888 |
if (!ExpressionTool.dependsOnVariable(getFilter(), bindings)) { |
| 889 |
FilterExpression result = new FilterExpression( |
| 890 |
new FilterExpression(fe.getBase(), getFilter()).promoteIndependentPredicates(bindings, opt, th), |
| 891 |
fe.getFilter()); |
| 892 |
opt.trace("Reordered filter predicates:", result); |
| 893 |
return result; |
| 894 |
} |
| 895 |
} |
| 896 |
return this; |
| 897 |
} |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
public static boolean isPositionalFilter(Expression exp, TypeHierarchy th) { |
| 910 |
ItemType type = exp.getItemType(); |
| 911 |
if (type.equals(BuiltInAtomicType.BOOLEAN)) { |
| 912 |
|
| 913 |
return isExplicitlyPositional(exp); |
| 914 |
} |
| 915 |
return type.equals(BuiltInAtomicType.ANY_ATOMIC) || |
| 916 |
type instanceof AnyItemType || |
| 917 |
type.equals(BuiltInAtomicType.INTEGER) || |
| 918 |
type.equals(NumericType.getInstance()) || |
| 919 |
th.isSubType(type, NumericType.getInstance()) || |
| 920 |
isExplicitlyPositional(exp); |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
private static boolean isExplicitlyPositional(Expression exp) { |
| 932 |
return (exp.getDependencies() & (StaticProperty.DEPENDS_ON_POSITION | StaticProperty.DEPENDS_ON_LAST)) != 0; |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
public int computeCardinality() { |
| 944 |
if (getFilter() instanceof Literal && ((Literal) getFilter()).getValue() instanceof NumericValue) { |
| 945 |
if (((NumericValue) ((Literal) getFilter()).getValue()).compareTo(1) == 0 && |
| 946 |
!Cardinality.allowsZero(getBase().getCardinality())) { |
| 947 |
return StaticProperty.ALLOWS_ONE; |
| 948 |
} else { |
| 949 |
return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 950 |
} |
| 951 |
} |
| 952 |
if (filterIsIndependent) { |
| 953 |
ItemType filterType = getFilter().getItemType().getPrimitiveItemType(); |
| 954 |
if (filterType == BuiltInAtomicType.INTEGER || filterType == BuiltInAtomicType.DOUBLE || |
| 955 |
filterType == BuiltInAtomicType.DECIMAL || filterType == BuiltInAtomicType.FLOAT) { |
| 956 |
return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 957 |
} |
| 958 |
if (getFilter() instanceof ArithmeticExpression) { |
| 959 |
return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 960 |
} |
| 961 |
} |
| 962 |
if (getFilter() instanceof IsLastExpression && ((IsLastExpression) getFilter()).getCondition()) { |
| 963 |
return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 964 |
} |
| 965 |
if (!Cardinality.allowsMany(getBase().getCardinality())) { |
| 966 |
return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 967 |
} |
| 968 |
|
| 969 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
public int computeSpecialProperties() { |
| 981 |
return getBase().getSpecialProperties(); |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
public boolean equals(Object other) { |
| 992 |
if (other instanceof FilterExpression) { |
| 993 |
FilterExpression f = (FilterExpression) other; |
| 994 |
return getBase().isEqual(f.getBase()) && |
| 995 |
getFilter().isEqual(f.getFilter()); |
| 996 |
} |
| 997 |
return false; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
public int computeHashCode() { |
| 1007 |
return "FilterExpression".hashCode() + getBase().hashCode() + getFilter().hashCode(); |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
@Override |
| 1018 |
public Pattern toPattern(Configuration config) throws XPathException { |
| 1019 |
Expression base = getSelectExpression(); |
| 1020 |
Expression filter = getFilter(); |
| 1021 |
TypeHierarchy th = config.getTypeHierarchy(); |
| 1022 |
Pattern basePattern = base.toPattern(config); |
| 1023 |
if (!isPositional(th)) { |
| 1024 |
return new BasePatternWithPredicate(basePattern, filter); |
| 1025 |
} else if (basePattern instanceof NodeTestPattern && |
| 1026 |
basePattern.getItemType() instanceof NodeTest && |
| 1027 |
filterIsPositional && |
| 1028 |
base instanceof AxisExpression && |
| 1029 |
((AxisExpression) base).getAxis() == AxisInfo.CHILD && |
| 1030 |
(filter.getDependencies() & StaticProperty.DEPENDS_ON_LAST) == 0) { |
| 1031 |
if (filter instanceof Literal && ((Literal) filter).getValue() instanceof IntegerValue) { |
| 1032 |
return new SimplePositionalPattern((NodeTest) basePattern.getItemType(), (int) ((IntegerValue) ((Literal) filter).getValue()).longValue()); |
| 1033 |
} else { |
| 1034 |
return new GeneralPositionalPattern((NodeTest) basePattern.getItemType(), filter); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
if (base.getItemType() instanceof NodeTest) { |
| 1038 |
return new GeneralNodePattern(this, (NodeTest) base.getItemType()); |
| 1039 |
} else { |
| 1040 |
throw new XPathException("The filtered expression in an XSLT 2.0 pattern must be a simple step"); |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
if (filterIsIndependent) { |
| 1058 |
try { |
| 1059 |
SequenceIterator it = getFilter().iterate(context); |
| 1060 |
Item first = it.next(); |
| 1061 |
if (first == null) { |
| 1062 |
return EmptyIterator.emptyIterator(); |
| 1063 |
} |
| 1064 |
if (first instanceof NumericValue) { |
| 1065 |
if (it.next() != null) { |
| 1066 |
ExpressionTool.ebvError("sequence of two or more items starting with a numeric value", getFilter()); |
| 1067 |
} else { |
| 1068 |
|
| 1069 |
int pos = ((NumericValue)first).asSubscript(); |
| 1070 |
if (pos != -1) { |
| 1071 |
if (getBase() instanceof VariableReference) { |
| 1072 |
Sequence<?> baseVal = ((VariableReference) getBase()).evaluateVariable(context); |
| 1073 |
if (baseVal instanceof MemoClosure) { |
| 1074 |
Item<?> m = ((MemoClosure) baseVal).itemAt(pos - 1); |
| 1075 |
return m == null ? EmptyIterator.emptyIterator() : m.iterate(); |
| 1076 |
} else { |
| 1077 |
Item<?> m = baseVal.materialize().itemAt(pos - 1); |
| 1078 |
return m == null ? EmptyIterator.emptyIterator() : m.iterate(); |
| 1079 |
} |
| 1080 |
} else if (getBase() instanceof Literal) { |
| 1081 |
Item<?> i = ((Literal) getBase()).getValue().itemAt(pos - 1); |
| 1082 |
return i == null ? EmptyIterator.emptyIterator() : i.iterate(); |
| 1083 |
} else { |
| 1084 |
SequenceIterator<?> baseIter = getBase().iterate(context); |
| 1085 |
return SubsequenceIterator.make(baseIter, pos, pos); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
return EmptyIterator.emptyIterator(); |
| 1090 |
} |
| 1091 |
} else { |
| 1092 |
|
| 1093 |
boolean ebv = false; |
| 1094 |
if (first instanceof NodeInfo) { |
| 1095 |
ebv = true; |
| 1096 |
} else if (first instanceof BooleanValue) { |
| 1097 |
ebv = ((BooleanValue) first).getBooleanValue(); |
| 1098 |
if (it.next() != null) { |
| 1099 |
ExpressionTool.ebvError("sequence of two or more items starting with a boolean value", getFilter()); |
| 1100 |
} |
| 1101 |
} else if (first instanceof StringValue) { |
| 1102 |
ebv = !((StringValue) first).isZeroLength(); |
| 1103 |
if (it.next() != null) { |
| 1104 |
ExpressionTool.ebvError("sequence of two or more items starting with a boolean value", getFilter()); |
| 1105 |
} |
| 1106 |
} else { |
| 1107 |
ExpressionTool.ebvError("sequence starting with an atomic value other than a boolean, number, or string", getFilter()); |
| 1108 |
} |
| 1109 |
if (ebv) { |
| 1110 |
return getBase().iterate(context); |
| 1111 |
} else { |
| 1112 |
return EmptyIterator.emptyIterator(); |
| 1113 |
} |
| 1114 |
} |
| 1115 |
} catch (XPathException e) { |
| 1116 |
e.maybeSetLocation(getLocation()); |
| 1117 |
throw e; |
| 1118 |
} |
| 1119 |
} |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
SequenceIterator<?> baseIter = getBase().iterate(context); |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
if (baseIter instanceof EmptyIterator) { |
| 1128 |
return baseIter; |
| 1129 |
} |
| 1130 |
|
| 1131 |
if (filterIsPositional && !filterIsSingletonBoolean) { |
| 1132 |
return new FilterIterator<>(baseIter, getFilter(), context); |
| 1133 |
} else { |
| 1134 |
return new FilterIterator.NonNumeric<>(baseIter, getFilter(), context); |
| 1135 |
} |
| 1136 |
|
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
public Expression copy(RebindingMap rebindings) { |
| 1150 |
FilterExpression fe = new FilterExpression(getBase().copy(rebindings), getFilter().copy(rebindings)); |
| 1151 |
ExpressionTool.copyLocationInfo(this, fe); |
| 1152 |
fe.filterIsIndependent = filterIsIndependent; |
| 1153 |
fe.filterIsPositional = filterIsPositional; |
| 1154 |
fe.filterIsSingletonBoolean = filterIsSingletonBoolean; |
| 1155 |
return fe; |
| 1156 |
} |
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
@Override |
| 1166 |
public String getStreamerName() { |
| 1167 |
return "FilterExpression"; |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
public String toString() { |
| 1177 |
return ExpressionTool.parenthesize(getBase()) + "[" + getFilter() + "]"; |
| 1178 |
} |
| 1179 |
|
| 1180 |
@Override |
| 1181 |
public String toShortString() { |
| 1182 |
return getBase().toShortString() + "[" + getFilter().toShortString() + "]"; |
| 1183 |
} |
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
public void export(ExpressionPresenter out) throws XPathException { |
| 1193 |
out.startElement("filter", this); |
| 1194 |
String flags = ""; |
| 1195 |
if (filterIsIndependent) { |
| 1196 |
flags += "i"; |
| 1197 |
} |
| 1198 |
if (filterIsPositional) { |
| 1199 |
flags += "p"; |
| 1200 |
} |
| 1201 |
if (filterIsSingletonBoolean) { |
| 1202 |
flags += "b"; |
| 1203 |
} |
| 1204 |
out.emitAttribute("flags", flags); |
| 1205 |
getBase().export(out); |
| 1206 |
getFilter().export(out); |
| 1207 |
out.endElement(); |
| 1208 |
} |
| 1209 |
|
| 1210 |
public void setFlags(String flags) { |
| 1211 |
filterIsIndependent = flags.contains("i"); |
| 1212 |
filterIsPositional = flags.contains("p"); |
| 1213 |
filterIsSingletonBoolean = flags.contains("b"); |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
} |
| 1218 |
|