Debian Codegraph @

net/sf/saxon/event/SequenceReceiver.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.Configuration;
11 import net.sf.saxon.expr.parser.ExplicitLocation;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.ma.arrays.ArrayItem;
14 import net.sf.saxon.ma.map.MapItem;
15 import net.sf.saxon.om.*;
16 import net.sf.saxon.trans.Err;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.util.Orphan;
19 import net.sf.saxon.type.SimpleType;
20 import net.sf.saxon.type.Type;
21 import net.sf.saxon.value.AtomicValue;
22 import net.sf.saxon.value.ExternalObject;
23
24 /**
25 * SequenceReceiver: this extension of the Receiver interface is used when processing
26 * a sequence constructor. It differs from the Receiver in allowing items (atomic values or
27 * nodes) to be added to the sequence, not just tree-building events.
28 */
29
30 public abstract class SequenceReceiver implements Receiver {
31
32 protected boolean previousAtomic = false;
33 /*@NotNull*/
34 protected PipelineConfiguration pipelineConfiguration;
35 /*@Nullable*/
36 protected String systemId = null;
37
38 /**
39 * Create a SequenceReceiver
40 *
41 * @param pipe the pipeline configuration
42 */
43
44 public SequenceReceiver(/*@NotNull*/ PipelineConfiguration pipe) {
45 this.pipelineConfiguration = pipe;
46 }
47
48 /*@NotNull*/
49 public final PipelineConfiguration getPipelineConfiguration() {
50 return pipelineConfiguration;
51 }
52
53 public void setPipelineConfiguration(/*@NotNull*/ PipelineConfiguration pipelineConfiguration) {
54 this.pipelineConfiguration = pipelineConfiguration;
55 }
56
57 /**
58 * Get the Saxon Configuration
59 *
60 * @return the Configuration
61 */
62
63 public final Configuration getConfiguration() {
64 return pipelineConfiguration.getConfiguration();
65 }
66
67 /**
68 * Set the system ID
69 *
70 * @param systemId the URI used to identify the tree being passed across this interface
71 */
72
73 public void setSystemId(/*@Nullable*/ String systemId) {
74 this.systemId = systemId;
75 }
76
77 /**
78 * Get the system ID
79 *
80 * @return the system ID that was supplied using the setSystemId() method
81 */
82
83 /*@Nullable*/
84 public String getSystemId() {
85 return systemId;
86 }
87
88 /**
89 * Notify an unparsed entity URI.
90 *
91 * @param name The name of the unparsed entity
92 * @param systemID The system identifier of the unparsed entity
93 * @param publicID The public identifier of the unparsed entity
94 */
95
96 public void setUnparsedEntity(String name, String systemID, String publicID) throws XPathException {
97 }
98
99 /**
100 * Start the output process
101 */
102
103 public void open() throws XPathException {
104 previousAtomic = false;
105 }
106
107 /**
108 * Append an arbitrary item (node, atomic value, or function) to the output
109 *
110 * @param item the item to be appended
111 * @param locationId the location of the calling instruction, for diagnostics
112 * @param properties if the item is an element node, this indicates whether its namespaces
113 * need to be copied. Values are {@link ReceiverOptions#ALL_NAMESPACES},
114 * {@link ReceiverOptions#LOCAL_NAMESPACES}; the default (0) means
115 * no namespaces
116 */
117
118 public abstract void append(Item item, Location locationId, int properties) throws XPathException;
119
120 /**
121 * Append an arbitrary item (node, atomic value, or function) to the output.
122 * By default, if the item is an element
123 * node, it is copied with all namespaces.
124 *
125 * @param item the item to be appended
126 * @throws net.sf.saxon.trans.XPathException if the operation fails
127 */
128
129 public void append(Item item) throws XPathException {
130 append(item, ExplicitLocation.UNKNOWN_LOCATION, ReceiverOptions.ALL_NAMESPACES);
131 }
132
133 /**
134 * Get the name pool
135 *
136 * @return the Name Pool that was supplied using the setConfiguration() method
137 */
138
139 public NamePool getNamePool() {
140 return pipelineConfiguration.getConfiguration().getNamePool();
141 }
142
143 /**
144 * Helper method for subclasses to invoke if required: flatten an array
145 */
146
147 protected void flatten(ArrayItem array, Location locationId, int copyNamespaces) throws XPathException {
148 for (Sequence<?> member : array.members()) {
149 member.iterate().forEachOrFail(it -> append(it, locationId, copyNamespaces));
150 }
151 }
152
153 /**
154 * Helper method for subclasses to invoke if required: decompose an item into a sequence
155 * of node events. Note that when this is used, methods such as characters(), comment(),
156 * startElement(), and processingInstruction() are responsible for setting previousAtomic to false.
157 */
158
159 protected void decompose(Item item, Location locationId, int copyNamespaces) throws XPathException {
160 if (item != null) {
161 if (item instanceof AtomicValue || item instanceof ExternalObject) {
162 if (previousAtomic) {
163 characters(" ", locationId, 0);
164 }
165 characters(item.getStringValueCS(), locationId, 0);
166 previousAtomic = true;
167 } else if (item instanceof ArrayItem) {
168 flatten((ArrayItem)item, locationId, copyNamespaces);
169 } else if (item instanceof Function) {
170 String thing = item instanceof MapItem ? "map" : "function item";
171 String errorCode = getErrorCodeForDecomposingFunctionItems();
172 if (errorCode.startsWith("SENR")) {
173 throw new XPathException("Cannot serialize a " + thing + " using this output method", errorCode, locationId);
174 } else {
175 throw new XPathException("Cannot add a " + thing + " to an XDM node tree", errorCode, locationId);
176 }
177 } else {
178 NodeInfo node = (NodeInfo)item;
179 if (node instanceof Orphan && ((Orphan) node).isDisableOutputEscaping()) {
180 // see test case doe-0801, -2 -3 - needed for output buffered within try/catch, xsl:fork etc
181 characters(item.getStringValueCS(), locationId, ReceiverOptions.DISABLE_ESCAPING);
182 previousAtomic = false;
183 } else if (node.getNodeKind() == Type.DOCUMENT) {
184 startDocument(0); // needed to ensure that illegal namespaces or attributes in the content are caught
185 node.iterateAxis(AxisInfo.CHILD).forEachOrFail(
186 child -> append(child, locationId, copyNamespaces)
187 );
188 previousAtomic = false;
189 endDocument();
190 } else {
191 if (node.getNodeKind() == Type.ATTRIBUTE && ((SimpleType) node.getSchemaType()).isNamespaceSensitive()) {
192 XPathException err = new XPathException("Cannot copy attributes whose type is namespace-sensitive (QName or NOTATION): " +
193 Err.wrap(node.getDisplayName(), Err.ATTRIBUTE));
194 err.setErrorCode(getPipelineConfiguration().getHostLanguage() == Configuration.XSLT ? "XTTE0950" : "XQTY0086");
195 throw err;
196 }
197 int copyOptions = CopyOptions.TYPE_ANNOTATIONS;
198 if (copyNamespaces == ReceiverOptions.LOCAL_NAMESPACES) {
199 copyOptions |= CopyOptions.LOCAL_NAMESPACES;
200 } else if (copyNamespaces == ReceiverOptions.ALL_NAMESPACES) {
201 copyOptions |= CopyOptions.ALL_NAMESPACES;
202 }
203 ((NodeInfo) item).copy(this, copyOptions, locationId);
204 previousAtomic = false;
205 }
206 }
207 }
208 }
209
210 protected String getErrorCodeForDecomposingFunctionItems() {
211 boolean isXSLT = getPipelineConfiguration().getHostLanguage() == Configuration.XSLT;
212 return isXSLT ? "XTDE0450" : "XQTY0105";
213 // Use SENR0001 when doing sequence normalization as per the Serialization spec.
214 }
215
216 /**
217 * Ask whether this Receiver can handle arbitrary items in its {@link #append} and
218 * {@link #append(Item, Location, int)} methods. If it cannot, then calling
219 * these methods will raise an exception (typically but not necessarily an
220 * {@code UnsupportedOperationException}). This implementation returns true.
221 *
222 * @return true if the Receiver is able to handle items supplied to
223 * its {@link #append} and {@link #append(Item, Location, int)} methods. A
224 * receiver that returns true may still reject some kinds of item, for example
225 * it may reject function items.
226 */
227
228 public boolean handlesAppend() {
229 return true;
230 }
231 }
232