KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > query > SequenceWrapper


1 package net.sf.saxon.query;
2
3 import net.sf.saxon.event.Receiver;
4 import net.sf.saxon.event.SequenceReceiver;
5 import net.sf.saxon.om.Item;
6 import net.sf.saxon.om.NamePool;
7 import net.sf.saxon.om.NamespaceConstant;
8 import net.sf.saxon.om.NodeInfo;
9 import net.sf.saxon.trans.XPathException;
10 import net.sf.saxon.type.AtomicType;
11 import net.sf.saxon.value.AtomicValue;
12 import net.sf.saxon.style.StandardNames;
13
14 /**
15  * This class can be used in a push pipeline: it accepts any sequence as input, and generates
16  * a document in which the items of the sequence are wrapped by elements containing information about
17  * the types of the items in the input sequence.
18  */

19
20 public class SequenceWrapper extends SequenceReceiver {
21
22     public static final String JavaDoc RESULT_NS = QueryResult.RESULT_NS;
23
24     private Receiver out;
25     private int depth = 0;
26
27     private int resultSequence;
28     private int resultDocument;
29     private int resultElement;
30     private int resultAttribute;
31     private int resultText;
32     private int resultComment;
33     private int resultPI;
34     private int resultNamespace;
35     private int resultAtomicValue;
36     private int xsiType;
37
38     public SequenceWrapper(Receiver destination) {
39         this.out = destination;
40         setPipelineConfiguration(destination.getPipelineConfiguration());
41     }
42
43     public void open() throws XPathException {
44
45         NamePool pool = getNamePool();
46
47         resultSequence = pool.allocate("result", RESULT_NS, "sequence");
48         resultDocument = pool.allocate("result", RESULT_NS, "document");
49         resultElement = pool.allocate("result", RESULT_NS, "element");
50         resultAttribute = pool.allocate("result", RESULT_NS, "attribute");
51         resultText = pool.allocate("result", RESULT_NS, "text");
52         resultComment = pool.allocate("result", RESULT_NS, "comment");
53         resultPI = pool.allocate("result", RESULT_NS, "processing-instruction");
54         resultNamespace = pool.allocate("result", RESULT_NS, "namespace");
55         resultAtomicValue = pool.allocate("result", RESULT_NS, "atomic-value");
56         xsiType = pool.allocate("xsi", NamespaceConstant.SCHEMA_INSTANCE, "type");
57
58         out.open();
59         out.startDocument(0);
60
61         out.startElement(resultSequence, StandardNames.XDT_UNTYPED, 0, 0);
62         out.namespace(pool.allocateNamespaceCode("result", RESULT_NS), 0);
63         out.namespace(pool.allocateNamespaceCode("xs", NamespaceConstant.SCHEMA), 0);
64         out.namespace(pool.allocateNamespaceCode("xsi", NamespaceConstant.SCHEMA_INSTANCE), 0);
65         out.startContent();
66
67     }
68
69     /**
70      * Start of a document node.
71      */

72
73     public void startDocument(int properties) throws XPathException {
74         out.startElement(resultDocument, StandardNames.XDT_UNTYPED, 0, 0);
75         out.startContent();
76         depth++;
77     }
78
79     /**
80      * Notify the end of a document node
81      */

82
83     public void endDocument() throws XPathException {
84         out.endElement();
85         depth--;
86     }
87
88     /**
89      * Notify the start of an element
90      *
91      * @param nameCode integer code identifying the name of the element within the name pool.
92      * @param typeCode integer code identifying the element's type within the name pool.
93      * @param properties properties of the element node
94      */

95
96     public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException {
97         if (depth++ == 0) {
98             out.startElement(resultElement, StandardNames.XDT_UNTYPED, 0, 0);
99             out.startContent();
100         }
101         out.startElement(nameCode, typeCode, locationId, properties);
102     }
103
104     /**
105      * End of element
106      */

107
108     public void endElement() throws XPathException {
109         out.endElement();
110         if (--depth == 0) {
111             out.endElement(); // close the wrapping element
112
}
113     }
114
115     /**
116      * Notify an attribute. Attributes are notified after the startElement event, and before any
117      * children. Namespaces and attributes may be intermingled.
118      *
119      * @param nameCode The name of the attribute, as held in the name pool
120      * @param typeCode The type of the attribute, as held in the name pool
121      * @param properties Bit significant value. The following bits are defined:
122      * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
123      * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
124      * @throws IllegalStateException: attempt to output an attribute when there is no open element
125      * start tag
126      */

127
128     public void attribute(int nameCode, int typeCode, CharSequence JavaDoc value, int locationId, int properties) throws XPathException {
129         if (depth==0) {
130             out.startElement(resultAttribute, StandardNames.XDT_UNTYPED, 0, 0);
131             out.attribute(nameCode, typeCode, value, locationId, properties);
132             out.startContent();
133             out.endElement();
134         } else {
135             out.attribute(nameCode, typeCode, value, locationId, properties);
136         }
137     }
138
139     /**
140      * Notify a namespace. Namespaces are notified <b>after</b> the startElement event, and before
141      * any children for the element. The namespaces that are reported are only required
142      * to include those that are different from the parent element; however, duplicates may be reported.
143      * A namespace must not conflict with any namespaces already used for element or attribute names.
144      *
145      * @param namespaceCode an integer: the top half is a prefix code, the bottom half a URI code.
146      * These may be translated into an actual prefix and URI using the name pool. A prefix code of
147      * zero represents the empty prefix (that is, the default namespace). A URI code of zero represents
148      * a URI of "", that is, a namespace undeclaration.
149      * @throws IllegalStateException: attempt to output a namespace when there is no open element
150      * start tag
151      */

152
153     public void namespace(int namespaceCode, int properties) throws XPathException {
154         if (depth == 0) {
155             out.startElement(resultNamespace, StandardNames.XDT_UNTYPED, 0, 0);
156             out.namespace(namespaceCode, properties);
157             out.startContent();
158             out.endElement();
159         } else {
160             out.namespace(namespaceCode, properties);
161         }
162     }
163
164     /**
165      * Character data
166      */

167
168     public void characters(CharSequence JavaDoc chars, int locationId, int properties) throws XPathException {
169         if (depth==0) {
170             out.startElement(resultText, StandardNames.XDT_UNTYPED, 0, 0);
171             out.startContent();
172             out.characters(chars, locationId, properties);
173             out.endElement();
174         } else {
175             out.characters(chars, locationId, properties);
176         }
177     }
178
179     /**
180      * Output a comment
181      */

182
183     public void comment(CharSequence JavaDoc chars, int locationId, int properties) throws XPathException {
184         if (depth==0) {
185             out.startElement(resultComment, StandardNames.XDT_UNTYPED, 0, 0);
186             out.startContent();
187             out.comment(chars, locationId, properties);
188             out.endElement();
189         } else {
190             out.comment(chars, locationId, properties);
191         }
192     }
193
194     /**
195      * Processing Instruction
196      */

197
198     public void processingInstruction(String JavaDoc target, CharSequence JavaDoc data, int locationId, int properties) throws XPathException {
199         if (depth==0) {
200             out.startElement(resultPI, StandardNames.XDT_UNTYPED, 0, 0);
201             out.startContent();
202             out.processingInstruction(target, data, locationId, properties);
203             out.endElement();
204         } else {
205             out.processingInstruction(target, data, locationId, properties);
206         }
207     }
208
209     /**
210      * Output an item (atomic value or node) to the sequence
211      */

212
213     public void append(Item item, int locationId, int copyNamespaces) throws XPathException {
214         if (item instanceof AtomicValue) {
215             NamePool pool = getNamePool();
216             out.startElement(resultAtomicValue, StandardNames.XDT_UNTYPED, 0, 0);
217             AtomicType type = (AtomicType)((AtomicValue)item).getItemType();
218             int nameCode = type.getNameCode();
219             String JavaDoc prefix = pool.getPrefix(nameCode);
220             String JavaDoc localName = pool.getLocalName(nameCode);
221             String JavaDoc uri = pool.getURI(nameCode);
222             if (prefix.equals("")) {
223                 prefix = pool.suggestPrefixForURI(uri);
224                 if (prefix == null) {
225                     prefix = "p" + uri.hashCode();
226                 }
227             }
228             int nscode = pool.allocateNamespaceCode(prefix, uri);
229             String JavaDoc displayName = prefix + ':' + localName;
230             out.namespace(nscode, 0);
231             out.attribute(xsiType, StandardNames.XDT_UNTYPED_ATOMIC, displayName, 0, 0);
232             out.startContent();
233             out.characters(item.getStringValue(), 0, 0);
234             out.endElement();
235         } else {
236             ((NodeInfo)item).copy(this, NodeInfo.ALL_NAMESPACES, true, locationId);
237         }
238     }
239
240     /**
241      * Notify the start of the content, that is, the completion of all attributes and namespaces.
242      * Note that the initial receiver of output from XSLT instructions will not receive this event,
243      * it has to detect it itself. Note that this event is reported for every element even if it has
244      * no attributes, no namespaces, and no content.
245      */

246
247     public void startContent() throws XPathException {
248         out.startContent();
249     }
250
251     /**
252      * Notify the end of the event stream
253      */

254
255     public void close() throws XPathException {
256         out.endElement(); // close the result:sequence element
257
out.endDocument();
258         out.close();
259     }
260
261 }
262
263 //
264
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
265
// you may not use this file except in compliance with the License. You may obtain a copy of the
266
// License at http://www.mozilla.org/MPL/
267
//
268
// Software distributed under the License is distributed on an "AS IS" basis,
269
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
270
// See the License for the specific language governing rights and limitations under the License.
271
//
272
// The Original Code is: all this file.
273
//
274
// The Initial Developer of the Original Code is Michael H. Kay.
275
//
276
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
277
//
278
// Contributor(s): none.
279
//
280
Popular Tags