Debian Codegraph @

net/sf/saxon/jaxp/TemplatesHandlerImpl.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.jaxp;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.event.CommentStripper;
12 import net.sf.saxon.event.ReceivingContentHandler;
13 import net.sf.saxon.event.StartTagBuffer;
14 import net.sf.saxon.event.Stripper;
15 import net.sf.saxon.om.StylesheetSpaceStrippingRule;
16 import net.sf.saxon.s9api.Processor;
17 import net.sf.saxon.s9api.SaxonApiException;
18 import net.sf.saxon.s9api.XsltCompiler;
19 import net.sf.saxon.style.Compilation;
20 import net.sf.saxon.style.StyleNodeFactory;
21 import net.sf.saxon.style.UseWhenFilter;
22 import net.sf.saxon.style.XSLModuleRoot;
23 import net.sf.saxon.trans.CompilerInfo;
24 import net.sf.saxon.tree.linked.DocumentImpl;
25 import net.sf.saxon.tree.linked.ElementImpl;
26 import net.sf.saxon.tree.linked.LinkedTreeBuilder;
27 import net.sf.saxon.value.NestedIntegerValue;
28 import org.xml.sax.Locator;
29
30 import javax.xml.transform.Templates;
31 import javax.xml.transform.sax.TemplatesHandler;
32
33
34 /**
35 * <b>TemplatesHandlerImpl</b> implements the javax.xml.transform.sax.TemplatesHandler
36 * interface. It acts as a ContentHandler which receives a stream of
37 * SAX events representing a stylesheet, and returns a Templates object that
38 * represents the compiled form of this stylesheet.
39 *
40 * @author Michael H. Kay
41 */
42
43 public class TemplatesHandlerImpl extends ReceivingContentHandler implements TemplatesHandler {
44
45 private Processor processor;
46 private LinkedTreeBuilder builder;
47 private StyleNodeFactory nodeFactory;
48 private Templates templates;
49 private String systemId;
50
51 /**
52 * Create a TemplatesHandlerImpl and initialise variables. The constructor is protected, because
53 * the Filter should be created using newTemplatesHandler() in the SAXTransformerFactory
54 * class
55 *
56 * @param processor the Saxon s9api processor
57 */
58
59 protected TemplatesHandlerImpl(Processor processor) {
60
61 this.processor = processor;
62 Configuration config = processor.getUnderlyingConfiguration();
63 setPipelineConfiguration(config.makePipelineConfiguration());
64
65 CompilerInfo info = new CompilerInfo(config.getDefaultXsltCompilerInfo());
66 Compilation compilation = new Compilation(config, info);
67 nodeFactory = compilation.getStyleNodeFactory(true);
68
69 builder = new LinkedTreeBuilder(getPipelineConfiguration());
70 builder.setNodeFactory(nodeFactory);
71 builder.setLineNumbering(true);
72
73 UseWhenFilter useWhenFilter = new UseWhenFilter(compilation, builder, NestedIntegerValue.TWO);
74
75 StartTagBuffer startTagBuffer = new StartTagBuffer(useWhenFilter);
76 useWhenFilter.setStartTagBuffer(startTagBuffer);
77
78 StylesheetSpaceStrippingRule rule = new StylesheetSpaceStrippingRule(config.getNamePool());
79 Stripper styleStripper = new Stripper(rule, startTagBuffer);
80 CommentStripper commentStripper = new CommentStripper(styleStripper);
81 setReceiver(commentStripper);
82
83 }
84
85 /**
86 * Get the Templates object to be used for a transformation
87 */
88
89 /*@Nullable*/
90 public Templates getTemplates() {
91 if (templates == null) {
92 DocumentImpl doc = (DocumentImpl) builder.getCurrentRoot();
93 if (doc == null) {
94 return null;
95 }
96 ElementImpl top = doc.getDocumentElement();
97 if (!(top instanceof XSLModuleRoot)) {
98 throw new IllegalStateException("Input is not a stylesheet");
99 }
100 builder.reset();
101
102 try {
103 XsltCompiler compiler = processor.newXsltCompiler();
104 templates = new TemplatesImpl(compiler.compile(doc));
105 } catch (SaxonApiException tce) {
106 // don't know why we aren't allowed to just throw it!
107 throw new IllegalStateException(tce.getMessage());
108 }
109 }
110
111 return templates;
112 }
113
114 /**
115 * Set the SystemId of the document. Note that if this method is called, any locator supplied
116 * to the setDocumentLocator() method is ignored. This also means that no line number information
117 * will be available.
118 *
119 * @param url the system ID (base URI) of the stylesheet document, which will be used in any error
120 * reporting and also for resolving relative URIs in xsl:include and xsl:import. It will also form
121 * the static base URI in the static context of XPath expressions.
122 */
123
124 public void setSystemId(String url) {
125 systemId = url;
126 builder.setSystemId(url);
127 super.setDocumentLocator(new Locator() {
128 public int getColumnNumber() {
129 return -1;
130 }
131
132 public int getLineNumber() {
133 return -1;
134 }
135
136 /*@Nullable*/
137 public String getPublicId() {
138 return null;
139 }
140
141 public String getSystemId() {
142 return systemId;
143 }
144 });
145 }
146
147 /**
148 * Callback interface for SAX: not for application use
149 */
150
151 public void setDocumentLocator(final Locator locator) {
152 // If the user has called setSystemId(), we use that system ID in preference to this one,
153 // which probably comes from the XML parser possibly via some chain of SAX filters
154 if (systemId == null) {
155 super.setDocumentLocator(locator);
156 }
157 }
158
159 /**
160 * Get the systemId of the document
161 */
162
163 public String getSystemId() {
164 return systemId;
165 }
166
167
168 }
169