KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > pull > PullPushTee


1 package net.sf.saxon.pull;
2
3 import net.sf.saxon.event.Receiver;
4 import net.sf.saxon.event.SequenceReceiver;
5 import net.sf.saxon.om.AttributeCollection;
6 import net.sf.saxon.om.NamespaceDeclarations;
7 import net.sf.saxon.om.Orphan;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.type.Type;
10
11 /**
12  * PullPushTee is a pass-through filter class that links one PullProvider to another PullProvider
13  * in a pipeline, copying all events that are read into a push pipeline, supplied in the form
14  * of a Receiver.
15  *
16  * <p>This class can be used to insert a schema validator into a pull pipeline, since Saxon's schema
17  * validation is push-based. It could also be used to insert a serializer into the pipeline, allowing
18  * the XML document being "pulled" to be displayed for diagnostic purposes.</p>
19 */

20
21 public class PullPushTee extends PullFilter {
22
23     private Receiver branch;
24     boolean previousAtomic = false;
25
26     /**
27      * Create a PullPushTee
28      * @param base the PullProvider to which requests are to be passed
29      * @param branch the Receiver to which all events are to be copied, as "push" events
30      */

31
32     public PullPushTee(PullProvider base, Receiver branch) throws XPathException {
33         super(base);
34         this.branch = branch;
35         branch.open();
36     }
37
38     /**
39      * Get the Receiver to which events are being tee'd.
40      */

41
42     public Receiver getReceiver() {
43         return branch;
44     }
45
46     /**
47      * Get the next event. This implementation gets the next event from the underlying PullProvider,
48      * copies it to the branch Receiver, and then returns the event to the caller.
49      *
50      * @return an integer code indicating the type of event. The code
51      * {@link #END_OF_INPUT} is returned at the end of the sequence.
52      */

53
54     public int next() throws XPathException {
55         currentEvent = super.next();
56         copyEvent(currentEvent);
57         return currentEvent;
58     }
59
60
61     /**
62      * Copy a pull event to a Receiver
63      */

64
65     private void copyEvent(int event) throws XPathException {
66         PullProvider in = getUnderlyingProvider();
67         Receiver out = branch;
68         switch (event) {
69             case START_DOCUMENT:
70                 out.startDocument(0);
71                 break;
72
73             case START_ELEMENT:
74                 out.startElement(in.getNameCode(), in.getTypeAnnotation(), 0, 0);
75
76                 NamespaceDeclarations decl = in.getNamespaceDeclarations();
77                 for (int i=0; i<decl.getLength(); i++) {
78                     out.namespace(decl.getNamespaceCode(i), 0);
79                 }
80
81                 AttributeCollection atts = in.getAttributes();
82                 for (int i=0; i<atts.getLength(); i++) {
83                     out.attribute(atts.getNameCode(i), atts.getTypeAnnotation(i),
84                             atts.getValue(i), 0, atts.getProperties(i));
85                 }
86
87                 out.startContent();
88                 break;
89
90             case TEXT:
91
92                 out.characters(in.getStringValue(), 0, 0);
93                 break;
94
95             case COMMENT:
96
97                 out.comment(in.getStringValue(), 0, 0);
98                 break;
99
100             case PROCESSING_INSTRUCTION:
101
102                 out.processingInstruction(
103                         in.getPipelineConfiguration().getConfiguration().getNamePool().getLocalName(in.getNameCode()),
104                         in.getStringValue(), 0, 0);
105                 break;
106
107             case END_ELEMENT:
108
109                 out.endElement();
110                 break;
111
112             case END_DOCUMENT:
113                 out.endDocument();
114                 break;
115
116             case END_OF_INPUT:
117                 in.close();
118                 out.close();
119                 break;
120
121             case ATOMIC_VALUE:
122                 if (out instanceof SequenceReceiver) {
123                     ((SequenceReceiver)out).append(super.getAtomicValue(), 0, 0);
124                     break;
125                 } else {
126                     if (previousAtomic) {
127                         out.characters(" ", 0, 0);
128                     }
129                     CharSequence JavaDoc chars = in.getStringValue();
130                     out.characters(chars, 0, 0);
131                     break;
132                 }
133
134             case ATTRIBUTE:
135                 if (out instanceof SequenceReceiver) {
136                     Orphan o = new Orphan(in.getPipelineConfiguration().getConfiguration());
137                     o.setNameCode(getNameCode());
138                     o.setNodeKind(Type.ATTRIBUTE);
139                     o.setStringValue(getStringValue());
140                     ((SequenceReceiver)out).append(o, 0, 0);
141                     break;
142                 } else {
143                     out.attribute(getNameCode(), getTypeAnnotation(), getStringValue(), 0, 0);
144                     break;
145                     //throw new DynamicError("Cannot serialize a free-standing attribute node");
146
}
147
148             case NAMESPACE:
149                  if (out instanceof SequenceReceiver) {
150                     Orphan o = new Orphan(in.getPipelineConfiguration().getConfiguration());
151                     o.setNameCode(getNameCode());
152                     o.setNodeKind(Type.NAMESPACE);
153                     o.setStringValue(getStringValue());
154                     ((SequenceReceiver)out).append(o, 0, 0);
155                     break;
156                 } else {
157                      int nsCode = getNamePool().getNamespaceCode(getNameCode());
158                      out.namespace(nsCode, 0);
159                      break;
160                     //throw new DynamicError("Cannot serialize a free-standing namespace node");
161
}
162
163             default:
164                 throw new UnsupportedOperationException JavaDoc("" + event);
165
166         }
167         previousAtomic = (event == ATOMIC_VALUE);
168     }
169 }
170
171 //
172
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
173
// you may not use this file except in compliance with the License. You may obtain a copy of the
174
// License at http://www.mozilla.org/MPL/
175
//
176
// Software distributed under the License is distributed on an "AS IS" basis,
177
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
178
// See the License for the specific language governing rights and limitations under the License.
179
//
180
// The Original Code is: all this file.
181
//
182
// The Initial Developer of the Original Code is Michael H. Kay.
183
//
184
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
185
//
186
// Contributor(s): none.
187
//
188
Popular Tags