| 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.expr.Component; |
| 12 |
import net.sf.saxon.expr.Expression; |
| 13 |
import net.sf.saxon.expr.accum.Accumulator; |
| 14 |
import net.sf.saxon.expr.accum.AccumulatorRegistry; |
| 15 |
import net.sf.saxon.expr.instruct.*; |
| 16 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 17 |
import net.sf.saxon.expr.parser.Optimizer; |
| 18 |
import net.sf.saxon.functions.DocumentFn; |
| 19 |
import net.sf.saxon.functions.ExecutableFunctionLibrary; |
| 20 |
import net.sf.saxon.lib.SaxonOutputKeys; |
| 21 |
import net.sf.saxon.om.*; |
| 22 |
import net.sf.saxon.pattern.NameTest; |
| 23 |
import net.sf.saxon.pattern.NodeKindTest; |
| 24 |
import net.sf.saxon.query.XQueryFunction; |
| 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.trans.*; |
| 29 |
import net.sf.saxon.trans.rules.RuleManager; |
| 30 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 31 |
import net.sf.saxon.tree.linked.DocumentImpl; |
| 32 |
import net.sf.saxon.type.Type; |
| 33 |
import net.sf.saxon.value.Whitespace; |
| 34 |
import net.sf.saxon.z.IntHashMap; |
| 35 |
|
| 36 |
import javax.xml.transform.URIResolver; |
| 37 |
import java.util.*; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class PrincipalStylesheetModule extends StylesheetModule implements GlobalVariableManager { |
| 49 |
|
| 50 |
private StylesheetPackage stylesheetPackage; |
| 51 |
private boolean declaredModes; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private HashMap<StructuredQName, ComponentDeclaration> globalVariableIndex = |
| 63 |
new HashMap<>(20); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private HashMap<StructuredQName, ComponentDeclaration> templateIndex = new HashMap<>(20); |
| 68 |
|
| 69 |
|
| 70 |
private HashMap<SymbolicName, ComponentDeclaration> functionIndex = new HashMap<>(8); |
| 71 |
|
| 72 |
|
| 73 |
private KeyManager keyManager; |
| 74 |
|
| 75 |
|
| 76 |
private DecimalFormatManager decimalFormatManager; |
| 77 |
|
| 78 |
|
| 79 |
private RuleManager ruleManager; |
| 80 |
|
| 81 |
|
| 82 |
private AccumulatorRegistry accumulatorManager = null; |
| 83 |
|
| 84 |
|
| 85 |
private int numberOfAliases = 0; |
| 86 |
private List<ComponentDeclaration> namespaceAliasList = new ArrayList<>(5); |
| 87 |
private HashMap<String, NamespaceBinding> namespaceAliasMap; |
| 88 |
private Set<String> aliasResultUriSet; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private Map<StructuredQName, List<ComponentDeclaration>> attributeSetDeclarations = new HashMap<>(); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private HashMap<DocumentURI, XSLModuleRoot> moduleCache = new HashMap<>(4); |
| 97 |
|
| 98 |
private TypeAliasManager typeAliasManager; |
| 99 |
|
| 100 |
|
| 101 |
private CharacterMapIndex characterMapIndex; |
| 102 |
|
| 103 |
private List<Action> fixupActions = new ArrayList<>(); |
| 104 |
private boolean needsDynamicOutputProperties = false; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public PrincipalStylesheetModule(XSLPackage sourceElement) throws XPathException { |
| 114 |
super(sourceElement, 0); |
| 115 |
declaredModes = sourceElement.isDeclaredModes(); |
| 116 |
stylesheetPackage = getConfiguration().makeStylesheetPackage(); |
| 117 |
CompilerInfo compilerInfo = sourceElement.getCompilation().getCompilerInfo(); |
| 118 |
stylesheetPackage.setTargetEdition(compilerInfo.getTargetEdition()); |
| 119 |
stylesheetPackage.setRelocatable(compilerInfo.isRelocatable()); |
| 120 |
stylesheetPackage.setJustInTimeCompilation(compilerInfo.isJustInTimeCompilation()); |
| 121 |
stylesheetPackage.setImplicitPackage(!sourceElement.getLocalPart().equals("package")); |
| 122 |
|
| 123 |
keyManager = stylesheetPackage.getKeyManager(); |
| 124 |
decimalFormatManager = stylesheetPackage.getDecimalFormatManager(); |
| 125 |
|
| 126 |
ruleManager = new RuleManager(stylesheetPackage, compilerInfo); |
| 127 |
ruleManager.getUnnamedMode().makeDeclaringComponent(Visibility.PRIVATE, stylesheetPackage); |
| 128 |
stylesheetPackage.setRuleManager(ruleManager); |
| 129 |
stylesheetPackage.setDeclaredModes(declaredModes); |
| 130 |
StructuredQName defaultMode = sourceElement.getDefaultMode(); |
| 131 |
stylesheetPackage.setDefaultMode(sourceElement.getDefaultMode()); |
| 132 |
if (defaultMode != null) { |
| 133 |
ruleManager.obtainMode(defaultMode, !declaredModes); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
characterMapIndex = new CharacterMapIndex(); |
| 138 |
stylesheetPackage.setCharacterMapIndex(characterMapIndex); |
| 139 |
|
| 140 |
typeAliasManager = getConfiguration().makeTypeAliasManager(); |
| 141 |
stylesheetPackage.setTypeAliasManager(typeAliasManager); |
| 142 |
|
| 143 |
try { |
| 144 |
setInputTypeAnnotations(sourceElement.getInputTypeAnnotationsAttribute()); |
| 145 |
} catch (XPathException err) { |
| 146 |
|
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public Component getComponent(SymbolicName name) { |
| 159 |
return stylesheetPackage.getComponentIndex().get(name); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
@Override |
| 170 |
public PrincipalStylesheetModule getPrincipalStylesheetModule() { |
| 171 |
return this; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public StylesheetPackage getStylesheetPackage() { |
| 181 |
return stylesheetPackage; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public KeyManager getKeyManager() { |
| 191 |
return keyManager; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public DecimalFormatManager getDecimalFormatManager() { |
| 201 |
return decimalFormatManager; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public RuleManager getRuleManager() { |
| 211 |
return ruleManager; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public boolean isDeclaredModes() { |
| 221 |
return declaredModes; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public void addFixupAction(Action action) { |
| 232 |
fixupActions.add(action); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public void setNeedsDynamicOutputProperties(boolean b) { |
| 242 |
needsDynamicOutputProperties = b; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public CharacterMapIndex getCharacterMapIndex() { |
| 253 |
return characterMapIndex; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public TypeAliasManager getTypeAliasManager() { |
| 263 |
return typeAliasManager; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public void declareXQueryFunction(XQueryFunction function) throws XPathException { |
| 275 |
XQueryFunctionLibrary lib = getStylesheetPackage().getXQueryFunctionLibrary(); |
| 276 |
if (getStylesheetPackage().getFunction(function.getUserFunction().getSymbolicName()) != null) { |
| 277 |
throw new XPathException("Duplication declaration of " + |
| 278 |
function.getUserFunction().getSymbolicName(), "XQST0034"); |
| 279 |
} |
| 280 |
lib.declareFunction(function); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public void putStylesheetDocument(DocumentURI key, XSLStylesheet module) { |
| 291 |
moduleCache.put(key, module); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public XSLModuleRoot getStylesheetDocument(DocumentURI key) { |
| 302 |
XSLModuleRoot sheet = moduleCache.get(key); |
| 303 |
if (sheet != null) { |
| 304 |
XPathException warning = new XPathException( |
| 305 |
"Stylesheet module " + key + " is included or imported more than once. " + |
| 306 |
"This is permitted, but may lead to errors or unexpected behavior"); |
| 307 |
sheet.issueWarning(warning); |
| 308 |
} |
| 309 |
return sheet; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public void preprocess() throws XPathException { |
| 322 |
|
| 323 |
|
| 324 |
net.sf.saxon.trans.Timer timer; |
| 325 |
if (Compilation.TIMING) { |
| 326 |
timer = new net.sf.saxon.trans.Timer(); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
spliceUsePackages((XSLPackage) getRootElement(), getRootElement().getCompilation()); |
| 332 |
|
| 333 |
if (Compilation.TIMING) { |
| 334 |
timer.report("spliceIncludes"); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
importSchemata(); |
| 340 |
|
| 341 |
if (Compilation.TIMING) { |
| 342 |
timer.report("importSchemata"); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
getTypeAliasManager().processAllDeclarations(topLevel); |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
buildIndexes(); |
| 352 |
|
| 353 |
if (Compilation.TIMING) { |
| 354 |
timer.report("buildIndexes"); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
checkForSchemaAwareness(); |
| 360 |
|
| 361 |
if (Compilation.TIMING) { |
| 362 |
timer.report("checkForSchemaAwareness"); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
processAllAttributes(); |
| 368 |
|
| 369 |
if (Compilation.TIMING) { |
| 370 |
timer.report("processAllAttributes"); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
collectNamespaceAliases(); |
| 375 |
|
| 376 |
if (Compilation.TIMING) { |
| 377 |
timer.report("collectNamespaceAliases"); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
for (ComponentDeclaration decl : topLevel) { |
| 383 |
StyleElement inst = decl.getSourceElement(); |
| 384 |
if (!inst.isActionCompleted(StyleElement.ACTION_FIXUP)) { |
| 385 |
inst.setActionCompleted(StyleElement.ACTION_FIXUP); |
| 386 |
inst.fixupReferences(); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
if (Compilation.TIMING) { |
| 391 |
timer.report("fixupReferences"); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
XSLPackage top = (XSLPackage) getStylesheetElement(); |
| 398 |
|
| 399 |
ComponentDeclaration decl = new ComponentDeclaration(this, top); |
| 400 |
if (!top.isActionCompleted(StyleElement.ACTION_VALIDATE)) { |
| 401 |
top.setActionCompleted(StyleElement.ACTION_VALIDATE); |
| 402 |
top.validate(null); |
| 403 |
for (ComponentDeclaration d : topLevel) { |
| 404 |
d.getSourceElement().validateSubtree(d, false); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
if (Compilation.TIMING) { |
| 409 |
timer.report("validate"); |
| 410 |
timer.reportCumulative("total preprocess"); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
Properties props = gatherOutputProperties(null); |
| 416 |
props.setProperty(SaxonOutputKeys.STYLESHEET_VERSION, getStylesheetPackage().getVersion() + ""); |
| 417 |
getStylesheetPackage().setDefaultOutputProperties(props); |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
HashSet<StructuredQName> outputNames = new HashSet<>(5); |
| 422 |
for (ComponentDeclaration outputDecl : topLevel) { |
| 423 |
if (outputDecl.getSourceElement() instanceof XSLOutput) { |
| 424 |
XSLOutput out = (XSLOutput) outputDecl.getSourceElement(); |
| 425 |
StructuredQName qName = out.getFormatQName(); |
| 426 |
if (qName != null) { |
| 427 |
outputNames.add(qName); |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
if (outputNames.isEmpty()) { |
| 432 |
if (needsDynamicOutputProperties) { |
| 433 |
throw new XPathException( |
| 434 |
"The stylesheet contains xsl:result-document instructions that calculate the output " + |
| 435 |
"format name at run-time, but there are no named xsl:output declarations", "XTDE1460"); |
| 436 |
} |
| 437 |
} else { |
| 438 |
for (StructuredQName qName : outputNames) { |
| 439 |
Properties oprops = gatherOutputProperties(qName); |
| 440 |
|
| 441 |
getStylesheetPackage().setNamedOutputProperties(qName, oprops); |
| 442 |
|
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
for (ComponentDeclaration d : topLevel) { |
| 449 |
StyleElement inst = d.getSourceElement(); |
| 450 |
if (inst instanceof XSLCharacterMap) { |
| 451 |
XSLCharacterMap xcm = (XSLCharacterMap) inst; |
| 452 |
StructuredQName qn = xcm.getCharacterMapName(); |
| 453 |
IntHashMap<String> map = new IntHashMap<>(); |
| 454 |
xcm.assemble(map); |
| 455 |
characterMapIndex.putCharacterMap(xcm.getCharacterMapName(), new CharacterMap(qn, map)); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
protected void spliceUsePackages(XSLPackage xslpackage, Compilation compilation) throws XPathException { |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
List<XSLUsePackage> useDeclarations = new ArrayList<>(); |
| 482 |
gatherUsePackageDeclarations(compilation, xslpackage, useDeclarations); |
| 483 |
|
| 484 |
|
| 485 |
Set<SymbolicName> overrides = new HashSet<>(); |
| 486 |
for (XSLUsePackage use : useDeclarations) { |
| 487 |
gatherOverridingDeclarations(use, compilation, overrides); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
StylesheetPackage thisPackage = getStylesheetPackage(); |
| 493 |
for (XSLUsePackage use : useDeclarations) { |
| 494 |
List<XSLAccept> acceptors = use.getAcceptors(); |
| 495 |
thisPackage.addComponentsFromUsedPackage(use.getUsedPackage(), acceptors, overrides); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
for (XSLUsePackage use : useDeclarations) { |
| 500 |
use.gatherRuleOverrides(this, overrides); |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
spliceIncludes(); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
private static void gatherUsePackageDeclarations(Compilation compilation, StyleElement wrapper, List<XSLUsePackage> declarations) throws XPathException { |
| 510 |
wrapper.iterateAxis(AxisInfo.CHILD).forEachOrFail(use -> { |
| 511 |
if (use instanceof XSLUsePackage) { |
| 512 |
declarations.add((XSLUsePackage) use); |
| 513 |
} else if (use instanceof XSLInclude) { |
| 514 |
String href = Whitespace.trim(use.getAttributeValue("", "href")); |
| 515 |
URIResolver resolver = compilation.getCompilerInfo().getURIResolver(); |
| 516 |
DocumentURI key = DocumentFn.computeDocumentKey(href, use.getBaseURI(), compilation.getPackageData(), resolver, false); |
| 517 |
TreeInfo includedTree = compilation.getStylesheetModules().get(key); |
| 518 |
StyleElement incWrapper = (StyleElement) ((DocumentImpl) includedTree.getRootNode()).getDocumentElement(); |
| 519 |
gatherUsePackageDeclarations(compilation, incWrapper, declarations); |
| 520 |
} |
| 521 |
}); |
| 522 |
} |
| 523 |
|
| 524 |
private void gatherOverridingDeclarations(XSLUsePackage use, Compilation compilation, Set<SymbolicName> overrides) throws XPathException { |
| 525 |
use.findUsedPackage(compilation.getCompilerInfo()); |
| 526 |
use.gatherNamedOverrides(this, topLevel, overrides); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
protected void importSchemata() throws XPathException { |
| 537 |
|
| 538 |
for (int i = topLevel.size() - 1; i >= 0; i--) { |
| 539 |
ComponentDeclaration decl = topLevel.get(i); |
| 540 |
if (decl.getSourceElement() instanceof XSLImportSchema) { |
| 541 |
XPathException xe = new XPathException("xsl:import-schema requires Saxon-EE"); |
| 542 |
xe.setErrorCode("XTSE1650"); |
| 543 |
xe.setLocator(decl.getSourceElement()); |
| 544 |
throw xe; |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
private void buildIndexes() throws XPathException { |
| 558 |
|
| 559 |
for (int i = topLevel.size() - 1; i >= 0; i--) { |
| 560 |
ComponentDeclaration decl = topLevel.get(i); |
| 561 |
decl.getSourceElement().index(decl, this); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
public void processAllAttributes() throws XPathException { |
| 572 |
getRootElement().processDefaultCollationAttribute(); |
| 573 |
getRootElement().processDefaultMode(); |
| 574 |
getRootElement().prepareAttributes(); |
| 575 |
for (XSLModuleRoot xss : moduleCache.values()) { |
| 576 |
xss.prepareAttributes(); |
| 577 |
} |
| 578 |
for (ComponentDeclaration decl : topLevel) { |
| 579 |
StyleElement inst = decl.getSourceElement(); |
| 580 |
if (!inst.isActionCompleted(StyleElement.ACTION_PROCESS_ATTRIBUTES)) { |
| 581 |
inst.setActionCompleted(StyleElement.ACTION_PROCESS_ATTRIBUTES); |
| 582 |
try { |
| 583 |
inst.processAllAttributes(); |
| 584 |
} catch (XPathException err) { |
| 585 |
decl.getSourceElement().compileError(err); |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
protected void indexFunction(ComponentDeclaration decl) { |
| 597 |
HashMap<SymbolicName, Component> componentIndex = stylesheetPackage.getComponentIndex(); |
| 598 |
XSLFunction sourceFunction = (XSLFunction) decl.getSourceElement(); |
| 599 |
UserFunction compiledFunction = sourceFunction.getCompiledFunction(); |
| 600 |
Component declaringComponent = compiledFunction.obtainDeclaringComponent(sourceFunction); |
| 601 |
SymbolicName.F sName = sourceFunction.getSymbolicName(); |
| 602 |
|
| 603 |
if (sName != null) { |
| 604 |
|
| 605 |
Component other = componentIndex.get(sName); |
| 606 |
if (other == null) { |
| 607 |
|
| 608 |
componentIndex.put(sName, declaringComponent); |
| 609 |
functionIndex.put(sName, decl); |
| 610 |
} else { |
| 611 |
if (other.getDeclaringPackage() == getStylesheetPackage()) { |
| 612 |
|
| 613 |
int thisPrecedence = decl.getPrecedence(); |
| 614 |
ComponentDeclaration otherFunction = functionIndex.get(sName); |
| 615 |
int otherPrecedence = otherFunction.getPrecedence(); |
| 616 |
if (thisPrecedence == otherPrecedence) { |
| 617 |
sourceFunction.compileError("Duplicate named function (see line " + |
| 618 |
otherFunction.getSourceElement().getLineNumber() + " of " + otherFunction.getSourceElement().getSystemId() + ')', "XTSE0770"); |
| 619 |
} else if (thisPrecedence < otherPrecedence) { |
| 620 |
|
| 621 |
} else { |
| 622 |
|
| 623 |
|
| 624 |
componentIndex.put(sName, declaringComponent); |
| 625 |
functionIndex.put(sName, decl); |
| 626 |
} |
| 627 |
} else if (sourceFunction.findAncestorElement(StandardNames.XSL_OVERRIDE) != null) { |
| 628 |
|
| 629 |
componentIndex.put(sName, declaringComponent); |
| 630 |
functionIndex.put(sName, decl); |
| 631 |
} else { |
| 632 |
sourceFunction.compileError("Function " + sName.getShortName() + |
| 633 |
" conflicts with a public function in package " + other.getDeclaringPackage().getPackageName(), "XTSE3050"); |
| 634 |
|
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
protected void indexVariableDeclaration(ComponentDeclaration decl) throws XPathException { |
| 649 |
XSLGlobalVariable varDecl = (XSLGlobalVariable) decl.getSourceElement(); |
| 650 |
StructuredQName qName = varDecl.getSourceBinding().getVariableQName(); |
| 651 |
GlobalVariable compiledVariable = (GlobalVariable) varDecl.getActor(); |
| 652 |
Component declaringComponent = compiledVariable.obtainDeclaringComponent(varDecl); |
| 653 |
HashMap<SymbolicName, Component> componentIndex = stylesheetPackage.getComponentIndex(); |
| 654 |
if (qName != null) { |
| 655 |
|
| 656 |
SymbolicName sName = varDecl.getSymbolicName(); |
| 657 |
Component other = componentIndex.get(sName); |
| 658 |
if (other == null) { |
| 659 |
|
| 660 |
globalVariableIndex.put(qName, decl); |
| 661 |
componentIndex.put(new SymbolicName(StandardNames.XSL_VARIABLE, qName), |
| 662 |
varDecl.getActor().getDeclaringComponent()); |
| 663 |
} else { |
| 664 |
if (other.getDeclaringPackage() == getStylesheetPackage()) { |
| 665 |
|
| 666 |
int thisPrecedence = decl.getPrecedence(); |
| 667 |
ComponentDeclaration otherVarDecl = globalVariableIndex.get(sName.getComponentName()); |
| 668 |
int otherPrecedence = otherVarDecl.getPrecedence(); |
| 669 |
if (thisPrecedence == otherPrecedence) { |
| 670 |
StyleElement v2 = otherVarDecl.getSourceElement(); |
| 671 |
if (v2 == varDecl) { |
| 672 |
varDecl.compileError( |
| 673 |
"Global variable or parameter $" + qName.getDisplayName() + " is declared more than once " + |
| 674 |
"(caused by including the containing module more than once)", |
| 675 |
"XTSE0630"); |
| 676 |
} else { |
| 677 |
varDecl.compileError("Duplicate global variable/parameter declaration (see line " + |
| 678 |
v2.getLineNumber() + " of " + v2.getSystemId() + ')', "XTSE0630"); |
| 679 |
} |
| 680 |
} else if (thisPrecedence < otherPrecedence && varDecl != otherVarDecl.getSourceElement()) { |
| 681 |
varDecl.setRedundant(true); |
| 682 |
} else if (varDecl != otherVarDecl.getSourceElement()) { |
| 683 |
((XSLGlobalVariable) otherVarDecl.getSourceElement()).setRedundant(true); |
| 684 |
globalVariableIndex.put(qName, decl); |
| 685 |
componentIndex.put(new SymbolicName(StandardNames.XSL_VARIABLE, qName), |
| 686 |
varDecl.getActor().getDeclaringComponent()); |
| 687 |
} |
| 688 |
} else if (varDecl.findAncestorElement(StandardNames.XSL_OVERRIDE) != null) { |
| 689 |
|
| 690 |
componentIndex.put(sName, declaringComponent); |
| 691 |
globalVariableIndex.put(sName.getComponentName(), decl); |
| 692 |
} else { |
| 693 |
String kind = varDecl instanceof XSLGlobalParam ? "parameter" : "variable"; |
| 694 |
varDecl.compileError("Global " + kind + " $" + sName.getComponentName().getDisplayName() + |
| 695 |
" conflicts with a public variable/parameter in package " + other.getDeclaringPackage().getPackageName(), "XTSE3050"); |
| 696 |
|
| 697 |
} |
| 698 |
|
| 699 |
} |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
public SourceBinding getGlobalVariableBinding(StructuredQName qName) { |
| 713 |
ComponentDeclaration decl = globalVariableIndex.get(qName); |
| 714 |
return decl == null ? null : ((XSLGlobalVariable) decl.getSourceElement()).getSourceBinding(); |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
protected void indexNamedTemplate(ComponentDeclaration decl) throws XPathException { |
| 724 |
HashMap<SymbolicName, Component> componentIndex = stylesheetPackage.getComponentIndex(); |
| 725 |
XSLTemplate sourceTemplate = (XSLTemplate) decl.getSourceElement(); |
| 726 |
SymbolicName sName = sourceTemplate.getSymbolicName(); |
| 727 |
if (sName != null) { |
| 728 |
|
| 729 |
Component other = componentIndex.get(sName); |
| 730 |
if (other == null) { |
| 731 |
|
| 732 |
|
| 733 |
NamedTemplate compiledTemplate = ((XSLTemplate) decl.getSourceElement()).getCompiledNamedTemplate(); |
| 734 |
Component declaringComponent = compiledTemplate.obtainDeclaringComponent(sourceTemplate); |
| 735 |
componentIndex.put(sName, declaringComponent); |
| 736 |
setLocalParamDetails(sourceTemplate, compiledTemplate); |
| 737 |
templateIndex.put(sName.getComponentName(), decl); |
| 738 |
} else { |
| 739 |
if (other.getDeclaringPackage() == getStylesheetPackage()) { |
| 740 |
|
| 741 |
int thisPrecedence = decl.getPrecedence(); |
| 742 |
ComponentDeclaration otherTemplate = templateIndex.get(sName.getComponentName()); |
| 743 |
int otherPrecedence = otherTemplate.getPrecedence(); |
| 744 |
if (thisPrecedence == otherPrecedence) { |
| 745 |
String errorCode = sourceTemplate.getParent() instanceof XSLOverride ? "XTSE3055" : "XTSE0660"; |
| 746 |
sourceTemplate.compileError("Duplicate named template (see line " + |
| 747 |
otherTemplate.getSourceElement().getLineNumber() + " of " + otherTemplate.getSourceElement().getSystemId() + ')', errorCode); |
| 748 |
} else if (thisPrecedence < otherPrecedence) { |
| 749 |
|
| 750 |
} else { |
| 751 |
|
| 752 |
|
| 753 |
NamedTemplate compiledTemplate = new NamedTemplate(sName.getComponentName()); |
| 754 |
Component declaringComponent = compiledTemplate.obtainDeclaringComponent(sourceTemplate); |
| 755 |
componentIndex.put(sName, declaringComponent); |
| 756 |
templateIndex.put(sName.getComponentName(), decl); |
| 757 |
setLocalParamDetails(sourceTemplate, compiledTemplate); |
| 758 |
} |
| 759 |
} else if (sourceTemplate.findAncestorElement(StandardNames.XSL_OVERRIDE) != null) { |
| 760 |
|
| 761 |
NamedTemplate compiledTemplate = sourceTemplate.getCompiledNamedTemplate(); |
| 762 |
Component declaringComponent = compiledTemplate.obtainDeclaringComponent(sourceTemplate); |
| 763 |
componentIndex.put(sName, declaringComponent); |
| 764 |
templateIndex.put(sName.getComponentName(), decl); |
| 765 |
} else { |
| 766 |
sourceTemplate.compileError("Named template " + sName.getComponentName().getDisplayName() + |
| 767 |
" conflicts with a public named template in package " + other.getDeclaringPackage().getPackageName(), "XTSE3050"); |
| 768 |
|
| 769 |
} |
| 770 |
} |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
private static void setLocalParamDetails(XSLTemplate source, NamedTemplate nt) throws XPathException { |
| 776 |
AxisIterator kids = source.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 777 |
List<NamedTemplate.LocalParamInfo> details = new ArrayList<>(); |
| 778 |
kids.forEachOrFail(child -> { |
| 779 |
if (child instanceof XSLLocalParam) { |
| 780 |
XSLLocalParam lp = (XSLLocalParam) child; |
| 781 |
lp.prepareTemplateSignatureAttributes(); |
| 782 |
NamedTemplate.LocalParamInfo info = new NamedTemplate.LocalParamInfo(); |
| 783 |
info.name = lp.getVariableQName(); |
| 784 |
info.requiredType = lp.getRequiredType(); |
| 785 |
info.isRequired = lp.isRequiredParam(); |
| 786 |
info.isTunnel = lp.isTunnelParam(); |
| 787 |
details.add(info); |
| 788 |
} |
| 789 |
}); |
| 790 |
nt.setLocalParamDetails(details); |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
public NamedTemplate getNamedTemplate(StructuredQName name) { |
| 805 |
HashMap<SymbolicName, Component> componentIndex = stylesheetPackage.getComponentIndex(); |
| 806 |
Component component = componentIndex.get(new SymbolicName(StandardNames.XSL_TEMPLATE, name)); |
| 807 |
return component == null ? null : (NamedTemplate) component.getActor(); |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
protected void indexAttributeSet(ComponentDeclaration decl) throws XPathException { |
| 817 |
|
| 818 |
XSLAttributeSet sourceAttributeSet = (XSLAttributeSet) decl.getSourceElement(); |
| 819 |
StructuredQName name = sourceAttributeSet.getAttributeSetName(); |
| 820 |
List<ComponentDeclaration> entries = attributeSetDeclarations.get(name); |
| 821 |
if (entries == null) { |
| 822 |
entries = new ArrayList<>(); |
| 823 |
attributeSetDeclarations.put(name, entries); |
| 824 |
} else { |
| 825 |
String thisVis = Whitespace.trim(sourceAttributeSet.getAttributeValue("", "visibility")); |
| 826 |
String firstVis = Whitespace.trim(entries.get(0).getSourceElement().getAttributeValue("", "visibility")); |
| 827 |
if (thisVis == null ? firstVis != null : !thisVis.equals(firstVis)) { |
| 828 |
throw new XPathException("Visibility attributes on attribute-sets sharing the same name must all be the same", "XTSE0010"); |
| 829 |
} |
| 830 |
} |
| 831 |
entries.add(0, decl); |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
public List<ComponentDeclaration> getAttributeSetDeclarations(StructuredQName name) { |
| 842 |
return attributeSetDeclarations.get(name); |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
public void combineAttributeSets(Compilation compilation) throws XPathException { |
| 855 |
Map<StructuredQName, AttributeSet> index = new HashMap<>(); |
| 856 |
for (Map.Entry<StructuredQName, List<ComponentDeclaration>> entry : attributeSetDeclarations.entrySet()) { |
| 857 |
AttributeSet as = new AttributeSet(); |
| 858 |
as.setName(entry.getKey()); |
| 859 |
as.setPackageData(stylesheetPackage); |
| 860 |
StyleElement firstDecl = entry.getValue().get(0).getSourceElement(); |
| 861 |
as.setSystemId(firstDecl.getSystemId()); |
| 862 |
as.setLineNumber(firstDecl.getLineNumber()); |
| 863 |
index.put(entry.getKey(), as); |
| 864 |
|
| 865 |
Component declaringComponent = as.getDeclaringComponent(); |
| 866 |
if (declaringComponent == null) { |
| 867 |
declaringComponent = as.makeDeclaringComponent(Visibility.PRIVATE, stylesheetPackage); |
| 868 |
} |
| 869 |
stylesheetPackage.addComponent(declaringComponent); |
| 870 |
} |
| 871 |
for (Map.Entry<StructuredQName, List<ComponentDeclaration>> entry : attributeSetDeclarations.entrySet()) { |
| 872 |
List<Expression> content = new ArrayList<>(); |
| 873 |
Visibility vis = null; |
| 874 |
boolean explicitVisibility = false; |
| 875 |
boolean streamable = false; |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
List<ComponentDeclaration> entries = new ArrayList<>(); |
| 882 |
Set<XSLAttributeSet> elements = new HashSet<>(); |
| 883 |
for (int i = entry.getValue().size() - 1; i >= 0; i--) { |
| 884 |
ComponentDeclaration decl = entry.getValue().get(i); |
| 885 |
XSLAttributeSet src = (XSLAttributeSet) decl.getSourceElement(); |
| 886 |
if (!elements.contains(src)) { |
| 887 |
entries.add(0, decl); |
| 888 |
elements.add(src); |
| 889 |
} |
| 890 |
} |
| 891 |
for (ComponentDeclaration decl : entries) { |
| 892 |
XSLAttributeSet src = (XSLAttributeSet) decl.getSourceElement(); |
| 893 |
streamable |= src.isDeclaredStreamable(); |
| 894 |
src.compileDeclaration(compilation, decl); |
| 895 |
content.addAll(src.getContainedInstructions()); |
| 896 |
vis = src.getVisibility(); |
| 897 |
explicitVisibility = explicitVisibility || src.getAttributeValue("", "visibility") != null; |
| 898 |
} |
| 899 |
|
| 900 |
AttributeSet aSet = index.get(entry.getKey()); |
| 901 |
aSet.setDeclaredStreamable(streamable); |
| 902 |
Expression block = Block.makeBlock(content); |
| 903 |
aSet.setBody(block); |
| 904 |
SlotManager frame = getConfiguration().makeSlotManager(); |
| 905 |
ExpressionTool.allocateSlots(block, 0, frame); |
| 906 |
aSet.setStackFrameMap(frame); |
| 907 |
VisibilityProvenance provenance = explicitVisibility ? VisibilityProvenance.EXPLICIT : VisibilityProvenance.DEFAULTED; |
| 908 |
aSet.getDeclaringComponent().setVisibility(vis, provenance); |
| 909 |
|
| 910 |
if (streamable) { |
| 911 |
checkStreamability(aSet); |
| 912 |
} |
| 913 |
|
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
protected void checkStreamability(AttributeSet aSet) throws XPathException { |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
protected void getAttributeSets(StructuredQName name, List<ComponentDeclaration> list) { |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
for (ComponentDeclaration decl : topLevel) { |
| 943 |
if (decl.getSourceElement() instanceof XSLAttributeSet) { |
| 944 |
XSLAttributeSet t = (XSLAttributeSet) decl.getSourceElement(); |
| 945 |
if (t.getAttributeSetName().equals(name)) { |
| 946 |
list.add(decl); |
| 947 |
} |
| 948 |
} |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
public void indexMode(ComponentDeclaration decl) { |
| 958 |
XSLMode sourceMode = (XSLMode) decl.getSourceElement(); |
| 959 |
StructuredQName modeName = sourceMode.getObjectName(); |
| 960 |
if (modeName == null) { |
| 961 |
return; |
| 962 |
} |
| 963 |
SymbolicName sName = new SymbolicName(StandardNames.XSL_MODE, modeName); |
| 964 |
|
| 965 |
Mode other = getStylesheetPackage().getRuleManager().obtainMode(modeName, false); |
| 966 |
if (other != null && other.getDeclaringComponent().getDeclaringPackage() != getStylesheetPackage()) { |
| 967 |
sourceMode.compileError("Mode " + sName.getComponentName().getDisplayName() + |
| 968 |
" conflicts with a public mode declared in package " + |
| 969 |
other.getDeclaringComponent().getDeclaringPackage().getPackageName(), "XTSE3050"); |
| 970 |
|
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
public boolean checkAcceptableModeForPackage(XSLTemplate template, Mode mode) { |
| 982 |
StylesheetPackage templatePack = template.getPackageData(); |
| 983 |
if (mode.getDeclaringComponent() == null) { |
| 984 |
return true; |
| 985 |
} |
| 986 |
StylesheetPackage modePack = mode.getDeclaringComponent().getDeclaringPackage(); |
| 987 |
if (templatePack != modePack) { |
| 988 |
NodeInfo parent = template.getParent(); |
| 989 |
boolean bad = false; |
| 990 |
if (!(parent instanceof XSLOverride)) { |
| 991 |
bad = true; |
| 992 |
} else { |
| 993 |
NodeInfo grandParent = parent.getParent(); |
| 994 |
if (!(grandParent instanceof XSLUsePackage)) { |
| 995 |
bad = true; |
| 996 |
} else { |
| 997 |
SymbolicName modeName = mode.getSymbolicName(); |
| 998 |
Component.M usedMode = (Component.M)((XSLUsePackage) grandParent).getUsedPackage().getComponent(modeName); |
| 999 |
if (usedMode == null) { |
| 1000 |
bad = true; |
| 1001 |
} else if (usedMode.getVisibility() == Visibility.FINAL) { |
| 1002 |
bad = true; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
} |
| 1006 |
if (bad) { |
| 1007 |
template.compileError("A template rule cannot be added to a mode declared in a used package " + |
| 1008 |
"unless the xsl:template declaration appears within an xsl:override child of the appropriate xsl:use-package element", |
| 1009 |
"XTSE3050"); |
| 1010 |
return false; |
| 1011 |
} |
| 1012 |
} |
| 1013 |
return true; |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
private void checkForSchemaAwareness() { |
| 1022 |
Compilation compilation = getRootElement().getCompilation(); |
| 1023 |
if (!compilation.isSchemaAware() && |
| 1024 |
getConfiguration().isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XSLT)) { |
| 1025 |
for (ComponentDeclaration decl : topLevel) { |
| 1026 |
StyleElement node = decl.getSourceElement(); |
| 1027 |
if (node instanceof XSLImportSchema) { |
| 1028 |
compilation.setSchemaAware(true); |
| 1029 |
return; |
| 1030 |
} |
| 1031 |
} |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
public AccumulatorRegistry getAccumulatorManager() { |
| 1042 |
return accumulatorManager; |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
public void setAccumulatorManager(AccumulatorRegistry accumulatorManager) { |
| 1052 |
this.accumulatorManager = accumulatorManager; |
| 1053 |
stylesheetPackage.setAccumulatorRegistry(accumulatorManager); |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
protected void addNamespaceAlias(ComponentDeclaration node) { |
| 1058 |
namespaceAliasList.add(node); |
| 1059 |
numberOfAliases++; |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
protected NamespaceBinding getNamespaceAlias(String uri) { |
| 1072 |
return namespaceAliasMap.get(uri); |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
protected boolean isAliasResultNamespace(String uri) { |
| 1083 |
return aliasResultUriSet.contains(uri); |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
private void collectNamespaceAliases() { |
| 1091 |
namespaceAliasMap = new HashMap<>(numberOfAliases); |
| 1092 |
aliasResultUriSet = new HashSet<>(numberOfAliases); |
| 1093 |
HashSet<String> aliasesAtThisPrecedence = new HashSet<>(); |
| 1094 |
int currentPrecedence = -1; |
| 1095 |
|
| 1096 |
|
| 1097 |
for (int i = 0; i < numberOfAliases; i++) { |
| 1098 |
ComponentDeclaration decl = namespaceAliasList.get(i); |
| 1099 |
XSLNamespaceAlias xna = (XSLNamespaceAlias) decl.getSourceElement(); |
| 1100 |
String scode = xna.getStylesheetURI(); |
| 1101 |
NamespaceBinding resultBinding = xna.getResultNamespaceBinding(); |
| 1102 |
int prec = decl.getPrecedence(); |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
if (currentPrecedence != prec) { |
| 1108 |
currentPrecedence = prec; |
| 1109 |
aliasesAtThisPrecedence.clear(); |
| 1110 |
|
| 1111 |
} |
| 1112 |
if (aliasesAtThisPrecedence.contains(scode)) { |
| 1113 |
if (!namespaceAliasMap.get(scode).getURI().equals(resultBinding.getURI())) { |
| 1114 |
xna.compileError("More than one alias is defined for the same namespace", "XTSE0810"); |
| 1115 |
} |
| 1116 |
} |
| 1117 |
if (namespaceAliasMap.get(scode) == null) { |
| 1118 |
namespaceAliasMap.put(scode, resultBinding); |
| 1119 |
aliasResultUriSet.add(resultBinding.getURI()); |
| 1120 |
} |
| 1121 |
aliasesAtThisPrecedence.add(scode); |
| 1122 |
} |
| 1123 |
namespaceAliasList = null; |
| 1124 |
} |
| 1125 |
|
| 1126 |
protected boolean hasNamespaceAliases() { |
| 1127 |
return numberOfAliases > 0; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
public Properties gatherOutputProperties( StructuredQName formatQName) throws XPathException { |
| 1141 |
boolean found = formatQName == null; |
| 1142 |
Configuration config = getConfiguration(); |
| 1143 |
Properties details = new Properties(config.getDefaultSerializationProperties()); |
| 1144 |
HashMap<String, Integer> precedences = new HashMap<>(10); |
| 1145 |
for (int i = topLevel.size() - 1; i >= 0; i--) { |
| 1146 |
ComponentDeclaration decl = topLevel.get(i); |
| 1147 |
if (decl.getSourceElement() instanceof XSLOutput) { |
| 1148 |
XSLOutput xo = (XSLOutput) decl.getSourceElement(); |
| 1149 |
if (formatQName == null |
| 1150 |
? xo.getFormatQName() == null |
| 1151 |
: formatQName.equals(xo.getFormatQName())) { |
| 1152 |
found = true; |
| 1153 |
xo.gatherOutputProperties(details, precedences, decl.getPrecedence()); |
| 1154 |
} |
| 1155 |
} |
| 1156 |
} |
| 1157 |
if (!found) { |
| 1158 |
compileError("Requested output format " + formatQName.getDisplayName() + |
| 1159 |
" has not been defined", "XTDE1460"); |
| 1160 |
} |
| 1161 |
return details; |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
protected void compile(Compilation compilation) throws XPathException { |
| 1173 |
|
| 1174 |
try { |
| 1175 |
|
| 1176 |
net.sf.saxon.trans.Timer timer; |
| 1177 |
if (Compilation.TIMING) { |
| 1178 |
timer = new net.sf.saxon.trans.Timer(); |
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
Configuration config = getConfiguration(); |
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
XQueryFunctionLibrary queryFunctions = stylesheetPackage.getXQueryFunctionLibrary(); |
| 1187 |
Iterator qf = queryFunctions.getFunctionDefinitions(); |
| 1188 |
while (qf.hasNext()) { |
| 1189 |
XQueryFunction f = (XQueryFunction) qf.next(); |
| 1190 |
f.fixupReferences(); |
| 1191 |
} |
| 1192 |
|
| 1193 |
if (Compilation.TIMING) { |
| 1194 |
timer.report("fixup Query functions"); |
| 1195 |
} |
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
boolean allowImplicit = !getStylesheetPackage().isDeclaredModes(); |
| 1200 |
for (ComponentDeclaration decl : topLevel) { |
| 1201 |
StyleElement snode = decl.getSourceElement(); |
| 1202 |
if (snode instanceof XSLMode) { |
| 1203 |
getRuleManager().obtainMode(snode.getObjectName(), true); |
| 1204 |
} |
| 1205 |
if (allowImplicit) { |
| 1206 |
registerImplicitModes(snode, getRuleManager()); |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
for (ComponentDeclaration decl : topLevel) { |
| 1213 |
StyleElement snode = decl.getSourceElement(); |
| 1214 |
if (snode instanceof XSLTemplate) { |
| 1215 |
((XSLTemplate) snode).register(decl); |
| 1216 |
} |
| 1217 |
} |
| 1218 |
|
| 1219 |
if (Compilation.TIMING) { |
| 1220 |
timer.report("register templates"); |
| 1221 |
} |
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
adjustExposedVisibility(); |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
for (ComponentDeclaration decl : topLevel) { |
| 1232 |
StyleElement snode = decl.getSourceElement(); |
| 1233 |
if (!snode.isActionCompleted(StyleElement.ACTION_COMPILE)) { |
| 1234 |
snode.setActionCompleted(StyleElement.ACTION_COMPILE); |
| 1235 |
snode.compileDeclaration(compilation, decl); |
| 1236 |
} |
| 1237 |
} |
| 1238 |
|
| 1239 |
if (Compilation.TIMING) { |
| 1240 |
timer.report("compile top-level objects"); |
| 1241 |
} |
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
for (ComponentDeclaration decl : functionIndex.values()) { |
| 1247 |
StyleElement node = decl.getSourceElement(); |
| 1248 |
if (!node.isActionCompleted(StyleElement.ACTION_TYPECHECK)) { |
| 1249 |
node.setActionCompleted(StyleElement.ACTION_TYPECHECK); |
| 1250 |
if (node.getVisibility() != Visibility.ABSTRACT) { |
| 1251 |
((XSLFunction) node).getCompiledFunction().typeCheck(node.makeExpressionVisitor()); |
| 1252 |
} |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
if (Compilation.TIMING) { |
| 1257 |
timer.report("typeCheck functions"); |
| 1258 |
} |
| 1259 |
|
| 1260 |
if (compilation.getErrorCount() > 0) { |
| 1261 |
|
| 1262 |
return; |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
optimizeTopLevel(); |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
for (ComponentDeclaration decl : functionIndex.values()) { |
| 1273 |
StyleElement node = decl.getSourceElement(); |
| 1274 |
if (!node.isActionCompleted(StyleElement.ACTION_OPTIMIZE)) { |
| 1275 |
node.setActionCompleted(StyleElement.ACTION_OPTIMIZE); |
| 1276 |
((StylesheetComponent) node).optimize(decl); |
| 1277 |
} |
| 1278 |
} |
| 1279 |
|
| 1280 |
if (Compilation.TIMING) { |
| 1281 |
timer.report("optimize"); |
| 1282 |
} |
| 1283 |
|
| 1284 |
if (Compilation.TIMING) { |
| 1285 |
timer.report("miscellanea"); |
| 1286 |
} |
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
getDecimalFormatManager().checkConsistency(); |
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
getRuleManager().checkConsistency(); |
| 1295 |
getRuleManager().computeRankings(); |
| 1296 |
|
| 1297 |
|
| 1298 |
if (Compilation.TIMING) { |
| 1299 |
timer.report("build template rule tables"); |
| 1300 |
} |
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
ExecutableFunctionLibrary overriding = new ExecutableFunctionLibrary(config); |
| 1310 |
ExecutableFunctionLibrary underriding = new ExecutableFunctionLibrary(config); |
| 1311 |
|
| 1312 |
for (Component decl : stylesheetPackage.getComponentIndex().values()) { |
| 1313 |
Visibility vis = decl.getVisibility(); |
| 1314 |
if ( decl.getActor() instanceof UserFunction) { |
| 1315 |
UserFunction f = (UserFunction) decl.getActor(); |
| 1316 |
if (f.isOverrideExtensionFunction()) { |
| 1317 |
overriding.addFunction(f); |
| 1318 |
} else { |
| 1319 |
underriding.addFunction(f); |
| 1320 |
} |
| 1321 |
} |
| 1322 |
} |
| 1323 |
|
| 1324 |
getStylesheetPackage().setFunctionLibraryDetails(null, overriding, underriding); |
| 1325 |
|
| 1326 |
if (Compilation.TIMING) { |
| 1327 |
timer.report("build runtime function tables"); |
| 1328 |
timer.reportCumulative("total compile phase"); |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
for (ComponentDeclaration decl : topLevel) { |
| 1334 |
StyleElement inst = decl.getSourceElement(); |
| 1335 |
if (inst instanceof XSLTemplate) { |
| 1336 |
NamedTemplate proc = ((XSLTemplate) inst).getActor(); |
| 1337 |
if (proc != null && proc.getTemplateName() == null) { |
| 1338 |
proc.allocateAllBindingSlots(stylesheetPackage); |
| 1339 |
} |
| 1340 |
} |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
HashMap<SymbolicName, Component> componentIndex = stylesheetPackage.getComponentIndex(); |
| 1346 |
for (Component decl : componentIndex.values()) { |
| 1347 |
Actor proc = decl.getActor(); |
| 1348 |
if (proc != null) { |
| 1349 |
proc.allocateAllBindingSlots(stylesheetPackage); |
| 1350 |
} |
| 1351 |
} |
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
KeyManager keyMan = getKeyManager(); |
| 1356 |
for (KeyDefinitionSet keySet : keyMan.getAllKeyDefinitionSets()) { |
| 1357 |
for (KeyDefinition keyDef : keySet.getKeyDefinitions()) { |
| 1358 |
keyDef.makeDeclaringComponent(Visibility.PRIVATE, getStylesheetPackage()); |
| 1359 |
keyDef.allocateAllBindingSlots(stylesheetPackage); |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
AccumulatorRegistry accMan = getAccumulatorManager(); |
| 1366 |
if (accMan != null) { |
| 1367 |
for (Accumulator acc : accMan.getAllAccumulators()) { |
| 1368 |
acc.allocateAllBindingSlots(stylesheetPackage); |
| 1369 |
} |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
if (compilation.getCompilerInfo().isGenerateByteCode() && |
| 1375 |
!config.isDeferredByteCode(Configuration.XSLT)) { |
| 1376 |
if (Compilation.TIMING) { |
| 1377 |
config.getStandardErrorOutput().println("Generating byte code..."); |
| 1378 |
} |
| 1379 |
|
| 1380 |
Optimizer opt = config.obtainOptimizer(); |
| 1381 |
for (ComponentDeclaration decl : topLevel) { |
| 1382 |
StyleElement inst = decl.getSourceElement(); |
| 1383 |
if (inst instanceof StylesheetComponent) { |
| 1384 |
((StylesheetComponent) inst).generateByteCode(opt); |
| 1385 |
} |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
} catch (RuntimeException err) { |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
if (compilation.getErrorCount() == 0) { |
| 1394 |
|
| 1395 |
throw err; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
} |
| 1399 |
|
| 1400 |
private void registerImplicitModes(StyleElement element, RuleManager manager) { |
| 1401 |
if (element instanceof XSLApplyTemplates || element instanceof XSLTemplate) { |
| 1402 |
String modeAtt = element.getAttributeValue("mode"); |
| 1403 |
if (modeAtt != null) { |
| 1404 |
String[] tokens = Whitespace.trim(modeAtt).split("[ \t\n\r]+"); |
| 1405 |
for (String s : tokens) { |
| 1406 |
if (!s.startsWith("#")) { |
| 1407 |
try { |
| 1408 |
StructuredQName modeName = element.makeQName(s); |
| 1409 |
SymbolicName sName = new SymbolicName(StandardNames.XSL_MODE, modeName); |
| 1410 |
HashMap<SymbolicName, Component> componentIndex = getStylesheetPackage().getComponentIndex(); |
| 1411 |
Component existing = componentIndex.get(sName); |
| 1412 |
if (existing != null && existing.getDeclaringPackage() != getStylesheetPackage()) { |
| 1413 |
if (element instanceof XSLTemplate && !(element.getParent() instanceof XSLOverride)) { |
| 1414 |
element.compileError("A template rule cannot be added to a mode declared in a used package " + |
| 1415 |
"unless the xsl:template declaration appears within an xsl:override child of the appropriate xsl:use-package element", |
| 1416 |
"XTSE3050"); |
| 1417 |
} |
| 1418 |
} else { |
| 1419 |
manager.obtainMode(modeName, true); |
| 1420 |
} |
| 1421 |
} catch (XPathException e) { |
| 1422 |
|
| 1423 |
} |
| 1424 |
} |
| 1425 |
} |
| 1426 |
} |
| 1427 |
} |
| 1428 |
NodeInfo child; |
| 1429 |
AxisIterator kids = element.iterateAxis(AxisInfo.CHILD); |
| 1430 |
while ((child = kids.next()) != null) { |
| 1431 |
if (child instanceof StyleElement) { |
| 1432 |
registerImplicitModes((StyleElement) child, manager); |
| 1433 |
} |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
public void optimizeTopLevel() throws XPathException { |
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
for (ComponentDeclaration decl : topLevel) { |
| 1442 |
StyleElement node = decl.getSourceElement(); |
| 1443 |
if (node instanceof StylesheetComponent && !(node instanceof XSLFunction) && |
| 1444 |
!node.isActionCompleted(StyleElement.ACTION_OPTIMIZE)) { |
| 1445 |
node.setActionCompleted(StyleElement.ACTION_OPTIMIZE); |
| 1446 |
((StylesheetComponent) node).optimize(decl); |
| 1447 |
} |
| 1448 |
if (node instanceof XSLTemplate) { |
| 1449 |
((XSLTemplate) node).allocatePatternSlotNumbers(); |
| 1450 |
} |
| 1451 |
} |
| 1452 |
} |
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
protected boolean isImportedSchema(String targetNamespace) { |
| 1464 |
return stylesheetPackage.getSchemaNamespaces().contains(targetNamespace); |
| 1465 |
} |
| 1466 |
|
| 1467 |
protected void addImportedSchema(String targetNamespace) { |
| 1468 |
stylesheetPackage.getSchemaNamespaces().add(targetNamespace); |
| 1469 |
} |
| 1470 |
|
| 1471 |
protected Set<String> getImportedSchemaTable() { |
| 1472 |
return stylesheetPackage.getSchemaNamespaces(); |
| 1473 |
} |
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
public ComponentDeclaration getCharacterMap(StructuredQName name) { |
| 1484 |
for (int i = topLevel.size() - 1; i >= 0; i--) { |
| 1485 |
ComponentDeclaration decl = topLevel.get(i); |
| 1486 |
if (decl.getSourceElement() instanceof XSLCharacterMap) { |
| 1487 |
XSLCharacterMap t = (XSLCharacterMap) decl.getSourceElement(); |
| 1488 |
if (t.getCharacterMapName().equals(name)) { |
| 1489 |
return decl; |
| 1490 |
} |
| 1491 |
} |
| 1492 |
} |
| 1493 |
return null; |
| 1494 |
} |
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
public void adjustExposedVisibility() throws XPathException { |
| 1501 |
List<XSLExpose> exposeDeclarations = new ArrayList<>(); |
| 1502 |
for (ComponentDeclaration decl : topLevel) { |
| 1503 |
if (decl.getSourceElement() instanceof XSLExpose) { |
| 1504 |
exposeDeclarations.add(0, (XSLExpose) decl.getSourceElement()); |
| 1505 |
} |
| 1506 |
} |
| 1507 |
if (exposeDeclarations.isEmpty()) { |
| 1508 |
return; |
| 1509 |
} |
| 1510 |
|
| 1511 |
NamePool pool = getConfiguration().getNamePool(); |
| 1512 |
Map<SymbolicName, Component> componentIndex = stylesheetPackage.getComponentIndex(); |
| 1513 |
for (Component component : componentIndex.values()) { |
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
int fp = component.getComponentKind(); |
| 1529 |
ComponentTest exactNameTest = |
| 1530 |
new ComponentTest(fp, |
| 1531 |
new NameTest(Type.ELEMENT, new FingerprintedQName(component.getActor().getObjectName(), pool), pool), -1); |
| 1532 |
ComponentTest exactFunctionTest = null; |
| 1533 |
if (fp == StandardNames.XSL_FUNCTION) { |
| 1534 |
Function fn = (Function)component.getActor(); |
| 1535 |
exactFunctionTest = |
| 1536 |
new ComponentTest(fp, |
| 1537 |
new NameTest(Type.ELEMENT, |
| 1538 |
new FingerprintedQName(fn.getFunctionName(), pool), pool), fn.getArity()); |
| 1539 |
} |
| 1540 |
boolean matched = false; |
| 1541 |
for (XSLExpose exposure : exposeDeclarations) { |
| 1542 |
Set<ComponentTest> explicitComponentTests = exposure.getExplicitComponentTests(); |
| 1543 |
if (explicitComponentTests.contains(exactNameTest) || |
| 1544 |
(exactFunctionTest != null && explicitComponentTests.contains(exactFunctionTest))) { |
| 1545 |
component.setVisibility(exposure.getVisibility(), VisibilityProvenance.EXPOSED); |
| 1546 |
matched = true; |
| 1547 |
break; |
| 1548 |
} |
| 1549 |
} |
| 1550 |
if (!matched && component.getVisibilityProvenance() == VisibilityProvenance.DEFAULTED) { |
| 1551 |
|
| 1552 |
partialWildcardSearch: |
| 1553 |
for (XSLExpose exposure : exposeDeclarations) { |
| 1554 |
for (ComponentTest test : exposure.getWildcardComponentTests()) { |
| 1555 |
if (test.isPartialWildcard() && test.matches(component.getActor())) { |
| 1556 |
component.setVisibility(exposure.getVisibility(), VisibilityProvenance.EXPOSED); |
| 1557 |
matched = true; |
| 1558 |
break partialWildcardSearch; |
| 1559 |
} |
| 1560 |
} |
| 1561 |
} |
| 1562 |
if (!matched) { |
| 1563 |
anyWildcardSearch: |
| 1564 |
for (XSLExpose exposure : exposeDeclarations) { |
| 1565 |
for (ComponentTest test : exposure.getWildcardComponentTests()) { |
| 1566 |
if (test.matches(component.getActor())) { |
| 1567 |
component.setVisibility(exposure.getVisibility(), VisibilityProvenance.EXPOSED); |
| 1568 |
break anyWildcardSearch; |
| 1569 |
} |
| 1570 |
} |
| 1571 |
} |
| 1572 |
} |
| 1573 |
|
| 1574 |
} |
| 1575 |
|
| 1576 |
} |
| 1577 |
} |
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
protected void compileError(String message, String errorCode) throws XPathException { |
| 1588 |
XPathException tce = new XPathException(message, errorCode); |
| 1589 |
compileError(tce); |
| 1590 |
} |
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
protected void compileError(XPathException error) { |
| 1599 |
error.setIsStaticError(true); |
| 1600 |
getRootElement().compileError(error); |
| 1601 |
} |
| 1602 |
|
| 1603 |
protected void fixup() throws XPathException { |
| 1604 |
|
| 1605 |
for (Action a : fixupActions) { |
| 1606 |
a.doAction(); |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
protected void complete() throws XPathException { |
| 1611 |
stylesheetPackage.complete(); |
| 1612 |
} |
| 1613 |
|
| 1614 |
public SlotManager getSlotManager() { |
| 1615 |
return null; |
| 1616 |
} |
| 1617 |
|
| 1618 |
|
| 1619 |
@Override |
| 1620 |
public GlobalVariable getEquivalentVariable(Expression select) { |
| 1621 |
return null; |
| 1622 |
} |
| 1623 |
|
| 1624 |
@Override |
| 1625 |
public void addGlobalVariable(GlobalVariable variable) { |
| 1626 |
addGlobalVariable(variable, Visibility.PRIVATE); |
| 1627 |
} |
| 1628 |
|
| 1629 |
public void addGlobalVariable(GlobalVariable variable, Visibility visibility) { |
| 1630 |
Component component = variable.makeDeclaringComponent(visibility, getStylesheetPackage()); |
| 1631 |
if (variable.getPackageData() == null) { |
| 1632 |
variable.setPackageData(stylesheetPackage); |
| 1633 |
} |
| 1634 |
if (visibility == Visibility.HIDDEN) { |
| 1635 |
stylesheetPackage.addHiddenComponent(component); |
| 1636 |
} else { |
| 1637 |
stylesheetPackage.getComponentIndex().put( |
| 1638 |
new SymbolicName(StandardNames.XSL_VARIABLE, variable.getVariableQName()), component); |
| 1639 |
} |
| 1640 |
} |
| 1641 |
|
| 1642 |
|
| 1643 |
} |
| 1644 |
|