Debian Codegraph @

net/sf/saxon/event/TreeReceiver.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.Item;
13 import net.sf.saxon.om.NamespaceBinding;
14 import net.sf.saxon.om.NamespaceBindingSet;
15 import net.sf.saxon.om.NodeName;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.SchemaType;
18 import net.sf.saxon.type.SimpleType;
19 import net.sf.saxon.type.Type;
20
21 import java.util.Arrays;
22
23 /**
24 * A TreeReceiver acts as a bridge between a SequenceReceiver, which can receive
25 * events for constructing any kind of sequence, and an ordinary Receiver, which
26 * only handles events relating to the building of trees. To do this, it has to
27 * process any items added to the sequence using the append() interface; all other
28 * events are passed through unchanged.
29 * <p>If atomic items are appended to the sequence, then adjacent atomic items are
30 * turned in to a text node by converting them to strings and adding a single space
31 * as a separator.</p>
32 * <p>If a document node is appended to the sequence, then the document node is ignored
33 * and its children are appended to the sequence.</p>
34 * <p>If any other node is appended to the sequence, then it is pushed to the result
35 * as a sequence of Receiver events, which may involve walking recursively through the
36 * contents of a tree.</p>
37 */
38
39 public class TreeReceiver extends SequenceReceiver {
40 private Receiver nextReceiver;
41 private int level = 0;
42 private boolean[] isDocumentLevel = new boolean[20];
43 // The sequence of events can include startElement/endElement pairs or startDocument/endDocument
44 // pairs at any level. A startDocument/endDocument pair is essentially ignored except at the
45 // outermost level, except that a namespace or attribute node cannot be sent when we're at a
46 // document level. See for example schema90963-err.xsl
47 private boolean inStartTag = false;
48
49 /**
50 * Create a TreeReceiver
51 *
52 * @param nextInChain the receiver to which events will be directed, after
53 * expanding append events into more primitive tree-based events
54 */
55
56 public TreeReceiver(/*@NotNull*/ Receiver nextInChain) {
57 super(nextInChain.getPipelineConfiguration());
58 nextReceiver = nextInChain;
59 previousAtomic = false;
60 setPipelineConfiguration(nextInChain.getPipelineConfiguration());
61 }
62
63 public void setSystemId(String systemId) {
64 if (systemId != null && !systemId.equals(this.systemId)) {
65 this.systemId = systemId;
66 if (nextReceiver != null) {
67 nextReceiver.setSystemId(systemId);
68 }
69 }
70 }
71
72 public void setPipelineConfiguration(/*@NotNull*/ PipelineConfiguration pipe) {
73 if (pipelineConfiguration != pipe) {
74 pipelineConfiguration = pipe;
75 if (nextReceiver != null) {
76 nextReceiver.setPipelineConfiguration(pipe);
77 }
78 }
79 }
80
81 /**
82 * Get the underlying Receiver (that is, the next one in the pipeline)
83 *
84 * @return the underlying Receiver
85 */
86
87 public Receiver getNextReceiver() {
88 return nextReceiver;
89 }
90
91 /**
92 * Start of event sequence
93 */
94
95 public void open() throws XPathException {
96 if (nextReceiver == null) {
97 throw new IllegalStateException("TreeReceiver.open(): no underlying receiver provided");
98 }
99 nextReceiver.open();
100 previousAtomic = false;
101 }
102
103 /**
104 * End of event sequence
105 */
106
107 public void close() throws XPathException {
108 if (nextReceiver != null) {
109 nextReceiver.close();
110 }
111 previousAtomic = false;
112 }
113
114 /**
115 * Start of a document node.
116 */
117
118 public void startDocument(int properties) throws XPathException {
119 if (level == 0) {
120 nextReceiver.startDocument(properties);
121 }
122 if (isDocumentLevel.length - 1 < level) {
123 isDocumentLevel = Arrays.copyOf(isDocumentLevel, level * 2);
124 }
125 isDocumentLevel[level++] = true;
126 }
127
128 /**
129 * Notify the end of a document node
130 */
131
132 public void endDocument() throws XPathException {
133 level--;
134 if (level == 0) {
135 nextReceiver.endDocument();
136 }
137 }
138
139 /**
140 * Notify the start of an element
141 * @param nameCode the name of the element
142 * @param typeCode the element's type
143 * @param location the location of the element
144 * @param properties bit-significant properties of the element node
145 */
146
147 public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException {
148 if (inStartTag) {
149 startContent();
150 }
151 inStartTag = true;
152 nextReceiver.startElement(nameCode, typeCode, location, properties);
153 previousAtomic = false;
154 if (isDocumentLevel.length - 1 < level) {
155 isDocumentLevel = Arrays.copyOf(isDocumentLevel, level * 2);
156 }
157 isDocumentLevel[level++] = false;
158 }
159
160 /**
161 * Notify a namespace. Namespaces are notified <b>after</b> the startElement event, and before
162 * any children for the element. The namespaces that are reported are only required
163 * to include those that are different from the parent element; however, duplicates may be reported.
164 * A namespace must not conflict with any namespaces already used for element or attribute names.
165 *
166 * @param namespaceBindings the prefix/uri pair
167 * @throws IllegalStateException: attempt to output a namespace when there is no open element
168 * start tag
169 */
170
171 public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException {
172 boolean documentLevel = level == 0 || isDocumentLevel[level - 1];
173 if (documentLevel || !inStartTag) {
174 //noinspection LoopStatementThatDoesntLoop
175 for (NamespaceBinding ns : namespaceBindings) {
176 // Throw an exception if there is an least one namespace binding in the set
177 throw NoOpenStartTagException.makeNoOpenStartTagException(
178 Type.NAMESPACE, ns.getPrefix(),
179 getPipelineConfiguration().getHostLanguage(),
180 documentLevel, ExplicitLocation.UNKNOWN_LOCATION);
181 }
182 }
183 nextReceiver.namespace(namespaceBindings, properties);
184 previousAtomic = false;
185 }
186
187 /**
188 * Notify an attribute. Attributes are notified after the startElement event, and before any
189 * children. Namespaces and attributes may be intermingled.
190 *
191 * @param nameCode The name of the attribute
192 * @param typeCode The type of the attribute
193 * @param locationId the location of the node in the source, or of the instruction that created it
194 * @param properties Bit significant value. The following bits are defined:
195 * <dl>
196 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
197 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
198 * </dl>
199 * @throws IllegalStateException: attempt to output an attribute when there is no open element
200 * start tag
201 */
202
203 public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties)
204 throws XPathException {
205 boolean documentLevel = level == 0 || isDocumentLevel[level - 1];
206 if (documentLevel || !inStartTag) {
207 throw NoOpenStartTagException.makeNoOpenStartTagException(
208 Type.ATTRIBUTE, nameCode.getDisplayName(),
209 getPipelineConfiguration().getHostLanguage(),
210 documentLevel, ExplicitLocation.UNKNOWN_LOCATION);
211 }
212 nextReceiver.attribute(nameCode, typeCode, value, locationId, properties);
213 previousAtomic = false;
214 }
215
216 /**
217 * Notify the start of the content, that is, the completion of all attributes and namespaces.
218 * Note that the initial receiver of output from XSLT instructions will not receive this event,
219 * it has to detect it itself. Note that this event is reported for every element even if it has
220 * no attributes, no namespaces, and no content.
221 */
222
223
224 public void startContent() throws XPathException {
225 inStartTag = false;
226 nextReceiver.startContent();
227 previousAtomic = false;
228 }
229
230 /**
231 * End of element
232 */
233
234 public void endElement() throws XPathException {
235 if (inStartTag) {
236 startContent();
237 }
238 nextReceiver.endElement();
239 previousAtomic = false;
240 level--;
241 }
242
243 /**
244 * Character data
245 */
246
247 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
248 if (chars.length() > 0) {
249 if (inStartTag) {
250 startContent();
251 }
252 nextReceiver.characters(chars, locationId, properties);
253 }
254 previousAtomic = false;
255 }
256
257
258 /**
259 * Processing Instruction
260 */
261
262 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
263 if (inStartTag) {
264 startContent();
265 }
266 nextReceiver.processingInstruction(target, data, locationId, properties);
267 previousAtomic = false;
268 }
269
270 /**
271 * Output a comment
272 */
273
274 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
275 if (inStartTag) {
276 startContent();
277 }
278 nextReceiver.comment(chars, locationId, properties);
279 previousAtomic = false;
280 }
281
282
283 /**
284 * Set the URI for an unparsed entity in the document.
285 */
286
287 public void setUnparsedEntity(String name, String uri, String publicId) throws XPathException {
288 nextReceiver.setUnparsedEntity(name, uri, publicId);
289 }
290
291 /**
292 * Append an arbitrary item (node or atomic value) to the output
293 */
294
295 public void append(/*@Nullable*/ Item item, Location locationId, int copyNamespaces) throws XPathException {
296 decompose(item, locationId, copyNamespaces);
297 }
298
299 /**
300 * Ask whether this Receiver (or the downstream pipeline) makes any use of the type annotations
301 * supplied on element and attribute events
302 *
303 * @return true if the Receiver makes any use of this information. If false, the caller
304 * may supply untyped nodes instead of supplying the type annotation
305 */
306
307 public boolean usesTypeAnnotations() {
308 return nextReceiver.usesTypeAnnotations();
309 }
310 }
311