Debian Codegraph @

net/sf/saxon/PreparedStylesheet.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * This <b>PreparedStylesheet</b> class represents a Stylesheet that has been
32 * prepared for execution (or "compiled").
33 *
34 * <p>Note that the PreparedStylesheet object does not contain a reference to the source stylesheet
35 * tree (rooted at an XSLStyleSheet object). This allows the source tree to be garbage-collected
36 * when it is no longer required.</p>
37 *
38 * <p>The PreparedStylesheet in XSLT 3.0 represents the set of all packages making up an executable
39 * stylesheet.</p>
40 */
41
42 public class PreparedStylesheet extends Executable {
43
44 private HashMap<URI, PreparedStylesheet> nextStylesheetCache;
45 // cache for stylesheets named as "saxon:next-in-chain"
46
47 // definitions of template rules (XSLT only)
48 private RuleManager ruleManager;
49
50 // index of named templates.
51 private HashMap<StructuredQName, NamedTemplate> namedTemplateTable;
52
53 // Table of components declared in this package or imported from used packages. Key is the symbolic identifier of the
54 // component; value is the component itself. Hidden components are not included in this table because their names
55 // need not be unique, and because they are not available for reference by name.
56 // Currently used only for named templates
57 private Map<SymbolicName, Component> componentIndex;
58
59 // manager class for accumulator rules (XSLT 3.0 only)
60 // private AccumulatorManager accumulatorManager = null;
61
62 //private FunctionLibraryList stylesheetFunctions;
63
64 //public FunctionLibraryList getStylesheetFunctions() {
65 // return stylesheetFunctions;
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 * Constructor - deliberately protected
78 *
79 * @param compilation Compilation options
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 //setSchemaAware(compilerInfo.isSchemaAware());
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 * Make a Controller from this stylesheet object.
106 *
107 * @return the new Controller
108 * @see net.sf.saxon.Controller
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 // ignore the error if the default initial mode is not defined
122 }
123 }
124 return c;
125 }
126
127 /**
128 * Get the parameters that were set at compile time. These will generally be static parameters,
129 * but it is also permitted to set non-static parameters at compile time.
130 *
131 * @return the parameters that were set prior to XSLT compilation
132 */
133
134 public GlobalParameterSet getCompileTimeParams() {
135 return compileTimeParams;
136 }
137
138 /**
139 * Check that all required parameters have been supplied. Also checks that the supplied
140 * parameters dynamically do not conflict with parameters supplied statically. Used in XSLT only.
141 *
142 * @param params the set of parameters that have been supplied dynamically to the transformer
143 * (null represents an empty set).
144 * @throws XPathException if there is a required parameter for which no value has been supplied
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 * Set the RuleManager that handles template rules
183 *
184 * @param rm the RuleManager containing details of all the template rules
185 */
186
187 public void setRuleManager(RuleManager rm) {
188 ruleManager = rm;
189 }
190
191 /**
192 * Get the RuleManager which handles template rules
193 *
194 * @return the RuleManager registered with setRuleManager
195 */
196
197 public RuleManager getRuleManager() {
198 return ruleManager;
199 }
200
201 /**
202 * Get the named template with a given name.
203 *
204 * @param qName The template name
205 * @return The template (of highest import precedence) with this name if there is one;
206 * null if none is found.
207 */
208
209 /*@Nullable*/
210 public NamedTemplate getNamedTemplate(StructuredQName qName) {
211 if (namedTemplateTable == null) {
212 return null;
213 }
214 return namedTemplateTable.get(qName);
215 }
216
217 /**
218 * Register the named template with a given name
219 *
220 * @param templateName the name of a named XSLT template
221 * @param template the template
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 * Get the default initial template name
233 */
234
235 public StructuredQName getDefaultInitialTemplateName() {
236 return defaultInitialTemplate;
237 }
238
239 /**
240 * Register the index of components
241 *
242 * @param index the component index
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 * Ask whether a component is invokable from outside the stylesheet
255 *
256 * @param component the component
257 * @return true if the component can be referenced from the calling application
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 * Ask whether a mode is eligible for invoking from outside the stylesheet
277 *
278 * @param component the component
279 * @return true if the component can be referenced from the calling application
280 */
281
282 public boolean isEligibleInitialMode(Component.M component) {
283 if (component == null) {
284 return false;
285 }
286 // Rules 1 and 4
287 if (component.getVisibility() == Visibility.PUBLIC || component.getVisibility() == Visibility.FINAL) {
288 return true;
289 }
290 // Rule 2
291 if (component.getActor().isUnnamedMode()) {
292 return true;
293 }
294 // Rule 3
295 StylesheetPackage top = getTopLevelPackage();
296 if (component.getActor().getModeName().equals(top.getDefaultMode())) {
297 return true;
298 }
299 // Rule 5 (but see also bug 30405)
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 * Iterate over all the named templates defined in this Executable
310 *
311 * @return an iterator, the items returned being of class {@link NamedTemplate}
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 * Explain the expression tree for named templates in a stylesheet
325 *
326 * @param presenter destination for the explanatory output
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 * Get the properties for xsl:output, including character maps. The object returned will
348 * be a clone of the internal values, and thus it can be mutated
349 * without affecting the value held in this {@code PreparedStylesheet}.
350 * <p>This method gets the output properties for the unnamed output
351 * format in the stylesheet.</p>
352 *
353 * @return An OutputProperties object reflecting the output properties defined
354 * for the default (unnamed) output format in the stylesheet. It may
355 * be mutated and supplied to the setOutputProperties() method of the
356 * Transformer, without affecting other transformations that use the
357 * same stylesheet.
358 * @see javax.xml.transform.Transformer#setOutputProperties
359 */
360
361 public SerializationProperties getDeclaredSerializationProperties() {
362 SerializationProperties details = getPrimarySerializationProperties();
363 return new SerializationProperties(new Properties(details.getProperties()), getCharacterMapIndex());
364 }
365
366 /**
367 * Get a "next in chain" stylesheet. This method is intended for internal use.
368 *
369 * @param href the relative URI of the next-in-chain stylesheet
370 * @param baseURI the baseURI against which this relativeURI is to be resolved
371 * @return the cached stylesheet if present in the cache, or null if not
372 */
373
374 /*@Nullable*/
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 * Save a "next in chain" stylesheet in compiled form, so that it can be reused repeatedly.
391 * This method is intended for internal use.
392 *
393 * @param href the relative URI of the stylesheet
394 * @param baseURI the base URI against which the relative URI is resolved
395 * @param pss the prepared stylesheet object to be cached
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 * Produce an XML representation of the compiled and optimized stylesheet,
415 * as a human-readable entity
416 *
417 * @param presenter defines the destination and format of the output
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