Debian Codegraph @

net/sf/saxon/trace/TracingFilter.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.trace;
9
10 import net.sf.saxon.event.ProxyReceiver;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.event.ReceiverOptions;
13 import net.sf.saxon.event.SequenceReceiver;
14 import net.sf.saxon.expr.parser.Location;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.om.NamespaceBinding;
17 import net.sf.saxon.om.NamespaceBindingSet;
18 import net.sf.saxon.om.NodeName;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.tree.util.FastStringBuffer;
21 import net.sf.saxon.type.SchemaType;
22 import net.sf.saxon.type.SimpleType;
23 import net.sf.saxon.value.Whitespace;
24
25 import java.io.PrintStream;
26
27 /**
28 * A filter that can be inserted into a Receiver pipeline to trace the events that pass through.
29 * This class is not normally used in Saxon, but is available for diagnostics when needed.
30 */
31 public class TracingFilter extends ProxyReceiver {
32
33 private static int nextid = 0;
34 private int id;
35 private String indent = "";
36 private PrintStream out = System.err;
37 private boolean closed = false;
38
39 /**
40 * Create a TracingFilter and allocate a unique Id.
41 *
42 * @param nextReceiver the underlying receiver to which the events will be sent
43 */
44
45 public TracingFilter(Receiver nextReceiver) {
46 super(nextReceiver);
47 id = nextid++;
48 }
49
50 /**
51 * Create a TracingFilter, allocate a unique Id, and supply the destination for diagnostic
52 * trace messages
53 *
54 * @param nextReceiver the underlying receiver to which the events will be sent
55 * @param diagnosticOutput the destination for diagnostic trace messages
56 */
57
58 public TracingFilter(Receiver nextReceiver, PrintStream diagnosticOutput) {
59 super(nextReceiver);
60 id = nextid++;
61 out = diagnosticOutput;
62 }
63
64 /**
65 * Get the unique id that was allocated to this TracingFilter
66 *
67 * @return the unique id (which is included in all diagnostic messages)
68 */
69
70 public int getId() {
71 return id;
72 }
73
74 /**
75 * Append an arbitrary item (node or atomic value) to the output
76 *
77 * @param item the item to be appended
78 * @param locationId the location of the calling instruction, for diagnostics
79 * @param copyNamespaces if the item is an element node, this indicates whether its namespaces
80 * need to be copied. Values are {@link ReceiverOptions#ALL_NAMESPACES},
81 * {@link ReceiverOptions#LOCAL_NAMESPACES}; the default (0) means
82 * no namespaces
83 */
84
85 public void append(Item item, Location locationId, int copyNamespaces) throws XPathException {
86 out.println("RCVR " + id + indent + " APPEND " + item.getClass().getName());
87 if (nextReceiver instanceof SequenceReceiver) {
88 ((SequenceReceiver) nextReceiver).append(item, locationId, copyNamespaces);
89 } else {
90 super.append(item, locationId, copyNamespaces);
91 }
92 }
93
94 /**
95 * Notify an attribute. Attributes are notified after the startElement event, and before any
96 * children. Namespaces and attributes may be intermingled.
97 *
98 * @param nameCode The name of the attribute
99 * @param typeCode The type of the attribute
100 * @param locationId the location of the node in the source, or of the instruction that created it
101 * @param properties Bit significant value. The following bits are defined:
102 * <dl>
103 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
104 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
105 * </dl>
106 * @throws IllegalStateException: attempt to output an attribute when there is no open element
107 * start tag
108 */
109
110 public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
111 out.println("RCVR " + id + indent + " ATTRIBUTE " + nameCode.getDisplayName());
112 nextReceiver.attribute(nameCode, typeCode, value, locationId, properties);
113 }
114
115 /**
116 * Character data
117 */
118
119 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
120 out.println("RCVR " + id + indent + " CHARACTERS " + (Whitespace.isWhite(chars) ? "(whitespace)" : ""));
121 FastStringBuffer sb = new FastStringBuffer(chars.length() * 3);
122 for (int i = 0; i < chars.length(); i++) {
123 sb.append((int) chars.charAt(i) + " ");
124 }
125 out.println(" \"" + sb + '\"');
126 nextReceiver.characters(chars, locationId, properties);
127 }
128
129 /**
130 * End of document
131 */
132
133 public void close() throws XPathException {
134 out.println("RCVR " + id + indent + " CLOSE");
135 nextReceiver.close();
136 }
137
138 /**
139 * Output a comment
140 */
141
142 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
143 out.println("RCVR " + id + indent + " COMMENT");
144 nextReceiver.comment(chars, locationId, properties);
145 }
146
147 /**
148 * Notify the end of a document node
149 */
150
151 public void endDocument() throws XPathException {
152 out.println("RCVR " + id + indent + " END DOCUMENT");
153 nextReceiver.endDocument();
154 }
155
156 /**
157 * End of element
158 */
159
160 public void endElement() throws XPathException {
161 if (indent.isEmpty()) {
162 throw new XPathException("RCVR " + id + "Unmatched end tag");
163 }
164 indent = indent.substring(2);
165 out.println("RCVR " + id + indent + " END ELEMENT");
166 nextReceiver.endElement();
167 }
168
169 /**
170 * Notify a namespace. Namespaces are notified <b>after</b> the startElement event, and before
171 * any children for the element. The namespaces that are reported are only required
172 * to include those that are different from the parent element; however, duplicates may be reported.
173 * A namespace must not conflict with any namespaces already used for element or attribute names.
174 *
175 * @param namespaceBindings the namespace (prefix, uri) pair to be notified
176 * @throws IllegalStateException: attempt to output a namespace when there is no open element
177 * start tag
178 */
179
180 public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException {
181 for (NamespaceBinding ns : namespaceBindings) {
182 out.println("RCVR " + id + indent + " NAMESPACE " +
183 ns.getPrefix() + "=" + ns.getURI());
184 }
185 nextReceiver.namespace(namespaceBindings, properties);
186 }
187
188 /**
189 * Start of event stream
190 */
191
192 public void open() throws XPathException {
193 out.println("RCVR " + id + indent + " OPEN");
194 nextReceiver.open();
195 }
196
197 /**
198 * Processing Instruction
199 */
200
201 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
202 out.println("RCVR " + id + indent + " PROCESSING INSTRUCTION");
203 nextReceiver.processingInstruction(target, data, locationId, properties);
204 }
205
206 /**
207 * Notify the start of the content, that is, the completion of all attributes and namespaces.
208 * Note that the initial receiver of output from XSLT instructions will not receive this event,
209 * it has to detect it itself. Note that this event is reported for every element even if it has
210 * no attributes, no namespaces, and no content.
211 */
212
213
214 public void startContent() throws XPathException {
215 out.println("RCVR " + id + indent + " START CONTENT");
216 nextReceiver.startContent();
217 }
218
219 /**
220 * Start of a document node.
221 */
222
223 public void startDocument(int properties) throws XPathException {
224 out.println("RCVR " + id + indent + " START DOCUMENT");
225 nextReceiver.startDocument(properties);
226 }
227
228 /**
229 * Notify the start of an element
230 * @param nameCode integer code identifying the name of the element within the name pool.
231 * @param typeCode integer code identifying the element's type within the name pool.
232 * @param location provides information such as line number and system ID.
233 * @param properties properties of the element node
234 */
235
236 public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException {
237 out.println("RCVR " + id + indent + " START ELEMENT " + nameCode.getDisplayName());
238 indent = indent + " ";
239 nextReceiver.startElement(nameCode, typeCode, location, properties);
240 }
241 }
242