| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Controller; |
| 12 |
import net.sf.saxon.PreparedStylesheet; |
| 13 |
import net.sf.saxon.expr.*; |
| 14 |
import net.sf.saxon.expr.parser.*; |
| 15 |
import net.sf.saxon.lib.NamespaceConstant; |
| 16 |
import net.sf.saxon.om.GroundedValue; |
| 17 |
import net.sf.saxon.om.SequenceTool; |
| 18 |
import net.sf.saxon.om.StandardNames; |
| 19 |
import net.sf.saxon.om.StructuredQName; |
| 20 |
import net.sf.saxon.query.XQueryFunction; |
| 21 |
import net.sf.saxon.query.XQueryFunctionLibrary; |
| 22 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 23 |
import net.sf.saxon.trace.InstructionInfo; |
| 24 |
import net.sf.saxon.trace.LocationKind; |
| 25 |
import net.sf.saxon.trans.SymbolicName; |
| 26 |
import net.sf.saxon.trans.Visibility; |
| 27 |
import net.sf.saxon.trans.XPathException; |
| 28 |
import net.sf.saxon.tree.iter.ManualIterator; |
| 29 |
import net.sf.saxon.type.AnyItemType; |
| 30 |
import net.sf.saxon.type.ItemType; |
| 31 |
import net.sf.saxon.type.TypeHierarchy; |
| 32 |
import net.sf.saxon.value.Cardinality; |
| 33 |
import net.sf.saxon.value.IntegerValue; |
| 34 |
import net.sf.saxon.value.SequenceType; |
| 35 |
|
| 36 |
import java.util.*; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class GlobalVariable extends Actor |
| 43 |
implements Binding, net.sf.saxon.query.Declaration, InstructionInfo, ContextOriginator { |
| 44 |
|
| 45 |
protected List<BindingReference> references = new ArrayList<>(10); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private Expression select; |
| 52 |
private StructuredQName variableQName; |
| 53 |
private SequenceType requiredType; |
| 54 |
|
| 55 |
private boolean indexed; |
| 56 |
private boolean isPrivate = false; |
| 57 |
private boolean isAssignable = false; |
| 58 |
private GlobalVariable originalVariable; |
| 59 |
private int binderySlotNumber; |
| 60 |
private boolean isRequiredParam; |
| 61 |
private boolean isStatic; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public GlobalVariable() { |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public void init(Expression select, StructuredQName qName) { |
| 78 |
this.select = select; |
| 79 |
variableQName = qName; |
| 80 |
setBody(select); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@Override |
| 89 |
public SymbolicName getSymbolicName() { |
| 90 |
return new SymbolicName(StandardNames.XSL_VARIABLE, variableQName); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public void setSelectExpression(Expression select) { |
| 100 |
this.select = select; |
| 101 |
setBody(select); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public Expression getSelectExpression() { |
| 111 |
return select; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public void setStatic(boolean declaredStatic) { |
| 120 |
isStatic = declaredStatic; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public boolean isStatic() { |
| 129 |
return this.isStatic; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public void setRequiredType(SequenceType required) { |
| 139 |
requiredType = required; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public SequenceType getRequiredType() { |
| 149 |
return requiredType; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
private Configuration getConfiguration() { |
| 158 |
return getPackageData().getConfiguration(); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public void setOriginalVariable(GlobalVariable var) { |
| 170 |
originalVariable = var; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public GlobalVariable getOriginalVariable() { |
| 180 |
return originalVariable; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public GlobalVariable getUltimateOriginalVariable() { |
| 191 |
if (originalVariable == null) { |
| 192 |
return this; |
| 193 |
} else { |
| 194 |
return originalVariable.getUltimateOriginalVariable(); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public void setUnused(boolean unused) { |
| 209 |
this.binderySlotNumber = -9234; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public boolean isUnused() { |
| 223 |
return this.binderySlotNumber == -9234; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public boolean isPrivate() { |
| 233 |
return isPrivate; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public void setPrivate(boolean b) { |
| 242 |
isPrivate = b; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public void setAssignable(boolean assignable) { |
| 252 |
isAssignable = assignable; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public final boolean isAssignable() { |
| 262 |
return isAssignable; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public int getConstructType() { |
| 274 |
return StandardNames.XSL_VARIABLE; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public StructuredQName getObjectName() { |
| 284 |
return getVariableQName(); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public Object getProperty(String name) { |
| 296 |
return null; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public Iterator<String> getProperties() { |
| 308 |
List<String> list = Collections.emptyList(); |
| 309 |
return list.iterator(); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
public int getHostLanguage() { |
| 319 |
return getPackageData().getHostLanguage(); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public void setIndexedVariable() { |
| 327 |
indexed = true; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public boolean isIndexedVariable() { |
| 337 |
return indexed; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public void setContainsLocals(SlotManager map) { |
| 349 |
setStackFrameMap(map); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public boolean isGlobal() { |
| 359 |
return true; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public void registerReference(BindingReference ref) { |
| 368 |
references.add(ref); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public Iterator iterateReferences() { |
| 378 |
return references.iterator(); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public int getBinderySlotNumber() { |
| 388 |
return binderySlotNumber; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public void setBinderySlotNumber(int s) { |
| 398 |
if (!isUnused()) { |
| 399 |
binderySlotNumber = s; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public void setRequiredParam(boolean requiredParam) { |
| 410 |
this.isRequiredParam = requiredParam; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
public boolean isRequiredParam() { |
| 420 |
return this.isRequiredParam; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public void compile(Executable exec, int slot) throws XPathException { |
| 432 |
TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 433 |
|
| 434 |
setBinderySlotNumber(slot); |
| 435 |
if (this instanceof GlobalParam) { |
| 436 |
setRequiredParam(select == null); |
| 437 |
} |
| 438 |
SequenceType type = getRequiredType(); |
| 439 |
for (BindingReference ref : references) { |
| 440 |
ref.fixup(this); |
| 441 |
GroundedValue<?> constantValue = null; |
| 442 |
int properties = 0; |
| 443 |
Expression select = getSelectExpression(); |
| 444 |
if (select instanceof Literal && !(this instanceof GlobalParam)) { |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
int relation = th.relationship(select.getItemType(), type.getPrimaryType()); |
| 449 |
if (relation == TypeHierarchy.SAME_TYPE || relation == TypeHierarchy.SUBSUMED_BY) { |
| 450 |
constantValue = ((Literal) select).getValue(); |
| 451 |
type = SequenceType.makeSequenceType(SequenceTool.getItemType(constantValue, th), SequenceTool.getCardinality(constantValue)); |
| 452 |
} |
| 453 |
} |
| 454 |
if (select != null) { |
| 455 |
properties = select.getSpecialProperties(); |
| 456 |
} |
| 457 |
properties |= StaticProperty.NO_NODES_NEWLY_CREATED; |
| 458 |
|
| 459 |
ref.setStaticType(type, constantValue, properties); |
| 460 |
} |
| 461 |
|
| 462 |
if (isRequiredParam()) { |
| 463 |
exec.registerGlobalParameter((GlobalParam) this); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
public void typeCheck( ExpressionVisitor visitor) throws XPathException { |
| 476 |
Expression value = getSelectExpression(); |
| 477 |
if (value != null) { |
| 478 |
value.checkForUpdatingSubexpressions(); |
| 479 |
if (value.isUpdatingExpression()) { |
| 480 |
throw new XPathException( |
| 481 |
"Initializing expression for global variable must not be an updating expression", "XUST0001"); |
| 482 |
} |
| 483 |
RoleDiagnostic role = new RoleDiagnostic( |
| 484 |
RoleDiagnostic.VARIABLE, getVariableQName().getDisplayName(), 0); |
| 485 |
ContextItemStaticInfo cit = getConfiguration().makeContextItemStaticInfo(AnyItemType.getInstance(), true); |
| 486 |
Expression value2 = TypeChecker.strictTypeCheck( |
| 487 |
value.simplify().typeCheck(visitor, cit), |
| 488 |
getRequiredType(), role, visitor.getStaticContext()); |
| 489 |
value2 = value2.optimize(visitor, cit); |
| 490 |
setSelectExpression(value2); |
| 491 |
|
| 492 |
SlotManager map = getConfiguration().makeSlotManager(); |
| 493 |
int slots = ExpressionTool.allocateSlots(value2, 0, map); |
| 494 |
if (slots > 0) { |
| 495 |
setContainsLocals(map); |
| 496 |
} |
| 497 |
|
| 498 |
if (getRequiredType() == SequenceType.ANY_SEQUENCE && !(this instanceof GlobalParam)) { |
| 499 |
|
| 500 |
try { |
| 501 |
final ItemType itemType = value.getItemType(); |
| 502 |
final int cardinality = value.getCardinality(); |
| 503 |
setRequiredType(SequenceType.makeSequenceType(itemType, cardinality)); |
| 504 |
GroundedValue<?> constantValue = null; |
| 505 |
if (value2 instanceof Literal) { |
| 506 |
constantValue = ((Literal) value2).getValue(); |
| 507 |
} |
| 508 |
for (BindingReference reference : references) { |
| 509 |
if (reference instanceof VariableReference) { |
| 510 |
((VariableReference) reference).refineVariableType( |
| 511 |
itemType, cardinality, constantValue, value.getSpecialProperties()); |
| 512 |
} |
| 513 |
} |
| 514 |
} catch (Exception err) { |
| 515 |
|
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
public void lookForCycles(Stack<Object> referees, XQueryFunctionLibrary globalFunctionLibrary) throws XPathException { |
| 537 |
if (referees.contains(this)) { |
| 538 |
int s = referees.indexOf(this); |
| 539 |
referees.push(this); |
| 540 |
|
| 541 |
StringBuilder messageBuilder = new StringBuilder("Circular definition of global variable: $" + getVariableQName().getDisplayName()); |
| 542 |
for (int i = s; i < referees.size() - 1; i++) { |
| 543 |
if (i != s) { |
| 544 |
messageBuilder.append(", which"); |
| 545 |
} |
| 546 |
if (referees.get(i + 1) instanceof GlobalVariable) { |
| 547 |
GlobalVariable next = (GlobalVariable) referees.get(i + 1); |
| 548 |
messageBuilder.append(" uses $").append(next.getVariableQName().getDisplayName()); |
| 549 |
} else if (referees.get(i + 1) instanceof XQueryFunction) { |
| 550 |
XQueryFunction next = (XQueryFunction) referees.get(i + 1); |
| 551 |
messageBuilder.append(" calls ").append(next.getFunctionName().getDisplayName()).append("#").append(next.getNumberOfArguments()).append("()"); |
| 552 |
} |
| 553 |
} |
| 554 |
String message = messageBuilder.toString(); |
| 555 |
message += '.'; |
| 556 |
XPathException err = new XPathException(message); |
| 557 |
String errorCode; |
| 558 |
if (getPackageData().getHostLanguage() == Configuration.XSLT) { |
| 559 |
errorCode = "XTDE0640"; |
| 560 |
} else if (s == 0 && referees.size() == 2) { |
| 561 |
|
| 562 |
errorCode = "XPST0008"; |
| 563 |
} else { |
| 564 |
errorCode = "XQDY0054"; |
| 565 |
} |
| 566 |
err.setErrorCode(errorCode); |
| 567 |
err.setIsStaticError(true); |
| 568 |
err.setLocation(getLocation()); |
| 569 |
throw err; |
| 570 |
} |
| 571 |
if (select != null) { |
| 572 |
referees.push(this); |
| 573 |
List<Binding> list = new ArrayList<>(10); |
| 574 |
ExpressionTool.gatherReferencedVariables(select, list); |
| 575 |
for (Binding b : list) { |
| 576 |
if (b instanceof GlobalVariable) { |
| 577 |
((GlobalVariable) b).lookForCycles(referees, globalFunctionLibrary); |
| 578 |
} |
| 579 |
} |
| 580 |
List<SymbolicName> flist = new ArrayList<>(); |
| 581 |
ExpressionTool.gatherCalledFunctionNames(select, flist); |
| 582 |
for (SymbolicName s : flist) { |
| 583 |
XQueryFunction f = globalFunctionLibrary.getDeclarationByKey(s); |
| 584 |
if (!referees.contains(f)) { |
| 585 |
|
| 586 |
lookForFunctionCycles(f, referees, globalFunctionLibrary); |
| 587 |
} |
| 588 |
} |
| 589 |
referees.pop(); |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
private static void lookForFunctionCycles( |
| 603 |
XQueryFunction f, Stack<Object> referees, XQueryFunctionLibrary globalFunctionLibrary) throws XPathException { |
| 604 |
Expression body = f.getBody(); |
| 605 |
referees.push(f); |
| 606 |
List<Binding> list = new ArrayList<>(10); |
| 607 |
ExpressionTool.gatherReferencedVariables(body, list); |
| 608 |
for (Binding b : list) { |
| 609 |
if (b instanceof GlobalVariable) { |
| 610 |
((GlobalVariable) b).lookForCycles(referees, globalFunctionLibrary); |
| 611 |
} |
| 612 |
} |
| 613 |
List<SymbolicName> flist = new ArrayList<>(); |
| 614 |
ExpressionTool.gatherCalledFunctionNames(body, flist); |
| 615 |
for (SymbolicName s : flist) { |
| 616 |
XQueryFunction qf = globalFunctionLibrary.getDeclarationByKey(s); |
| 617 |
if (!referees.contains(qf)) { |
| 618 |
|
| 619 |
lookForFunctionCycles(qf, referees, globalFunctionLibrary); |
| 620 |
} |
| 621 |
} |
| 622 |
referees.pop(); |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
public GroundedValue<?> getSelectValue(XPathContext context, Component target) throws XPathException { |
| 636 |
if (select == null) { |
| 637 |
throw new AssertionError("*** No select expression for global variable $" + |
| 638 |
getVariableQName().getDisplayName() + "!!"); |
| 639 |
} else if (select instanceof Literal) { |
| 640 |
|
| 641 |
return ((Literal)select).getValue(); |
| 642 |
} else { |
| 643 |
try { |
| 644 |
Controller controller = context.getController(); |
| 645 |
Executable exec = controller.getExecutable(); |
| 646 |
boolean hasAccessToGlobalContext = true; |
| 647 |
if (exec instanceof PreparedStylesheet) { |
| 648 |
hasAccessToGlobalContext = target == null || target.getDeclaringPackage() == ((PreparedStylesheet) exec).getTopLevelPackage(); |
| 649 |
} |
| 650 |
XPathContextMajor c2 = context.newCleanContext(); |
| 651 |
c2.setOrigin(this); |
| 652 |
if (hasAccessToGlobalContext) { |
| 653 |
ManualIterator<?> mi = |
| 654 |
new ManualIterator<>(context.getController().getGlobalContextItem()); |
| 655 |
c2.setCurrentIterator(mi); |
| 656 |
} else { |
| 657 |
c2.setCurrentIterator(null); |
| 658 |
} |
| 659 |
if (getStackFrameMap() != null) { |
| 660 |
c2.openStackFrame(getStackFrameMap()); |
| 661 |
} |
| 662 |
c2.setCurrentComponent(target); |
| 663 |
int savedOutputState = c2.getTemporaryOutputState(); |
| 664 |
c2.setTemporaryOutputState(StandardNames.XSL_VARIABLE); |
| 665 |
c2.setCurrentOutputUri(null); |
| 666 |
GroundedValue<?> result; |
| 667 |
if (indexed) { |
| 668 |
result = c2.getConfiguration().makeSequenceExtent(select, FilterExpression.FILTERED, c2); |
| 669 |
} else { |
| 670 |
result = select.iterate(c2).materialize(); |
| 671 |
} |
| 672 |
c2.setTemporaryOutputState(savedOutputState); |
| 673 |
return result; |
| 674 |
} catch (XPathException e) { |
| 675 |
if (!getVariableQName().hasURI(NamespaceConstant.SAXON_GENERATED_VARIABLE)) { |
| 676 |
e.setIsGlobalError(true); |
| 677 |
} |
| 678 |
throw e; |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
public GroundedValue<?> evaluateVariable(XPathContext context) throws XPathException { |
| 688 |
final Controller controller = context.getController(); |
| 689 |
assert controller != null; |
| 690 |
final Bindery b = controller.getBindery(getPackageData()); |
| 691 |
|
| 692 |
final GroundedValue<?> v = b.getGlobalVariable(getBinderySlotNumber()); |
| 693 |
|
| 694 |
if (v != null) { |
| 695 |
return v; |
| 696 |
} else { |
| 697 |
return actuallyEvaluate(context, null); |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
public GroundedValue evaluateVariable(XPathContext context, Component target) throws XPathException { |
| 706 |
final Controller controller = context.getController(); |
| 707 |
assert controller != null; |
| 708 |
Bindery b = controller.getBindery(getPackageData()); |
| 709 |
if (b == null) { |
| 710 |
|
| 711 |
throw new AssertionError(); |
| 712 |
|
| 713 |
} |
| 714 |
final GroundedValue v = b.getGlobalVariable(getBinderySlotNumber()); |
| 715 |
|
| 716 |
if (v != null) { |
| 717 |
if (v instanceof Bindery.FailureValue) { |
| 718 |
throw ((Bindery.FailureValue)v).getObject(); |
| 719 |
} |
| 720 |
return v; |
| 721 |
} else { |
| 722 |
return actuallyEvaluate(context, target); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
protected GroundedValue<?> actuallyEvaluate(XPathContext context, Component target) throws XPathException { |
| 736 |
final Controller controller = context.getController(); |
| 737 |
assert controller != null; |
| 738 |
final Bindery b = controller.getBindery(getPackageData()); |
| 739 |
|
| 740 |
try { |
| 741 |
|
| 742 |
|
| 743 |
setDependencies(this, context); |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
boolean go = b.setExecuting(this); |
| 748 |
if (!go) { |
| 749 |
|
| 750 |
return b.getGlobalVariable(getBinderySlotNumber()); |
| 751 |
} |
| 752 |
|
| 753 |
GroundedValue<?> value = getSelectValue(context, target); |
| 754 |
if (indexed) { |
| 755 |
value = controller.getConfiguration().obtainOptimizer().makeIndexedValue(value.iterate()); |
| 756 |
} |
| 757 |
return b.saveGlobalVariableValue(this, value); |
| 758 |
|
| 759 |
} catch (XPathException err) { |
| 760 |
b.setNotExecuting(this); |
| 761 |
if (err instanceof XPathException.Circularity) { |
| 762 |
String errorCode; |
| 763 |
if (getPackageData().getHostLanguage() == Configuration.XSLT) { |
| 764 |
errorCode = "XTDE0640"; |
| 765 |
} else { |
| 766 |
errorCode = "XQDY0054"; |
| 767 |
} |
| 768 |
err.setErrorCode(errorCode); |
| 769 |
err.setXPathContext(context); |
| 770 |
err.setIsGlobalError(true); |
| 771 |
|
| 772 |
b.setGlobalVariable(this, new Bindery.FailureValue(err)); |
| 773 |
err.setLocation(getLocation()); |
| 774 |
throw err; |
| 775 |
} else { |
| 776 |
throw err; |
| 777 |
} |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
protected static void setDependencies(GlobalVariable var, XPathContext context) throws XPathException { |
| 793 |
Controller controller = context.getController(); |
| 794 |
if (!(context instanceof XPathContextMajor)) { |
| 795 |
context = getMajorCaller(context); |
| 796 |
} |
| 797 |
while (context != null) { |
| 798 |
do { |
| 799 |
ContextOriginator origin = ((XPathContextMajor) context).getOrigin(); |
| 800 |
if (origin instanceof GlobalVariable) { |
| 801 |
controller.registerGlobalVariableDependency((GlobalVariable) origin, var); |
| 802 |
return; |
| 803 |
} |
| 804 |
context = getMajorCaller(context); |
| 805 |
} while (context != null); |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
private static XPathContextMajor getMajorCaller(XPathContext context) { |
| 810 |
XPathContext caller = context.getCaller(); |
| 811 |
while (!(caller == null || caller instanceof XPathContextMajor)) { |
| 812 |
caller = caller.getCaller(); |
| 813 |
} |
| 814 |
return (XPathContextMajor) caller; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
public IntegerValue[] getIntegerBoundsForVariable() { |
| 825 |
return select==null ? null : select.getIntegerBounds(); |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
public int getLocalSlotNumber() { |
| 835 |
return 0; |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
public void setVariableQName(StructuredQName s) { |
| 845 |
variableQName = s; |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
public StructuredQName getVariableQName() { |
| 854 |
return variableQName; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
public void addReference(VariableReference ref, boolean isLoopingReference) { |
| 865 |
|
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
@Override |
| 873 |
public int getComponentKind() { |
| 874 |
return StandardNames.XSL_VARIABLE; |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
@Override |
| 884 |
public void export(ExpressionPresenter presenter) throws XPathException { |
| 885 |
boolean asParam = this instanceof GlobalParam && !isStatic(); |
| 886 |
presenter.startElement(asParam ? "globalParam" : "globalVariable"); |
| 887 |
presenter.emitAttribute("name", getVariableQName()); |
| 888 |
presenter.emitAttribute("type", getRequiredType().toExportString()); |
| 889 |
ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) presenter.getOptions(); |
| 890 |
if (options.target != null && options.target.equals("JS") && options.targetVersion == 2) { |
| 891 |
presenter.emitAttribute("typeJ", getRequiredType().toExportString2()); |
| 892 |
} |
| 893 |
presenter.emitAttribute("line", getLineNumber() + ""); |
| 894 |
presenter.emitAttribute("module", getSystemId()); |
| 895 |
if (getStackFrameMap() != null) { |
| 896 |
presenter.emitAttribute("slots", getStackFrameMap().getNumberOfVariables() + ""); |
| 897 |
} |
| 898 |
if (getDeclaringComponent() != null) { |
| 899 |
Visibility vis = getDeclaringComponent().getVisibility(); |
| 900 |
if (vis != null) { |
| 901 |
presenter.emitAttribute("visibility", vis.toString()); |
| 902 |
} |
| 903 |
} |
| 904 |
String flags = getFlags(); |
| 905 |
if (!flags.isEmpty()) { |
| 906 |
presenter.emitAttribute("flags", flags); |
| 907 |
} |
| 908 |
|
| 909 |
if ("JS".equals(((ExpressionPresenter.ExportOptions) presenter.getOptions()).target)) { |
| 910 |
int targetVersion = ((ExpressionPresenter.ExportOptions) presenter.getOptions()).targetVersion; |
| 911 |
presenter.emitAttribute("jsAcceptor", getRequiredType().getPrimaryType().generateJavaScriptItemTypeAcceptor("XTTE0590", targetVersion)); |
| 912 |
presenter.emitAttribute("jsCardCheck", Cardinality.generateJavaScriptChecker(getRequiredType().getCardinality())); |
| 913 |
} |
| 914 |
|
| 915 |
if (getSelectExpression() != null) { |
| 916 |
getSelectExpression().export(presenter); |
| 917 |
} |
| 918 |
presenter.endElement(); |
| 919 |
} |
| 920 |
|
| 921 |
protected String getFlags() { |
| 922 |
String flags = ""; |
| 923 |
if (isAssignable) { |
| 924 |
flags += "a"; |
| 925 |
} |
| 926 |
if (indexed) { |
| 927 |
flags += "x"; |
| 928 |
} |
| 929 |
if (isRequiredParam) { |
| 930 |
flags += "r"; |
| 931 |
} |
| 932 |
return flags; |
| 933 |
} |
| 934 |
} |
| 935 |
|