Debian Codegraph @

net/sf/saxon/serialize/UncommittedSerializer.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.serialize;
9
10 import net.sf.saxon.event.EventBuffer;
11 import net.sf.saxon.event.ProxyReceiver;
12 import net.sf.saxon.event.Receiver;
13 import net.sf.saxon.event.ReceiverOptions;
14 import net.sf.saxon.expr.parser.Location;
15 import net.sf.saxon.lib.NamespaceConstant;
16 import net.sf.saxon.lib.SaxonOutputKeys;
17 import net.sf.saxon.lib.SerializerFactory;
18 import net.sf.saxon.om.CopyOptions;
19 import net.sf.saxon.om.Item;
20 import net.sf.saxon.om.NodeInfo;
21 import net.sf.saxon.om.NodeName;
22 import net.sf.saxon.trans.XPathException;
23 import net.sf.saxon.type.SchemaType;
24 import net.sf.saxon.value.Whitespace;
25
26 import javax.xml.transform.OutputKeys;
27 import javax.xml.transform.Result;
28 import java.util.Properties;
29
30 /**
31 * This class is used when the decision on which serialization method to use has to be delayed until the first
32 * element is read. It buffers comments and processing instructions until that happens; then when the first
33 * element arrives it creates a real serialization pipeline and uses that for future output.
34 *
35 * @author Michael H. Kay
36 */
37
38 public class UncommittedSerializer extends ProxyReceiver {
39
40 private boolean committed = false;
41 private EventBuffer pending = null;
42 private Result finalResult;
43 private SerializationProperties properties;
44
45 /**
46 * Create an uncommitted Serializer
47 *
48 * @param finalResult the output destination
49 * @param next the next receiver in the pipeline
50 * @param params the serialization properties including character maps
51 */
52
53 public UncommittedSerializer(Result finalResult, Receiver next, SerializationProperties params) {
54 super(next);
55 this.finalResult = finalResult;
56 this.properties = params;
57 }
58
59 public void open() throws XPathException {
60 committed = false;
61 }
62
63 /**
64 * End of document
65 */
66
67 public void close() throws XPathException {
68 // empty output: must send a beginDocument()/endDocument() pair to the content handler
69 if (!committed) {
70 switchToMethod("xml");
71 }
72 getNextReceiver().close();
73 }
74
75 /**
76 * Produce character output using the current Writer. <BR>
77 */
78
79 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
80 if (committed) {
81 getNextReceiver().characters(chars, locationId, properties);
82 } else {
83 if (pending == null) {
84 pending = new EventBuffer(getPipelineConfiguration());
85 }
86 pending.characters(chars, locationId, properties);
87 if (!Whitespace.isWhite(chars)) {
88 switchToMethod("xml");
89 }
90 }
91 }
92
93 /**
94 * Processing Instruction
95 */
96
97 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
98 if (committed) {
99 getNextReceiver().processingInstruction(target, data, locationId, properties);
100 } else {
101 if (pending == null) {
102 pending = new EventBuffer(getPipelineConfiguration());
103 }
104 pending.processingInstruction(target, data, locationId, properties);
105 }
106 }
107
108 /**
109 * Output a comment
110 */
111
112 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
113 if (committed) {
114 getNextReceiver().comment(chars, locationId, properties);
115 } else {
116 if (pending == null) {
117 pending = new EventBuffer(getPipelineConfiguration());
118 }
119 pending.comment(chars, locationId, properties);
120 }
121 }
122
123 /**
124 * Output an element start tag. <br>
125 * This can only be called once: it switches to a substitute output generator for XML, XHTML, or HTML,
126 * depending on the element name.
127 * @param elemName The element name (tag)
128 * @param typeCode The type annotation
129 * @param location Location of the instruction emitting this event
130 * @param properties Bit field holding special properties of the element
131 */
132
133 public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException {
134 if (!committed) {
135 String name = elemName.getLocalPart();
136 String uri = elemName.getURI();
137 if (name.equalsIgnoreCase("html") && uri.isEmpty()) {
138 switchToMethod("html");
139 } else if (name.equals("html") && uri.equals(NamespaceConstant.XHTML)) {
140 String version = this.properties.getProperties().getProperty(SaxonOutputKeys.STYLESHEET_VERSION);
141 if ("10".equals(version)) {
142 switchToMethod("xml");
143 } else {
144 switchToMethod("xhtml");
145 }
146 } else {
147 switchToMethod("xml");
148 }
149 }
150 getNextReceiver().startElement(elemName, typeCode, location, properties);
151 }
152
153 @Override
154 public void startDocument(int properties) throws XPathException {
155 if (committed) {
156 getNextReceiver().startDocument(properties);
157 } else {
158 if (pending == null) {
159 pending = new EventBuffer(getPipelineConfiguration());
160 }
161 pending.startDocument(properties);
162 }
163 }
164
165 @Override
166 public void endDocument() throws XPathException {
167 // empty output: must send a beginDocument()/endDocument() pair to the content handler
168 if (!committed) {
169 switchToMethod("xml");
170 }
171 getNextReceiver().endDocument();
172 }
173
174 /**
175 * Switch to a specific emitter once the output method is known
176 *
177 * @param method the method to switch to (xml, html, xhtml)
178 */
179
180 private void switchToMethod(String method) throws XPathException {
181 Properties newProperties = new Properties(properties.getProperties());
182 newProperties.setProperty(OutputKeys.METHOD, method);
183 SerializerFactory sf = getConfiguration().getSerializerFactory();
184 SerializationProperties newParams = new SerializationProperties(newProperties, properties.getCharacterMapIndex());
185 newParams.setValidationFactory(properties.getValidationFactory());
186 Receiver target = sf.getReceiver(finalResult, newParams, getPipelineConfiguration());
187 committed = true;
188 target.open();
189 if (pending != null) {
190 pending.replay(target);
191 pending = null;
192 }
193 setUnderlyingReceiver(target);
194 }
195
196 /**
197 * Append an arbitrary item (node or atomic value) to the output
198 *
199 * @param item the item to be appended
200 * @param locationId the location of the calling instruction, for diagnostics
201 * @param copyNamespaces if the item is an element node, this indicates whether its namespaces
202 * need to be copied. Values are {@link ReceiverOptions#ALL_NAMESPACES},
203 * {@link ReceiverOptions#LOCAL_NAMESPACES}; the default (0) means
204 * no namespaces
205 */
206 @Override
207 public void append(Item item, Location locationId, int copyNamespaces) throws XPathException {
208 if (item instanceof NodeInfo) {
209 ((NodeInfo) item).copy(this, CopyOptions.ALL_NAMESPACES, locationId);
210 } else {
211 if (!committed) {
212 switchToMethod("xml");
213 }
214 getNextReceiver().append(item);
215 }
216 }
217
218 }
219