| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Component; |
| 11 |
import net.sf.saxon.expr.accum.Accumulator; |
| 12 |
import net.sf.saxon.expr.instruct.*; |
| 13 |
import net.sf.saxon.functions.ExecutableFunctionLibrary; |
| 14 |
import net.sf.saxon.functions.FunctionLibrary; |
| 15 |
import net.sf.saxon.functions.FunctionLibraryList; |
| 16 |
import net.sf.saxon.lib.NamespaceConstant; |
| 17 |
import net.sf.saxon.lib.OutputURIResolver; |
| 18 |
import net.sf.saxon.om.StructuredQName; |
| 19 |
import net.sf.saxon.serialize.SerializationProperties; |
| 20 |
import net.sf.saxon.style.Compilation; |
| 21 |
import net.sf.saxon.style.StylesheetPackage; |
| 22 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 23 |
import net.sf.saxon.trans.*; |
| 24 |
import net.sf.saxon.trans.rules.RuleManager; |
| 25 |
|
| 26 |
import java.net.URI; |
| 27 |
import java.net.URISyntaxException; |
| 28 |
import java.util.*; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class PreparedStylesheet extends Executable { |
| 43 |
|
| 44 |
private HashMap<URI, PreparedStylesheet> nextStylesheetCache; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private RuleManager ruleManager; |
| 49 |
|
| 50 |
|
| 51 |
private HashMap<StructuredQName, NamedTemplate> namedTemplateTable; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private Map<SymbolicName, Component> componentIndex; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
private StructuredQName defaultInitialTemplate; |
| 69 |
private StructuredQName defaultInitialMode; |
| 70 |
private int recoveryPolicy; |
| 71 |
private String messageReceiverClassName; |
| 72 |
private OutputURIResolver outputURIResolver; |
| 73 |
private GlobalParameterSet compileTimeParams; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public PreparedStylesheet(Compilation compilation) { |
| 83 |
super(compilation.getConfiguration()); |
| 84 |
CompilerInfo compilerInfo = compilation.getCompilerInfo(); |
| 85 |
setHostLanguage(Configuration.XSLT); |
| 86 |
if (compilerInfo.isSchemaAware()) { |
| 87 |
int localLic = compilation.getPackageData().getLocalLicenseId(); |
| 88 |
getConfiguration().checkLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XSLT, "schema-aware XSLT", localLic); |
| 89 |
schemaAware = true; |
| 90 |
} |
| 91 |
|
| 92 |
if (compilerInfo.getErrorListener() == null) { |
| 93 |
compilerInfo.setErrorListener(getConfiguration().getErrorListener()); |
| 94 |
} |
| 95 |
defaultInitialMode = compilerInfo.getDefaultInitialMode(); |
| 96 |
defaultInitialTemplate = compilerInfo.getDefaultInitialTemplate(); |
| 97 |
recoveryPolicy = compilerInfo.getRecoveryPolicy(); |
| 98 |
messageReceiverClassName = compilerInfo.getMessageReceiverClassName(); |
| 99 |
outputURIResolver = compilerInfo.getOutputURIResolver(); |
| 100 |
compileTimeParams = compilation.getParameters(); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public XsltController newController() { |
| 112 |
Configuration config = getConfiguration(); |
| 113 |
XsltController c = new XsltController(config, this); |
| 114 |
c.setMessageReceiverClassName(messageReceiverClassName); |
| 115 |
c.setOutputURIResolver(outputURIResolver); |
| 116 |
c.setRecoveryPolicy(recoveryPolicy); |
| 117 |
if (defaultInitialMode != null) { |
| 118 |
try { |
| 119 |
c.setInitialMode(defaultInitialMode); |
| 120 |
} catch (XPathException e) { |
| 121 |
|
| 122 |
} |
| 123 |
} |
| 124 |
return c; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public GlobalParameterSet getCompileTimeParams() { |
| 135 |
return compileTimeParams; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
@Override |
| 148 |
public void checkSuppliedParameters(GlobalParameterSet params) throws XPathException { |
| 149 |
for (Map.Entry<StructuredQName, GlobalParam> entry : getGlobalParameters().entrySet()) { |
| 150 |
if (entry.getValue().isRequiredParam()) { |
| 151 |
StructuredQName req = entry.getKey(); |
| 152 |
if (getCompileTimeParams().get(req) == null && (params == null || params.get(req) == null)) { |
| 153 |
XPathException err = new XPathException("No value supplied for required parameter " + |
| 154 |
req.getDisplayName()); |
| 155 |
err.setErrorCode(getHostLanguage() == Configuration.XQUERY ? "XPDY0002" : "XTDE0050"); |
| 156 |
throw err; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
for (StructuredQName name : params.getKeys()) { |
| 161 |
GlobalParam decl = getGlobalParameter(name); |
| 162 |
if (decl != null && decl.isStatic()) { |
| 163 |
throw new XPathException("Parameter $" + name.getDisplayName() + |
| 164 |
" cannot be supplied dynamically because it is declared as static"); |
| 165 |
} |
| 166 |
if (compileTimeParams.containsKey(name)) { |
| 167 |
throw new XPathException("Parameter $" + name.getDisplayName() + |
| 168 |
" cannot be supplied dynamically because a value was already supplied at compile time"); |
| 169 |
} |
| 170 |
} |
| 171 |
for (StructuredQName name : compileTimeParams.getKeys()) { |
| 172 |
params.put(name, compileTimeParams.get(name)); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public StylesheetPackage getTopLevelPackage() { |
| 178 |
return (StylesheetPackage)super.getTopLevelPackage(); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public void setRuleManager(RuleManager rm) { |
| 188 |
ruleManager = rm; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public RuleManager getRuleManager() { |
| 198 |
return ruleManager; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public NamedTemplate getNamedTemplate(StructuredQName qName) { |
| 211 |
if (namedTemplateTable == null) { |
| 212 |
return null; |
| 213 |
} |
| 214 |
return namedTemplateTable.get(qName); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public void putNamedTemplate(StructuredQName templateName, NamedTemplate template) { |
| 225 |
if (namedTemplateTable == null) { |
| 226 |
namedTemplateTable = new HashMap<>(32); |
| 227 |
} |
| 228 |
namedTemplateTable.put(templateName, template); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public StructuredQName getDefaultInitialTemplateName() { |
| 236 |
return defaultInitialTemplate; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public void setComponentIndex(Map<SymbolicName, Component> index) { |
| 246 |
componentIndex = index; |
| 247 |
} |
| 248 |
|
| 249 |
public Component getComponent(SymbolicName name) { |
| 250 |
return componentIndex.get(name); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public boolean isEligibleInitialComponent(Component component) { |
| 261 |
if (component == null) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
if (component.getVisibility() == Visibility.PUBLIC || component.getVisibility() == Visibility.FINAL) { |
| 265 |
return true; |
| 266 |
} |
| 267 |
StylesheetPackage top = getTopLevelPackage(); |
| 268 |
if (top.isImplicitPackage() && component.getActor().getDeclaredVisibility() == null) { |
| 269 |
return true; |
| 270 |
} |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public boolean isEligibleInitialMode(Component.M component) { |
| 283 |
if (component == null) { |
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
if (component.getVisibility() == Visibility.PUBLIC || component.getVisibility() == Visibility.FINAL) { |
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
if (component.getActor().isUnnamedMode()) { |
| 292 |
return true; |
| 293 |
} |
| 294 |
|
| 295 |
StylesheetPackage top = getTopLevelPackage(); |
| 296 |
if (component.getActor().getModeName().equals(top.getDefaultMode())) { |
| 297 |
return true; |
| 298 |
} |
| 299 |
|
| 300 |
if (!top.isDeclaredModes() && !component.getActor().isEmpty() && |
| 301 |
(component.getVisibilityProvenance() == VisibilityProvenance.DEFAULTED || component.getVisibility() != Visibility.PRIVATE)) { |
| 302 |
return true; |
| 303 |
} |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public Iterator<NamedTemplate> iterateNamedTemplates() { |
| 315 |
if (namedTemplateTable == null) { |
| 316 |
List<NamedTemplate> list = Collections.emptyList(); |
| 317 |
return list.iterator(); |
| 318 |
} else { |
| 319 |
return namedTemplateTable.values().iterator(); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public void explainNamedTemplates(ExpressionPresenter presenter) throws XPathException { |
| 330 |
presenter.startElement("namedTemplates"); |
| 331 |
if (namedTemplateTable != null) { |
| 332 |
for (NamedTemplate t : namedTemplateTable.values()) { |
| 333 |
presenter.startElement("template"); |
| 334 |
presenter.emitAttribute("name", t.getTemplateName().getDisplayName()); |
| 335 |
presenter.emitAttribute("line", t.getLineNumber() + ""); |
| 336 |
presenter.emitAttribute("module", t.getSystemId()); |
| 337 |
if (t.getBody() != null) { |
| 338 |
t.getBody().export(presenter); |
| 339 |
} |
| 340 |
presenter.endElement(); |
| 341 |
} |
| 342 |
} |
| 343 |
presenter.endElement(); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public SerializationProperties getDeclaredSerializationProperties() { |
| 362 |
SerializationProperties details = getPrimarySerializationProperties(); |
| 363 |
return new SerializationProperties(new Properties(details.getProperties()), getCharacterMapIndex()); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public PreparedStylesheet getCachedStylesheet(String href, String baseURI) { |
| 376 |
URI abs = null; |
| 377 |
try { |
| 378 |
abs = new URI(baseURI).resolve(href); |
| 379 |
} catch (URISyntaxException err) { |
| 380 |
|
| 381 |
} |
| 382 |
PreparedStylesheet result = null; |
| 383 |
if (abs != null && nextStylesheetCache != null) { |
| 384 |
result = nextStylesheetCache.get(abs); |
| 385 |
} |
| 386 |
return result; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public void putCachedStylesheet(String href, String baseURI, PreparedStylesheet pss) { |
| 399 |
URI abs = null; |
| 400 |
try { |
| 401 |
abs = new URI(baseURI).resolve(href); |
| 402 |
} catch (URISyntaxException err) { |
| 403 |
|
| 404 |
} |
| 405 |
if (abs != null) { |
| 406 |
if (nextStylesheetCache == null) { |
| 407 |
nextStylesheetCache = new HashMap<>(4); |
| 408 |
} |
| 409 |
nextStylesheetCache.put(abs, pss); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public void explain(ExpressionPresenter presenter) throws XPathException { |
| 421 |
presenter.startElement("stylesheet"); |
| 422 |
presenter.namespace("fn", NamespaceConstant.FN); |
| 423 |
presenter.namespace("xs", NamespaceConstant.SCHEMA); |
| 424 |
explainGlobalVariables(presenter); |
| 425 |
ruleManager.explainTemplateRules(presenter); |
| 426 |
explainNamedTemplates(presenter); |
| 427 |
presenter.startElement("accumulators"); |
| 428 |
for (Accumulator acc : getTopLevelPackage().getAccumulatorRegistry().getAllAccumulators()) { |
| 429 |
acc.export(presenter); |
| 430 |
} |
| 431 |
presenter.endElement(); |
| 432 |
FunctionLibraryList libList = getFunctionLibrary(); |
| 433 |
List<FunctionLibrary> libraryList = libList.getLibraryList(); |
| 434 |
presenter.startElement("functions"); |
| 435 |
for (FunctionLibrary lib : libraryList) { |
| 436 |
if (lib instanceof ExecutableFunctionLibrary) { |
| 437 |
for (Iterator f = ((ExecutableFunctionLibrary) lib).iterateFunctions(); f.hasNext(); ) { |
| 438 |
UserFunction func = (UserFunction) f.next(); |
| 439 |
func.export(presenter); |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
presenter.endElement(); |
| 444 |
presenter.endElement(); |
| 445 |
presenter.close(); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
} |
| 450 |
|