Debian Codegraph @

net/sf/saxon/jaxp/IdentityTransformerHandler.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.Receiver;
12 import net.sf.saxon.event.ReceivingContentHandler;
13 import net.sf.saxon.event.Stripper;
14 import net.sf.saxon.lib.SerializerFactory;
15 import net.sf.saxon.om.AllElementsSpaceStrippingRule;
16 import net.sf.saxon.serialize.SerializationProperties;
17 import net.sf.saxon.trans.XPathException;
18 import org.xml.sax.SAXException;
19
20 import javax.xml.transform.Result;
21 import javax.xml.transform.Transformer;
22 import javax.xml.transform.sax.TransformerHandler;
23 import javax.xml.transform.stream.StreamResult;
24 import java.util.Properties;
25
26 /**
27 * <b>IdentityTransformerHandler</b> implements the javax.xml.transform.sax.TransformerHandler
28 * interface. It acts as a ContentHandler and LexicalHandler which receives a stream of
29 * SAX events representing an input document, and performs an identity transformation passing
30 * these events to a Result
31 *
32 * @author Michael H. Kay
33 */
34
35 public class IdentityTransformerHandler extends ReceivingContentHandler implements TransformerHandler {
36
37 /*@Nullable*/ private Result result;
38 private String systemId;
39 private IdentityTransformer controller;
40
41 /**
42 * Create a IdentityTransformerHandler and initialise variables. The constructor is protected, because
43 * the Filter should be created using newTransformerHandler() in the SAXTransformerFactory
44 * class
45 *
46 * @param controller the Controller for this transformation
47 */
48
49 protected IdentityTransformerHandler(IdentityTransformer controller) {
50 this.controller = controller;
51 setPipelineConfiguration(controller.getConfiguration().makePipelineConfiguration());
52 }
53
54 /**
55 * Get the Transformer used for this transformation
56 */
57
58 public Transformer getTransformer() {
59 return controller;
60 }
61
62 /**
63 * Set the SystemId of the document
64 */
65
66 public void setSystemId(String url) {
67 systemId = url;
68 }
69
70 /**
71 * Get the systemId of the document
72 */
73
74 public String getSystemId() {
75 return systemId;
76 }
77
78 /**
79 * Set the output destination of the transformation
80 */
81
82 public void setResult(Result result) {
83 if (result == null) {
84 throw new IllegalArgumentException("Result must not be null");
85 }
86 this.result = result;
87 }
88
89 /**
90 * Get the output destination of the transformation
91 *
92 * @return the output destination
93 */
94
95 public Result getResult() {
96 return result;
97 }
98
99 /**
100 * Override the behaviour of startDocument() in ReceivingContentHandler
101 */
102
103 public void startDocument() throws SAXException {
104 if (result == null) {
105 result = new StreamResult(System.out);
106 }
107 try {
108 Properties props = controller.getOutputProperties();
109 Configuration config = getConfiguration();
110 SerializerFactory sf = config.getSerializerFactory();
111 Receiver out = sf.getReceiver(result, new SerializationProperties(props));
112 setPipelineConfiguration(out.getPipelineConfiguration());
113 if (config.isStripsAllWhiteSpace()) {
114 out = new Stripper(AllElementsSpaceStrippingRule.getInstance(), out);
115 }
116 setReceiver(out);
117 } catch (XPathException err) {
118 throw new SAXException(err);
119 }
120 super.startDocument();
121 }
122
123 }
124