| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.PreparedStylesheet; |
| 12 |
import net.sf.saxon.expr.Component; |
| 13 |
import net.sf.saxon.expr.ComponentBinding; |
| 14 |
import net.sf.saxon.expr.PackageData; |
| 15 |
import net.sf.saxon.expr.instruct.*; |
| 16 |
import net.sf.saxon.expr.parser.OptimizerOptions; |
| 17 |
import net.sf.saxon.functions.ExecutableFunctionLibrary; |
| 18 |
import net.sf.saxon.functions.FunctionLibrary; |
| 19 |
import net.sf.saxon.functions.FunctionLibraryList; |
| 20 |
import net.sf.saxon.functions.registry.ConstructorFunctionLibrary; |
| 21 |
import net.sf.saxon.om.Action; |
| 22 |
import net.sf.saxon.om.SpaceStrippingRule; |
| 23 |
import net.sf.saxon.om.StructuredQName; |
| 24 |
import net.sf.saxon.pattern.NodeTest; |
| 25 |
import net.sf.saxon.query.XQueryFunctionLibrary; |
| 26 |
import net.sf.saxon.serialize.CharacterMap; |
| 27 |
import net.sf.saxon.serialize.CharacterMapIndex; |
| 28 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 29 |
import net.sf.saxon.trans.*; |
| 30 |
import net.sf.saxon.trans.rules.RuleManager; |
| 31 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 32 |
import net.sf.saxon.type.TypeHierarchy; |
| 33 |
|
| 34 |
import java.util.*; |
| 35 |
|
| 36 |
import static net.sf.saxon.trans.Visibility.PRIVATE; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class StylesheetPackage extends PackageData { |
| 43 |
|
| 44 |
private final static boolean TRACING = false; |
| 45 |
|
| 46 |
private PackageVersion packageVersion = null; |
| 47 |
private String packageName; |
| 48 |
private List<StylesheetPackage> usedPackages = new ArrayList<>(); |
| 49 |
private int xsltVersion; |
| 50 |
|
| 51 |
private RuleManager ruleManager; |
| 52 |
private CharacterMapIndex characterMapIndex; |
| 53 |
|
| 54 |
private boolean createsSecondaryResultDocuments; |
| 55 |
|
| 56 |
private List<Action> completionActions = new ArrayList<>(); |
| 57 |
protected GlobalContextRequirement globalContextRequirement = null; |
| 58 |
private boolean containsGlobalContextItemDeclaration = false; |
| 59 |
protected SpaceStrippingRule stripperRules; |
| 60 |
private boolean stripsWhitespace = false; |
| 61 |
private boolean stripsTypeAnnotations = false; |
| 62 |
protected Properties defaultOutputProperties; |
| 63 |
private StructuredQName defaultMode; |
| 64 |
private boolean declaredModes; |
| 65 |
protected Map<StructuredQName, Properties> namedOutputProperties = new HashMap<>(4); |
| 66 |
private boolean implicitPackage; |
| 67 |
|
| 68 |
|
| 69 |
protected Set<String> schemaIndex = new HashSet<>(10); |
| 70 |
|
| 71 |
private FunctionLibraryList functionLibrary; |
| 72 |
private XQueryFunctionLibrary queryFunctions; |
| 73 |
private ExecutableFunctionLibrary overriding; |
| 74 |
private ExecutableFunctionLibrary underriding; |
| 75 |
private int maxFunctionArity = -1; |
| 76 |
private boolean retainUnusedFunctions = false; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
private HashMap<SymbolicName, Component> componentIndex = new HashMap<>(20); |
| 84 |
|
| 85 |
protected List<Component> hiddenComponents = new ArrayList<>(); |
| 86 |
|
| 87 |
protected HashMap<SymbolicName, Component> overriddenComponents = new HashMap<>(); |
| 88 |
|
| 89 |
private HashMap<SymbolicName, Component> abstractComponents = new HashMap<>(); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public StylesheetPackage(Configuration config) { |
| 98 |
super(config); |
| 99 |
setHostLanguage(Configuration.XSLT); |
| 100 |
setAccumulatorRegistry(config.makeAccumulatorRegistry()); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public HashMap<SymbolicName, Component> getComponentIndex() { |
| 111 |
return componentIndex; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public Iterable<StylesheetPackage> getUsedPackages() { |
| 121 |
return usedPackages; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public void addUsedPackage(StylesheetPackage pack) { |
| 130 |
usedPackages.add(pack); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public boolean contains(StylesheetPackage pack) { |
| 140 |
for (StylesheetPackage p : usedPackages) { |
| 141 |
if (p == pack || p.contains(pack)) { |
| 142 |
return true; |
| 143 |
} |
| 144 |
} |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public void setVersion(int version) { |
| 154 |
this.xsltVersion = version; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public void setPackageName(String packageName) { |
| 163 |
this.packageName = packageName; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public int getVersion() { |
| 172 |
return xsltVersion; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public PackageVersion getPackageVersion() { |
| 181 |
return packageVersion; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public void setPackageVersion(PackageVersion version) { |
| 190 |
packageVersion = version; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public String getPackageName() { |
| 199 |
return packageName; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public boolean isImplicitPackage() { |
| 207 |
return implicitPackage; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public void setImplicitPackage(boolean implicitPackage) { |
| 216 |
this.implicitPackage = implicitPackage; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public boolean isJustInTimeCompilation() { |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public void setJustInTimeCompilation(boolean justInTimeCompilation) { |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public RuleManager getRuleManager() { |
| 248 |
return ruleManager; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public void setRuleManager(RuleManager ruleManager) { |
| 257 |
this.ruleManager = ruleManager; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public StructuredQName getDefaultMode() { |
| 268 |
return defaultMode; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public void setDefaultMode(StructuredQName defaultMode) { |
| 277 |
this.defaultMode = defaultMode; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public void setDeclaredModes(boolean declared) { |
| 287 |
declaredModes = declared; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public boolean isDeclaredModes() { |
| 297 |
return declaredModes; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public SpaceStrippingRule getSpaceStrippingRule() { |
| 307 |
return stripperRules; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public CharacterMapIndex getCharacterMapIndex() { |
| 316 |
return characterMapIndex; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public void setCharacterMapIndex(CharacterMapIndex characterMapIndex) { |
| 325 |
this.characterMapIndex = characterMapIndex; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public boolean isCreatesSecondaryResultDocuments() { |
| 334 |
return createsSecondaryResultDocuments; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public void setCreatesSecondaryResultDocuments(boolean createsSecondaryResultDocuments) { |
| 343 |
this.createsSecondaryResultDocuments = createsSecondaryResultDocuments; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public boolean isStripsTypeAnnotations() { |
| 353 |
return stripsTypeAnnotations; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
public void setStripsTypeAnnotations(boolean stripsTypeAnnotations) { |
| 366 |
this.stripsTypeAnnotations = stripsTypeAnnotations; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public SpaceStrippingRule getStripperRules() { |
| 375 |
return stripperRules; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
public void setStripperRules(SpaceStrippingRule stripperRules) { |
| 384 |
this.stripperRules = stripperRules; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public void setDefaultOutputProperties(Properties props) { |
| 394 |
defaultOutputProperties = props; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public void setNamedOutputProperties(StructuredQName name, Properties props) { |
| 405 |
namedOutputProperties.put(name, props); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public Properties getNamedOutputProperties(StructuredQName name) { |
| 415 |
return namedOutputProperties.get(name); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
public Set<String> getSchemaNamespaces() { |
| 424 |
return schemaIndex; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public void setContextItemRequirements(GlobalContextRequirement requirement) throws XPathException { |
| 433 |
if (containsGlobalContextItemDeclaration) { |
| 434 |
|
| 435 |
if ((!requirement.isAbsentFocus() && globalContextRequirement.isAbsentFocus()) || |
| 436 |
(requirement.isMayBeOmitted() && !globalContextRequirement.isMayBeOmitted())) { |
| 437 |
throw new XPathException( |
| 438 |
"The package contains two xsl:global-context-item declarations with conflicting @use attributes", "XTSE3087"); |
| 439 |
} |
| 440 |
TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 441 |
if (th.relationship(requirement.getRequiredItemType(), globalContextRequirement.getRequiredItemType()) != TypeHierarchy.SAME_TYPE) { |
| 442 |
throw new XPathException("The package contains two xsl:global-context-item declarations with conflicting item types", "XTSE3087"); |
| 443 |
} |
| 444 |
} |
| 445 |
containsGlobalContextItemDeclaration = true; |
| 446 |
globalContextRequirement = requirement; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
public GlobalContextRequirement getContextItemRequirements() { |
| 456 |
return globalContextRequirement; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public void setStripsWhitespace(boolean strips) { |
| 466 |
this.stripsWhitespace = strips; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
public boolean isStripsWhitespace() { |
| 476 |
return this.stripsWhitespace; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public void addCompletionAction(Action action) { |
| 486 |
completionActions.add(action); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
protected void complete() throws XPathException { |
| 495 |
|
| 496 |
for (Action a : completionActions) { |
| 497 |
a.doAction(); |
| 498 |
} |
| 499 |
allocateBinderySlots(); |
| 500 |
|
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
public void allocateBinderySlots() { |
| 508 |
SlotManager slotManager = getConfiguration().makeSlotManager(); |
| 509 |
for (Component c : componentIndex.values()) { |
| 510 |
registerGlobalVariable(c, slotManager); |
| 511 |
} |
| 512 |
for (Component c : hiddenComponents) { |
| 513 |
registerGlobalVariable(c, slotManager); |
| 514 |
} |
| 515 |
setGlobalSlotManager(slotManager); |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
private void registerGlobalVariable(Component c, SlotManager slotManager) { |
| 528 |
if (c.getActor() instanceof GlobalVariable) { |
| 529 |
GlobalVariable var = (GlobalVariable) c.getActor(); |
| 530 |
int slot = slotManager.allocateSlotNumber(var.getVariableQName()); |
| 531 |
var.setPackageData(this); |
| 532 |
var.setBinderySlotNumber(slot); |
| 533 |
if (c.getVisibility() != Visibility.HIDDEN) { |
| 534 |
addGlobalVariable(var); |
| 535 |
} |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
public void addComponent(Component component) { |
| 545 |
SymbolicName name = component.getActor().getSymbolicName(); |
| 546 |
componentIndex.put(name, component); |
| 547 |
if (component.getVisibility() == Visibility.ABSTRACT && component.getContainingPackage() == this) { |
| 548 |
abstractComponents.put(component.getActor().getSymbolicName(), component); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
@Override |
| 553 |
public void addGlobalVariable(GlobalVariable variable) { |
| 554 |
super.addGlobalVariable(variable); |
| 555 |
SymbolicName name = variable.getSymbolicName(); |
| 556 |
if (componentIndex.get(name) == null) { |
| 557 |
Component comp = variable.getDeclaringComponent(); |
| 558 |
if (comp == null) { |
| 559 |
comp = variable.makeDeclaringComponent(PRIVATE, this); |
| 560 |
} |
| 561 |
addComponent(comp); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
public int getMaxFunctionArity() { |
| 571 |
if (maxFunctionArity == -1) { |
| 572 |
for (Component c : componentIndex.values()) { |
| 573 |
if (c.getActor() instanceof UserFunction) { |
| 574 |
if (((UserFunction) c.getActor()).getArity() > maxFunctionArity) { |
| 575 |
maxFunctionArity = ((UserFunction) c.getActor()).getArity(); |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
return maxFunctionArity; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
public Component getComponent(SymbolicName name) { |
| 591 |
return componentIndex.get(name); |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
public void addHiddenComponent(Component component) { |
| 599 |
hiddenComponents.add(component); |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
public Component getOverriddenComponent(SymbolicName name) { |
| 611 |
return overriddenComponents.get(name); |
| 612 |
} |
| 613 |
|
| 614 |
public void addOverriddenComponent(Component comp) { |
| 615 |
overriddenComponents.put(comp.getActor().getSymbolicName(), comp); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
public void addComponentsFromUsedPackage(StylesheetPackage usedPackage, |
| 628 |
List<XSLAccept> acceptors, |
| 629 |
final Set<SymbolicName> overrides) throws XPathException { |
| 630 |
usedPackages.add(usedPackage); |
| 631 |
|
| 632 |
trace("=== Adding components from " + usedPackage.getPackageName() + " to " + getPackageName() + " ==="); |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
final Map<Component, Component> correspondence = new HashMap<>(); |
| 640 |
|
| 641 |
for (Map.Entry<SymbolicName, Component> namedComponentEntry : usedPackage.componentIndex.entrySet()) { |
| 642 |
SymbolicName name = namedComponentEntry.getKey(); |
| 643 |
Component oldC = namedComponentEntry.getValue(); |
| 644 |
|
| 645 |
Visibility oldV = oldC.getVisibility(); |
| 646 |
|
| 647 |
Visibility newV = null; |
| 648 |
|
| 649 |
if (overrides.contains(name) && !(oldC.getActor() instanceof Mode)) { |
| 650 |
newV = Visibility.HIDDEN; |
| 651 |
} else { |
| 652 |
Visibility acceptedVisibility = explicitAcceptedVisibility(name, acceptors); |
| 653 |
if (acceptedVisibility != null) { |
| 654 |
if (!XSLAccept.isCompatible(oldV, acceptedVisibility)) { |
| 655 |
throw new XPathException("Cannot accept a " + oldV.show() + |
| 656 |
" component (" + name + ") from package " + usedPackage.getPackageName() |
| 657 |
+ " with visibility " + acceptedVisibility.show(), "XTSE3040"); |
| 658 |
} |
| 659 |
newV = acceptedVisibility; |
| 660 |
} else { |
| 661 |
acceptedVisibility = wildcardAcceptedVisibility(name, acceptors); |
| 662 |
if (acceptedVisibility != null) { |
| 663 |
if (XSLAccept.isCompatible(oldV, acceptedVisibility)) { |
| 664 |
newV = acceptedVisibility; |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
if (newV == null) { |
| 670 |
if (oldV == Visibility.PUBLIC || oldV == Visibility.FINAL) { |
| 671 |
newV = Visibility.PRIVATE; |
| 672 |
} else { |
| 673 |
newV = Visibility.HIDDEN; |
| 674 |
} |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
trace(oldC.getActor().getSymbolicName() + " (" + oldV.show() + ") becomes " + newV.show()); |
| 679 |
|
| 680 |
final Component newC = Component.makeComponent(oldC.getActor(), newV, VisibilityProvenance.DERIVED, this, oldC.getDeclaringPackage()); |
| 681 |
correspondence.put(oldC, newC); |
| 682 |
newC.setBaseComponent(oldC); |
| 683 |
|
| 684 |
if (overrides.contains(name)) { |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
overriddenComponents.put(name, newC); |
| 689 |
if (newV != Visibility.ABSTRACT) { |
| 690 |
abstractComponents.remove(name); |
| 691 |
} |
| 692 |
} |
| 693 |
if (newC.getVisibility() == Visibility.HIDDEN) { |
| 694 |
hiddenComponents.add(newC); |
| 695 |
} else if (componentIndex.get(name) != null) { |
| 696 |
if (!(oldC.getActor() instanceof Mode)) { |
| 697 |
throw new XPathException("Duplicate " + namedComponentEntry.getKey(), "XTSE3050", oldC.getActor()); |
| 698 |
} |
| 699 |
} else { |
| 700 |
componentIndex.put(name, newC); |
| 701 |
if (oldC.getActor() instanceof Mode && (oldV == Visibility.PUBLIC || oldV == Visibility.FINAL) ) { |
| 702 |
Mode existing = getRuleManager().obtainMode(name.getComponentName(), false); |
| 703 |
if (existing != null) { |
| 704 |
throw new XPathException("Duplicate " + namedComponentEntry.getKey(), "XTSE3050", oldC.getActor()); |
| 705 |
} else { |
| 706 |
|
| 707 |
} |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
if (newC.getActor() instanceof Mode && overrides.contains(name)) { |
| 712 |
addCompletionAction(() -> { |
| 713 |
trace("Doing mode completion for " + newC.getActor().getSymbolicName()); |
| 714 |
List<ComponentBinding> oldBindings = newC.getBaseComponent().getComponentBindings(); |
| 715 |
List<ComponentBinding> newBindings = newC.getComponentBindings(); |
| 716 |
for (int i=0; i<oldBindings.size(); i++) { |
| 717 |
SymbolicName name12 = oldBindings.get(i).getSymbolicName(); |
| 718 |
Component target; |
| 719 |
if (overrides.contains(name12)) { |
| 720 |
|
| 721 |
target = getComponent(name12); |
| 722 |
if (target == null) { |
| 723 |
throw new AssertionError("We know there's an override for " + name12 + ", but we can't find it"); |
| 724 |
} |
| 725 |
} else { |
| 726 |
|
| 727 |
target = correspondence.get(oldBindings.get(i).getTarget()); |
| 728 |
if (target == null) { |
| 729 |
throw new AssertionError("Saxon can't find the new component corresponding to " + name12); |
| 730 |
} |
| 731 |
} |
| 732 |
ComponentBinding newBinding = new ComponentBinding(name12, target); |
| 733 |
newBindings.set(i, newBinding); |
| 734 |
} |
| 735 |
|
| 736 |
}); |
| 737 |
} else { |
| 738 |
addCompletionAction(() -> { |
| 739 |
trace("Doing normal completion for " + newC.getActor().getSymbolicName()); |
| 740 |
List<ComponentBinding> oldBindings = newC.getBaseComponent().getComponentBindings(); |
| 741 |
List<ComponentBinding> newBindings = new ArrayList<>(oldBindings.size()); |
| 742 |
makeNewComponentBindings(overrides, correspondence, oldBindings, newBindings); |
| 743 |
newC.setComponentBindings(newBindings); |
| 744 |
}); |
| 745 |
} |
| 746 |
|
| 747 |
} |
| 748 |
|
| 749 |
for (Component oldC : usedPackage.hiddenComponents) { |
| 750 |
|
| 751 |
trace(oldC.getActor().getSymbolicName() + " (HIDDEN, declared in " + oldC.getDeclaringPackage().getPackageName() + ") becomes HIDDEN"); |
| 752 |
|
| 753 |
final Component newC = Component.makeComponent(oldC.getActor(), Visibility.HIDDEN, VisibilityProvenance.DERIVED, this, oldC.getDeclaringPackage()); |
| 754 |
correspondence.put(oldC, newC); |
| 755 |
newC.setBaseComponent(oldC); |
| 756 |
|
| 757 |
hiddenComponents.add(newC); |
| 758 |
|
| 759 |
addCompletionAction(() -> { |
| 760 |
List<ComponentBinding> oldBindings = newC.getBaseComponent().getComponentBindings(); |
| 761 |
List<ComponentBinding> newBindings = new ArrayList<>(oldBindings.size()); |
| 762 |
makeNewComponentBindings(overrides, correspondence, oldBindings, newBindings); |
| 763 |
newC.setComponentBindings(newBindings); |
| 764 |
}); |
| 765 |
|
| 766 |
} |
| 767 |
|
| 768 |
if (usedPackage.isCreatesSecondaryResultDocuments()) { |
| 769 |
setCreatesSecondaryResultDocuments(true); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
private void makeNewComponentBindings(Set<SymbolicName> overrides, Map<Component, Component> correspondence, List<ComponentBinding> oldBindings, List<ComponentBinding> newBindings) { |
| 774 |
for (ComponentBinding oldBinding : oldBindings) { |
| 775 |
SymbolicName name = oldBinding.getSymbolicName(); |
| 776 |
Component target; |
| 777 |
if (overrides.contains(name)) { |
| 778 |
|
| 779 |
target = getComponent(name); |
| 780 |
if (target == null) { |
| 781 |
throw new AssertionError("We know there's an override for " + name + ", but we can't find it"); |
| 782 |
} |
| 783 |
} else { |
| 784 |
|
| 785 |
target = correspondence.get(oldBinding.getTarget()); |
| 786 |
if (target == null) { |
| 787 |
throw new AssertionError("Saxon can't find the new component corresponding to " + name); |
| 788 |
} |
| 789 |
} |
| 790 |
ComponentBinding newBinding = new ComponentBinding(name, target); |
| 791 |
newBindings.add(newBinding); |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
private void trace(String message) { |
| 796 |
if (TRACING) { |
| 797 |
System.err.println(message); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
private Visibility explicitAcceptedVisibility(SymbolicName name, List<XSLAccept> acceptors) throws XPathException { |
| 802 |
for (XSLAccept acceptor : acceptors) { |
| 803 |
for (ComponentTest test : acceptor.getExplicitComponentTests()) { |
| 804 |
if (test.matches(name)) { |
| 805 |
return acceptor.getVisibility(); |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
return null; |
| 810 |
} |
| 811 |
|
| 812 |
private Visibility wildcardAcceptedVisibility(SymbolicName name, List<XSLAccept> acceptors) throws XPathException { |
| 813 |
|
| 814 |
Visibility vis = null; |
| 815 |
for (XSLAccept acceptor : acceptors) { |
| 816 |
for (ComponentTest test : acceptor.getWildcardComponentTests()) { |
| 817 |
if (((NodeTest) test.getQNameTest()).getDefaultPriority() == -0.25 && test.matches(name)) { |
| 818 |
vis = acceptor.getVisibility(); |
| 819 |
} |
| 820 |
} |
| 821 |
} |
| 822 |
if (vis != null) { |
| 823 |
return vis; |
| 824 |
} |
| 825 |
for (XSLAccept acceptor : acceptors) { |
| 826 |
for (ComponentTest test : acceptor.getWildcardComponentTests()) { |
| 827 |
if (test.matches(name)) { |
| 828 |
vis = acceptor.getVisibility(); |
| 829 |
} |
| 830 |
} |
| 831 |
} |
| 832 |
return vis; |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
public void createFunctionLibrary() { |
| 841 |
|
| 842 |
String target = getTargetEdition(); |
| 843 |
FunctionLibraryList functionLibrary = new FunctionLibraryList(); |
| 844 |
boolean includeHOF = !("HE".equals(target) || "JS".equals(target)); |
| 845 |
functionLibrary.addFunctionLibrary(includeHOF ? config.getXSLT30FunctionSet() : new Configuration().getXSLT30FunctionSet()); |
| 846 |
functionLibrary.addFunctionLibrary(new StylesheetFunctionLibrary(this, true)); |
| 847 |
functionLibrary.addFunctionLibrary(config.getBuiltInExtensionLibraryList()); |
| 848 |
functionLibrary.addFunctionLibrary(new ConstructorFunctionLibrary(config)); |
| 849 |
if ("JS".equals(target) || "JS2".equals(target)) { |
| 850 |
addIxslFunctionLibrary(functionLibrary); |
| 851 |
} |
| 852 |
|
| 853 |
queryFunctions = new XQueryFunctionLibrary(config); |
| 854 |
functionLibrary.addFunctionLibrary(queryFunctions); |
| 855 |
functionLibrary.addFunctionLibrary(config.getIntegratedFunctionLibrary()); |
| 856 |
config.addExtensionBinders(functionLibrary); |
| 857 |
functionLibrary.addFunctionLibrary(new StylesheetFunctionLibrary(this, false)); |
| 858 |
this.functionLibrary = functionLibrary; |
| 859 |
} |
| 860 |
|
| 861 |
protected void addIxslFunctionLibrary(FunctionLibraryList functionLibrary) {} |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
public FunctionLibraryList getFunctionLibrary() { |
| 870 |
return functionLibrary; |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
public XQueryFunctionLibrary getXQueryFunctionLibrary() { |
| 879 |
return queryFunctions; |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
public void setFunctionLibraryDetails(FunctionLibraryList library, |
| 893 |
ExecutableFunctionLibrary overriding, |
| 894 |
ExecutableFunctionLibrary underriding) { |
| 895 |
if (library != null) { |
| 896 |
this.functionLibrary = library; |
| 897 |
} |
| 898 |
this.overriding = overriding; |
| 899 |
this.underriding = underriding; |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
protected UserFunction getFunction(SymbolicName.F name) { |
| 910 |
if (name.getArity() == -1) { |
| 911 |
|
| 912 |
int maximumArity = 20; |
| 913 |
for (int a = 0; a < maximumArity; a++) { |
| 914 |
SymbolicName.F sn = new SymbolicName.F(name.getComponentName(), a); |
| 915 |
UserFunction uf = getFunction(sn); |
| 916 |
if (uf != null) { |
| 917 |
uf.incrementReferenceCount(); |
| 918 |
return uf; |
| 919 |
} |
| 920 |
} |
| 921 |
return null; |
| 922 |
} else { |
| 923 |
Component component = getComponentIndex().get(name); |
| 924 |
if (component != null) { |
| 925 |
UserFunction uf = (UserFunction) component.getActor(); |
| 926 |
uf.incrementReferenceCount(); |
| 927 |
return uf; |
| 928 |
} else { |
| 929 |
return null; |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
public boolean isRetainUnusedFunctions() { |
| 942 |
return retainUnusedFunctions; |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
public void setRetainUnusedFunctions() { |
| 951 |
this.retainUnusedFunctions = true; |
| 952 |
} |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
public void updatePreparedStylesheet(PreparedStylesheet pss) throws XPathException { |
| 962 |
|
| 963 |
for (Map.Entry<SymbolicName, Component> entry : componentIndex.entrySet()) { |
| 964 |
if (entry.getValue().getVisibility() == Visibility.ABSTRACT) { |
| 965 |
abstractComponents.put(entry.getKey(), entry.getValue()); |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
pss.setTopLevelPackage(this); |
| 970 |
if (isSchemaAware() || !schemaIndex.isEmpty()) { |
| 971 |
pss.setSchemaAware(true); |
| 972 |
} |
| 973 |
pss.setHostLanguage(Configuration.XSLT); |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
FunctionLibraryList libraryList = new FunctionLibraryList(); |
| 978 |
for (FunctionLibrary lib : functionLibrary.getLibraryList()) { |
| 979 |
if (lib instanceof StylesheetFunctionLibrary) { |
| 980 |
if (((StylesheetFunctionLibrary) lib).isOverrideExtensionFunction()) { |
| 981 |
libraryList.addFunctionLibrary(overriding); |
| 982 |
|
| 983 |
} else { |
| 984 |
libraryList.addFunctionLibrary(underriding); |
| 985 |
|
| 986 |
} |
| 987 |
} else { |
| 988 |
libraryList.addFunctionLibrary(lib); |
| 989 |
} |
| 990 |
} |
| 991 |
pss.setFunctionLibrary(libraryList); |
| 992 |
|
| 993 |
if (!pss.createsSecondaryResult()) { |
| 994 |
pss.setCreatesSecondaryResult(mayCreateSecondaryResultDocuments()); |
| 995 |
} |
| 996 |
pss.setDefaultOutputProperties(defaultOutputProperties); |
| 997 |
for (Map.Entry<StructuredQName, Properties> entry : namedOutputProperties.entrySet()) { |
| 998 |
pss.setOutputProperties(entry.getKey(), entry.getValue()); |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
if (characterMapIndex != null) { |
| 1004 |
for (CharacterMap cm : characterMapIndex) { |
| 1005 |
pss.getCharacterMapIndex().putCharacterMap(cm.getName(), cm); |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
ruleManager.checkConsistency(); |
| 1012 |
ruleManager.computeRankings(); |
| 1013 |
ruleManager.invertStreamableTemplates(); |
| 1014 |
if (config.obtainOptimizer().isOptionSet(OptimizerOptions.RULE_SET)) { |
| 1015 |
ruleManager.optimizeRules(); |
| 1016 |
} |
| 1017 |
pss.setRuleManager(ruleManager); |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
for (Component comp : componentIndex.values()) { |
| 1023 |
if (comp.getActor() instanceof NamedTemplate) { |
| 1024 |
NamedTemplate t = (NamedTemplate) comp.getActor(); |
| 1025 |
pss.putNamedTemplate(t.getTemplateName(), t); |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
pss.setComponentIndex(componentIndex); |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
for (Component comp : componentIndex.values()) { |
| 1036 |
if (comp.getActor() instanceof GlobalParam) { |
| 1037 |
GlobalParam gv = (GlobalParam) comp.getActor(); |
| 1038 |
pss.registerGlobalParameter(gv); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
if (globalContextRequirement != null) { |
| 1045 |
pss.setGlobalContextRequirement(globalContextRequirement); |
| 1046 |
} |
| 1047 |
|
| 1048 |
} |
| 1049 |
|
| 1050 |
private boolean mayCreateSecondaryResultDocuments() { |
| 1051 |
if (createsSecondaryResultDocuments) { |
| 1052 |
return true; |
| 1053 |
} |
| 1054 |
for (StylesheetPackage p : usedPackages) { |
| 1055 |
if (p.mayCreateSecondaryResultDocuments()) { |
| 1056 |
return true; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
return false; |
| 1060 |
} |
| 1061 |
|
| 1062 |
public Map<SymbolicName, Component> getAbstractComponents() { |
| 1063 |
return abstractComponents; |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
public void export(ExpressionPresenter presenter) throws XPathException { |
| 1073 |
throw new XPathException("Exporting a stylesheet requires Saxon-EE"); |
| 1074 |
} |
| 1075 |
|
| 1076 |
public void checkForAbstractComponents() throws XPathException { |
| 1077 |
for (Map.Entry<SymbolicName, Component> entry : componentIndex.entrySet()) { |
| 1078 |
if (entry.getValue().getVisibility() == Visibility.ABSTRACT && entry.getValue().getContainingPackage() == this) { |
| 1079 |
abstractComponents.put(entry.getKey(), entry.getValue()); |
| 1080 |
} |
| 1081 |
} |
| 1082 |
if (!abstractComponents.isEmpty()) { |
| 1083 |
FastStringBuffer buff = new FastStringBuffer(256); |
| 1084 |
int count = 0; |
| 1085 |
for (SymbolicName name : abstractComponents.keySet()) { |
| 1086 |
if (count++ > 0) { |
| 1087 |
buff.append(", "); |
| 1088 |
} |
| 1089 |
buff.append(name.toString()); |
| 1090 |
if (buff.length() > 300) { |
| 1091 |
buff.append(" ..."); |
| 1092 |
break; |
| 1093 |
} |
| 1094 |
} |
| 1095 |
throw new XPathException( |
| 1096 |
"The package is not executable, because it contains abstract components: " + |
| 1097 |
buff, "XTSE3080"); |
| 1098 |
} |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
public boolean isFallbackToNonStreaming() { |
| 1109 |
return true; |
| 1110 |
} |
| 1111 |
|
| 1112 |
public void setFallbackToNonStreaming() { |
| 1113 |
|
| 1114 |
} |
| 1115 |
|
| 1116 |
|
| 1117 |
} |
| 1118 |
|