| 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.functions.BooleanFn; |
| 13 |
import net.sf.saxon.functions.SystemFunction; |
| 14 |
import net.sf.saxon.om.Item; |
| 15 |
import net.sf.saxon.om.SequenceIterator; |
| 16 |
import net.sf.saxon.om.StandardNames; |
| 17 |
import net.sf.saxon.om.StructuredQName; |
| 18 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 21 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 22 |
import net.sf.saxon.type.*; |
| 23 |
import net.sf.saxon.value.BooleanValue; |
| 24 |
import net.sf.saxon.value.Cardinality; |
| 25 |
import net.sf.saxon.value.SequenceType; |
| 26 |
|
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.Arrays; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class Choose extends Instruction implements ConditionalInstruction { |
| 38 |
|
| 39 |
private Operand[] conditionOps; |
| 40 |
private Operand[] actionOps; |
| 41 |
private boolean isInstruction; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public final static OperandRole CHOICE_ACTION = |
| 54 |
new OperandRole(OperandRole.IN_CHOICE_GROUP, OperandUsage.TRANSMISSION, SequenceType.ANY_SEQUENCE); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public Choose(Expression[] conditions, Expression[] actions) { |
| 65 |
conditionOps = new Operand[conditions.length]; |
| 66 |
for (int i=0; i<conditions.length; i++) { |
| 67 |
conditionOps[i] = new Operand(this, conditions[i], OperandRole.INSPECT); |
| 68 |
} |
| 69 |
actionOps = new Operand[actions.length]; |
| 70 |
for (int i=0; i<actions.length; i++) { |
| 71 |
actionOps[i] = new Operand(this, actions[i], CHOICE_ACTION); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static Expression makeConditional(Expression condition, Expression thenExp, Expression elseExp) { |
| 85 |
if (Literal.isEmptySequence(elseExp)) { |
| 86 |
Expression[] conditions = new Expression[]{condition}; |
| 87 |
Expression[] actions = new Expression[]{thenExp}; |
| 88 |
return new Choose(conditions, actions); |
| 89 |
} else { |
| 90 |
Expression[] conditions = new Expression[]{condition, Literal.makeLiteral(BooleanValue.TRUE, condition)}; |
| 91 |
Expression[] actions = new Expression[]{thenExp, elseExp}; |
| 92 |
return new Choose(conditions, actions); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public static Expression makeConditional(Expression condition, Expression thenExp) { |
| 105 |
Expression[] conditions = new Expression[]{condition}; |
| 106 |
Expression[] actions = new Expression[]{thenExp}; |
| 107 |
return new Choose(conditions, actions); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public void setInstruction(boolean inst) { |
| 117 |
isInstruction = inst; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
@Override |
| 128 |
public boolean isInstruction() { |
| 129 |
return isInstruction; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
public int size() { |
| 134 |
return conditionOps.length; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public static boolean isSingleBranchChoice(Expression exp) { |
| 147 |
return exp instanceof Choose && ((Choose) exp).size() == 1; |
| 148 |
} |
| 149 |
|
| 150 |
public int getNumberOfConditions() { |
| 151 |
return size(); |
| 152 |
} |
| 153 |
|
| 154 |
public Expression getCondition(int i) { |
| 155 |
return conditionOps[i].getChildExpression(); |
| 156 |
} |
| 157 |
|
| 158 |
public void setCondition(int i, Expression condition) { |
| 159 |
conditionOps[i].setChildExpression(condition); |
| 160 |
} |
| 161 |
|
| 162 |
public Iterable<Operand> conditions() { |
| 163 |
return Arrays.asList(conditionOps); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public Operand getActionOperand(int i) { |
| 173 |
return actionOps[i]; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public Expression getAction(int i) { |
| 184 |
return actionOps[i].getChildExpression(); |
| 185 |
} |
| 186 |
|
| 187 |
public void setAction(int i, Expression action) { |
| 188 |
actionOps[i].setChildExpression(action); |
| 189 |
} |
| 190 |
|
| 191 |
public Iterable<Operand> actions() { |
| 192 |
return Arrays.asList(actionOps); |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public Iterable<Operand> operands() { |
| 197 |
List<Operand> operanda = new ArrayList<Operand>(size()*2); |
| 198 |
for (int i=0; i<size(); i++) { |
| 199 |
operanda.add(conditionOps[i]); |
| 200 |
operanda.add(actionOps[i]); |
| 201 |
} |
| 202 |
return operanda; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
@Override |
| 214 |
public boolean allowExtractingCommonSubexpressions() { |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public void atomizeActions() { |
| 223 |
for (int i=0; i<size(); i++) { |
| 224 |
setAction(i, Atomizer.makeAtomizer(getAction(i), null)); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public int getInstructionNameCode() { |
| 237 |
return size() == 1 ? StandardNames.XSL_IF : StandardNames.XSL_CHOOSE; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public Expression simplify() throws XPathException { |
| 252 |
for (int i = 0; i < size(); i++) { |
| 253 |
setCondition(i, getCondition(i).simplify()); |
| 254 |
try { |
| 255 |
setAction(i, getAction(i).simplify()); |
| 256 |
} catch (XPathException err) { |
| 257 |
|
| 258 |
if (err.isTypeError()) { |
| 259 |
throw err; |
| 260 |
} else { |
| 261 |
setAction(i, new ErrorExpression(err)); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
return this; |
| 266 |
} |
| 267 |
|
| 268 |
private Expression removeRedundantBranches(ExpressionVisitor visitor) { |
| 269 |
Expression result = removeRedundantBranches0(visitor); |
| 270 |
if (result != this) { |
| 271 |
ExpressionTool.copyLocationInfo(this, result); |
| 272 |
} |
| 273 |
return result; |
| 274 |
} |
| 275 |
|
| 276 |
private Expression removeRedundantBranches0(ExpressionVisitor visitor) { |
| 277 |
|
| 278 |
|
| 279 |
boolean compress = false; |
| 280 |
for (int i = 0; i < size(); i++) { |
| 281 |
Expression condition = getCondition(i); |
| 282 |
if (condition instanceof Literal) { |
| 283 |
compress = true; |
| 284 |
break; |
| 285 |
} |
| 286 |
} |
| 287 |
int size = size(); |
| 288 |
boolean changed = false; |
| 289 |
if (compress) { |
| 290 |
List<Expression> conditions = new ArrayList<Expression>(size); |
| 291 |
List<Expression> actions = new ArrayList<Expression>(size); |
| 292 |
for (int i = 0; i < size; i++) { |
| 293 |
Expression condition = getCondition(i); |
| 294 |
if (!Literal.hasEffectiveBooleanValue(condition, false)) { |
| 295 |
conditions.add(condition); |
| 296 |
actions.add(getAction(i)); |
| 297 |
} |
| 298 |
if (Literal.hasEffectiveBooleanValue(condition, true)) { |
| 299 |
break; |
| 300 |
} |
| 301 |
} |
| 302 |
if (conditions.isEmpty()) { |
| 303 |
Literal lit = Literal.makeEmptySequence(); |
| 304 |
ExpressionTool.copyLocationInfo(this, lit); |
| 305 |
return lit; |
| 306 |
} else if (conditions.size() == 1 && Literal.hasEffectiveBooleanValue(conditions.get(0), true)) { |
| 307 |
return actions.get(0); |
| 308 |
} else if (conditions.size() != size) { |
| 309 |
Expression[] c = conditions.toArray(new Expression[conditions.size()]); |
| 310 |
Expression[] a = actions.toArray(new Expression[actions.size()]); |
| 311 |
Choose result = new Choose(c, a); |
| 312 |
result.setRetainedStaticContext(getRetainedStaticContext()); |
| 313 |
return result; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if (size() == 1 && Literal.hasEffectiveBooleanValue(getCondition(0), true)) { |
| 320 |
return getAction(0); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
if (Literal.isEmptySequence(getAction(size() - 1))) { |
| 326 |
if (size() == 1) { |
| 327 |
Literal lit = Literal.makeEmptySequence(); |
| 328 |
ExpressionTool.copyLocationInfo(this, lit); |
| 329 |
return lit; |
| 330 |
} else { |
| 331 |
Expression[] conditions = new Expression[size-1]; |
| 332 |
Expression[] actions = new Expression[size-1]; |
| 333 |
for (int i = 0; i < size-1; i++) { |
| 334 |
conditions[i] = getCondition(i); |
| 335 |
actions[i] = getAction(i); |
| 336 |
} |
| 337 |
return new Choose(conditions, actions); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
if (Literal.hasEffectiveBooleanValue(getCondition(size - 1), true) && |
| 344 |
getAction(size - 1) instanceof Choose) { |
| 345 |
Choose choose2 = (Choose) getAction(size - 1); |
| 346 |
int newLen = size + choose2.size() - 1; |
| 347 |
Expression[] c2 = new Expression[newLen]; |
| 348 |
Expression[] a2 = new Expression[newLen]; |
| 349 |
for (int i=0; i<size-1; i++) { |
| 350 |
c2[i] = getCondition(i); |
| 351 |
a2[i] = getAction(i); |
| 352 |
} |
| 353 |
for (int i=0; i<choose2.size(); i++) { |
| 354 |
c2[i + size - 1] = choose2.getCondition(i); |
| 355 |
a2[i + size - 1] = choose2.getAction(i); |
| 356 |
} |
| 357 |
return new Choose(c2, a2); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
if (size == 2 && |
| 363 |
Literal.isConstantBoolean(getAction(0), true) && |
| 364 |
Literal.isConstantBoolean(getAction(1), false) && |
| 365 |
Literal.hasEffectiveBooleanValue(getCondition(1), true)) { |
| 366 |
TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); |
| 367 |
if (th.isSubType(getCondition(0).getItemType(), BuiltInAtomicType.BOOLEAN) && |
| 368 |
getCondition(0).getCardinality() == StaticProperty.EXACTLY_ONE) { |
| 369 |
return getCondition(0); |
| 370 |
} else { |
| 371 |
return SystemFunction.makeCall("boolean", getRetainedStaticContext(), getCondition(0)); |
| 372 |
} |
| 373 |
} |
| 374 |
return this; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 379 |
TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); |
| 380 |
for (int i = 0; i < size(); i++) { |
| 381 |
conditionOps[i].typeCheck(visitor, contextInfo); |
| 382 |
XPathException err = TypeChecker.ebvError(getCondition(i), th); |
| 383 |
if (err != null) { |
| 384 |
err.setLocator(getCondition(i).getLocation()); |
| 385 |
err.maybeSetFailingExpression(getCondition(i)); |
| 386 |
throw err; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
for (int i = 0; i < size(); i++) { |
| 393 |
try { |
| 394 |
actionOps[i].typeCheck(visitor, contextInfo); |
| 395 |
} catch (XPathException err) { |
| 396 |
err.maybeSetLocation(getLocation()); |
| 397 |
err.maybeSetFailingExpression(getAction(i)); |
| 398 |
|
| 399 |
if (err.isStaticError()) { |
| 400 |
throw err; |
| 401 |
} else if (err.isTypeError()) { |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
if (Literal.isEmptySequence(getAction(i)) || Literal.hasEffectiveBooleanValue(getCondition(i), false)) { |
| 408 |
setAction(i, new ErrorExpression(err)); |
| 409 |
} else { |
| 410 |
throw err; |
| 411 |
} |
| 412 |
} else { |
| 413 |
setAction(i, new ErrorExpression(err)); |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
Expression reduced = removeRedundantBranches(visitor); |
| 418 |
if (reduced != this) { |
| 419 |
return reduced.typeCheck(visitor, contextInfo); |
| 420 |
} |
| 421 |
return reduced; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public boolean implementsStaticTypeCheck() { |
| 432 |
return true; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public Expression staticTypeCheck(SequenceType req, |
| 454 |
boolean backwardsCompatible, |
| 455 |
RoleDiagnostic role, ExpressionVisitor visitor) |
| 456 |
throws XPathException { |
| 457 |
int size = size(); |
| 458 |
TypeChecker tc = getConfiguration().getTypeChecker(backwardsCompatible); |
| 459 |
for (int i = 0; i < size; i++) { |
| 460 |
try { |
| 461 |
setAction(i, tc.staticTypeCheck(getAction(i), req, role, visitor)); |
| 462 |
} catch (XPathException err) { |
| 463 |
if (err.isStaticError()) { |
| 464 |
throw err; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
ErrorExpression ee = new ErrorExpression(err); |
| 471 |
ExpressionTool.copyLocationInfo(getAction(i), ee); |
| 472 |
setAction(i, ee); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
if (!Literal.hasEffectiveBooleanValue(getCondition(size - 1), true) && |
| 478 |
!Cardinality.allowsZero(req.getCardinality())) { |
| 479 |
Expression[] c = new Expression[size + 1]; |
| 480 |
Expression[] a = new Expression[size + 1]; |
| 481 |
for (int i=0; i<size; i++) { |
| 482 |
c[i] = getCondition(i); |
| 483 |
a[i] = getAction(i); |
| 484 |
} |
| 485 |
c[size] = Literal.makeLiteral(BooleanValue.TRUE, this); |
| 486 |
String cond = size == 1 ? "The condition is not" : "None of the conditions is"; |
| 487 |
String message = |
| 488 |
"Conditional expression: " + cond + " satisfied, so an empty sequence is returned, " + |
| 489 |
"but this is not allowed as the " + role.getMessage(); |
| 490 |
ErrorExpression errExp = new ErrorExpression(message, role.getErrorCode(), true); |
| 491 |
ExpressionTool.copyLocationInfo(this, errExp); |
| 492 |
a[size] = errExp; |
| 493 |
return new Choose(c, a); |
| 494 |
} |
| 495 |
return this; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 500 |
int size = size(); |
| 501 |
for (int i = 0; i < size; i++) { |
| 502 |
conditionOps[i].optimize(visitor, contextItemType); |
| 503 |
Expression ebv = BooleanFn.rewriteEffectiveBooleanValue(getCondition(i), visitor, contextItemType); |
| 504 |
if (ebv != null && ebv != getCondition(i)) { |
| 505 |
setCondition(i, ebv); |
| 506 |
} |
| 507 |
if (getCondition(i) instanceof Literal && |
| 508 |
!(((Literal) getCondition(i)).getValue() instanceof BooleanValue)) { |
| 509 |
final boolean b; |
| 510 |
try { |
| 511 |
b = ((Literal) getCondition(i)).getValue().effectiveBooleanValue(); |
| 512 |
} catch (XPathException err) { |
| 513 |
err.setLocation(getLocation()); |
| 514 |
throw err; |
| 515 |
} |
| 516 |
setCondition(i, Literal.makeLiteral(BooleanValue.get(b), this)); |
| 517 |
} |
| 518 |
} |
| 519 |
for (int i = 0; i < size; i++) { |
| 520 |
try { |
| 521 |
actionOps[i].optimize(visitor, contextItemType); |
| 522 |
} catch (XPathException err) { |
| 523 |
|
| 524 |
if (err.isTypeError()) { |
| 525 |
throw err; |
| 526 |
} else { |
| 527 |
ErrorExpression ee = new ErrorExpression(err); |
| 528 |
ExpressionTool.copyLocationInfo(actionOps[i].getChildExpression(), ee); |
| 529 |
setAction(i, ee); |
| 530 |
} |
| 531 |
} |
| 532 |
if (getAction(i) instanceof ErrorExpression && |
| 533 |
((ErrorExpression)getAction(i)).isTypeError() && |
| 534 |
!Literal.isConstantBoolean(getCondition(i), false) && |
| 535 |
!Literal.isConstantBoolean(getCondition(i), true)) { |
| 536 |
|
| 537 |
visitor.issueWarning("Branch " + (i + 1) + " of conditional will fail with a type error if executed. " |
| 538 |
+ ((ErrorExpression) getAction(i)).getMessage(), getAction(i).getLocation()); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
if (size == 0) { |
| 543 |
return Literal.makeEmptySequence(); |
| 544 |
} |
| 545 |
Expression e = removeRedundantBranches(visitor); |
| 546 |
if (e instanceof Choose) { |
| 547 |
return visitor.obtainOptimizer().trySwitch((Choose) e, visitor); |
| 548 |
} else { |
| 549 |
return e; |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
public Expression copy(RebindingMap rebindings) { |
| 562 |
int size = size(); |
| 563 |
Expression[] c2 = new Expression[size]; |
| 564 |
Expression[] a2 = new Expression[size]; |
| 565 |
for (int c = 0; c < size; c++) { |
| 566 |
c2[c] = getCondition(c).copy(rebindings); |
| 567 |
a2[c] = getAction(c).copy(rebindings); |
| 568 |
} |
| 569 |
Choose ch2 = new Choose(c2, a2); |
| 570 |
ExpressionTool.copyLocationInfo(this, ch2); |
| 571 |
ch2.setInstruction(isInstruction()); |
| 572 |
return ch2; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
public void checkForUpdatingSubexpressions() throws XPathException { |
| 585 |
for (Operand o : conditions()) { |
| 586 |
Expression condition = o.getChildExpression(); |
| 587 |
condition.checkForUpdatingSubexpressions(); |
| 588 |
if (condition.isUpdatingExpression()) { |
| 589 |
XPathException err = new XPathException( |
| 590 |
"Updating expression appears in a context where it is not permitted", "XUST0001"); |
| 591 |
err.setLocator(condition.getLocation()); |
| 592 |
throw err; |
| 593 |
} |
| 594 |
} |
| 595 |
boolean updating = false; |
| 596 |
boolean nonUpdating = false; |
| 597 |
for (Operand o : actions()) { |
| 598 |
Expression act = o.getChildExpression(); |
| 599 |
act.checkForUpdatingSubexpressions(); |
| 600 |
if (ExpressionTool.isNotAllowedInUpdatingContext(act)) { |
| 601 |
if (updating) { |
| 602 |
XPathException err = new XPathException( |
| 603 |
"If any branch of a conditional is an updating expression, then all must be updating expressions (or vacuous)", |
| 604 |
"XUST0001"); |
| 605 |
err.setLocator(act.getLocation()); |
| 606 |
throw err; |
| 607 |
} |
| 608 |
nonUpdating = true; |
| 609 |
} |
| 610 |
if (act.isUpdatingExpression()) { |
| 611 |
if (nonUpdating) { |
| 612 |
XPathException err = new XPathException( |
| 613 |
"If any branch of a conditional is an updating expression, then all must be updating expressions (or vacuous)", |
| 614 |
"XUST0001"); |
| 615 |
err.setLocator(act.getLocation()); |
| 616 |
throw err; |
| 617 |
} |
| 618 |
updating = true; |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
public boolean isUpdatingExpression() { |
| 630 |
for (Operand o : actions()) { |
| 631 |
if (o.getChildExpression().isUpdatingExpression()) { |
| 632 |
return true; |
| 633 |
} |
| 634 |
} |
| 635 |
return false; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
public boolean isVacuousExpression() { |
| 645 |
|
| 646 |
for (Operand action : actions()) { |
| 647 |
if (!action.getChildExpression().isVacuousExpression()) { |
| 648 |
return false; |
| 649 |
} |
| 650 |
} |
| 651 |
return true; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
public int getImplementationMethod() { |
| 660 |
int m = Expression.PROCESS_METHOD | Expression.ITERATE_METHOD | Expression.WATCH_METHOD; |
| 661 |
if (!Cardinality.allowsMany(getCardinality())) { |
| 662 |
m |= Expression.EVALUATE_METHOD; |
| 663 |
} |
| 664 |
return m; |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
public int markTailFunctionCalls(StructuredQName qName, int arity) { |
| 675 |
int result = UserFunctionCall.NOT_TAIL_CALL; |
| 676 |
for (Operand action : actions()) { |
| 677 |
result = Math.max(result, action.getChildExpression().markTailFunctionCalls(qName, arity)); |
| 678 |
} |
| 679 |
return result; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
public ItemType getItemType() { |
| 690 |
TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 691 |
ItemType type = getAction(0).getItemType(); |
| 692 |
for (int i = 1; i < size(); i++) { |
| 693 |
type = Type.getCommonSuperType(type, getAction(i).getItemType(), th); |
| 694 |
} |
| 695 |
return type; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
@Override |
| 706 |
public UType getStaticUType(UType contextItemType) { |
| 707 |
if (isInstruction()) { |
| 708 |
return super.getStaticUType(contextItemType); |
| 709 |
} else { |
| 710 |
UType type = getAction(0).getStaticUType(contextItemType); |
| 711 |
for (int i = 1; i < size(); i++) { |
| 712 |
type = type.union(getAction(i).getStaticUType(contextItemType)); |
| 713 |
} |
| 714 |
return type; |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
public int computeCardinality() { |
| 725 |
int card = 0; |
| 726 |
boolean includesTrue = false; |
| 727 |
for (int i = 0; i < size(); i++) { |
| 728 |
card = Cardinality.union(card, getAction(i).getCardinality()); |
| 729 |
if (Literal.hasEffectiveBooleanValue(getCondition(i), true)) { |
| 730 |
includesTrue = true; |
| 731 |
} |
| 732 |
} |
| 733 |
if (!includesTrue) { |
| 734 |
|
| 735 |
card = Cardinality.union(card, StaticProperty.ALLOWS_ZERO); |
| 736 |
} |
| 737 |
return card; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
public int computeSpecialProperties() { |
| 749 |
|
| 750 |
int props = getAction(0).getSpecialProperties(); |
| 751 |
for (int i = 1; i < size(); i++) { |
| 752 |
props &= getAction(i).getSpecialProperties(); |
| 753 |
} |
| 754 |
return props; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
public final boolean mayCreateNewNodes() { |
| 764 |
for (Operand action : actions()) { |
| 765 |
int props = action.getChildExpression().getSpecialProperties(); |
| 766 |
if ((props & StaticProperty.NO_NODES_NEWLY_CREATED) == 0) { |
| 767 |
return true; |
| 768 |
} |
| 769 |
} |
| 770 |
return false; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
@Override |
| 782 |
public Expression unordered(boolean retainAllNodes, boolean forStreaming) throws XPathException { |
| 783 |
for (int i = 0; i < size(); i++) { |
| 784 |
setAction(i, getAction(i).unordered(retainAllNodes, forStreaming)); |
| 785 |
} |
| 786 |
return this; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
public void checkPermittedContents(SchemaType parentType, boolean whole) throws XPathException { |
| 798 |
for (Operand action : actions()) { |
| 799 |
action.getChildExpression().checkPermittedContents(parentType, whole); |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 825 |
|
| 826 |
for (Operand condition : conditions()) { |
| 827 |
condition.getChildExpression().addToPathMap(pathMap, pathMapNodeSet); |
| 828 |
} |
| 829 |
PathMap.PathMapNodeSet result = new PathMap.PathMapNodeSet(); |
| 830 |
for (Operand action : actions()) { |
| 831 |
PathMap.PathMapNodeSet temp = action.getChildExpression().addToPathMap(pathMap, pathMapNodeSet); |
| 832 |
result.addNodeSet(temp); |
| 833 |
} |
| 834 |
return result; |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
public String toString() { |
| 846 |
FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C64); |
| 847 |
sb.append("if ("); |
| 848 |
for (int i = 0; i < size(); i++) { |
| 849 |
sb.append(getCondition(i).toString()); |
| 850 |
sb.append(") then ("); |
| 851 |
sb.append(getAction(i).toString()); |
| 852 |
if (i == size() - 1) { |
| 853 |
sb.append(")"); |
| 854 |
} else { |
| 855 |
sb.append(") else if ("); |
| 856 |
} |
| 857 |
} |
| 858 |
return sb.toString(); |
| 859 |
} |
| 860 |
|
| 861 |
@Override |
| 862 |
public String toShortString() { |
| 863 |
return "if(" + getCondition(0).toShortString() + ") then ... else ..."; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
public void export(ExpressionPresenter out) throws XPathException { |
| 872 |
out.startElement("choose", this); |
| 873 |
for (int i = 0; i < size(); i++) { |
| 874 |
getCondition(i).export(out); |
| 875 |
getAction(i).export(out); |
| 876 |
} |
| 877 |
out.endElement(); |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
public TailCall processLeavingTail(XPathContext context) throws XPathException { |
| 892 |
int i = choose(context); |
| 893 |
if (i >= 0) { |
| 894 |
Expression action = getAction(i); |
| 895 |
if (action instanceof TailCallReturner) { |
| 896 |
return ((TailCallReturner) action).processLeavingTail(context); |
| 897 |
} else { |
| 898 |
action.process(context); |
| 899 |
return null; |
| 900 |
} |
| 901 |
} |
| 902 |
return null; |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
private int choose(XPathContext context) throws XPathException { |
| 914 |
int size = size(); |
| 915 |
for (int i = 0; i < size; i++) { |
| 916 |
final boolean b; |
| 917 |
try { |
| 918 |
b = getCondition(i).effectiveBooleanValue(context); |
| 919 |
} catch (XPathException e) { |
| 920 |
e.maybeSetFailingExpression(getCondition(i)); |
| 921 |
throw e; |
| 922 |
} |
| 923 |
if (b) { |
| 924 |
return i; |
| 925 |
} |
| 926 |
} |
| 927 |
return -1; |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
public Item evaluateItem(XPathContext context) throws XPathException { |
| 946 |
int i = choose(context); |
| 947 |
return i < 0 ? null : getAction(i).evaluateItem(context); |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 968 |
int i = choose(context); |
| 969 |
return i < 0 ? EmptyIterator.emptyIterator() : getAction(i).iterate(context); |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
public void evaluatePendingUpdates(XPathContext context, PendingUpdateList pul) throws XPathException { |
| 983 |
int i = choose(context); |
| 984 |
if (i >= 0) { |
| 985 |
getAction(i).evaluatePendingUpdates(context, pul); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
@Override |
| 997 |
public String getExpressionName() { |
| 998 |
return "choose"; |
| 999 |
} |
| 1000 |
|
| 1001 |
@Override |
| 1002 |
public String getStreamerName() { |
| 1003 |
return "Choose"; |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|