Debian Codegraph @

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