| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 29 |
|
| 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 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public TracingFilter(Receiver nextReceiver) { |
| 46 |
super(nextReceiver); |
| 47 |
id = nextid++; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public TracingFilter(Receiver nextReceiver, PrintStream diagnosticOutput) { |
| 59 |
super(nextReceiver); |
| 60 |
id = nextid++; |
| 61 |
out = diagnosticOutput; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public int getId() { |
| 71 |
return id; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 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 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 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 |
|
| 131 |
|
| 132 |
|
| 133 |
public void close() throws XPathException { |
| 134 |
out.println("RCVR " + id + indent + " CLOSE"); |
| 135 |
nextReceiver.close(); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 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 |
|
| 149 |
|
| 150 |
|
| 151 |
public void endDocument() throws XPathException { |
| 152 |
out.println("RCVR " + id + indent + " END DOCUMENT"); |
| 153 |
nextReceiver.endDocument(); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 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 |
|
| 190 |
|
| 191 |
|
| 192 |
public void open() throws XPathException { |
| 193 |
out.println("RCVR " + id + indent + " OPEN"); |
| 194 |
nextReceiver.open(); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public void startContent() throws XPathException { |
| 215 |
out.println("RCVR " + id + indent + " START CONTENT"); |
| 216 |
nextReceiver.startContent(); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 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 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 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 |
|