Debian Codegraph @

net/sf/saxon/event/SequenceNormalizer.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.Location;
11 import net.sf.saxon.om.NamespaceBindingSet;
12 import net.sf.saxon.om.NodeName;
13 import net.sf.saxon.s9api.Action;
14 import net.sf.saxon.s9api.Destination;
15 import net.sf.saxon.s9api.SaxonApiException;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.SchemaType;
18 import net.sf.saxon.type.SimpleType;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 /**
24 * Implement the "sequence normalization" logic as defined in the XSLT 3.0/XQuery 3.0
25 * serialization spec.
26 * <p>
27 * There are two subclasses, which handle the different logic for the case where an
28 * {@code item-separator} is provided, and the case where whitespace-separation is used.
29 * Note that the whitespace case behaves differently from the case where the item-separator
30 * is set to a single space.
31 * <p>Because this Receiver is often used as the entry point to the pipeline for a
32 * {@link Destination}, it includes logic allowing {@code onClose} action for the
33 * Destination to be triggered when the Receiver is closed.</p>
34 */
35
36 public abstract class SequenceNormalizer extends ProxyReceiver {
37
38 protected int level = 0;
39 protected boolean inStartTag = false;
40 private List<Action> actionList;
41 private boolean failed = false;
42
43 public SequenceNormalizer(Receiver next) {
44 super(next);
45 }
46
47 /**
48 * Start of event stream
49 */
50 @Override
51 public void open() throws XPathException {
52 level = 0;
53 previousAtomic = false;
54 super.open();
55 getNextReceiver().startDocument(0);
56 }
57
58 /**
59 * Start of a document node.
60 */
61 @Override
62 public void startDocument(int properties) throws XPathException {
63 level++;
64 previousAtomic = false;
65 }
66
67 /**
68 * Notify the end of a document node
69 */
70 @Override
71 public void endDocument() throws XPathException {
72 level--;
73 previousAtomic = false;
74 }
75
76 /**
77 * Notify the start of an element
78 * @param elemName integer code identifying the name of the element within the name pool.
79 * @param typeCode integer code identifying the element's type within the name pool.
80 * @param location
81 * @param properties properties of the element node
82 */
83 @Override
84 public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException {
85 try {
86 maybeStartContent();
87 level++;
88 super.startElement(elemName, typeCode, location, properties);
89 inStartTag = true;
90 previousAtomic = false;
91 } catch (XPathException e) {
92 failed = true;
93 e.maybeSetLocation(location);
94 throw e;
95 }
96 }
97
98 /**
99 * Notify an attribute. Attributes are notified after the startElement event, and before any
100 * children. Namespaces and attributes may be intermingled.
101 *
102 * @param name The name of the attribute
103 * @param typeCode The type of the attribute
104 * @param value
105 * @param locationId the location of the node in the source, or of the instruction that created it
106 * @param properties Bit significant value. The following bits are defined:
107 * <dl>
108 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
109 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
110 * </dl>
111 * @throws IllegalStateException: attempt to output an attribute when there is no open element
112 */
113 @Override
114 public void attribute(NodeName name, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
115 if (level == 0) {
116 failed = true;
117 XPathException err = new XPathException("Cannot serialize a free-standing attribute node (" + name + ')', "SENR0001");
118 err.setLocation(locationId);
119 throw err;
120 }
121 try {
122 super.attribute(name, typeCode, value, locationId, properties);
123 } catch (XPathException e) {
124 failed = true;
125 throw e;
126 }
127 }
128
129 /**
130 * Notify a namespace. Namespaces are notified <b>after</b> the startElement event, and before
131 * any children for the element. The namespaces that are reported are only required
132 * to include those that are different from the parent element; however, duplicates may be reported.
133 * A namespace must not conflict with any namespaces already used for element or attribute names.
134 *
135 * @param namespaceBindings the prefix/uri pair representing the namespace binding
136 * @param properties any special properties to be passed on this call
137 * @throws IllegalStateException: attempt to output a namespace when there is no open element
138 * start tag
139 */
140 @Override
141 public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException {
142 if (level == 0) {
143 failed = true;
144 throw new XPathException("Cannot serialize a free-standing namespace node", "SENR0001");
145 }
146 try {
147 super.namespace(namespaceBindings, properties);
148 } catch (XPathException e) {
149 failed = true;
150 throw e;
151 }
152 }
153
154 /**
155 * Notify the start of the content, that is, the completion of all attributes and namespaces.
156 * Note that the initial receiver of output from XSLT instructions will not receive this event,
157 * it has to detect it itself. Note that this event is reported for every element even if it has
158 * no attributes, no namespaces, and no content.
159 */
160 @Override
161 public void startContent() throws XPathException {
162 try {
163 super.startContent();
164 inStartTag = false;
165 } catch (XPathException e) {
166 failed = true;
167 throw e;
168 }
169 }
170
171 private void maybeStartContent() throws XPathException {
172 if (inStartTag) {
173 startContent();
174 }
175 }
176
177 /**
178 * Character data
179 */
180 @Override
181 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
182 try {
183 maybeStartContent();
184 super.characters(chars, locationId, properties);
185 previousAtomic = false;
186 } catch (XPathException e) {
187 failed = true;
188 throw e;
189 }
190 }
191
192 /**
193 * Processing Instruction
194 */
195 @Override
196 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
197 try {
198 maybeStartContent();
199 super.processingInstruction(target, data, locationId, properties);
200 previousAtomic = false;
201 } catch (XPathException e) {
202 failed = true;
203 throw e;
204 }
205 }
206
207 /**
208 * Output a comment
209 */
210 @Override
211 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
212 try {
213 maybeStartContent();
214 super.comment(chars, locationId, properties);
215 previousAtomic = false;
216 } catch (XPathException e) {
217 failed = true;
218 throw e;
219 }
220 }
221
222 /**
223 * End of element
224 */
225 @Override
226 public void endElement() throws XPathException {
227 try {
228 maybeStartContent();
229 level--;
230 super.endElement();
231 previousAtomic = false;
232 } catch (XPathException e) {
233 failed = true;
234 throw e;
235 }
236 }
237
238 /**
239 * End of output. Note that closing this receiver also closes the rest of the
240 * pipeline.
241 */
242 @Override
243 public void close() throws XPathException {
244 if (failed) {
245 super.close();
246 } else {
247 getNextReceiver().endDocument();
248 super.close();
249 try {
250 if (actionList != null) {
251 for (Action action : actionList) {
252 action.act();
253 }
254 }
255 } catch (SaxonApiException e) {
256 throw XPathException.makeXPathException(e);
257 }
258 }
259 }
260
261 /**
262 * Set actions to be performed when this {@code Receiver} is closed
263 * @param actionList a list of actions to be performed
264 */
265
266 public void onClose(List<Action> actionList) {
267 this.actionList = actionList;
268 }
269
270 public void onClose(Action action) {
271 if (actionList == null) {
272 actionList = new ArrayList<>();
273 }
274 actionList.add(action);
275 }
276
277 }
278