Debian Codegraph @

net/sf/saxon/event/SequenceWriter.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.event;
9
10 import net.sf.saxon.expr.parser.ExplicitLocation;
11 import net.sf.saxon.expr.parser.Location;
12 import net.sf.saxon.om.*;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.linked.LinkedTreeBuilder;
15 import net.sf.saxon.tree.tiny.TinyElementImpl;
16 import net.sf.saxon.tree.util.Orphan;
17 import net.sf.saxon.type.SchemaType;
18 import net.sf.saxon.type.SimpleType;
19 import net.sf.saxon.type.Type;
20
21 /**
22 * The <tt>SequenceWriter</tt> is used when writing a sequence of items, for
23 * example, when {@code xsl:variable} is used with content and an "as" attribute. The {@code SequenceWriter}
24 * builds the sequence; the concrete subclass is responsible for deciding what to do with the
25 * resulting items.
26 * <p>
27 * Items may be supplied in either "composed" form (by calling the {@link #append(Item)} method,
28 * or in "decomposed" form (by a sequence of calls representing XML push events: {@link #startDocument(int)},
29 * {@link #startElement(NodeName, SchemaType, Location, int)}, and so on. When items are supplied
30 * in decomposed form, a tree will be built, and the resulting document or element node is then
31 * written to the sequence in the same way as if it were supplied directly as a {@link NodeInfo} item.
32 * <p>
33 * This class is not used to build temporary trees. For that, the {@link ComplexContentOutputter}
34 * is used. The {@code ComplexContentOutputter} provides richer functionality than this class:
35 * it detects illegal sequences of events, detects duplicate or inconsistent attributes and namespaces,
36 * performs namespace fixup, allocates prefixes where necessary, and detects where it is necessary
37 * to call {@link #startContent()} to mark the end of a start tag. By contrast, this class should
38 * only be used where it is known that the sequence of events is complete and consistent.</p>
39 */
40
41 public abstract class SequenceWriter extends SequenceReceiver {
42 private Receiver outputter = null;
43 private TreeModel treeModel = null;
44 private Builder builder = null;
45 private int level = 0;
46 private boolean inStartTag = false;
47
48 public SequenceWriter(/*@NotNull*/ PipelineConfiguration pipe) {
49 super(pipe);
50 //System.err.println("SequenceWriter init");
51 }
52
53 /**
54 * Abstract method to be supplied by subclasses: output one item in the sequence.
55 *
56 * @param item the item to be written to the sequence
57 * @throws net.sf.saxon.trans.XPathException
58 * if any failure occurs while writing the item
59 */
60
61 public abstract void write(Item item) throws XPathException;
62
63 @Override
64 public void startDocument(int properties) throws XPathException {
65 if (outputter == null) {
66 createTree((properties & ReceiverOptions.MUTABLE_TREE) != 0);
67 }
68 if (level++ == 0) {
69 outputter.startDocument(properties);
70 }
71 }
72
73 @Override
74 public void setUnparsedEntity(String name, String systemID, String publicID) throws XPathException {
75 if (builder != null) {
76 builder.setUnparsedEntity(name, systemID, publicID);
77 }
78 }
79
80 /**
81 * Create a (skeletal) tree to hold a document or element node.
82 *
83 * @param mutable set to true if the tree is required to support in-situ updates (other that the initial
84 * sequential writing of nodes to construct the tree)
85 * @throws net.sf.saxon.trans.XPathException
86 * if any error occurs while creating the tree
87 */
88 private void createTree(boolean mutable) throws XPathException {
89 PipelineConfiguration pipe = getPipelineConfiguration();
90
91 if (treeModel != null) {
92 builder = treeModel.makeBuilder(pipe);
93 } else if (pipe.getController() != null) {
94 if (mutable) {
95 TreeModel model = pipe.getController().getModel();
96 if (model.isMutable()) {
97 builder = pipe.getController().makeBuilder();
98 } else {
99 builder = new LinkedTreeBuilder(pipe);
100 }
101 } else {
102 builder = pipe.getController().makeBuilder();
103 }
104 } else {
105 TreeModel model = getConfiguration().getParseOptions().getModel();
106 builder = model.makeBuilder(pipe);
107 }
108
109 builder.setPipelineConfiguration(pipe);
110 builder.setSystemId(systemId);
111 builder.setBaseURI(systemId);
112 builder.setTiming(false);
113 builder.setUseEventLocation(false);
114
115 NamespaceReducer reducer = new NamespaceReducer(builder);
116
117 outputter = new ComplexContentOutputter(reducer);
118
119 outputter.setSystemId(systemId);
120 outputter.setPipelineConfiguration(pipe);
121 outputter.open();
122 }
123
124 /**
125 * Get the tree model that will be used for creating trees when events are written to the sequence
126 * @return the tree model, if one has been set using setTreeModel(); otherwise null
127 */
128
129 public TreeModel getTreeModel() {
130 return treeModel;
131 }
132
133 /**
134 * Set the tree model that will be used for creating trees when events are written to the sequence
135 * @param treeModel the tree model to be used. If none has been set, the default tree model for the configuration
136 * is used, unless a mutable tree is required and the default tree model is not mutable, in which case a linked
137 * tree is used.
138 */
139
140 public void setTreeModel(TreeModel treeModel) {
141 this.treeModel = treeModel;
142 }
143
144 @Override
145 public void endDocument() throws XPathException {
146 if (--level == 0) {
147 outputter.endDocument();
148 outputter = null;
149 NodeInfo doc = builder.getCurrentRoot();
150 // add the constructed document to the result sequence
151 append(doc, ExplicitLocation.UNKNOWN_LOCATION, ReceiverOptions.ALL_NAMESPACES);
152 systemId = null;
153 }
154 previousAtomic = false;
155 }
156
157 @Override
158 public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException {
159
160 if (inStartTag) {
161 startContent();
162 }
163
164 if (outputter == null) {
165 createTree((properties & ReceiverOptions.MUTABLE_TREE) != 0);
166 }
167 //System.err.println("SEQUENCE_WRITER startElement " + this);
168 outputter.startElement(elemName, typeCode, location, properties);
169 level++;
170 inStartTag = true;
171 previousAtomic = false;
172 }
173
174 @Override
175 public void endElement() throws XPathException {
176 if (inStartTag) {
177 startContent();
178 }
179 //System.err.println("SEQUENCE_WRITER endElement " + this);
180 outputter.endElement();
181 if (--level == 0) {
182 outputter.close();
183 outputter = null;
184 NodeInfo element = builder.getCurrentRoot();
185 append(element, ExplicitLocation.UNKNOWN_LOCATION, ReceiverOptions.ALL_NAMESPACES);
186 systemId = null;
187 }
188 previousAtomic = false;
189 }
190
191 @Override
192 public void namespace(NamespaceBindingSet namespaceBindings, int properties)
193 throws XPathException {
194 if (level == 0) {
195 for (NamespaceBinding ns : namespaceBindings) {
196 Orphan o = new Orphan(getConfiguration());
197 o.setNodeKind(Type.NAMESPACE);
198 o.setNodeName(new NoNamespaceName(ns.getPrefix()));
199 o.setStringValue(ns.getURI());
200 append(o, ExplicitLocation.UNKNOWN_LOCATION, ReceiverOptions.ALL_NAMESPACES);
201 }
202 } else {
203 outputter.namespace(namespaceBindings, properties);
204 }
205 previousAtomic = false;
206 }
207
208 @Override
209 public void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location locationId, int properties)
210 throws XPathException {
211 if (level == 0) {
212 Orphan o = new Orphan(getConfiguration());
213 o.setNodeKind(Type.ATTRIBUTE);
214 o.setNodeName(attName);
215 o.setStringValue(value);
216 o.setTypeAnnotation(typeCode);
217 append(o, locationId, ReceiverOptions.ALL_NAMESPACES);
218 } else {
219 outputter.attribute(attName, typeCode, value, locationId, properties);
220 }
221 previousAtomic = false;
222 }
223
224 @Override
225 public void startContent() throws XPathException {
226 inStartTag = false;
227 outputter.startContent();
228 previousAtomic = false;
229 }
230
231 @Override
232 public void characters(CharSequence s, Location locationId, int properties) throws XPathException {
233 if (level == 0) {
234 Orphan o = new Orphan(getConfiguration());
235 o.setNodeKind(Type.TEXT);
236 o.setStringValue(s.toString());
237 write(o);
238 } else {
239 if (s.length() > 0) {
240 if (inStartTag) {
241 startContent();
242 }
243 outputter.characters(s, locationId, properties);
244 }
245 }
246 previousAtomic = false;
247 }
248
249 @Override
250 public void comment(CharSequence comment, Location locationId, int properties) throws XPathException {
251 if (inStartTag) {
252 startContent();
253 }
254 if (level == 0) {
255 Orphan o = new Orphan(getConfiguration());
256 o.setNodeKind(Type.COMMENT);
257 o.setStringValue(comment);
258 write(o);
259 } else {
260 outputter.comment(comment, locationId, properties);
261 }
262 previousAtomic = false;
263 }
264
265 @Override
266 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
267 if (inStartTag) {
268 startContent();
269 }
270 if (level == 0) {
271 Orphan o = new Orphan(getConfiguration());
272 o.setNodeName(new NoNamespaceName(target));
273 o.setNodeKind(Type.PROCESSING_INSTRUCTION);
274 o.setStringValue(data);
275 write(o);
276 } else {
277 outputter.processingInstruction(target, data, locationId, properties);
278 }
279 previousAtomic = false;
280 }
281
282 @Override
283 public void close() throws XPathException {
284 previousAtomic = false;
285 if (outputter != null) {
286 outputter.close();
287 }
288 }
289
290 @Override
291 public void append(/*@Nullable*/ Item item, Location locationId, int copyNamespaces) throws XPathException {
292 if (item != null) {
293 if (level == 0) {
294 write(item);
295 previousAtomic = false;
296 } else {
297 decompose(item, locationId, copyNamespaces);
298 }
299 }
300 }
301
302 @Override
303 public boolean usesTypeAnnotations() {
304 return outputter == null || outputter.usesTypeAnnotations();
305 }
306
307
308 public boolean isReadyForBulkCopy() {
309 return level > 0 && outputter instanceof ComplexContentOutputter &&
310 ((ComplexContentOutputter)outputter).isReadyForBulkCopy();
311 }
312
313 public void bulkCopyElementNode(TinyElementImpl node, int copyOptions) throws XPathException {
314 if (inStartTag) {
315 startContent();
316 }
317 ((ComplexContentOutputter) outputter).bulkCopyElementNode(node, copyOptions);
318 }
319
320 public boolean isReadyForGrafting() {
321 return level > 0 && outputter instanceof ComplexContentOutputter &&
322 ((ComplexContentOutputter) outputter).isReadyForGrafting();
323 }
324
325 public void graftElementNode(TinyElementImpl node, int copyOptions) throws XPathException {
326 if (inStartTag) {
327 startContent();
328 }
329 ((ComplexContentOutputter) outputter).graftElementNode(node, copyOptions);
330 }
331 }
332