Debian Codegraph @

net/sf/saxon/jaxp/TransformerHandlerImpl.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.Controller;
12 import net.sf.saxon.event.Builder;
13 import net.sf.saxon.event.PipelineConfiguration;
14 import net.sf.saxon.event.Receiver;
15 import net.sf.saxon.event.ReceivingContentHandler;
16 import net.sf.saxon.lib.ParseOptions;
17 import net.sf.saxon.lib.Validation;
18 import net.sf.saxon.om.NoElementsSpaceStrippingRule;
19 import net.sf.saxon.om.NodeInfo;
20 import net.sf.saxon.tree.tiny.TinyBuilder;
21 import org.xml.sax.SAXException;
22
23 import javax.xml.transform.Result;
24 import javax.xml.transform.Transformer;
25 import javax.xml.transform.TransformerException;
26 import javax.xml.transform.sax.TransformerHandler;
27
28
29 /**
30 * <b>TransformerHandlerImpl</b> implements the javax.xml.transform.sax.TransformerHandler
31 * interface. It acts as a ContentHandler and LexicalHandler which receives a stream of
32 * SAX events representing an input document, and performs a transformation treating this
33 * SAX stream as the source document of the transformation.
34 *
35 * @author Michael H. Kay
36 */
37
38 public class TransformerHandlerImpl extends ReceivingContentHandler implements TransformerHandler {
39
40 TransformerImpl transformer;
41 Builder builder;
42 Receiver receiver;
43 /*@Nullable*/ Result result;
44 String systemId;
45 boolean started = false;
46
47 /**
48 * Create a TransformerHandlerImpl and initialise variables. The constructor is protected, because
49 * the Filter should be created using newTransformerHandler() in the SAXTransformerFactory
50 * class
51 *
52 * @param transformer the Controller to be used
53 */
54
55 protected TransformerHandlerImpl(TransformerImpl transformer) {
56 this.transformer = transformer;
57 Controller controller = transformer.getUnderlyingXsltTransformer().getUnderlyingController();
58 Configuration config = transformer.getConfiguration();
59 int validation = controller.getSchemaValidationMode();
60 builder = controller.makeBuilder();
61 if (builder instanceof TinyBuilder) {
62 ((TinyBuilder) builder).setStatistics(config.getTreeStatistics().SOURCE_DOCUMENT_STATISTICS);
63 }
64 PipelineConfiguration pipe = builder.getPipelineConfiguration();
65 ParseOptions options = pipe.getParseOptions();
66 options.setCheckEntityReferences(true);
67 setPipelineConfiguration(pipe);
68 receiver = controller.makeStripper(builder);
69 if (controller.isStylesheetStrippingTypeAnnotations()) {
70 receiver = config.getAnnotationStripper(receiver);
71 }
72 if (validation != Validation.PRESERVE) {
73 options.setSchemaValidationMode(validation);
74 options.setSpaceStrippingRule(NoElementsSpaceStrippingRule.getInstance());
75 receiver = config.getDocumentValidator(receiver, getSystemId(), options, null);
76 }
77 setReceiver(receiver);
78 }
79
80 /**
81 * Start of a new document. The TransformerHandler is not serially reusable, so this method
82 * must only be called once.
83 *
84 * @throws SAXException only if an overriding subclass throws this exception
85 * @throws UnsupportedOperationException if an attempt is made to reuse the TransformerHandler by calling
86 * startDocument() more than once.
87 */
88
89 public void startDocument() throws SAXException {
90 if (started) {
91 throw new UnsupportedOperationException(
92 "The TransformerHandler is not serially reusable. The startDocument() method must be called once only.");
93 }
94 started = true;
95 super.startDocument();
96 }
97
98 /**
99 * Get the Transformer used for this transformation
100 */
101
102 public Transformer getTransformer() {
103 return transformer;
104 }
105
106 /**
107 * Set the SystemId of the document. Note that in reporting location information, Saxon gives
108 * priority to the system Id reported by the SAX Parser in the Locator passed to the
109 * {@link #setDocumentLocator(org.xml.sax.Locator)} method. The SystemId passed to this method
110 * is used as the base URI for resolving relative references.
111 *
112 * @param url the systemId of the source document
113 */
114
115 public void setSystemId(String url) {
116 systemId = url;
117 receiver.setSystemId(url);
118 }
119
120 /**
121 * Get the systemId of the document. This will be the systemId obtained from the Locator passed to the
122 * {@link #setDocumentLocator(org.xml.sax.Locator)} method if available, otherwise the SystemId passed
123 * to the {@link #setSystemId(String)} method.
124 */
125
126 public String getSystemId() {
127 return systemId;
128 }
129
130
131 /**
132 * Set the <code>Result</code> associated with this
133 * <code>TransformerHandler</code> to be used for the transformation.
134 *
135 * @param result A <code>Result</code> instance, should not be
136 * <code>null</code>.
137 * @throws IllegalArgumentException if result is invalid for some reason.
138 */
139
140 public void setResult(Result result) {
141 if (result == null) {
142 throw new IllegalArgumentException("Result must not be null");
143 }
144 this.result = result;
145 }
146
147 /**
148 * Get the output destination of the transformation
149 *
150 * @return the output destination
151 */
152
153 public Result getResult() {
154 return result;
155 }
156
157 /**
158 * Override the behaviour of endDocument() in ReceivingContentHandler, so that it fires off
159 * the transformation of the constructed document
160 */
161
162 public void endDocument() throws SAXException {
163 super.endDocument();
164 NodeInfo doc = builder.getCurrentRoot();
165 doc.getTreeInfo().setSpaceStrippingRule(
166 transformer.getUnderlyingXsltTransformer().getUnderlyingController().getSpaceStrippingRule());
167 builder.reset();
168 if (doc == null) {
169 throw new SAXException("No source document has been built");
170 }
171
172 try {
173 transformer.transform(doc, result);
174 } catch (TransformerException err) {
175 throw new SAXException(err);
176 }
177 }
178
179 }
180