001: package net.sf.saxon.event;
002:
003: import net.sf.saxon.om.*;
004: import net.sf.saxon.trans.XPathException;
005: import net.sf.saxon.value.AtomicValue;
006: import net.sf.saxon.type.Type;
007:
008: /**
009: * A TreeReceiver acts as a bridge between a SequenceReceiver, which can receive
010: * events for constructing any kind of sequence, and an ordinary Receiver, which
011: * only handles events relating to the building of trees. To do this, it has to
012: * process any items added to the sequence using the append() interface; all other
013: * events are passed through unchanged.
014: */
015:
016: public class TreeReceiver extends SequenceReceiver {
017: private Receiver baseReceiver;
018: private String systemId;
019: private boolean contentStarted = true;
020:
021: public TreeReceiver(Receiver nextInChain) {
022: baseReceiver = nextInChain;
023: previousAtomic = false;
024: setPipelineConfiguration(nextInChain.getPipelineConfiguration());
025: }
026:
027: public void setSystemId(String systemId) {
028: if (systemId != this .systemId) {
029: this .systemId = systemId;
030: if (baseReceiver != null) {
031: baseReceiver.setSystemId(systemId);
032: }
033: }
034: }
035:
036: public String getSystemId() {
037: return systemId;
038: }
039:
040: /**
041: * Get the underlying Receiver (that is, the next one in the pipeline)
042: */
043:
044: public Receiver getUnderlyingReceiver() {
045: return baseReceiver;
046: }
047:
048: /**
049: * Start of event sequence
050: */
051:
052: public void open() throws XPathException {
053: if (baseReceiver == null) {
054: throw new IllegalStateException(
055: "TreeReceiver.open(): no underlying receiver provided");
056: }
057: baseReceiver.open();
058: previousAtomic = false;
059: }
060:
061: /**
062: * End of event sequence
063: */
064:
065: public void close() throws XPathException {
066: if (baseReceiver != null) {
067: baseReceiver.close();
068: }
069: previousAtomic = false;
070: }
071:
072: /**
073: * Start of a document node.
074: */
075:
076: public void startDocument(int properties) throws XPathException {
077: baseReceiver.startDocument(properties);
078: }
079:
080: /**
081: * Notify the end of a document node
082: */
083:
084: public void endDocument() throws XPathException {
085: baseReceiver.endDocument();
086: }
087:
088: /**
089: * Notify the start of an element
090: * @param nameCode integer code identifying the name of the element within the name pool.
091: * @param typeCode integer code identifying the element's type within the name pool.
092: * @param properties bit-significant properties of the element node
093: */
094:
095: public void startElement(int nameCode, int typeCode,
096: int locationId, int properties) throws XPathException {
097: if (!contentStarted) {
098: startContent();
099: }
100: contentStarted = false;
101: baseReceiver.startElement(nameCode, typeCode, locationId,
102: properties);
103: previousAtomic = false;
104: }
105:
106: /**
107: * Notify a namespace. Namespaces are notified <b>after</b> the startElement event, and before
108: * any children for the element. The namespaces that are reported are only required
109: * to include those that are different from the parent element; however, duplicates may be reported.
110: * A namespace must not conflict with any namespaces already used for element or attribute names.
111: * @param namespaceCode an integer: the top half is a prefix code, the bottom half a URI code.
112: * These may be translated into an actual prefix and URI using the name pool. A prefix code of
113: * zero represents the empty prefix (that is, the default namespace). A URI code of zero represents
114: * a URI of "", that is, a namespace undeclaration.
115: * @throws IllegalStateException: attempt to output a namespace when there is no open element
116: * start tag
117: */
118:
119: public void namespace(int namespaceCode, int properties)
120: throws XPathException {
121: baseReceiver.namespace(namespaceCode, properties);
122: previousAtomic = false;
123: }
124:
125: /**
126: * Notify an attribute. Attributes are notified after the startElement event, and before any
127: * children. Namespaces and attributes may be intermingled.
128: * @param nameCode The name of the attribute, as held in the name pool
129: * @param typeCode The type of the attribute, as held in the name pool
130: * @param properties Bit significant value. The following bits are defined:
131: * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
132: * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
133: * @throws IllegalStateException: attempt to output an attribute when there is no open element
134: * start tag
135: */
136:
137: public void attribute(int nameCode, int typeCode,
138: CharSequence value, int locationId, int properties)
139: throws XPathException {
140: baseReceiver.attribute(nameCode, typeCode, value, locationId,
141: properties);
142: previousAtomic = false;
143: }
144:
145: /**
146: * Notify the start of the content, that is, the completion of all attributes and namespaces.
147: * Note that the initial receiver of output from XSLT instructions will not receive this event,
148: * it has to detect it itself. Note that this event is reported for every element even if it has
149: * no attributes, no namespaces, and no content.
150: */
151:
152: public void startContent() throws XPathException {
153: contentStarted = true;
154: baseReceiver.startContent();
155: previousAtomic = false;
156: }
157:
158: /**
159: * End of element
160: */
161:
162: public void endElement() throws XPathException {
163: if (!contentStarted) {
164: startContent();
165: }
166: baseReceiver.endElement();
167: previousAtomic = false;
168: }
169:
170: /**
171: * Character data
172: */
173:
174: public void characters(CharSequence chars, int locationId,
175: int properties) throws XPathException {
176: if (!contentStarted) {
177: startContent();
178: }
179: baseReceiver.characters(chars, locationId, properties);
180: previousAtomic = false;
181: }
182:
183: /**
184: * Processing Instruction
185: */
186:
187: public void processingInstruction(String target, CharSequence data,
188: int locationId, int properties) throws XPathException {
189: if (!contentStarted) {
190: startContent();
191: }
192: baseReceiver.processingInstruction(target, data, locationId,
193: properties);
194: previousAtomic = false;
195: }
196:
197: /**
198: * Output a comment
199: */
200:
201: public void comment(CharSequence chars, int locationId,
202: int properties) throws XPathException {
203: if (!contentStarted) {
204: startContent();
205: }
206: baseReceiver.comment(chars, locationId, properties);
207: previousAtomic = false;
208: }
209:
210: /**
211: * Set the URI for an unparsed entity in the document.
212: */
213:
214: public void setUnparsedEntity(String name, String uri,
215: String publicId) throws XPathException {
216: baseReceiver.setUnparsedEntity(name, uri, publicId);
217: }
218:
219: /**
220: * Append an arbitrary item (node or atomic value) to the output
221: */
222:
223: public void append(Item item, int locationId, int copyNamespaces)
224: throws XPathException {
225: if (item instanceof AtomicValue) {
226: if (previousAtomic) {
227: characters(" ", locationId, 0);
228: }
229: characters(item.getStringValueCS(), locationId, 0);
230: previousAtomic = true;
231: } else if (((NodeInfo) item).getNodeKind() == Type.DOCUMENT) {
232: SequenceIterator iter = ((NodeInfo) item)
233: .iterateAxis(Axis.CHILD);
234: while (true) {
235: Item it = iter.next();
236: if (it == null)
237: break;
238: append(it, locationId, NodeInfo.ALL_NAMESPACES);
239: }
240: } else {
241: ((NodeInfo) item).copy(this , NodeInfo.ALL_NAMESPACES, true,
242: locationId);
243: previousAtomic = false;
244: }
245: }
246:
247: }
248:
249: //
250: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
251: // you may not use this file except in compliance with the License. You may obtain a copy of the
252: // License at http://www.mozilla.org/MPL/
253: //
254: // Software distributed under the License is distributed on an "AS IS" basis,
255: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
256: // See the License for the specific language governing rights and limitations under the License.
257: //
258: // The Original Code is: all this file.
259: //
260: // The Initial Developer of the Original Code is Michael H. Kay
261: //
262: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
263: //
264: // Contributor(s): none.
265: //
|