Debian Codegraph @

net/sf/saxon/query/SequenceWrapper.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.query;
9
10 import net.sf.saxon.event.Receiver;
11 import net.sf.saxon.event.SequenceReceiver;
12 import net.sf.saxon.expr.parser.ExplicitLocation;
13 import net.sf.saxon.expr.parser.Location;
14 import net.sf.saxon.lib.NamespaceConstant;
15 import net.sf.saxon.ma.arrays.ArrayItem;
16 import net.sf.saxon.ma.map.MapItem;
17 import net.sf.saxon.om.*;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.type.*;
20 import net.sf.saxon.value.AtomicValue;
21 import net.sf.saxon.value.ObjectValue;
22
23 /**
24 * This class can be used in a push pipeline: it accepts any sequence as input, and generates
25 * a document in which the items of the sequence are wrapped by elements containing information about
26 * the types of the items in the input sequence.
27 */
28
29 public class SequenceWrapper extends SequenceReceiver {
30
31 public static final String RESULT_NS = QueryResult.RESULT_NS;
32
33 private Receiver out;
34 private int depth = 0;
35
36 private FingerprintedQName resultDocument;
37 private FingerprintedQName resultElement;
38 private FingerprintedQName resultAttribute;
39 private FingerprintedQName resultText;
40 private FingerprintedQName resultComment;
41 private FingerprintedQName resultPI;
42 private FingerprintedQName resultNamespace;
43 private FingerprintedQName resultAtomicValue;
44 private FingerprintedQName resultFunction;
45 private FingerprintedQName resultArray;
46 private FingerprintedQName resultMap;
47 private FingerprintedQName resultExternalValue;
48 private FingerprintedQName xsiType;
49
50 /**
51 * Create a sequence wrapper. This creates an XML representation of the items sent to destination
52 * in which the types of all items are made explicit
53 *
54 * @param destination the sequence being wrapped
55 */
56
57 public SequenceWrapper(Receiver destination) {
58 super(destination.getPipelineConfiguration());
59 out = destination;
60 // out = new TracingFilter(out);
61 }
62
63 public Receiver getDestination() {
64 return out;
65 }
66
67 public void open() throws XPathException {
68
69 //@SuppressWarnings({"FieldCanBeLocal"})
70 FingerprintedQName resultSequence = new FingerprintedQName("result", RESULT_NS, "sequence");
71 resultDocument = new FingerprintedQName("result", RESULT_NS, "document");
72 resultElement = new FingerprintedQName("result", RESULT_NS, "element");
73 resultAttribute = new FingerprintedQName("result", RESULT_NS, "attribute");
74 resultText = new FingerprintedQName("result", RESULT_NS, "text");
75 resultComment = new FingerprintedQName("result", RESULT_NS, "comment");
76 resultPI = new FingerprintedQName("result", RESULT_NS, "processing-instruction");
77 resultNamespace = new FingerprintedQName("result", RESULT_NS, "namespace");
78 resultAtomicValue = new FingerprintedQName("result", RESULT_NS, "atomic-value");
79 resultFunction = new FingerprintedQName("result", RESULT_NS, "function");
80 resultArray = new FingerprintedQName("result", RESULT_NS, "array");
81 resultMap = new FingerprintedQName("result", RESULT_NS, "map");
82 resultExternalValue = new FingerprintedQName("result", RESULT_NS, "external-object");
83 xsiType = new FingerprintedQName("xsi", NamespaceConstant.SCHEMA_INSTANCE, "type");
84
85 out.open();
86 out.startDocument(0);
87
88 out.startElement(resultSequence, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
89 out.namespace(new NamespaceBinding("result", RESULT_NS), 0);
90 out.namespace(new NamespaceBinding("xs", NamespaceConstant.SCHEMA), 0);
91 out.namespace(new NamespaceBinding("xsi", NamespaceConstant.SCHEMA_INSTANCE), 0);
92 out.startContent();
93
94 }
95
96 /**
97 * Start of a document node.
98 */
99
100 public void startDocument(int properties) throws XPathException {
101 out.startElement(resultDocument, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
102 out.startContent();
103 depth++;
104 }
105
106 /**
107 * Notify the end of a document node
108 */
109
110 public void endDocument() throws XPathException {
111 out.endElement();
112 depth--;
113 }
114
115 /**
116 * Notify the start of an element
117 * @param nameCode integer code identifying the name of the element within the name pool.
118 * @param typeCode integer code identifying the element's type within the name pool.
119 * @param location location of the element
120 * @param properties properties of the element node
121 */
122
123 public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException {
124 if (depth++ == 0) {
125 out.startElement(resultElement, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
126 out.startContent();
127 }
128 out.startElement(nameCode, typeCode, location, properties);
129 }
130
131 /**
132 * End of element
133 */
134
135 public void endElement() throws XPathException {
136 out.endElement();
137 if (--depth == 0) {
138 out.endElement(); // close the wrapping element
139 }
140 }
141
142 /**
143 * Notify an attribute. Attributes are notified after the startElement event, and before any
144 * children. Namespaces and attributes may be intermingled.
145 *
146 * @param attName The name of the attribute
147 * @param typeCode The type of the attribute
148 * @param locationId location of the attribute
149 * @param properties Bit significant value. The following bits are defined:
150 * <dl>
151 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
152 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
153 * </dl>
154 * @throws IllegalStateException: attempt to output an attribute when there is no open element
155 * start tag
156 */
157
158 public void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
159 if (depth == 0) {
160 out.startElement(resultAttribute, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
161 if (!attName.hasURI("")) {
162 out.namespace(attName.getNamespaceBinding(), 0);
163 }
164 out.attribute(attName, typeCode, value, locationId, properties);
165 out.startContent();
166 out.endElement();
167 } else {
168 out.attribute(attName, typeCode, value, locationId, properties);
169 }
170 }
171
172 /**
173 * Notify a namespace. Namespaces are notified <b>after</b> the startElement event, and before
174 * any children for the element. The namespaces that are reported are only required
175 * to include those that are different from the parent element; however, duplicates may be reported.
176 * A namespace must not conflict with any namespaces already used for element or attribute names.
177 *
178 * @param namespaceBindings the namespace binding or bindings being notified
179 * @throws IllegalStateException: attempt to output a namespace when there is no open element
180 * start tag
181 */
182
183 public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException {
184 if (depth == 0) {
185 out.startElement(resultNamespace, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
186 out.namespace(namespaceBindings, properties);
187 out.startContent();
188 out.endElement();
189 } else {
190 out.namespace(namespaceBindings, properties);
191 }
192 }
193
194 /**
195 * Character data
196 */
197
198 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
199 if (depth == 0) {
200 out.startElement(resultText, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
201 out.startContent();
202 out.characters(chars, locationId, properties);
203 out.endElement();
204 } else {
205 out.characters(chars, locationId, properties);
206 }
207 }
208
209 /**
210 * Output a comment
211 */
212
213 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
214 if (depth == 0) {
215 out.startElement(resultComment, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
216 out.startContent();
217 out.comment(chars, locationId, properties);
218 out.endElement();
219 } else {
220 out.comment(chars, locationId, properties);
221 }
222 }
223
224 /**
225 * Processing Instruction
226 */
227
228 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
229 if (depth == 0) {
230 out.startElement(resultPI, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
231 out.startContent();
232 out.processingInstruction(target, data, locationId, properties);
233 out.endElement();
234 } else {
235 out.processingInstruction(target, data, locationId, properties);
236 }
237 }
238
239 /**
240 * Output an item (atomic value or node) to the sequence
241 */
242
243 public void append(/*@NotNull*/ Item item, Location locationId, int copyNamespaces) throws XPathException {
244 if (item instanceof AtomicValue) {
245 final NamePool pool = getNamePool();
246 out.startElement(resultAtomicValue, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
247 AtomicType type = ((AtomicValue) item).getItemType();
248 StructuredQName name = type.getStructuredQName();
249 String prefix = name.getPrefix();
250 String localName = name.getLocalPart();
251 String uri = name.getURI();
252 if (prefix.isEmpty()) {
253 prefix = pool.suggestPrefixForURI(uri);
254 if (prefix == null) {
255 prefix = "p" + uri.hashCode();
256 }
257 }
258 String displayName = prefix + ':' + localName;
259 out.namespace(new NamespaceBinding(prefix, uri), 0);
260 out.attribute(xsiType, BuiltInAtomicType.UNTYPED_ATOMIC, displayName, locationId, 0);
261 out.startContent();
262 out.characters(item.getStringValue(), locationId, 0);
263 out.endElement();
264 } else if (item instanceof NodeInfo) {
265 ((NodeInfo) item).copy(this, CopyOptions.ALL_NAMESPACES | CopyOptions.TYPE_ANNOTATIONS, locationId);
266 } else if (item instanceof Function) {
267 if (item instanceof MapItem) {
268 out.startElement(resultMap, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
269 out.startContent();
270 out.characters(item.toShortString(), locationId, 0);
271 out.endElement();
272 } else if (item instanceof ArrayItem) {
273 out.startElement(resultArray, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
274 out.startContent();
275 out.characters(item.toShortString(), locationId, 0);
276 out.endElement();
277 } else {
278 out.startElement(resultFunction, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
279 out.startContent();
280 out.characters(((Function)item).getDescription(), locationId, 0);
281 out.endElement();
282 }
283 } else if (item instanceof ObjectValue) {
284 Object obj = ((ObjectValue)item).getObject();
285 out.startElement(resultExternalValue, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0);
286 out.attribute(new NoNamespaceName("class"), BuiltInAtomicType.UNTYPED_ATOMIC,
287 obj.getClass().getName(), ExplicitLocation.UNKNOWN_LOCATION, 0);
288 out.startContent();
289 out.characters(obj.toString(), locationId, 0);
290 out.endElement();
291 }
292 }
293
294 /**
295 * Notify the start of the content, that is, the completion of all attributes and namespaces.
296 * Note that the initial receiver of output from XSLT instructions will not receive this event,
297 * it has to detect it itself. Note that this event is reported for every element even if it has
298 * no attributes, no namespaces, and no content.
299 */
300
301 public void startContent() throws XPathException {
302 out.startContent();
303 }
304
305 /**
306 * Notify the end of the event stream
307 */
308
309 public void close() throws XPathException {
310 out.endElement(); // close the result:sequence element
311 out.endDocument();
312 out.close();
313 }
314
315 /**
316 * Ask whether this Receiver (or the downstream pipeline) makes any use of the type annotations
317 * supplied on element and attribute events
318 *
319 * @return true if the Receiver makes any use of this information. If false, the caller
320 * may supply untyped nodes instead of supplying the type annotation
321 */
322
323 public boolean usesTypeAnnotations() {
324 return true;
325 }
326 }
327