KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > TreeReceiver


1 package net.sf.saxon.event;
2
3 import net.sf.saxon.om.*;
4 import net.sf.saxon.trans.XPathException;
5 import net.sf.saxon.value.AtomicValue;
6
7 /**
8  * A TreeReceiver acts as a bridge between a SequenceReceiver, which can receive
9  * events for constructing any kind of sequence, and an ordinary Receiver, which
10  * only handles events relating to the building of trees. To do this, it has to
11  * process any items added to the sequence using the append() interface; all other
12  * events are passed through unchanged.
13  */

14
15 public class TreeReceiver extends SequenceReceiver {
16     private Receiver baseReceiver;
17     private String JavaDoc systemId;
18     private boolean contentStarted = true;
19
20     public TreeReceiver(Receiver nextInChain) {
21         baseReceiver = nextInChain;
22         previousAtomic = false;
23         setPipelineConfiguration(nextInChain.getPipelineConfiguration());
24     }
25
26     public void setSystemId(String JavaDoc systemId) {
27         if (systemId != this.systemId) {
28             this.systemId = systemId;
29             if (baseReceiver != null) {
30                 baseReceiver.setSystemId(systemId);
31             }
32         }
33     }
34
35     public String JavaDoc getSystemId() {
36         return systemId;
37     }
38
39     /**
40      * Get the underlying Receiver (that is, the next one in the pipeline)
41      */

42
43     public Receiver getUnderlyingReceiver() {
44         return baseReceiver;
45     }
46
47     /**
48      * Start of event sequence
49      */

50
51     public void open() throws XPathException {
52         if (baseReceiver == null) {
53             throw new IllegalStateException JavaDoc("TreeReceiver.open(): no underlying receiver provided");
54         }
55         baseReceiver.open();
56         previousAtomic = false;
57     }
58
59     /**
60      * End of event sequence
61      */

62
63     public void close() throws XPathException {
64         if (baseReceiver != null) {
65             baseReceiver.close();
66         }
67         previousAtomic = false;
68     }
69
70     /**
71      * Start of a document node.
72     */

73
74     public void startDocument(int properties) throws XPathException {
75         //if (baseReceiver!=null) {
76
baseReceiver.startDocument(properties);
77         //}
78
}
79
80     /**
81      * Notify the end of a document node
82      */

83
84     public void endDocument() throws XPathException {
85         //if (baseReceiver!=null) {
86
baseReceiver.endDocument();
87         //}
88
}
89
90     /**
91      * Notify the start of an element
92      * @param nameCode integer code identifying the name of the element within the name pool.
93      * @param typeCode integer code identifying the element's type within the name pool.
94      * @param properties bit-significant properties of the element node
95      */

96
97     public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException {
98         if (!contentStarted) {
99             startContent();
100         }
101         contentStarted = false;
102         //if (baseReceiver != null) {
103
baseReceiver.startElement(nameCode, typeCode, locationId, properties);
104         //}
105
previousAtomic = false;
106     }
107
108     /**
109      * Notify a namespace. Namespaces are notified <b>after</b> the startElement event, and before
110      * any children for the element. The namespaces that are reported are only required
111      * to include those that are different from the parent element; however, duplicates may be reported.
112      * A namespace must not conflict with any namespaces already used for element or attribute names.
113      * @param namespaceCode an integer: the top half is a prefix code, the bottom half a URI code.
114      * These may be translated into an actual prefix and URI using the name pool. A prefix code of
115      * zero represents the empty prefix (that is, the default namespace). A URI code of zero represents
116      * a URI of "", that is, a namespace undeclaration.
117      * @throws IllegalStateException: attempt to output a namespace when there is no open element
118      * start tag
119      */

120
121     public void namespace(int namespaceCode, int properties) throws XPathException {
122         //if (baseReceiver != null) {
123
baseReceiver.namespace(namespaceCode, properties);
124         //}
125
previousAtomic = false;
126     }
127
128     /**
129      * Notify an attribute. Attributes are notified after the startElement event, and before any
130      * children. Namespaces and attributes may be intermingled.
131      * @param nameCode The name of the attribute, as held in the name pool
132      * @param typeCode The type of the attribute, as held in the name pool
133      * @param properties Bit significant value. The following bits are defined:
134      * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
135      * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
136      * @throws IllegalStateException: attempt to output an attribute when there is no open element
137      * start tag
138      */

139
140     public void attribute(int nameCode, int typeCode, CharSequence JavaDoc value, int locationId, int properties)
141             throws XPathException {
142         //if (baseReceiver != null) {
143
baseReceiver.attribute(nameCode, typeCode, value, locationId, properties);
144         //}
145
previousAtomic = false;
146     }
147
148     /**
149      * Notify the start of the content, that is, the completion of all attributes and namespaces.
150      * Note that the initial receiver of output from XSLT instructions will not receive this event,
151      * it has to detect it itself. Note that this event is reported for every element even if it has
152      * no attributes, no namespaces, and no content.
153      */

154
155
156     public void startContent() throws XPathException {
157         contentStarted = true;
158         //if (baseReceiver != null) {
159
baseReceiver.startContent();
160         //}
161
previousAtomic = false;
162     }
163
164     /**
165      * End of element
166      */

167
168     public void endElement() throws XPathException {
169         if (!contentStarted) {
170             startContent();
171         }
172         //if (baseReceiver != null) {
173
baseReceiver.endElement();
174         //}
175
previousAtomic = false;
176     }
177
178     /**
179      * Character data
180      */

181
182     public void characters(CharSequence JavaDoc chars, int locationId, int properties) throws XPathException {
183         if (!contentStarted) {
184             startContent();
185         }
186         //if (baseReceiver != null) {
187
baseReceiver.characters(chars, locationId, properties);
188         //}
189
previousAtomic = false;
190     }
191
192
193     /**
194      * Processing Instruction
195      */

196
197     public void processingInstruction(String JavaDoc target, CharSequence JavaDoc data, int locationId, int properties) throws XPathException {
198         if (!contentStarted) {
199             startContent();
200         }
201         //if (baseReceiver != null) {
202
baseReceiver.processingInstruction(target, data, locationId, properties);
203         //}
204
previousAtomic = false;
205     }
206
207     /**
208      * Output a comment
209      */

210
211     public void comment(CharSequence JavaDoc chars, int locationId, int properties) throws XPathException {
212         if (!contentStarted) {
213             startContent();
214         }
215         //if (baseReceiver != null) {
216
baseReceiver.comment(chars, locationId, properties);
217         //}
218
previousAtomic = false;
219     }
220
221
222     /**
223      * Set the URI for an unparsed entity in the document.
224      */

225
226     public void setUnparsedEntity(String JavaDoc name, String JavaDoc uri, String JavaDoc publicId) throws XPathException {
227         //if (baseReceiver != null) {
228
baseReceiver.setUnparsedEntity(name, uri, publicId);
229         //}
230
}
231
232     /**
233      * Append an arbitrary item (node or atomic value) to the output
234      */

235
236     public void append(Item item, int locationId, int copyNamespaces) throws XPathException {
237         if (item instanceof AtomicValue) {
238             if (previousAtomic) {
239                 characters(" ", locationId, 0);
240             }
241             characters(item.getStringValueCS(), locationId, 0);
242             previousAtomic = true;
243         } else if (item instanceof DocumentInfo) {
244             SequenceIterator iter = ((DocumentInfo)item).iterateAxis(Axis.CHILD);
245             while (true) {
246                 Item it = iter.next();
247                 if (it == null) break;
248                 append(it, locationId, NodeInfo.ALL_NAMESPACES);
249             }
250         } else {
251             ((NodeInfo)item).copy(this, NodeInfo.ALL_NAMESPACES, true, locationId);
252             previousAtomic = false;
253         }
254     }
255
256 }
257
258 //
259
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
260
// you may not use this file except in compliance with the License. You may obtain a copy of the
261
// License at http://www.mozilla.org/MPL/
262
//
263
// Software distributed under the License is distributed on an "AS IS" basis,
264
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
265
// See the License for the specific language governing rights and limitations under the License.
266
//
267
// The Original Code is: all this file.
268
//
269
// The Initial Developer of the Original Code is Michael H. Kay
270
//
271
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
272
//
273
// Contributor(s): none.
274
//
275
Popular Tags