| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.parser.*; |
| 12 |
import net.sf.saxon.om.*; |
| 13 |
import net.sf.saxon.pattern.NodeKindTest; |
| 14 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 17 |
import net.sf.saxon.tree.iter.ListIterator; |
| 18 |
import net.sf.saxon.type.*; |
| 19 |
import net.sf.saxon.value.*; |
| 20 |
|
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.Arrays; |
| 23 |
import java.util.List; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class Block extends Instruction { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
private final Operand[] operanda; |
| 36 |
private boolean allNodesUntyped; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public Block(Expression[] children) { |
| 44 |
operanda = new Operand[children.length]; |
| 45 |
for (int i=0; i<children.length; i++) { |
| 46 |
operanda[i] = new Operand(this, children[i], OperandRole.SAME_FOCUS_ACTION); |
| 47 |
} |
| 48 |
for (Expression e : children) { |
| 49 |
adoptChildExpression(e); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public boolean isInstruction() { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
private Expression child(int n) { |
| 65 |
return operanda[n].getChildExpression(); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private void setChild(int n, Expression child) { |
| 75 |
operanda[n].setChildExpression(child); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
private int size() { |
| 84 |
return operanda.length; |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public Iterable<Operand> operands() { |
| 89 |
return Arrays.asList(operanda); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
@Override |
| 101 |
public boolean hasVariableBinding(Binding binding) { |
| 102 |
if (binding instanceof LocalParam) { |
| 103 |
for (Operand o : operanda) { |
| 104 |
if (o.getChildExpression() == binding) { |
| 105 |
return true; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static Expression makeBlock( Expression e1, Expression e2) { |
| 123 |
if (e1 == null || Literal.isEmptySequence(e1)) { |
| 124 |
return e2; |
| 125 |
} |
| 126 |
if (e2 == null || Literal.isEmptySequence(e2)) { |
| 127 |
return e1; |
| 128 |
} |
| 129 |
if (e1 instanceof Block || e2 instanceof Block) { |
| 130 |
List<Expression> list = new ArrayList<>(10); |
| 131 |
if (e1 instanceof Block) { |
| 132 |
for (Operand o : e1.operands()) { |
| 133 |
list.add(o.getChildExpression()); |
| 134 |
} |
| 135 |
} else { |
| 136 |
list.add(e1); |
| 137 |
} |
| 138 |
if (e2 instanceof Block) { |
| 139 |
for (Operand o : e2.operands()) { |
| 140 |
list.add(o.getChildExpression()); |
| 141 |
} |
| 142 |
} else { |
| 143 |
list.add(e2); |
| 144 |
} |
| 145 |
|
| 146 |
Expression[] exps = new Expression[list.size()]; |
| 147 |
exps = list.toArray(exps); |
| 148 |
return new Block(exps); |
| 149 |
|
| 150 |
} else { |
| 151 |
Expression[] exps = {e1, e2}; |
| 152 |
return new Block(exps); |
| 153 |
|
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public static Expression makeBlock(List<Expression> list) { |
| 168 |
if (list.isEmpty()) { |
| 169 |
return Literal.makeEmptySequence(); |
| 170 |
} else if (list.size() == 1) { |
| 171 |
return list.get(0); |
| 172 |
} else { |
| 173 |
Expression[] exps = new Expression[list.size()]; |
| 174 |
exps = list.toArray(exps); |
| 175 |
return new Block(exps); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
public String getExpressionName() { |
| 180 |
return "sequence"; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public Operand[] getOperanda() { |
| 191 |
return operanda; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
public int computeSpecialProperties() { |
| 196 |
if (size() == 0) { |
| 197 |
|
| 198 |
return StaticProperty.SPECIAL_PROPERTY_MASK & ~StaticProperty.HAS_SIDE_EFFECTS; |
| 199 |
} |
| 200 |
int p = super.computeSpecialProperties(); |
| 201 |
if (allNodesUntyped) { |
| 202 |
p |= StaticProperty.ALL_NODES_UNTYPED; |
| 203 |
} |
| 204 |
|
| 205 |
boolean allAxisExpressions = true; |
| 206 |
boolean allChildAxis = true; |
| 207 |
boolean allSubtreeAxis = true; |
| 208 |
for (Operand o : operands()) { |
| 209 |
Expression child = o.getChildExpression(); |
| 210 |
if (!(child instanceof AxisExpression)) { |
| 211 |
allAxisExpressions = false; |
| 212 |
allChildAxis = false; |
| 213 |
allSubtreeAxis = false; |
| 214 |
break; |
| 215 |
} |
| 216 |
byte axis = ((AxisExpression) child).getAxis(); |
| 217 |
if (axis != AxisInfo.CHILD) { |
| 218 |
allChildAxis = false; |
| 219 |
} |
| 220 |
if (!AxisInfo.isSubtreeAxis[axis]) { |
| 221 |
allSubtreeAxis = false; |
| 222 |
} |
| 223 |
} |
| 224 |
if (allAxisExpressions) { |
| 225 |
p |= StaticProperty.CONTEXT_DOCUMENT_NODESET | |
| 226 |
StaticProperty.SINGLE_DOCUMENT_NODESET | |
| 227 |
StaticProperty.NO_NODES_NEWLY_CREATED; |
| 228 |
|
| 229 |
if (allChildAxis) { |
| 230 |
p |= StaticProperty.PEER_NODESET; |
| 231 |
} |
| 232 |
if (allSubtreeAxis) { |
| 233 |
p |= StaticProperty.SUBTREE_NODESET; |
| 234 |
} |
| 235 |
|
| 236 |
if (size() == 2 && |
| 237 |
((AxisExpression) child(0)).getAxis() == AxisInfo.ATTRIBUTE && |
| 238 |
((AxisExpression) child(1)).getAxis() == AxisInfo.CHILD) { |
| 239 |
p |= StaticProperty.ORDERED_NODESET; |
| 240 |
} |
| 241 |
} |
| 242 |
return p; |
| 243 |
} |
| 244 |
|
| 245 |
public boolean implementsStaticTypeCheck() { |
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public Expression staticTypeCheck(SequenceType req, |
| 263 |
boolean backwardsCompatible, |
| 264 |
RoleDiagnostic role, ExpressionVisitor visitor) |
| 265 |
throws XPathException { |
| 266 |
|
| 267 |
TypeChecker tc = visitor.getConfiguration().getTypeChecker(backwardsCompatible); |
| 268 |
if (backwardsCompatible && !Cardinality.allowsMany(req.getCardinality())) { |
| 269 |
Expression first = FirstItemExpression.makeFirstItemExpression(this); |
| 270 |
return tc.staticTypeCheck(first, req, role, visitor); |
| 271 |
} |
| 272 |
Expression[] checked = new Expression[operanda.length]; |
| 273 |
SequenceType subReq = req; |
| 274 |
if (req.getCardinality() != StaticProperty.ALLOWS_ZERO_OR_MORE) { |
| 275 |
subReq = SequenceType.makeSequenceType(req.getPrimaryType(), StaticProperty.ALLOWS_ZERO_OR_MORE); |
| 276 |
} |
| 277 |
for (int i=0; i<operanda.length; i++) { |
| 278 |
checked[i] = tc.staticTypeCheck(operanda[i].getChildExpression(), subReq, role, visitor); |
| 279 |
} |
| 280 |
Block b2 = new Block(checked); |
| 281 |
ExpressionTool.copyLocationInfo(this, b2); |
| 282 |
b2.allNodesUntyped = allNodesUntyped; |
| 283 |
|
| 284 |
int reqCard = req.getCardinality(); |
| 285 |
int suppliedCard = b2.getCardinality(); |
| 286 |
if (!Cardinality.subsumes(req.getCardinality(), suppliedCard)) { |
| 287 |
if ((reqCard & suppliedCard) == 0) { |
| 288 |
XPathException err = new XPathException( |
| 289 |
"The required cardinality of the " + role.getMessage() + |
| 290 |
" is " + Cardinality.toString(reqCard) + |
| 291 |
", but the supplied cardinality is " + Cardinality.toString(suppliedCard), role.getErrorCode(), getLocation()); |
| 292 |
err.setIsTypeError(true); |
| 293 |
err.setFailingExpression(this); |
| 294 |
throw err; |
| 295 |
} else { |
| 296 |
return CardinalityChecker.makeCardinalityChecker(b2, reqCard, role); |
| 297 |
} |
| 298 |
} |
| 299 |
return b2; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public static boolean neverReturnsTypedNodes(Instruction insn, TypeHierarchy th) { |
| 311 |
for (Operand o : insn.operands()) { |
| 312 |
Expression exp = o.getChildExpression(); |
| 313 |
if (!exp.hasSpecialProperty(StaticProperty.ALL_NODES_UNTYPED)) { |
| 314 |
ItemType it = exp.getItemType(); |
| 315 |
if (th.relationship(it, NodeKindTest.ELEMENT) != TypeHierarchy.DISJOINT || |
| 316 |
th.relationship(it, NodeKindTest.ATTRIBUTE) != TypeHierarchy.DISJOINT) { |
| 317 |
return false; |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
return true; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public Expression mergeAdjacentTextInstructions() { |
| 331 |
boolean[] isLiteralText = new boolean[size()]; |
| 332 |
boolean hasAdjacentTextNodes = false; |
| 333 |
for (int i = 0; i < size(); i++) { |
| 334 |
isLiteralText[i] = child(i) instanceof ValueOf && |
| 335 |
((ValueOf) child(i)).getSelect() instanceof StringLiteral && |
| 336 |
!((ValueOf) child(i)).isDisableOutputEscaping(); |
| 337 |
|
| 338 |
if (i > 0 && isLiteralText[i] && isLiteralText[i - 1]) { |
| 339 |
hasAdjacentTextNodes = true; |
| 340 |
} |
| 341 |
} |
| 342 |
if (hasAdjacentTextNodes) { |
| 343 |
List<Expression> content = new ArrayList<>(size()); |
| 344 |
String pendingText = null; |
| 345 |
for (int i = 0; i < size(); i++) { |
| 346 |
if (isLiteralText[i]) { |
| 347 |
pendingText = (pendingText == null ? "" : pendingText) + |
| 348 |
((StringLiteral) ((ValueOf) child(i)).getSelect()).getStringValue(); |
| 349 |
} else { |
| 350 |
if (pendingText != null) { |
| 351 |
ValueOf inst = new ValueOf(new StringLiteral(pendingText), false, false); |
| 352 |
content.add(inst); |
| 353 |
pendingText = null; |
| 354 |
} |
| 355 |
content.add(child(i)); |
| 356 |
} |
| 357 |
} |
| 358 |
if (pendingText != null) { |
| 359 |
ValueOf inst = new ValueOf(new StringLiteral(pendingText), false, false); |
| 360 |
content.add(inst); |
| 361 |
} |
| 362 |
return makeBlock(content); |
| 363 |
} else { |
| 364 |
return this; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public Expression copy(RebindingMap rebindings) { |
| 378 |
Expression[] c2 = new Expression[size()]; |
| 379 |
for (int c = 0; c < size(); c++) { |
| 380 |
c2[c] = child(c).copy(rebindings); |
| 381 |
} |
| 382 |
Block b2 = new Block(c2); |
| 383 |
for (int c = 0; c < size(); c++) { |
| 384 |
b2.adoptChildExpression(c2[c]); |
| 385 |
} |
| 386 |
b2.allNodesUntyped = allNodesUntyped; |
| 387 |
ExpressionTool.copyLocationInfo(this, b2); |
| 388 |
return b2; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public final ItemType getItemType() { |
| 399 |
if (size() == 0) { |
| 400 |
return ErrorType.getInstance(); |
| 401 |
} |
| 402 |
ItemType t1 = null; |
| 403 |
TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 404 |
for (int i = 0; i < size(); i++) { |
| 405 |
Expression child = child(i); |
| 406 |
if (!(child instanceof Message)) { |
| 407 |
ItemType t = child.getItemType(); |
| 408 |
t1 = t1 == null ? t : Type.getCommonSuperType(t1, t, th); |
| 409 |
if (t1 instanceof AnyItemType) { |
| 410 |
return t1; |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
return t1 == null ? ErrorType.getInstance() : t1; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
@Override |
| 425 |
public UType getStaticUType(UType contextItemType) { |
| 426 |
if (isInstruction()) { |
| 427 |
return super.getStaticUType(contextItemType); |
| 428 |
} else { |
| 429 |
if (size() == 0) { |
| 430 |
return UType.VOID; |
| 431 |
} |
| 432 |
UType t1 = child(0).getStaticUType(contextItemType); |
| 433 |
for (int i = 1; i < size(); i++) { |
| 434 |
t1 = t1.union(child(i).getStaticUType(contextItemType)); |
| 435 |
if (t1 == UType.ANY) { |
| 436 |
return t1; |
| 437 |
} |
| 438 |
} |
| 439 |
return t1; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public int computeCardinality() { |
| 448 |
if (size() == 0) { |
| 449 |
return StaticProperty.EMPTY; |
| 450 |
} |
| 451 |
int c1 = child(0).getCardinality(); |
| 452 |
for (int i = 1; i < size(); i++) { |
| 453 |
c1 = Cardinality.sum(c1, child(i).getCardinality()); |
| 454 |
if (c1 == StaticProperty.ALLOWS_MANY) { |
| 455 |
break; |
| 456 |
} |
| 457 |
} |
| 458 |
return c1; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
public final boolean mayCreateNewNodes() { |
| 468 |
return someOperandCreatesNewNodes(); |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
public void checkForUpdatingSubexpressions() throws XPathException { |
| 481 |
if (size() < 2) { |
| 482 |
return; |
| 483 |
} |
| 484 |
boolean updating = false; |
| 485 |
boolean nonUpdating = false; |
| 486 |
for (Operand o : operands()) { |
| 487 |
Expression child = o.getChildExpression(); |
| 488 |
if (ExpressionTool.isNotAllowedInUpdatingContext(child)) { |
| 489 |
if (updating) { |
| 490 |
XPathException err = new XPathException( |
| 491 |
"If any subexpression is updating, then all must be updating", "XUST0001"); |
| 492 |
err.setLocation(child.getLocation()); |
| 493 |
throw err; |
| 494 |
} |
| 495 |
nonUpdating = true; |
| 496 |
} |
| 497 |
if (child.isUpdatingExpression()) { |
| 498 |
if (nonUpdating) { |
| 499 |
XPathException err = new XPathException( |
| 500 |
"If any subexpression is updating, then all must be updating", "XUST0001"); |
| 501 |
err.setLocation(child.getLocation()); |
| 502 |
throw err; |
| 503 |
} |
| 504 |
updating = true; |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
public boolean isVacuousExpression() { |
| 516 |
|
| 517 |
for (Operand o : operands()) { |
| 518 |
Expression child = o.getChildExpression(); |
| 519 |
if (!child.isVacuousExpression()) { |
| 520 |
return false; |
| 521 |
} |
| 522 |
} |
| 523 |
return true; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
public Expression simplify() throws XPathException { |
| 538 |
boolean allAtomic = true; |
| 539 |
boolean nested = false; |
| 540 |
|
| 541 |
for (int c = 0; c < size(); c++) { |
| 542 |
setChild(c, child(c).simplify()); |
| 543 |
if (!Literal.isAtomic(child(c))) { |
| 544 |
allAtomic = false; |
| 545 |
} |
| 546 |
if (child(c) instanceof Block) { |
| 547 |
nested = true; |
| 548 |
} else if (Literal.isEmptySequence(child(c))) { |
| 549 |
nested = true; |
| 550 |
} |
| 551 |
} |
| 552 |
if (size() == 1) { |
| 553 |
Expression e = getOperanda()[0].getChildExpression(); |
| 554 |
e.setParentExpression(getParentExpression()); |
| 555 |
return e; |
| 556 |
} else if (size() == 0) { |
| 557 |
Expression result = Literal.makeEmptySequence(); |
| 558 |
ExpressionTool.copyLocationInfo(this, result); |
| 559 |
result.setParentExpression(getParentExpression()); |
| 560 |
return result; |
| 561 |
} else if (nested) { |
| 562 |
List<Expression> list = new ArrayList<>(size() * 2); |
| 563 |
flatten(list); |
| 564 |
Expression[] children = new Expression[list.size()]; |
| 565 |
for (int i = 0; i < list.size(); i++) { |
| 566 |
children[i] = list.get(i); |
| 567 |
} |
| 568 |
Block newBlock = new Block(children); |
| 569 |
ExpressionTool.copyLocationInfo(this, newBlock); |
| 570 |
return newBlock.simplify(); |
| 571 |
} else if (allAtomic) { |
| 572 |
AtomicValue[] values = new AtomicValue[size()]; |
| 573 |
for (int c = 0; c < size(); c++) { |
| 574 |
values[c] = (AtomicValue) ((Literal) child(c)).getValue(); |
| 575 |
} |
| 576 |
Expression result = Literal.makeLiteral(new SequenceExtent<>(values), this); |
| 577 |
result.setParentExpression(getParentExpression()); |
| 578 |
return result; |
| 579 |
} else { |
| 580 |
return this; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
private void flatten(List<Expression> targetList) throws XPathException { |
| 594 |
List<Item<?>> currentLiteralList = null; |
| 595 |
for (Operand o : operands()) { |
| 596 |
Expression child = o.getChildExpression(); |
| 597 |
if (Literal.isEmptySequence(child)) { |
| 598 |
|
| 599 |
} else if (child instanceof Block) { |
| 600 |
flushCurrentLiteralList(currentLiteralList, targetList); |
| 601 |
currentLiteralList = null; |
| 602 |
((Block) child).flatten(targetList); |
| 603 |
} else if (child instanceof Literal && !(((Literal) child).getValue() instanceof IntegerRange)) { |
| 604 |
SequenceIterator<?> iterator = ((Literal) child).getValue().iterate(); |
| 605 |
if (currentLiteralList == null) { |
| 606 |
currentLiteralList = new ArrayList<>(10); |
| 607 |
} |
| 608 |
Item item; |
| 609 |
while ((item = iterator.next()) != null) { |
| 610 |
currentLiteralList.add(item); |
| 611 |
} |
| 612 |
|
| 613 |
} else { |
| 614 |
flushCurrentLiteralList(currentLiteralList, targetList); |
| 615 |
currentLiteralList = null; |
| 616 |
targetList.add(child); |
| 617 |
} |
| 618 |
} |
| 619 |
flushCurrentLiteralList(currentLiteralList, targetList); |
| 620 |
} |
| 621 |
|
| 622 |
private void flushCurrentLiteralList(List<Item<?>> currentLiteralList, List<Expression> list) { |
| 623 |
if (currentLiteralList != null) { |
| 624 |
ListIterator<Item<?>> iter = new ListIterator<>(currentLiteralList); |
| 625 |
Literal lit = Literal.makeLiteral(iter.materialize(), this); |
| 626 |
list.add(lit); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
public boolean isCandidateForSharedAppend() { |
| 637 |
for (Operand o : operands()) { |
| 638 |
Expression exp = o.getChildExpression(); |
| 639 |
if (exp instanceof VariableReference || exp instanceof Literal) { |
| 640 |
return true; |
| 641 |
} |
| 642 |
} |
| 643 |
return false; |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 648 |
typeCheckChildren(visitor, contextInfo); |
| 649 |
if (neverReturnsTypedNodes(this, visitor.getConfiguration().getTypeHierarchy())) { |
| 650 |
resetLocalStaticProperties(); |
| 651 |
allNodesUntyped = true; |
| 652 |
} |
| 653 |
return this; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 658 |
optimizeChildren(visitor, contextInfo); |
| 659 |
boolean canSimplify = false; |
| 660 |
boolean prevLiteral = false; |
| 661 |
|
| 662 |
for (Operand o : operands()) { |
| 663 |
Expression child = o.getChildExpression(); |
| 664 |
if (child instanceof Block) { |
| 665 |
canSimplify = true; |
| 666 |
break; |
| 667 |
} |
| 668 |
if (child instanceof Literal && !(((Literal) child).getValue() instanceof IntegerRange)) { |
| 669 |
if (prevLiteral || Literal.isEmptySequence(child)) { |
| 670 |
canSimplify = true; |
| 671 |
break; |
| 672 |
} |
| 673 |
prevLiteral = true; |
| 674 |
} else { |
| 675 |
prevLiteral = false; |
| 676 |
} |
| 677 |
} |
| 678 |
if (canSimplify) { |
| 679 |
List<Expression> list = new ArrayList<>(size() * 2); |
| 680 |
flatten(list); |
| 681 |
Expression result = Block.makeBlock(list); |
| 682 |
result.setRetainedStaticContext(getRetainedStaticContext()); |
| 683 |
return result; |
| 684 |
} |
| 685 |
if (size() == 0) { |
| 686 |
return Literal.makeEmptySequence(); |
| 687 |
} else if (size() == 1) { |
| 688 |
return child(0); |
| 689 |
} else { |
| 690 |
return this; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
public void checkPermittedContents(SchemaType parentType, boolean whole) throws XPathException { |
| 704 |
for (Operand o : operands()) { |
| 705 |
Expression child = o.getChildExpression(); |
| 706 |
child.checkPermittedContents(parentType, false); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
public void export(ExpressionPresenter out) throws XPathException { |
| 716 |
out.startElement("sequence", this); |
| 717 |
for (Operand o : operands()) { |
| 718 |
Expression child = o.getChildExpression(); |
| 719 |
child.export(out); |
| 720 |
} |
| 721 |
out.endElement(); |
| 722 |
} |
| 723 |
|
| 724 |
@Override |
| 725 |
public String toShortString() { |
| 726 |
return "(" + child(0).toShortString() + ", ...)"; |
| 727 |
} |
| 728 |
|
| 729 |
public TailCall processLeavingTail(XPathContext context) throws XPathException { |
| 730 |
TailCall tc = null; |
| 731 |
for (Operand o : operands()) { |
| 732 |
Expression child = o.getChildExpression(); |
| 733 |
try { |
| 734 |
if (child instanceof TailCallReturner) { |
| 735 |
tc = ((TailCallReturner) child).processLeavingTail(context); |
| 736 |
} else { |
| 737 |
child.process(context); |
| 738 |
tc = null; |
| 739 |
} |
| 740 |
} catch (XPathException e) { |
| 741 |
e.maybeSetLocation(child.getLocation()); |
| 742 |
e.maybeSetContext(context); |
| 743 |
throw e; |
| 744 |
} |
| 745 |
} |
| 746 |
return tc; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
public int getImplementationMethod() { |
| 756 |
return ITERATE_METHOD | PROCESS_METHOD; |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 765 |
if (size() == 0) { |
| 766 |
return EmptyIterator.emptyIterator(); |
| 767 |
} else if (size() == 1) { |
| 768 |
return child(0).iterate(context); |
| 769 |
} else { |
| 770 |
return new BlockIterator(operanda, context); |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
public void evaluatePendingUpdates(XPathContext context, PendingUpdateList pul) throws XPathException { |
| 785 |
for (Operand o : operands()) { |
| 786 |
Expression child = o.getChildExpression(); |
| 787 |
child.evaluatePendingUpdates(context, pul); |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
@Override |
| 792 |
public String getStreamerName() { |
| 793 |
return "Block"; |
| 794 |
} |
| 795 |
|
| 796 |
} |