| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.parser; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Controller; |
| 12 |
import net.sf.saxon.event.Receiver; |
| 13 |
import net.sf.saxon.event.SequenceOutputter; |
| 14 |
import net.sf.saxon.expr.*; |
| 15 |
import net.sf.saxon.expr.flwor.Clause; |
| 16 |
import net.sf.saxon.expr.flwor.FLWORExpression; |
| 17 |
import net.sf.saxon.expr.flwor.LocalVariableBinding; |
| 18 |
import net.sf.saxon.expr.instruct.*; |
| 19 |
import net.sf.saxon.expr.sort.ConditionalSorter; |
| 20 |
import net.sf.saxon.expr.sort.DocumentSorter; |
| 21 |
import net.sf.saxon.functions.*; |
| 22 |
import net.sf.saxon.lib.Logger; |
| 23 |
import net.sf.saxon.lib.NamespaceConstant; |
| 24 |
import net.sf.saxon.lib.StandardLogger; |
| 25 |
import net.sf.saxon.ma.arrays.ArrayItem; |
| 26 |
import net.sf.saxon.ma.map.MapItem; |
| 27 |
import net.sf.saxon.om.*; |
| 28 |
import net.sf.saxon.pattern.Pattern; |
| 29 |
import net.sf.saxon.query.QueryModule; |
| 30 |
import net.sf.saxon.style.Compilation; |
| 31 |
import net.sf.saxon.style.ExpressionContext; |
| 32 |
import net.sf.saxon.trans.Err; |
| 33 |
import net.sf.saxon.trans.SymbolicName; |
| 34 |
import net.sf.saxon.trans.XPathException; |
| 35 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 36 |
import net.sf.saxon.type.AnyItemType; |
| 37 |
import net.sf.saxon.type.ItemType; |
| 38 |
import net.sf.saxon.value.*; |
| 39 |
|
| 40 |
import javax.xml.transform.SourceLocator; |
| 41 |
import java.io.File; |
| 42 |
import java.net.URI; |
| 43 |
import java.net.URISyntaxException; |
| 44 |
import java.util.Iterator; |
| 45 |
import java.util.List; |
| 46 |
import java.util.function.Predicate; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public class ExpressionTool { |
| 55 |
|
| 56 |
public static final int UNDECIDED = -1; |
| 57 |
public static final int EVALUATE_LITERAL = 0; |
| 58 |
public static final int EVALUATE_VARIABLE = 1; |
| 59 |
public static final int MAKE_CLOSURE = 3; |
| 60 |
public static final int MAKE_MEMO_CLOSURE = 4; |
| 61 |
public static final int RETURN_EMPTY_SEQUENCE = 5; |
| 62 |
public static final int EVALUATE_AND_MATERIALIZE_VARIABLE = 6; |
| 63 |
public static final int CALL_EVALUATE_OPTIONAL_ITEM = 7; |
| 64 |
public static final int ITERATE_AND_MATERIALIZE = 8; |
| 65 |
public static final int PROCESS = 9; |
| 66 |
public static final int LAZY_TAIL_EXPRESSION = 10; |
| 67 |
public static final int SHARED_APPEND_EXPRESSION = 11; |
| 68 |
public static final int MAKE_INDEXED_VARIABLE = 12; |
| 69 |
public static final int MAKE_SINGLETON_CLOSURE = 13; |
| 70 |
public static final int EVALUATE_SUPPLIED_PARAMETER = 14; |
| 71 |
public static final int STREAMING_ARGUMENT = 15; |
| 72 |
public static final int CALL_EVALUATE_SINGLE_ITEM = 16; |
| 73 |
|
| 74 |
private ExpressionTool() { |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public static Expression make(String expression, StaticContext env, |
| 97 |
int start, int terminator, |
| 98 |
CodeInjector codeInjector) throws XPathException { |
| 99 |
int languageLevel = env.getXPathVersion(); |
| 100 |
XPathParser parser = env.getConfiguration().newExpressionParser("XP", false, languageLevel); |
| 101 |
if (codeInjector != null) { |
| 102 |
parser.setCodeInjector(codeInjector); |
| 103 |
} |
| 104 |
if (terminator == -1) { |
| 105 |
terminator = Token.EOF; |
| 106 |
} |
| 107 |
Expression exp = parser.parse(expression, start, terminator, env); |
| 108 |
|
| 109 |
setDeepRetainedStaticContext(exp, env.makeRetainedStaticContext()); |
| 110 |
exp = exp.simplify(); |
| 111 |
return exp; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static void setDeepRetainedStaticContext(Expression exp, RetainedStaticContext rsc) { |
| 123 |
if (exp.getLocalRetainedStaticContext() == null) { |
| 124 |
exp.setRetainedStaticContextLocally(rsc); |
| 125 |
} else { |
| 126 |
rsc = exp.getLocalRetainedStaticContext(); |
| 127 |
} |
| 128 |
for (Operand o : exp.operands()) { |
| 129 |
setDeepRetainedStaticContext(o.getChildExpression(), rsc); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public static void copyLocationInfo(Expression from, Expression to) { |
| 142 |
if (from != null && to != null) { |
| 143 |
if (to.getLocation() == null || to.getLocation() == ExplicitLocation.UNKNOWN_LOCATION) { |
| 144 |
to.setLocation(from.getLocation()); |
| 145 |
} |
| 146 |
if (to.getLocalRetainedStaticContext() == null) { |
| 147 |
to.setRetainedStaticContextLocally(from.getLocalRetainedStaticContext()); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public static Expression unsortedIfHomogeneous(Expression exp, boolean forStreaming) |
| 166 |
throws XPathException { |
| 167 |
if (exp instanceof Literal) { |
| 168 |
return exp; |
| 169 |
} |
| 170 |
if (exp.getItemType() instanceof AnyItemType) { |
| 171 |
return exp; |
| 172 |
} else { |
| 173 |
return exp.unordered(false, forStreaming); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public static Evaluator lazyEvaluator(Expression exp, boolean repeatable) { |
| 188 |
if (exp instanceof Literal) { |
| 189 |
return Evaluator.LITERAL; |
| 190 |
|
| 191 |
} else if (exp instanceof VariableReference) { |
| 192 |
return Evaluator.VARIABLE; |
| 193 |
|
| 194 |
} else if (exp instanceof SuppliedParameterReference) { |
| 195 |
return Evaluator.SUPPLIED_PARAMETER; |
| 196 |
|
| 197 |
} else if ((exp.getDependencies() & |
| 198 |
(StaticProperty.DEPENDS_ON_POSITION | |
| 199 |
StaticProperty.DEPENDS_ON_LAST | |
| 200 |
StaticProperty.DEPENDS_ON_CURRENT_ITEM | |
| 201 |
StaticProperty.DEPENDS_ON_CURRENT_GROUP | |
| 202 |
StaticProperty.DEPENDS_ON_REGEX_GROUP)) != 0) { |
| 203 |
|
| 204 |
|
| 205 |
return eagerEvaluator(exp); |
| 206 |
|
| 207 |
} else if (exp instanceof ErrorExpression) { |
| 208 |
return Evaluator.SINGLE_ITEM; |
| 209 |
|
| 210 |
|
| 211 |
} else if (!Cardinality.allowsMany(exp.getCardinality())) { |
| 212 |
|
| 213 |
return eagerEvaluator(exp); |
| 214 |
|
| 215 |
} else if (exp instanceof TailExpression) { |
| 216 |
|
| 217 |
|
| 218 |
TailExpression tail = (TailExpression) exp; |
| 219 |
Expression base = tail.getBaseExpression(); |
| 220 |
if (base instanceof VariableReference) { |
| 221 |
return Evaluator.LAZY_TAIL; |
| 222 |
} else if (repeatable) { |
| 223 |
return Evaluator.MEMO_CLOSURE; |
| 224 |
} else { |
| 225 |
return Evaluator.LAZY_SEQUENCE; |
| 226 |
} |
| 227 |
|
| 228 |
} else if (exp instanceof Block && ((Block) exp).isCandidateForSharedAppend()) { |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
return Evaluator.SHARED_APPEND; |
| 237 |
|
| 238 |
} else if (repeatable) { |
| 239 |
|
| 240 |
return Evaluator.MEMO_CLOSURE; |
| 241 |
} else { |
| 242 |
return Evaluator.LAZY_SEQUENCE; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public static Evaluator eagerEvaluator(Expression exp) { |
| 257 |
if (exp instanceof Literal && !(((Literal) exp).getValue() instanceof Closure)) { |
| 258 |
return Evaluator.LITERAL; |
| 259 |
} |
| 260 |
if (exp instanceof VariableReference) { |
| 261 |
return Evaluator.VARIABLE; |
| 262 |
} |
| 263 |
int m = exp.getImplementationMethod(); |
| 264 |
if ((m & Expression.EVALUATE_METHOD) != 0 && !Cardinality.allowsMany(exp.getCardinality())) { |
| 265 |
if (Cardinality.allowsZero(exp.getCardinality())) { |
| 266 |
return Evaluator.OPTIONAL_ITEM; |
| 267 |
} else { |
| 268 |
return Evaluator.SINGLE_ITEM; |
| 269 |
} |
| 270 |
|
| 271 |
} else if ((m & Expression.ITERATE_METHOD) != 0) { |
| 272 |
return Evaluator.EAGER_SEQUENCE; |
| 273 |
} else { |
| 274 |
return Evaluator.PROCESS; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public static Sequence<?> evaluate( |
| 302 |
Expression exp, int evaluationMode, XPathContext context, int ref) |
| 303 |
throws XPathException { |
| 304 |
switch (evaluationMode) { |
| 305 |
|
| 306 |
case EVALUATE_LITERAL: |
| 307 |
return ((Literal) exp).getValue(); |
| 308 |
|
| 309 |
case EVALUATE_VARIABLE: |
| 310 |
return ((VariableReference) exp).evaluateVariable(context); |
| 311 |
|
| 312 |
case EVALUATE_SUPPLIED_PARAMETER: |
| 313 |
return ((SuppliedParameterReference) exp).evaluateVariable(context); |
| 314 |
|
| 315 |
case MAKE_CLOSURE: |
| 316 |
return Closure.make(exp, context, ref); |
| 317 |
|
| 318 |
|
| 319 |
case MAKE_MEMO_CLOSURE: |
| 320 |
return Closure.make(exp, context, ref == 1 ? 10 : ref); |
| 321 |
|
| 322 |
|
| 323 |
case MAKE_SINGLETON_CLOSURE: |
| 324 |
return new SingletonClosure(exp, context); |
| 325 |
|
| 326 |
|
| 327 |
case RETURN_EMPTY_SEQUENCE: |
| 328 |
return EmptySequence.getInstance(); |
| 329 |
|
| 330 |
case EVALUATE_AND_MATERIALIZE_VARIABLE: |
| 331 |
Sequence<?> v = ((VariableReference) exp).evaluateVariable(context); |
| 332 |
if (v instanceof Closure) { |
| 333 |
return v.iterate().materialize(); |
| 334 |
} else { |
| 335 |
return v; |
| 336 |
} |
| 337 |
|
| 338 |
case CALL_EVALUATE_OPTIONAL_ITEM: |
| 339 |
Item item = exp.evaluateItem(context); |
| 340 |
if (item == null) { |
| 341 |
return EmptySequence.getInstance(); |
| 342 |
} else { |
| 343 |
return item; |
| 344 |
} |
| 345 |
|
| 346 |
case CALL_EVALUATE_SINGLE_ITEM: |
| 347 |
return exp.evaluateItem(context); |
| 348 |
|
| 349 |
case UNDECIDED: |
| 350 |
case ITERATE_AND_MATERIALIZE: |
| 351 |
if (ref == FilterExpression.FILTERED) { |
| 352 |
return context.getConfiguration().makeSequenceExtent(exp, ref, context); |
| 353 |
} else { |
| 354 |
return exp.iterate(context).materialize(); |
| 355 |
} |
| 356 |
|
| 357 |
case PROCESS: |
| 358 |
return Evaluator.PROCESS.evaluate(exp, context); |
| 359 |
|
| 360 |
case LAZY_TAIL_EXPRESSION: { |
| 361 |
TailExpression tail = (TailExpression) exp; |
| 362 |
VariableReference vr = (VariableReference) tail.getBaseExpression(); |
| 363 |
Sequence<?> base = evaluate(vr, EVALUATE_VARIABLE, context, ref); |
| 364 |
if (base instanceof MemoClosure) { |
| 365 |
SequenceIterator<?> it = base.iterate(); |
| 366 |
base = it.materialize(); |
| 367 |
} |
| 368 |
if (base instanceof IntegerRange) { |
| 369 |
long start = ((IntegerRange) base).getStart() + tail.getStart() - 1; |
| 370 |
long end = ((IntegerRange) base).getEnd(); |
| 371 |
if (start == end) { |
| 372 |
return Int64Value.makeIntegerValue(end); |
| 373 |
} else if (start > end) { |
| 374 |
return EmptySequence.getInstance(); |
| 375 |
} else { |
| 376 |
return new IntegerRange(start, end); |
| 377 |
} |
| 378 |
} |
| 379 |
if (base instanceof SequenceExtent) { |
| 380 |
if (tail.getStart() > ((SequenceExtent) base).getLength()) { |
| 381 |
return EmptySequence.getInstance(); |
| 382 |
} else { |
| 383 |
return new SequenceExtent( |
| 384 |
(SequenceExtent) base, |
| 385 |
tail.getStart() - 1, |
| 386 |
((SequenceExtent) base).getLength() - tail.getStart() + 1).reduce(); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
return Closure.make(tail, context, ref); |
| 391 |
} |
| 392 |
|
| 393 |
case SHARED_APPEND_EXPRESSION: { |
| 394 |
return Evaluator.SHARED_APPEND.evaluate(exp, context); |
| 395 |
} |
| 396 |
|
| 397 |
case MAKE_INDEXED_VARIABLE: |
| 398 |
return context.getConfiguration().obtainOptimizer().makeIndexedValue(exp.iterate(context)); |
| 399 |
|
| 400 |
case STREAMING_ARGUMENT: |
| 401 |
return context.getConfiguration().obtainOptimizer().evaluateStreamingArgument(exp, context); |
| 402 |
|
| 403 |
default: |
| 404 |
throw new IllegalArgumentException("Unknown evaluation mode " + evaluationMode); |
| 405 |
|
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
public static Sequence<?> lazyEvaluate(Expression exp, XPathContext context, boolean repeatable) throws XPathException { |
| 429 |
Evaluator evaluator = lazyEvaluator(exp, repeatable); |
| 430 |
return evaluator.evaluate(exp, context); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public static GroundedValue<?> eagerEvaluate(Expression exp, XPathContext context) throws XPathException { |
| 444 |
Evaluator evaluator = eagerEvaluator(exp); |
| 445 |
return evaluator.evaluate(exp, context).materialize(); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
public static int markTailFunctionCalls(Expression exp, StructuredQName qName, int arity) { |
| 461 |
return exp.markTailFunctionCalls(qName, arity); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
public static String indent(int level) { |
| 472 |
FastStringBuffer fsb = new FastStringBuffer(level); |
| 473 |
for (int i = 0; i < level; i++) { |
| 474 |
fsb.append(" "); |
| 475 |
} |
| 476 |
return fsb.toString(); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public static boolean contains(Expression a, Expression b) { |
| 487 |
Expression temp = b; |
| 488 |
while (temp != null) { |
| 489 |
if (temp == a) { |
| 490 |
return true; |
| 491 |
} else { |
| 492 |
temp = temp.getParentExpression(); |
| 493 |
} |
| 494 |
} |
| 495 |
return false; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public static boolean containsLocalParam(Expression exp) { |
| 506 |
return contains(exp, true, e -> e instanceof LocalParam); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public static boolean containsLocalVariableReference(final Expression exp) { |
| 520 |
return contains(exp, false, e -> { |
| 521 |
if (e instanceof LocalVariableReference) { |
| 522 |
LocalVariableReference vref = (LocalVariableReference)e; |
| 523 |
LocalBinding binding = vref.getBinding(); |
| 524 |
return !(binding instanceof Expression && contains(exp, (Expression) binding)); |
| 525 |
} |
| 526 |
return false; |
| 527 |
}); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
public static boolean contains(Expression exp, boolean sameFocusOnly, Predicate<Expression> predicate) { |
| 542 |
if (predicate.test(exp)) { |
| 543 |
return true; |
| 544 |
} |
| 545 |
for (Operand info : exp.operands()) { |
| 546 |
if ((info.hasSameFocus() || !sameFocusOnly) && contains(info.getChildExpression(), sameFocusOnly, predicate)) { |
| 547 |
return true; |
| 548 |
} |
| 549 |
} |
| 550 |
return false; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
public static boolean changesXsltContext(Expression exp) { |
| 567 |
exp = exp.getInterpretedExpression(); |
| 568 |
if (exp instanceof ResultDocument || exp instanceof CallTemplate || exp instanceof ApplyTemplates || |
| 569 |
exp instanceof NextMatch || exp instanceof ApplyImports || exp.isCallOn(RegexGroup.class) |
| 570 |
|| exp.isCallOn(CurrentGroup.class)) { |
| 571 |
return true; |
| 572 |
} |
| 573 |
for (Operand o : exp.operands()) { |
| 574 |
if (changesXsltContext(o.getChildExpression())) { |
| 575 |
return true; |
| 576 |
} |
| 577 |
} |
| 578 |
return false; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
public static boolean isLoopingSubexpression(Expression child, Expression ancestor) { |
| 592 |
while (true) { |
| 593 |
|
| 594 |
Expression parent = child.getParentExpression(); |
| 595 |
if (parent == null) { |
| 596 |
return false; |
| 597 |
} |
| 598 |
if (hasLoopingSubexpression(parent, child)) { |
| 599 |
return true; |
| 600 |
} |
| 601 |
if (parent == ancestor) { |
| 602 |
return false; |
| 603 |
} |
| 604 |
child = parent; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
public static boolean isLoopingReference(VariableReference reference, Binding binding) { |
| 609 |
Expression child = reference; |
| 610 |
Expression parent = child.getParentExpression(); |
| 611 |
while (true) { |
| 612 |
if (parent == null) { |
| 613 |
|
| 614 |
return true; |
| 615 |
} |
| 616 |
if (parent instanceof FLWORExpression) { |
| 617 |
if (parent.hasVariableBinding(binding)) { |
| 618 |
|
| 619 |
return ((FLWORExpression) parent).hasLoopingVariableReference(binding); |
| 620 |
} else { |
| 621 |
|
| 622 |
if (hasLoopingSubexpression(parent, child)) { |
| 623 |
return true; |
| 624 |
} |
| 625 |
} |
| 626 |
} else if (parent.getExpressionName().equals("tryCatch")) { |
| 627 |
return true; |
| 628 |
} else { |
| 629 |
if (parent instanceof ForEachGroup && parent.hasVariableBinding(binding)) { |
| 630 |
return false; |
| 631 |
} |
| 632 |
if (hasLoopingSubexpression(parent, child)) { |
| 633 |
return true; |
| 634 |
} |
| 635 |
if (parent.hasVariableBinding(binding)) { |
| 636 |
return false; |
| 637 |
} |
| 638 |
} |
| 639 |
child = parent; |
| 640 |
parent = child.getParentExpression(); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
public static boolean hasLoopingSubexpression(Expression parent, Expression child) { |
| 645 |
for (Operand info : parent.operands()) { |
| 646 |
if (info.getChildExpression() == child) { |
| 647 |
return info.isEvaluatedRepeatedly(); |
| 648 |
} |
| 649 |
} |
| 650 |
return false; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
public static Expression getFocusSettingContainer(Expression exp) { |
| 665 |
Expression child = exp; |
| 666 |
Expression parent = child.getParentExpression(); |
| 667 |
while (parent != null) { |
| 668 |
Operand o = findOperand(parent, child); |
| 669 |
if (o == null) { |
| 670 |
throw new AssertionError(); |
| 671 |
} |
| 672 |
if (!o.hasSameFocus()) { |
| 673 |
return parent; |
| 674 |
} |
| 675 |
child = parent; |
| 676 |
parent = child.getParentExpression(); |
| 677 |
} |
| 678 |
return null; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
public static Expression getContextDocumentSettingContainer(Expression exp) { |
| 694 |
Expression child = exp; |
| 695 |
Expression parent = child.getParentExpression(); |
| 696 |
while (parent != null) { |
| 697 |
if (parent instanceof ContextSwitchingExpression) { |
| 698 |
ContextSwitchingExpression switcher = (ContextSwitchingExpression)parent; |
| 699 |
if (child == switcher.getActionExpression()) { |
| 700 |
if (switcher.getSelectExpression().hasSpecialProperty(StaticProperty.CONTEXT_DOCUMENT_NODESET)) { |
| 701 |
parent.resetLocalStaticProperties(); |
| 702 |
parent.getSpecialProperties(); |
| 703 |
return getContextDocumentSettingContainer(parent); |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
Operand o = findOperand(parent, child); |
| 708 |
if (o == null) { |
| 709 |
throw new AssertionError(); |
| 710 |
} |
| 711 |
if (!o.hasSameFocus()) { |
| 712 |
return parent; |
| 713 |
} |
| 714 |
child = parent; |
| 715 |
parent = child.getParentExpression(); |
| 716 |
} |
| 717 |
return null; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
public static void resetStaticProperties(Expression exp) { |
| 730 |
int i = 0; |
| 731 |
while (exp != null) { |
| 732 |
exp.resetLocalStaticProperties(); |
| 733 |
exp = exp.getParentExpression(); |
| 734 |
if (i++ > 100000) { |
| 735 |
throw new IllegalStateException("Loop in parent expression chain"); |
| 736 |
} |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
public static boolean equalOrNull(Object x, Object y) { |
| 749 |
if (x == null) { |
| 750 |
return y == null; |
| 751 |
} else { |
| 752 |
return x.equals(y); |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
public static SequenceIterator<?> getIteratorFromProcessMethod( |
| 767 |
Expression exp, XPathContext context) throws XPathException { |
| 768 |
Controller controller = context.getController(); |
| 769 |
assert controller != null; |
| 770 |
Receiver saved = context.getReceiver(); |
| 771 |
SequenceOutputter seq = controller.allocateSequenceOutputter(20); |
| 772 |
seq.getPipelineConfiguration().setHostLanguage(exp.getPackageData().getHostLanguage()); |
| 773 |
context.setReceiver(seq); |
| 774 |
exp.process(context); |
| 775 |
context.setReceiver(saved); |
| 776 |
seq.close(); |
| 777 |
return seq.iterate(); |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
public static Item getItemFromProcessMethod(Expression exp, XPathContext context) throws XPathException { |
| 790 |
Controller controller = context.getController(); |
| 791 |
assert controller != null; |
| 792 |
Receiver saved = context.getReceiver(); |
| 793 |
SequenceOutputter seq = controller.allocateSequenceOutputter(1); |
| 794 |
seq.getPipelineConfiguration().setHostLanguage(exp.getPackageData().getHostLanguage()); |
| 795 |
context.setReceiver(seq); |
| 796 |
|
| 797 |
exp.process(context); |
| 798 |
|
| 799 |
context.setReceiver(saved); |
| 800 |
seq.close(); |
| 801 |
Item result = seq.getFirstItem(); |
| 802 |
seq.reset(); |
| 803 |
return result; |
| 804 |
} |
| 805 |
|
| 806 |
@FunctionalInterface |
| 807 |
public interface ExpressionPredicate { |
| 808 |
boolean test(Expression e); |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
public static int allocateSlots(Expression exp, int nextFree, SlotManager frame) { |
| 822 |
|
| 823 |
if (exp instanceof Assignation) { |
| 824 |
((Assignation) exp).setSlotNumber(nextFree); |
| 825 |
int count = ((Assignation) exp).getRequiredSlots(); |
| 826 |
nextFree += count; |
| 827 |
if (frame != null) { |
| 828 |
frame.allocateSlotNumber(((Assignation) exp).getVariableQName()); |
| 829 |
} |
| 830 |
} |
| 831 |
if (exp instanceof LocalParam && ((LocalParam) exp).getSlotNumber() < 0) { |
| 832 |
((LocalParam) exp).setSlotNumber(nextFree++); |
| 833 |
} |
| 834 |
|
| 835 |
if (exp instanceof FLWORExpression) { |
| 836 |
for (Clause c : ((FLWORExpression) exp).getClauseList()) { |
| 837 |
for (LocalVariableBinding b : c.getRangeVariables()) { |
| 838 |
b.setSlotNumber(nextFree++); |
| 839 |
frame.allocateSlotNumber(b.getVariableQName()); |
| 840 |
} |
| 841 |
} |
| 842 |
} |
| 843 |
if (exp instanceof VariableReference) { |
| 844 |
VariableReference var = (VariableReference) exp; |
| 845 |
Binding binding = var.getBinding(); |
| 846 |
if (exp instanceof LocalVariableReference) { |
| 847 |
((LocalVariableReference) var).setSlotNumber(((LocalBinding) binding).getLocalSlotNumber()); |
| 848 |
} |
| 849 |
if (binding instanceof Assignation && ((LocalBinding) binding).getLocalSlotNumber() < 0) { |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
Assignation decl = (Assignation) binding; |
| 857 |
Logger err; |
| 858 |
try { |
| 859 |
err = exp.getConfiguration().getLogger(); |
| 860 |
} catch (Exception ex) { |
| 861 |
err = new StandardLogger(); |
| 862 |
} |
| 863 |
String msg = "*** Internal Saxon error: local variable encountered whose binding has been deleted"; |
| 864 |
err.error(msg); |
| 865 |
err.error("Variable name: " + decl.getVariableName()); |
| 866 |
err.error("Line number of reference: " + var.getLocation().getLineNumber() + " in " + var.getLocation().getSystemId()); |
| 867 |
err.error("Line number of declaration: " + decl.getLocation().getLineNumber() + " in " + decl.getLocation().getSystemId()); |
| 868 |
err.error("DECLARATION:"); |
| 869 |
try { |
| 870 |
decl.explain(err); |
| 871 |
} catch (Exception e) { |
| 872 |
|
| 873 |
} |
| 874 |
throw new IllegalStateException(msg); |
| 875 |
} |
| 876 |
|
| 877 |
} |
| 878 |
|
| 879 |
if (exp instanceof Pattern) { |
| 880 |
nextFree = ((Pattern) exp).allocateSlots(frame, nextFree); |
| 881 |
} else { |
| 882 |
for (Operand o : exp.operands()) { |
| 883 |
nextFree = allocateSlots(o.getChildExpression(), nextFree, frame); |
| 884 |
} |
| 885 |
} |
| 886 |
return nextFree; |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
} |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
public static boolean effectiveBooleanValue(SequenceIterator iterator) throws XPathException { |
| 901 |
Item first = iterator.next(); |
| 902 |
if (first == null) { |
| 903 |
return false; |
| 904 |
} |
| 905 |
if (first instanceof NodeInfo) { |
| 906 |
iterator.close(); |
| 907 |
return true; |
| 908 |
} else if (first instanceof AtomicValue) { |
| 909 |
if (first instanceof BooleanValue) { |
| 910 |
if (iterator.next() != null) { |
| 911 |
iterator.close(); |
| 912 |
ebvError("a sequence of two or more items starting with a boolean"); |
| 913 |
} |
| 914 |
iterator.close(); |
| 915 |
return ((BooleanValue) first).getBooleanValue(); |
| 916 |
} else if (first instanceof StringValue) { |
| 917 |
if (iterator.next() != null) { |
| 918 |
iterator.close(); |
| 919 |
ebvError("a sequence of two or more items starting with a string"); |
| 920 |
} |
| 921 |
return !((StringValue) first).isZeroLength(); |
| 922 |
} else if (first instanceof NumericValue) { |
| 923 |
if (iterator.next() != null) { |
| 924 |
iterator.close(); |
| 925 |
ebvError("a sequence of two or more items starting with a numeric value"); |
| 926 |
} |
| 927 |
final NumericValue n = (NumericValue) first; |
| 928 |
return (n.compareTo(0) != 0) && !n.isNaN(); |
| 929 |
} else { |
| 930 |
iterator.close(); |
| 931 |
ebvError("a sequence starting with an atomic value of type " + ((AtomicValue) first).getItemType().getTypeName().getDisplayName()); |
| 932 |
return false; |
| 933 |
} |
| 934 |
} else if (first instanceof Function) { |
| 935 |
iterator.close(); |
| 936 |
if (first instanceof ArrayItem) { |
| 937 |
ebvError("a sequence starting with an array item (" + first.toShortString() + ")"); |
| 938 |
return false; |
| 939 |
} else if (first instanceof MapItem) { |
| 940 |
ebvError("a sequence starting with a map (" + first.toShortString() + ")"); |
| 941 |
return false; |
| 942 |
} else { |
| 943 |
ebvError("a sequence starting with a function (" + first.toShortString() + ")"); |
| 944 |
return false; |
| 945 |
} |
| 946 |
} else if (first instanceof ObjectValue) { |
| 947 |
if (iterator.next() != null) { |
| 948 |
iterator.close(); |
| 949 |
ebvError("a sequence of two or more items starting with an external object value"); |
| 950 |
} |
| 951 |
return true; |
| 952 |
} |
| 953 |
ebvError("a sequence starting with an item of unknown kind"); |
| 954 |
return false; |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
public static boolean effectiveBooleanValue(Item item) throws XPathException { |
| 965 |
if (item == null) { |
| 966 |
return false; |
| 967 |
} |
| 968 |
if (item instanceof NodeInfo) { |
| 969 |
return true; |
| 970 |
} else { |
| 971 |
if (item instanceof BooleanValue) { |
| 972 |
return ((BooleanValue) item).getBooleanValue(); |
| 973 |
} else if (item instanceof StringValue) { |
| 974 |
return !((StringValue) item).isZeroLength(); |
| 975 |
} else if (item instanceof NumericValue) { |
| 976 |
final NumericValue n = (NumericValue) item; |
| 977 |
return (n.compareTo(0) != 0) && !n.isNaN(); |
| 978 |
} else if (item instanceof ExternalObject) { |
| 979 |
return true; |
| 980 |
} else { |
| 981 |
ebvError("an atomic value other than a boolean, number, string, or URI"); |
| 982 |
return false; |
| 983 |
} |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
public static void ebvError(String reason) throws XPathException { |
| 995 |
XPathException err = new XPathException("Effective boolean value is not defined for " + reason); |
| 996 |
err.setErrorCode("FORG0006"); |
| 997 |
err.setIsTypeError(true); |
| 998 |
throw err; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
public static void ebvError(String reason, Expression cause) throws XPathException { |
| 1009 |
XPathException err = new XPathException("Effective boolean value is not defined for " + reason); |
| 1010 |
err.setErrorCode("FORG0006"); |
| 1011 |
err.setIsTypeError(true); |
| 1012 |
err.setFailingExpression(cause); |
| 1013 |
throw err; |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
public static boolean dependsOnFocus(Expression exp) { |
| 1024 |
return (exp.getDependencies() & StaticProperty.DEPENDS_ON_FOCUS) != 0; |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
public static boolean dependsOnVariable(Expression exp, final Binding[] bindingList) { |
| 1036 |
return !(bindingList == null || bindingList.length == 0) && |
| 1037 |
contains(exp, false, e -> { |
| 1038 |
if (e instanceof VariableReference) { |
| 1039 |
for (Binding binding : bindingList) { |
| 1040 |
if (((VariableReference) e).getBinding() == binding) { |
| 1041 |
return true; |
| 1042 |
} |
| 1043 |
} |
| 1044 |
} |
| 1045 |
return false; |
| 1046 |
}); |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
public static void gatherReferencedVariables(Expression e, List<Binding> list) { |
| 1062 |
if (e instanceof VariableReference) { |
| 1063 |
Binding binding = ((VariableReference) e).getBinding(); |
| 1064 |
if (!list.contains(binding)) { |
| 1065 |
list.add(binding); |
| 1066 |
} |
| 1067 |
} else { |
| 1068 |
for (Operand o : e.operands()) { |
| 1069 |
if (!o.getOperandRole().isInChoiceGroup()) { |
| 1070 |
gatherReferencedVariables(o.getChildExpression(), list); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
public static boolean refersToVariableOrFunction(Expression exp) { |
| 1087 |
return contains(exp, false, |
| 1088 |
e -> e instanceof VariableReference |
| 1089 |
|| e instanceof UserFunctionCall |
| 1090 |
|| e instanceof Binding |
| 1091 |
|| e instanceof CallTemplate |
| 1092 |
|| e instanceof ApplyTemplates |
| 1093 |
|| e instanceof ApplyImports |
| 1094 |
|| isCallOnSystemFunction(e, "function-lookup") |
| 1095 |
|| e.isCallOn(ApplyFn.class)); |
| 1096 |
} |
| 1097 |
|
| 1098 |
public static boolean isCallOnSystemFunction(Expression e, String localName) { |
| 1099 |
return e instanceof StaticFunctionCall && localName.equals(((StaticFunctionCall)e).getFunctionName().getLocalPart()); |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
public static boolean callsFunction(Expression exp, final StructuredQName qName, boolean sameFocusOnly) { |
| 1112 |
return contains(exp, sameFocusOnly, |
| 1113 |
e -> e instanceof FunctionCall && qName.equals(((FunctionCall) e).getFunctionName()) |
| 1114 |
); |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
public static boolean containsSubexpression(Expression exp, final Class<? extends Expression> subClass) { |
| 1126 |
return contains(exp, false, e -> subClass.isAssignableFrom(e.getClass())); |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
public static void gatherCalledFunctions(Expression e, List<UserFunction> list) { |
| 1139 |
if (e instanceof UserFunctionCall) { |
| 1140 |
UserFunction function = ((UserFunctionCall) e).getFunction(); |
| 1141 |
if (!list.contains(function)) { |
| 1142 |
list.add(function); |
| 1143 |
} |
| 1144 |
} else { |
| 1145 |
for (Operand o : e.operands()) { |
| 1146 |
gatherCalledFunctions(o.getChildExpression(), list); |
| 1147 |
} |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
public static void gatherCalledFunctionNames(Expression e, List<SymbolicName> list) { |
| 1160 |
if (e instanceof UserFunctionCall) { |
| 1161 |
list.add(((UserFunctionCall) e).getSymbolicName()); |
| 1162 |
} else { |
| 1163 |
for (Operand o : e.operands()) { |
| 1164 |
gatherCalledFunctionNames(o.getChildExpression(), list); |
| 1165 |
} |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
public static Expression optimizeComponentBody( |
| 1182 |
Expression body, final Compilation compilation, ExpressionVisitor visitor, ContextItemStaticInfo cisi, boolean extractGlobals) |
| 1183 |
throws XPathException { |
| 1184 |
final Configuration config = visitor.getConfiguration(); |
| 1185 |
Optimizer opt = visitor.obtainOptimizer(); |
| 1186 |
StaticContext env = visitor.getStaticContext(); |
| 1187 |
boolean compileWithTracing = config.isCompileWithTracing(); |
| 1188 |
if (!compileWithTracing) { |
| 1189 |
|
| 1190 |
if (compilation != null) { |
| 1191 |
compileWithTracing = compilation.getCompilerInfo().isCompileWithTracing(); |
| 1192 |
} else if (env instanceof QueryModule) { |
| 1193 |
compileWithTracing = ((QueryModule) env).getUserQueryContext().isCompileWithTracing(); |
| 1194 |
} else if (env instanceof ExpressionContext) { |
| 1195 |
compileWithTracing = ((ExpressionContext) env).getStyleElement().getCompilation().getCompilerInfo().isCompileWithTracing(); |
| 1196 |
} |
| 1197 |
} |
| 1198 |
if (opt.isOptionSet(OptimizerOptions.MISCELLANEOUS) && !compileWithTracing) { |
| 1199 |
ExpressionTool.resetPropertiesWithinSubtree(body); |
| 1200 |
if (opt.isOptionSet(OptimizerOptions.MISCELLANEOUS)) { |
| 1201 |
body = body.optimize(visitor, cisi); |
| 1202 |
} |
| 1203 |
body.setParentExpression(null); |
| 1204 |
if (opt.isOptionSet(OptimizerOptions.LOOP_LIFTING)) { |
| 1205 |
body = LoopLifter.process(body, visitor, cisi); |
| 1206 |
} |
| 1207 |
if (extractGlobals && compilation != null) { |
| 1208 |
Expression exp2 = opt.promoteExpressionsToGlobal(body, compilation.getPrincipalStylesheetModule(), visitor); |
| 1209 |
if (exp2 != null) { |
| 1210 |
|
| 1211 |
ExpressionTool.resetPropertiesWithinSubtree(exp2); |
| 1212 |
body = exp2.optimize(visitor, cisi); |
| 1213 |
} |
| 1214 |
} |
| 1215 |
} else { |
| 1216 |
body = avoidDocumentSort(body); |
| 1217 |
} |
| 1218 |
|
| 1219 |
if (!visitor.isOptimizeForStreaming()) { |
| 1220 |
body = opt.eliminateCommonSubexpressions(body); |
| 1221 |
} |
| 1222 |
opt.injectByteCodeCandidates(body); |
| 1223 |
opt.prepareForStreaming(body); |
| 1224 |
|
| 1225 |
computeEvaluationModesForUserFunctionCalls(body); |
| 1226 |
body.restoreParentPointers(); |
| 1227 |
return body; |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
private static Expression avoidDocumentSort(Expression exp) { |
| 1239 |
if (exp instanceof DocumentSorter) { |
| 1240 |
Expression base = ((DocumentSorter) exp).getBaseExpression(); |
| 1241 |
if (base.hasSpecialProperty(StaticProperty.ORDERED_NODESET)) { |
| 1242 |
return base; |
| 1243 |
} |
| 1244 |
return exp; |
| 1245 |
} else if (exp instanceof ConditionalSorter) { |
| 1246 |
DocumentSorter sorter = ((ConditionalSorter) exp).getDocumentSorter(); |
| 1247 |
Expression eliminatedSorter = avoidDocumentSort(sorter); |
| 1248 |
if (eliminatedSorter != sorter) { |
| 1249 |
return eliminatedSorter; |
| 1250 |
} |
| 1251 |
} |
| 1252 |
for (Operand o : exp.operands()) { |
| 1253 |
o.setChildExpression(avoidDocumentSort(o.getChildExpression())); |
| 1254 |
} |
| 1255 |
return exp; |
| 1256 |
} |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
public static void computeEvaluationModesForUserFunctionCalls(Expression exp) throws XPathException { |
| 1264 |
ExpressionTool.processExpressionTree(exp, null, (expression, result) -> { |
| 1265 |
if (expression instanceof UserFunctionCall) { |
| 1266 |
((UserFunctionCall) expression).allocateArgumentEvaluators(); |
| 1267 |
} |
| 1268 |
if (expression instanceof LocalParam) { |
| 1269 |
((LocalParam)expression).computeEvaluationMode(); |
| 1270 |
} |
| 1271 |
return false; |
| 1272 |
}); |
| 1273 |
} |
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
public static void clearStreamabilityData(Expression exp) throws XPathException { |
| 1282 |
ExpressionTool.processExpressionTree(exp, null, (expression, result) -> { |
| 1283 |
expression.setExtraProperty("P+S", null); |
| 1284 |
expression.setExtraProperty("inversion", null); |
| 1285 |
return false; |
| 1286 |
}); |
| 1287 |
} |
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
public static void resetPropertiesWithinSubtree(Expression exp) { |
| 1297 |
exp.resetLocalStaticProperties(); |
| 1298 |
if (exp instanceof LocalVariableReference) { |
| 1299 |
LocalVariableReference ref = (LocalVariableReference) exp; |
| 1300 |
Binding binding = ref.getBinding(); |
| 1301 |
if (binding instanceof Assignation) { |
| 1302 |
binding.addReference(ref, ref.isInLoop()); |
| 1303 |
} |
| 1304 |
} |
| 1305 |
for (Operand o : exp.operands()) { |
| 1306 |
resetPropertiesWithinSubtree(o.getChildExpression()); |
| 1307 |
o.getChildExpression().setParentExpression(exp); |
| 1308 |
} |
| 1309 |
|
| 1310 |
} |
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
public static Expression resolveCallsToCurrentFunction(Expression exp) { |
| 1320 |
if (exp.isCallOn(Current.class)) { |
| 1321 |
ContextItemExpression cie = new ContextItemExpression(); |
| 1322 |
copyLocationInfo(exp, cie); |
| 1323 |
return cie; |
| 1324 |
} else { |
| 1325 |
if (callsFunction(exp, Current.FN_CURRENT, true)) { |
| 1326 |
|
| 1327 |
replaceTrivialCallsToCurrent(exp); |
| 1328 |
} |
| 1329 |
if (callsFunction(exp, Current.FN_CURRENT, false)) { |
| 1330 |
|
| 1331 |
LetExpression let = new LetExpression(); |
| 1332 |
let.setVariableQName( |
| 1333 |
new StructuredQName("vv", NamespaceConstant.SAXON_GENERATED_VARIABLE, "current" + exp.hashCode())); |
| 1334 |
let.setRequiredType(SequenceType.SINGLE_ITEM); |
| 1335 |
let.setSequence(new CurrentItemExpression()); |
| 1336 |
replaceCallsToCurrent(exp, let); |
| 1337 |
let.setAction(exp); |
| 1338 |
return let; |
| 1339 |
} else { |
| 1340 |
return exp; |
| 1341 |
} |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
public static void gatherVariableReferences(Expression exp, Binding binding, List<VariableReference> list) { |
| 1354 |
if (exp instanceof VariableReference && |
| 1355 |
((VariableReference) exp).getBinding() == binding) { |
| 1356 |
list.add((VariableReference) exp); |
| 1357 |
} else { |
| 1358 |
for (Operand o : exp.operands()) { |
| 1359 |
gatherVariableReferences(o.getChildExpression(), binding, list); |
| 1360 |
} |
| 1361 |
} |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
public static boolean processExpressionTree(Expression root, Object result, ExpressionAction action) throws XPathException { |
| 1377 |
boolean done = action.process(root, result); |
| 1378 |
if (!done) { |
| 1379 |
for (Operand o : root.operands()) { |
| 1380 |
done = processExpressionTree(o.getChildExpression(), result, action); |
| 1381 |
if (done) { |
| 1382 |
return true; |
| 1383 |
} |
| 1384 |
} |
| 1385 |
} |
| 1386 |
return false; |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
public interface ExpressionSelector { |
| 1417 |
boolean matches(Expression exp); |
| 1418 |
} |
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
public static boolean replaceSelectedSubexpressions( |
| 1432 |
Expression exp, ExpressionSelector selector, Expression replacement, boolean mustCopy) { |
| 1433 |
boolean replaced = false; |
| 1434 |
for (Operand o : exp.operands()) { |
| 1435 |
if (replaced) { |
| 1436 |
mustCopy = true; |
| 1437 |
} |
| 1438 |
Expression child = o.getChildExpression(); |
| 1439 |
if (selector.matches(child)) { |
| 1440 |
Expression e2 = mustCopy ? replacement.copy(new RebindingMap()) : replacement; |
| 1441 |
o.setChildExpression(e2); |
| 1442 |
replaced = true; |
| 1443 |
} else { |
| 1444 |
replaced = replaceSelectedSubexpressions(child, selector, replacement, mustCopy); |
| 1445 |
} |
| 1446 |
} |
| 1447 |
return replaced; |
| 1448 |
} |
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
public static void replaceVariableReferences(Expression exp, final Binding binding, Expression replacement, boolean mustCopy) { |
| 1460 |
ExpressionSelector selector = |
| 1461 |
child -> child instanceof VariableReference && ((VariableReference) child).getBinding() == binding; |
| 1462 |
replaceSelectedSubexpressions(exp, selector, replacement, mustCopy); |
| 1463 |
} |
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
public static int getReferenceCount(Expression exp, Binding binding, boolean inLoop) { |
| 1485 |
int rcount = 0; |
| 1486 |
if (exp instanceof VariableReference && ((VariableReference) exp).getBinding() == binding) { |
| 1487 |
if (((VariableReference) exp).isFiltered()) { |
| 1488 |
return FilterExpression.FILTERED; |
| 1489 |
} else { |
| 1490 |
rcount += inLoop ? 10 : 1; |
| 1491 |
} |
| 1492 |
} else if ((exp.getDependencies() & StaticProperty.DEPENDS_ON_LOCAL_VARIABLES) == 0) { |
| 1493 |
return 0; |
| 1494 |
} else { |
| 1495 |
for (Operand info : exp.operands()) { |
| 1496 |
Expression child = info.getChildExpression(); |
| 1497 |
boolean childLoop = inLoop || info.isEvaluatedRepeatedly(); |
| 1498 |
rcount += getReferenceCount(child, binding, childLoop); |
| 1499 |
if (rcount >= FilterExpression.FILTERED) { |
| 1500 |
break; |
| 1501 |
} |
| 1502 |
} |
| 1503 |
} |
| 1504 |
return rcount; |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
public static int expressionSize(Expression exp) { |
| 1515 |
exp = exp.getInterpretedExpression(); |
| 1516 |
int total = 1; |
| 1517 |
for (Operand o : exp.operands()) { |
| 1518 |
total += expressionSize(o.getChildExpression()); |
| 1519 |
} |
| 1520 |
return total; |
| 1521 |
} |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
public static void rebindVariableReferences( |
| 1533 |
Expression exp, Binding oldBinding, Binding newBinding) { |
| 1534 |
if (exp instanceof VariableReference) { |
| 1535 |
if (((VariableReference) exp).getBinding() == oldBinding) { |
| 1536 |
((VariableReference) exp).fixup(newBinding); |
| 1537 |
} |
| 1538 |
} else { |
| 1539 |
for (Operand o : exp.operands()) { |
| 1540 |
rebindVariableReferences(o.getChildExpression(), oldBinding, newBinding); |
| 1541 |
} |
| 1542 |
} |
| 1543 |
} |
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
public static Expression makePathExpression(Expression start, Expression step) { |
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
if (start instanceof RootExpression && |
| 1560 |
step instanceof AxisExpression && ((AxisExpression) step).getAxis() == AxisInfo.PARENT) { |
| 1561 |
return Literal.makeEmptySequence(); |
| 1562 |
} |
| 1563 |
|
| 1564 |
SlashExpression expr = new SlashExpression(start, step); |
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
if (step instanceof SlashExpression) { |
| 1576 |
SlashExpression stepPath = (SlashExpression) step; |
| 1577 |
if (isFilteredAxisPath(stepPath.getSelectExpression()) && isFilteredAxisPath(stepPath.getActionExpression())) { |
| 1578 |
expr.setStart(ExpressionTool.makePathExpression(start, stepPath.getSelectExpression())); |
| 1579 |
expr.setStep(stepPath.getActionExpression()); |
| 1580 |
} |
| 1581 |
} |
| 1582 |
|
| 1583 |
return expr; |
| 1584 |
} |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
public static Operand findOperand(Expression parentExpression, Expression childExpression) { |
| 1596 |
for (Operand o : parentExpression.operands()) { |
| 1597 |
if (o.getChildExpression() == childExpression) { |
| 1598 |
return o; |
| 1599 |
} |
| 1600 |
} |
| 1601 |
return null; |
| 1602 |
} |
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
private static boolean isFilteredAxisPath(Expression exp) { |
| 1613 |
return unfilteredExpression(exp, true) instanceof AxisExpression; |
| 1614 |
} |
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
public static Expression unfilteredExpression(Expression exp, boolean allowPositional) { |
| 1625 |
if (exp instanceof FilterExpression && (allowPositional || !((FilterExpression) exp).isFilterIsPositional())) { |
| 1626 |
return unfilteredExpression(((FilterExpression) exp).getSelectExpression(), allowPositional); |
| 1627 |
} else if (exp instanceof SingleItemFilter && allowPositional) { |
| 1628 |
return unfilteredExpression(((SingleItemFilter) exp).getBaseExpression(), allowPositional); |
| 1629 |
} else { |
| 1630 |
return exp; |
| 1631 |
} |
| 1632 |
} |
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
public static Expression tryToFactorOutDot(Expression exp, ItemType contextItemType) { |
| 1648 |
if (exp instanceof ContextItemExpression) { |
| 1649 |
return null; |
| 1650 |
} else if (exp instanceof LetExpression && ((LetExpression) exp).getSequence() instanceof ContextItemExpression) { |
| 1651 |
Expression action = ((LetExpression) exp).getAction(); |
| 1652 |
boolean changed = factorOutDot(action, (LetExpression) exp); |
| 1653 |
if (changed) { |
| 1654 |
exp.resetLocalStaticProperties(); |
| 1655 |
} |
| 1656 |
return exp; |
| 1657 |
} else if ((exp.getDependencies() & |
| 1658 |
(StaticProperty.DEPENDS_ON_CONTEXT_ITEM | StaticProperty.DEPENDS_ON_CONTEXT_DOCUMENT)) != 0) { |
| 1659 |
LetExpression let = new LetExpression(); |
| 1660 |
let.setVariableQName( |
| 1661 |
new StructuredQName("saxon", NamespaceConstant.SAXON, "dot" + exp.hashCode())); |
| 1662 |
let.setRequiredType(SequenceType.makeSequenceType(contextItemType, StaticProperty.EXACTLY_ONE)); |
| 1663 |
let.setSequence(new ContextItemExpression()); |
| 1664 |
let.setAction(exp); |
| 1665 |
boolean changed = factorOutDot(exp, let); |
| 1666 |
if (changed) { |
| 1667 |
return let; |
| 1668 |
} else { |
| 1669 |
return exp; |
| 1670 |
} |
| 1671 |
} else { |
| 1672 |
return null; |
| 1673 |
} |
| 1674 |
} |
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
public static boolean factorOutDot(Expression exp, Binding variable) { |
| 1685 |
boolean changed = false; |
| 1686 |
if ((exp.getDependencies() & |
| 1687 |
(StaticProperty.DEPENDS_ON_CONTEXT_ITEM | StaticProperty.DEPENDS_ON_CONTEXT_DOCUMENT)) != 0) { |
| 1688 |
for (Operand info : exp.operands()) { |
| 1689 |
if (info.hasSameFocus()) { |
| 1690 |
Expression child = info.getChildExpression(); |
| 1691 |
if (child instanceof ContextItemExpression) { |
| 1692 |
VariableReference ref = variable.isGlobal() ? |
| 1693 |
new GlobalVariableReference((GlobalVariable) variable) : |
| 1694 |
new LocalVariableReference((LocalBinding) variable); |
| 1695 |
copyLocationInfo(child, ref); |
| 1696 |
info.setChildExpression(ref); |
| 1697 |
changed = true; |
| 1698 |
} else if (child instanceof AxisExpression || |
| 1699 |
child instanceof RootExpression) { |
| 1700 |
VariableReference ref = variable.isGlobal() ? |
| 1701 |
new GlobalVariableReference((GlobalVariable) variable) : |
| 1702 |
new LocalVariableReference((LocalBinding) variable); |
| 1703 |
copyLocationInfo(child, ref); |
| 1704 |
Expression path = ExpressionTool.makePathExpression(ref, child); |
| 1705 |
info.setChildExpression(path); |
| 1706 |
changed = true; |
| 1707 |
} else { |
| 1708 |
changed |= factorOutDot(child, variable); |
| 1709 |
} |
| 1710 |
} |
| 1711 |
} |
| 1712 |
} |
| 1713 |
if (changed) { |
| 1714 |
exp.resetLocalStaticProperties(); |
| 1715 |
} |
| 1716 |
return changed; |
| 1717 |
} |
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
public static boolean inlineVariableReferences(Expression expr, Binding binding, Expression replacement) { |
| 1729 |
return inlineVariableReferencesInternal(expr, binding, replacement); |
| 1730 |
} |
| 1731 |
|
| 1732 |
public static boolean inlineVariableReferencesInternal(Expression expr, Binding binding, Expression replacement) { |
| 1733 |
if (expr instanceof TryCatch && !(replacement instanceof Literal)) { |
| 1734 |
|
| 1735 |
|
| 1736 |
return false; |
| 1737 |
} else { |
| 1738 |
boolean found = false; |
| 1739 |
for (Operand o : expr.operands()) { |
| 1740 |
Expression child = o.getChildExpression(); |
| 1741 |
if (child instanceof VariableReference && |
| 1742 |
((VariableReference) child).getBinding() == binding) { |
| 1743 |
Expression copy; |
| 1744 |
try { |
| 1745 |
copy = replacement.copy(new RebindingMap()); |
| 1746 |
ExpressionTool.copyLocationInfo(child, copy); |
| 1747 |
} catch (UnsupportedOperationException err) { |
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
copy = replacement; |
| 1752 |
} |
| 1753 |
o.setChildExpression(copy); |
| 1754 |
found = true; |
| 1755 |
} else { |
| 1756 |
found |= inlineVariableReferencesInternal(child, binding, replacement); |
| 1757 |
} |
| 1758 |
} |
| 1759 |
if (found) { |
| 1760 |
expr.resetLocalStaticProperties(); |
| 1761 |
} |
| 1762 |
return found; |
| 1763 |
} |
| 1764 |
} |
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
public static boolean replaceTrivialCallsToCurrent(Expression expr) { |
| 1774 |
boolean found = false; |
| 1775 |
for (Operand o : expr.operands()) { |
| 1776 |
if (o.hasSameFocus()) { |
| 1777 |
Expression child = o.getChildExpression(); |
| 1778 |
if (child.isCallOn(Current.class)) { |
| 1779 |
CurrentItemExpression var = new CurrentItemExpression(); |
| 1780 |
ExpressionTool.copyLocationInfo(child, var); |
| 1781 |
o.setChildExpression(var); |
| 1782 |
found = true; |
| 1783 |
} else { |
| 1784 |
found = replaceTrivialCallsToCurrent(child); |
| 1785 |
} |
| 1786 |
} |
| 1787 |
} |
| 1788 |
if (found) { |
| 1789 |
expr.resetLocalStaticProperties(); |
| 1790 |
} |
| 1791 |
return found; |
| 1792 |
} |
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
public static boolean replaceCallsToCurrent(Expression expr, LocalBinding binding) { |
| 1803 |
boolean found = false; |
| 1804 |
for (Operand o : expr.operands()) { |
| 1805 |
Expression child = o.getChildExpression(); |
| 1806 |
if (child.isCallOn(Current.class)) { |
| 1807 |
LocalVariableReference var = new LocalVariableReference(binding); |
| 1808 |
ExpressionTool.copyLocationInfo(child, var); |
| 1809 |
o.setChildExpression(var); |
| 1810 |
binding.addReference(var, true); |
| 1811 |
found = true; |
| 1812 |
} else { |
| 1813 |
found = replaceCallsToCurrent(child, binding); |
| 1814 |
} |
| 1815 |
} |
| 1816 |
if (found) { |
| 1817 |
expr.resetLocalStaticProperties(); |
| 1818 |
} |
| 1819 |
return found; |
| 1820 |
} |
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
public static boolean isNotAllowedInUpdatingContext(Expression exp) { |
| 1833 |
return !exp.isUpdatingExpression() && !exp.isVacuousExpression(); |
| 1834 |
} |
| 1835 |
|
| 1836 |
|
| 1837 |
public static String getCurrentDirectory() { |
| 1838 |
String dir; |
| 1839 |
try { |
| 1840 |
dir = System.getProperty("user.dir"); |
| 1841 |
} catch (Exception geterr) { |
| 1842 |
|
| 1843 |
return null; |
| 1844 |
} |
| 1845 |
if (!dir.endsWith("/")) { |
| 1846 |
dir = dir + '/'; |
| 1847 |
} |
| 1848 |
|
| 1849 |
URI currentDirectoryURL = new File(dir).toURI(); |
| 1850 |
return currentDirectoryURL.toString(); |
| 1851 |
} |
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
public static URI getBaseURI(StaticContext env, SourceLocator locator, boolean fail) throws XPathException { |
| 1866 |
URI expressionBaseURI = null; |
| 1867 |
String base = null; |
| 1868 |
try { |
| 1869 |
base = env.getStaticBaseURI(); |
| 1870 |
if (base == null) { |
| 1871 |
base = getCurrentDirectory(); |
| 1872 |
} |
| 1873 |
if (base != null) { |
| 1874 |
expressionBaseURI = new URI(base); |
| 1875 |
} |
| 1876 |
} catch (URISyntaxException e) { |
| 1877 |
|
| 1878 |
|
| 1879 |
String esc = IriToUri.iriToUri(base).toString(); |
| 1880 |
try { |
| 1881 |
expressionBaseURI = new URI(esc); |
| 1882 |
} catch (URISyntaxException e2) { |
| 1883 |
|
| 1884 |
expressionBaseURI = null; |
| 1885 |
} |
| 1886 |
|
| 1887 |
if (expressionBaseURI == null && fail) { |
| 1888 |
XPathException err = new XPathException("The base URI " + Err.wrap(env.getStaticBaseURI(), Err.URI) + |
| 1889 |
" is not a valid URI"); |
| 1890 |
err.setLocator(locator); |
| 1891 |
throw err; |
| 1892 |
} |
| 1893 |
} |
| 1894 |
return expressionBaseURI; |
| 1895 |
} |
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
public static String parenthesize(Expression exp) { |
| 1906 |
if (exp.operands().iterator().hasNext()) { |
| 1907 |
return "(" + exp.toString() + ")"; |
| 1908 |
} else { |
| 1909 |
return exp.toString(); |
| 1910 |
} |
| 1911 |
} |
| 1912 |
|
| 1913 |
public static String parenthesizeShort(Expression exp) { |
| 1914 |
if (hasTwoOrMoreOperands(exp)) { |
| 1915 |
return "(" + exp.toShortString() + ")"; |
| 1916 |
} else { |
| 1917 |
return exp.toShortString(); |
| 1918 |
} |
| 1919 |
} |
| 1920 |
|
| 1921 |
private static boolean hasTwoOrMoreOperands(Expression exp) { |
| 1922 |
Iterator ops = exp.operands().iterator(); |
| 1923 |
if (!ops.hasNext()) { |
| 1924 |
return false; |
| 1925 |
} |
| 1926 |
ops.next(); |
| 1927 |
return ops.hasNext(); |
| 1928 |
} |
| 1929 |
|
| 1930 |
|
| 1931 |
public static void validateTree(Expression exp) { |
| 1932 |
try { |
| 1933 |
for (Operand o : exp.checkedOperands()) { |
| 1934 |
validateTree(o.getChildExpression()); |
| 1935 |
} |
| 1936 |
} catch (IllegalStateException e) { |
| 1937 |
e.printStackTrace(); |
| 1938 |
} |
| 1939 |
} |
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
public static boolean isLocalConstructor(Expression child) { |
| 1950 |
if (!(child instanceof ParentNodeConstructor || child instanceof SimpleNodeConstructor)) { |
| 1951 |
return false; |
| 1952 |
} |
| 1953 |
Expression parent = child.getParentExpression(); |
| 1954 |
while (parent != null) { |
| 1955 |
if (parent instanceof ParentNodeConstructor) { |
| 1956 |
return true; |
| 1957 |
} |
| 1958 |
Operand o = findOperand(parent, child); |
| 1959 |
if (o.getUsage() != OperandUsage.TRANSMISSION) { |
| 1960 |
return false; |
| 1961 |
} |
| 1962 |
child = parent; |
| 1963 |
parent = parent.getParentExpression(); |
| 1964 |
} |
| 1965 |
return false; |
| 1966 |
} |
| 1967 |
} |
| 1968 |
|