Debian Codegraph @

net/sf/saxon/style/StyleNodeFactory.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.style;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.event.PipelineConfiguration;
12 import net.sf.saxon.expr.accum.AccumulatorRegistry;
13 import net.sf.saxon.expr.parser.Location;
14 import net.sf.saxon.lib.NamespaceConstant;
15 import net.sf.saxon.om.*;
16 import net.sf.saxon.trans.Err;
17 import net.sf.saxon.trans.SaxonErrorCode;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.tree.linked.ElementImpl;
20 import net.sf.saxon.tree.linked.NodeFactory;
21 import net.sf.saxon.tree.linked.NodeImpl;
22 import net.sf.saxon.tree.linked.TextImpl;
23 import net.sf.saxon.tree.util.AttributeCollectionImpl;
24 import net.sf.saxon.type.SchemaType;
25 import net.sf.saxon.type.Type;
26
27 import javax.xml.transform.TransformerException;
28 import javax.xml.transform.TransformerFactoryConfigurationError;
29
30 /**
31 * Class StyleNodeFactory. <br>
32 * A Factory for nodes in the stylesheet tree. <br>
33 * Currently only allows Element nodes to be user-constructed.
34 *
35 * @author Michael H. Kay
36 */
37
38 public class StyleNodeFactory implements NodeFactory {
39
40
41 protected Configuration config;
42 protected NamePool namePool;
43 private Compilation compilation;
44 private boolean topLevelModule;
45
46 /**
47 * Create the node factory for representing an XSLT stylesheet as a tree structure
48 *
49 * @param config the Saxon configuration
50 * @param compilation the compilation episode (compiling one package)
51 */
52
53 public StyleNodeFactory(Configuration config, Compilation compilation) {
54 this.config = config;
55 this.compilation = compilation;
56 namePool = config.getNamePool();
57 }
58
59 /**
60 * Say that this is the top-level module of a package
61 * @param topLevelModule true if this stylesheet module is the top level of a package; false
62 * if it is included or imported
63 */
64
65 public void setTopLevelModule(boolean topLevelModule) {
66 this.topLevelModule = topLevelModule;
67 }
68
69 /**
70 * Ask whether this is the top-level module of a package
71 * @return true if this stylesheet module is the top level of a package; false
72 * if it is included or imported
73 */
74
75 public boolean isTopLevelModule() {
76 return topLevelModule;
77 }
78
79 public Compilation getCompilation() {
80 return compilation;
81 }
82
83
84 public Configuration getConfiguration() {
85 return config;
86 }
87
88 /**
89 * Create an Element node. Note, if there is an error detected while constructing
90 * the Element, we add the element anyway, and return success, but flag the element
91 * with a validation error. This allows us to report more than
92 * one error from a single compilation.
93 */
94
95 public ElementImpl makeElementNode(
96 NodeInfo parent,
97 NodeName elemName,
98 SchemaType elemType,
99 boolean isNilled,
100 AttributeCollectionImpl attlist,
101 NamespaceBinding[] namespaces,
102 int namespacesUsed,
103 PipelineConfiguration pipe,
104 Location location,
105 int sequence) {
106 int f = elemName.obtainFingerprint(pipe.getConfiguration().getNamePool());
107 boolean toplevel = parent instanceof XSLModuleRoot;
108 String baseURI = null;
109 int lineNumber = -1;
110 int columnNumber = -1;
111 baseURI = location.getSystemId();
112 lineNumber = location.getLineNumber();
113 columnNumber = location.getColumnNumber();
114
115 if (parent instanceof DataElement) {
116 DataElement d = new DataElement();
117 d.setNamespaceDeclarations(namespaces, namespacesUsed);
118 d.initialise(elemName, elemType, attlist, parent, sequence);
119 d.setLocation(baseURI, lineNumber, columnNumber);
120 return d;
121 }
122
123 // Try first to make an XSLT element
124
125 StyleElement e = makeXSLElement(f, (NodeImpl)parent);
126 if ((e instanceof XSLStylesheet || e instanceof XSLPackage) && parent.getNodeKind() != Type.DOCUMENT) {
127 e = new AbsentExtensionElement();
128 final XPathException reason =
129 new XPathException(elemName.getDisplayName() + " can only appear at the outermost level", "XTSE0010");
130 e.setValidationError(reason, StyleElement.REPORT_ALWAYS);
131 }
132
133 if (e != null) { // recognized as an XSLT element
134
135 e.setCompilation(compilation);
136 e.setNamespaceDeclarations(namespaces, namespacesUsed);
137 e.initialise(elemName, elemType, attlist, parent, sequence);
138 e.setLocation(baseURI, lineNumber, columnNumber);
139 e.processExtensionElementAttribute("");
140 e.processExcludedNamespaces("");
141 e.processVersionAttribute("");
142 e.processDefaultXPathNamespaceAttribute("");
143 e.processExpandTextAttribute("");
144 e.processDefaultValidationAttribute("");
145
146 // if (e.isInstruction() && e.getEffectiveVersion() == 10 &&
147 // !config.isLicensedFeature(Configuration.LicenseFeature.PROFESSIONAL_EDITION)) {
148 // e.setValidationError(
149 // new XPathException("XSLT 1.0 compatibility mode is not available in this configuration", "XTDE0160"),
150 // StyleElement.REPORT_IF_INSTANTIATED
151 // );
152 // }
153
154 if (toplevel && !e.isDeclaration() && e.forwardsCompatibleModeIsEnabled()) {
155 DataElement d = new DataElement();
156 d.setNamespaceDeclarations(namespaces, namespacesUsed);
157 d.initialise(elemName, elemType, attlist, parent, sequence);
158 d.setLocation(baseURI, lineNumber, columnNumber);
159 return d;
160 }
161
162 if (parent instanceof AbsentExtensionElement &&
163 ((AbsentExtensionElement)parent).forwardsCompatibleModeIsEnabled() &&
164 parent.getURI().equals(NamespaceConstant.XSLT) &&
165 !(e instanceof XSLFallback)) {
166 // Parent is an unknown XSLT element in forwards-compatibility mode; siblings of xsl:fallback are ignored
167 AbsentExtensionElement temp = new AbsentExtensionElement();
168 temp.initialise(elemName, elemType, attlist, parent, sequence);
169 temp.setLocation(baseURI, lineNumber, columnNumber);
170 temp.setCompilation(compilation);
171 temp.setIgnoreInstruction();
172 return temp;
173 }
174 return e;
175
176 }
177
178 String uri = elemName.getURI();
179
180 if (toplevel && !uri.isEmpty() && !uri.equals(NamespaceConstant.XSLT)) {
181 DataElement d = new DataElement();
182 d.setNamespaceDeclarations(namespaces, namespacesUsed);
183 d.initialise(elemName, elemType, attlist, parent, sequence);
184 d.setLocation(baseURI, lineNumber, columnNumber);
185 return d;
186
187 } else { // not recognized as an XSLT element, not top-level
188
189 String localname = elemName.getLocalPart();
190 StyleElement temp = null;
191
192 // Detect a misspelt XSLT element, or a 3.0 element used in a 2.0 stylesheet
193
194 if (uri.equals(NamespaceConstant.XSLT)) {
195 if (parent instanceof XSLStylesheet) {
196 if (((XSLStylesheet) parent).getEffectiveVersion() <= 20) {
197 temp = new AbsentExtensionElement();
198 temp.setCompilation(compilation);
199 temp.setValidationError(new XPathException("Unknown top-level XSLT declaration"),
200 StyleElement.REPORT_UNLESS_FORWARDS_COMPATIBLE);
201 }
202 } else {
203 temp = new AbsentExtensionElement();
204 temp.initialise(elemName, elemType, attlist, parent, sequence);
205 temp.setLocation(baseURI, lineNumber, columnNumber);
206 temp.setCompilation(compilation);
207 temp.processStandardAttributes("");
208 if (temp.getEffectiveVersion() > 20) {
209 temp.setValidationError(new XPathException("Unknown XSLT instruction"),
210 StyleElement.REPORT_STATICALLY_UNLESS_FALLBACK_AVAILABLE);
211 } else {
212 temp.setValidationError(new XPathException("Unknown XSLT instruction"),
213 StyleElement.REPORT_IF_INSTANTIATED);
214 }
215 }
216 }
217
218 // Detect an unrecognized element in the Saxon namespace
219
220 if (uri.equals(NamespaceConstant.SAXON)) {
221 String message = elemName.getDisplayName() + " is not recognized as a Saxon instruction";
222 if (config.getEditionCode().equals("HE")) {
223 message += ". Saxon extensions require Saxon-PE or higher";
224 } else if (!config.isLicensedFeature(Configuration.LicenseFeature.PROFESSIONAL_EDITION)) {
225 message += ". No Saxon-PE or -EE license was found";
226 }
227 XPathException te = new XPathException(message);
228 te.setLocator(location.saveLocation());
229 te.setErrorCode(SaxonErrorCode.SXWN9008);
230 pipe.getErrorListener().warning(te);
231 }
232
233 Class assumedClass = LiteralResultElement.class;
234
235 // We can't work out the final class of the node until we've examined its attributes
236 // such as version and extension-element-prefixes; but we can have a good guess, and
237 // change it later if need be.
238
239 if (temp == null) {
240 temp = new LiteralResultElement();
241 }
242
243 temp.setNamespaceDeclarations(namespaces, namespacesUsed);
244 temp.setCompilation(compilation);
245 temp.initialise(elemName, elemType, attlist, parent, sequence);
246 temp.setLocation(baseURI, lineNumber, columnNumber);
247 temp.processStandardAttributes(NamespaceConstant.XSLT);
248
249 // Now we work out what class of element we really wanted, and change it if necessary
250
251 TransformerException reason;
252 Class actualClass;
253
254 if (uri.equals(NamespaceConstant.XSLT)) {
255 reason = new XPathException("Unknown XSLT element: " + Err.wrap(localname, Err.ELEMENT));
256 ((XPathException) reason).setErrorCode("XTSE0010");
257 ((XPathException) reason).setIsStaticError(true);
258 actualClass = AbsentExtensionElement.class;
259 temp.setValidationError(reason, StyleElement.REPORT_STATICALLY_UNLESS_FALLBACK_AVAILABLE);
260
261 } else if (temp.isExtensionNamespace(uri) && !toplevel) {
262
263 // if we can't instantiate an extension element, we don't give up
264 // immediately, because there might be an xsl:fallback defined. We
265 // create a surrogate element called AbsentExtensionElement, and
266 // save the reason for failure just in case there is no xsl:fallback
267
268 actualClass = AbsentExtensionElement.class;
269 if (NamespaceConstant.isReserved(uri)) {
270 reason = new XPathException("Cannot use a reserved namespace for extension instructions", "XTSE0800");
271 temp.setValidationError(reason, StyleElement.REPORT_ALWAYS);
272 } else {
273 reason = new XPathException("Unknown extension instruction " + Err.wrap(elemName.getDisplayName(), Err.ELEMENT), "XTDE1450", temp);
274 temp.setValidationError(reason, StyleElement.REPORT_DYNAMICALLY_UNLESS_FALLBACK_AVAILABLE);
275 }
276
277 } else {
278 actualClass = LiteralResultElement.class;
279 }
280
281 StyleElement node;
282 if (actualClass.equals(assumedClass)) {
283 node = temp; // the original element will do the job
284 } else {
285 try {
286 node = (StyleElement) actualClass.newInstance();
287 } catch (InstantiationException err1) {
288 throw new TransformerFactoryConfigurationError(err1, "Failed to create instance of " + actualClass.getName());
289 } catch (IllegalAccessException err2) {
290 throw new TransformerFactoryConfigurationError(err2, "Failed to access class " + actualClass.getName());
291 }
292 node.substituteFor(temp); // replace temporary node with the new one
293 }
294 return node;
295 }
296 }
297
298 /**
299 * Make an XSL element node
300 *
301 * @param f the fingerprint of the node name
302 * @param parent the parent node
303 * @return the constructed element node
304 */
305
306 /*@Nullable*/
307 protected StyleElement makeXSLElement(int f, NodeImpl parent) {
308 switch (f) {
309 case StandardNames.XSL_ACCEPT:
310 return new XSLAccept();
311 case StandardNames.XSL_ACCUMULATOR:
312 return new XSLAccumulator();
313 case StandardNames.XSL_ACCUMULATOR_RULE:
314 return new XSLAccumulatorRule();
315 case StandardNames.XSL_ANALYZE_STRING:
316 return new XSLAnalyzeString();
317 case StandardNames.XSL_APPLY_IMPORTS:
318 return new XSLApplyImports();
319 case StandardNames.XSL_APPLY_TEMPLATES:
320 return new XSLApplyTemplates();
321 case StandardNames.XSL_ASSERT:
322 return new XSLAssert();
323 case StandardNames.XSL_ATTRIBUTE:
324 return new XSLAttribute();
325 case StandardNames.XSL_ATTRIBUTE_SET:
326 return new XSLAttributeSet();
327 case StandardNames.XSL_BREAK:
328 return new XSLBreak();
329 case StandardNames.XSL_CALL_TEMPLATE:
330 return new XSLCallTemplate();
331 case StandardNames.XSL_CATCH:
332 return new XSLCatch();
333 case StandardNames.XSL_CONTEXT_ITEM:
334 return new XSLContextItem();
335 case StandardNames.XSL_CHARACTER_MAP:
336 return new XSLCharacterMap();
337 case StandardNames.XSL_CHOOSE:
338 return new XSLChoose();
339 case StandardNames.XSL_COMMENT:
340 return new XSLComment();
341 case StandardNames.XSL_COPY:
342 return new XSLCopy();
343 case StandardNames.XSL_COPY_OF:
344 return new XSLCopyOf();
345 case StandardNames.XSL_DECIMAL_FORMAT:
346 return new XSLDecimalFormat();
347 case StandardNames.XSL_DOCUMENT:
348 return new XSLDocument();
349 case StandardNames.XSL_ELEMENT:
350 return new XSLElement();
351 case StandardNames.XSL_EVALUATE:
352 return new XSLEvaluate();
353 case StandardNames.XSL_EXPOSE:
354 return new XSLExpose();
355 case StandardNames.XSL_FALLBACK:
356 return new XSLFallback();
357 case StandardNames.XSL_FOR_EACH:
358 return new XSLForEach();
359 case StandardNames.XSL_FOR_EACH_GROUP:
360 return new XSLForEachGroup();
361 case StandardNames.XSL_FORK:
362 return new XSLFork();
363 case StandardNames.XSL_FUNCTION:
364 return new XSLFunction();
365 case StandardNames.XSL_GLOBAL_CONTEXT_ITEM:
366 return new XSLGlobalContextItem();
367 case StandardNames.XSL_IF:
368 return new XSLIf();
369 case StandardNames.XSL_IMPORT:
370 return new XSLImport();
371 case StandardNames.XSL_IMPORT_SCHEMA:
372 return new XSLImportSchema();
373 case StandardNames.XSL_INCLUDE:
374 return new XSLInclude();
375 case StandardNames.XSL_ITERATE:
376 return new XSLIterate();
377 case StandardNames.XSL_KEY:
378 return new XSLKey();
379 case StandardNames.XSL_MAP:
380 return new XSLMap();
381 case StandardNames.XSL_MAP_ENTRY:
382 return new XSLMapEntry();
383 case StandardNames.XSL_MATCHING_SUBSTRING:
384 return new XSLMatchingSubstring();
385 case StandardNames.XSL_MERGE:
386 return new XSLMerge();
387 case StandardNames.XSL_MERGE_ACTION:
388 return new XSLMergeAction();
389 case StandardNames.XSL_MERGE_KEY:
390 return new XSLMergeKey();
391 case StandardNames.XSL_MERGE_SOURCE:
392 return new XSLMergeSource();
393 case StandardNames.XSL_MESSAGE:
394 return new XSLMessage();
395 case StandardNames.XSL_MODE:
396 return new XSLMode();
397 case StandardNames.XSL_NEXT_ITERATION:
398 return new XSLNextIteration();
399 case StandardNames.XSL_NEXT_MATCH:
400 return new XSLNextMatch();
401 case StandardNames.XSL_NON_MATCHING_SUBSTRING:
402 return new XSLMatchingSubstring(); //sic
403 case StandardNames.XSL_NUMBER:
404 return new XSLNumber();
405 case StandardNames.XSL_NAMESPACE:
406 return new XSLNamespace();
407 case StandardNames.XSL_NAMESPACE_ALIAS:
408 return new XSLNamespaceAlias();
409 case StandardNames.XSL_ON_COMPLETION:
410 return new XSLOnCompletion();
411 case StandardNames.XSL_ON_EMPTY:
412 return new XSLOnEmpty();
413 case StandardNames.XSL_ON_NON_EMPTY:
414 return new XSLOnNonEmpty();
415 case StandardNames.XSL_OTHERWISE:
416 return new XSLOtherwise();
417 case StandardNames.XSL_OUTPUT:
418 return new XSLOutput();
419 case StandardNames.XSL_OUTPUT_CHARACTER:
420 return new XSLOutputCharacter();
421 case StandardNames.XSL_OVERRIDE:
422 return new XSLOverride();
423 case StandardNames.XSL_PACKAGE:
424 return new XSLPackage();
425 case StandardNames.XSL_PARAM:
426 return parent instanceof XSLModuleRoot || parent instanceof XSLOverride ? new XSLGlobalParam() : new XSLLocalParam();
427 case StandardNames.XSL_PERFORM_SORT:
428 return new XSLPerformSort();
429 case StandardNames.XSL_PRESERVE_SPACE:
430 return new XSLPreserveSpace();
431 case StandardNames.XSL_PROCESSING_INSTRUCTION:
432 return new XSLProcessingInstruction();
433 case StandardNames.XSL_RESULT_DOCUMENT:
434 compilation.setCreatesSecondaryResultDocuments(true);
435 return new XSLResultDocument();
436 case StandardNames.XSL_SEQUENCE:
437 return new XSLSequence();
438 case StandardNames.XSL_SORT:
439 return new XSLSort();
440 case StandardNames.XSL_SOURCE_DOCUMENT:
441 return new XSLSourceDocument();
442 case StandardNames.XSL_STRIP_SPACE:
443 return new XSLPreserveSpace();
444 case StandardNames.XSL_STYLESHEET:
445 return topLevelModule ? new XSLPackage() : new XSLStylesheet();
446 case StandardNames.XSL_TEMPLATE:
447 return new XSLTemplate();
448 case StandardNames.XSL_TEXT:
449 return new XSLText();
450 case StandardNames.XSL_TRANSFORM:
451 return topLevelModule ? new XSLPackage() : new XSLStylesheet();
452 case StandardNames.XSL_TRY:
453 return new XSLTry();
454 case StandardNames.XSL_USE_PACKAGE:
455 return new XSLUsePackage();
456 case StandardNames.XSL_VALUE_OF:
457 return new XSLValueOf();
458 case StandardNames.XSL_VARIABLE:
459 return parent instanceof XSLModuleRoot || parent instanceof XSLOverride ? new XSLGlobalVariable() : new XSLLocalVariable();
460 case StandardNames.XSL_WITH_PARAM:
461 return new XSLWithParam();
462 case StandardNames.XSL_WHEN:
463 return new XSLWhen();
464 case StandardNames.XSL_WHERE_POPULATED:
465 return new XSLWherePopulated();
466 default:
467 return null;
468 }
469 }
470
471 /**
472 * Make a text node
473 *
474 * @param parent the parent element
475 * @param content the content of the text node
476 * @return the constructed text node
477 */
478 public TextImpl makeTextNode(NodeInfo parent, CharSequence content) {
479 if (parent instanceof StyleElement && ((StyleElement) parent).isExpandingText()) {
480 return new TextValueTemplateNode(content.toString());
481 } else {
482 return new TextImpl(content.toString());
483 }
484 }
485
486 /**
487 * Method to support the element-available() function
488 *
489 *
490 * @param uri the namespace URI
491 * @param localName the local Name
492 * @param instructionsOnly true if only instruction elements qualify
493 * @return true if an extension element of this name is recognized
494 */
495
496 public boolean isElementAvailable(String uri, String localName, boolean instructionsOnly) {
497 int fingerprint = namePool.getFingerprint(uri, localName);
498 if (uri.equals(NamespaceConstant.XSLT)) {
499 if (fingerprint == -1) {
500 return false; // all names are pre-registered
501 }
502 StyleElement e = makeXSLElement(fingerprint, null);
503 if (e != null) {
504 return !instructionsOnly || e.isInstruction();
505 }
506 }
507 return false;
508 }
509
510 public AccumulatorRegistry makeAccumulatorManager() {
511 return new AccumulatorRegistry();
512 }
513
514 /**
515 * Create a stylesheet package
516 * @param node the XSLPackage element
517 * @return a new stylesheet package
518 */
519
520 public PrincipalStylesheetModule newPrincipalModule(XSLPackage node) throws XPathException {
521 return new PrincipalStylesheetModule(node);
522 }
523
524 }
525