Debian Codegraph @

net/sf/saxon/pull/PullPushTee.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.pull;
9
10 import net.sf.saxon.event.Receiver;
11 import net.sf.saxon.event.ReceiverOptions;
12 import net.sf.saxon.event.SequenceReceiver;
13 import net.sf.saxon.expr.parser.ExplicitLocation;
14 import net.sf.saxon.expr.parser.Location;
15 import net.sf.saxon.om.AttributeCollection;
16 import net.sf.saxon.om.NamespaceBinding;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.util.Orphan;
19 import net.sf.saxon.type.SimpleType;
20 import net.sf.saxon.type.Type;
21
22 import java.util.List;
23
24 /**
25 * PullPushTee is a pass-through filter class that links one PullProvider to another PullProvider
26 * in a pipeline, copying all events that are read into a push pipeline, supplied in the form
27 * of a Receiver.
28 * <p>This class can be used to insert a schema validator into a pull pipeline, since Saxon's schema
29 * validation is push-based. It could also be used to insert a serializer into the pipeline, allowing
30 * the XML document being "pulled" to be displayed for diagnostic purposes.</p>
31 */
32
33 public class PullPushTee extends PullFilter {
34
35 private Receiver branch;
36 boolean previousAtomic = false;
37
38 /**
39 * Create a PullPushTee
40 *
41 * @param base the PullProvider to which requests are to be passed
42 * @param branch the Receiver to which all events are to be copied, as "push" events.
43 * This Receiver must already be open before use
44 */
45
46 public PullPushTee(/*@NotNull*/ PullProvider base, Receiver branch) throws XPathException {
47 super(base);
48 this.branch = branch;
49 }
50
51 /**
52 * Get the Receiver to which events are being tee'd.
53 *
54 * @return the Receiver
55 */
56
57 public Receiver getReceiver() {
58 return branch;
59 }
60
61 /**
62 * Get the next event. This implementation gets the next event from the underlying PullProvider,
63 * copies it to the branch Receiver, and then returns the event to the caller.
64 *
65 * @return an integer code indicating the type of event. The code
66 * {@link #END_OF_INPUT} is returned at the end of the sequence.
67 */
68
69 public int next() throws XPathException {
70 currentEvent = super.next();
71 copyEvent(currentEvent);
72 return currentEvent;
73 }
74
75
76 /**
77 * Copy a pull event to a Receiver
78 *
79 * @param event the pull event to be copied
80 */
81
82 private void copyEvent(int event) throws XPathException {
83 PullProvider in = getUnderlyingProvider();
84 Location loc = in.getSourceLocator();
85 if (loc == null) {
86 loc = ExplicitLocation.UNKNOWN_LOCATION;
87 }
88 Receiver out = branch;
89 switch (event) {
90 case START_DOCUMENT:
91 out.startDocument(0);
92 break;
93
94 case START_ELEMENT:
95 out.startElement(in.getNodeName(), in.getSchemaType(), loc, 0);
96
97 NamespaceBinding[] decl = in.getNamespaceDeclarations();
98 for (NamespaceBinding aDecl : decl) {
99 if (aDecl == null) {
100 break;
101 }
102 out.namespace(aDecl, 0);
103 }
104
105 AttributeCollection atts = in.getAttributes();
106 for (int i = 0; i < atts.getLength(); i++) {
107 out.attribute(atts.getNodeName(i), atts.getTypeAnnotation(i),
108 atts.getValue(i), loc, atts.getProperties(i));
109 }
110
111 out.startContent();
112 break;
113
114 case TEXT:
115
116 out.characters(in.getStringValue(), loc, ReceiverOptions.WHOLE_TEXT_NODE);
117 break;
118
119 case COMMENT:
120
121 out.comment(in.getStringValue(), loc, 0);
122 break;
123
124 case PROCESSING_INSTRUCTION:
125
126 out.processingInstruction(
127 in.getNodeName().getLocalPart(),
128 in.getStringValue(), loc, 0);
129 break;
130
131 case END_ELEMENT:
132
133 out.endElement();
134 break;
135
136 case END_DOCUMENT:
137 List entities = in.getUnparsedEntities();
138 if (entities != null) {
139 for (Object entity : entities) {
140 UnparsedEntity ue = (UnparsedEntity) entity;
141 out.setUnparsedEntity(ue.getName(), ue.getSystemId(), ue.getPublicId());
142 }
143 }
144 out.endDocument();
145 break;
146
147 case END_OF_INPUT:
148 in.close();
149 break;
150
151 case ATOMIC_VALUE:
152 if (out instanceof SequenceReceiver) {
153 out.append(super.getAtomicValue(), loc, 0);
154 break;
155 } else {
156 if (previousAtomic) {
157 out.characters(" ", loc, 0);
158 }
159 CharSequence chars = in.getStringValue();
160 out.characters(chars, loc, 0);
161 break;
162 }
163
164 case ATTRIBUTE:
165 if (out instanceof SequenceReceiver) {
166 Orphan o = new Orphan(in.getPipelineConfiguration().getConfiguration());
167 o.setNodeName(getNodeName());
168 o.setNodeKind(Type.ATTRIBUTE);
169 o.setStringValue(getStringValue());
170 out.append(o, loc, 0);
171 break;
172 } else {
173 out.attribute(getNodeName(),
174 (SimpleType) in.getSchemaType(), getStringValue(), loc, 0);
175 break;
176 }
177
178 case NAMESPACE:
179 if (out instanceof SequenceReceiver) {
180 Orphan o = new Orphan(in.getPipelineConfiguration().getConfiguration());
181 o.setNodeName(getNodeName());
182 o.setNodeKind(Type.NAMESPACE);
183 o.setStringValue(getStringValue());
184 out.append(o, loc, 0);
185 break;
186 } else {
187 out.namespace(new NamespaceBinding(getNodeName().getPrefix(), getNodeName().getURI()), 0);
188 break;
189 }
190
191 default:
192 throw new UnsupportedOperationException("" + event);
193
194 }
195 previousAtomic = event == ATOMIC_VALUE;
196 }
197 }
198