001: /*
002: * (C) Copyright 2002-2003, Andy Clark. All rights reserved.
003: *
004: * This file is distributed under an Apache style license. Please
005: * refer to the LICENSE file for specific details.
006: */
007:
008: package sample;
009:
010: import org.cyberneko.pull.XMLEvent;
011: import org.cyberneko.pull.XMLPullParser;
012: import org.cyberneko.pull.event.BoundedEvent;
013: import org.cyberneko.pull.event.CharactersEvent;
014: import org.cyberneko.pull.event.CommentEvent;
015: import org.cyberneko.pull.event.DoctypeDeclEvent;
016: import org.cyberneko.pull.event.DocumentEvent;
017: import org.cyberneko.pull.event.ElementEvent;
018: import org.cyberneko.pull.event.GeneralEntityEvent;
019: import org.cyberneko.pull.event.PrefixMappingEvent;
020: import org.cyberneko.pull.event.ProcessingInstructionEvent;
021: import org.cyberneko.pull.event.TextDeclEvent;
022: import org.cyberneko.pull.parsers.Xerces2;
023:
024: import java.io.IOException;
025: import java.io.OutputStreamWriter;
026: import java.io.PrintWriter;
027:
028: import org.apache.xerces.xni.XMLAttributes;
029: import org.apache.xerces.xni.XMLString;
030: import org.apache.xerces.xni.XNIException;
031: import org.apache.xerces.xni.parser.XMLInputSource;
032:
033: /**
034: * A simple document tracer program to show the use of the pull parsing
035: * API. This program is useful for seeing the contents of event objects
036: * returned by a pull parser implementation.
037: *
038: * @author Andy Clark
039: *
040: * @version $Id$
041: */
042: public class DocumentTracer {
043:
044: //
045: // Constructors
046: //
047:
048: /** This class cannot be constructed. */
049: private DocumentTracer() {
050: } // <init>()
051:
052: //
053: // Public static methods
054: //
055:
056: /**
057: * Traces the event callbacks.
058: *
059: * @param parser The pull parser to use.
060: * @param sysid The system id of the document to read.
061: */
062: public static void trace(XMLPullParser parser, String sysid)
063: throws XNIException, IOException {
064:
065: // variables
066: PrintWriter out = new PrintWriter(new OutputStreamWriter(
067: System.out, "UTF8"), true);
068: int indent = 0;
069:
070: // initialize parser
071: XMLInputSource source = new XMLInputSource(null, sysid, null);
072: parser.setInputSource(source);
073:
074: // parse document
075: XMLEvent event;
076: while ((event = parser.nextEvent()) != null) {
077: BoundedEvent boundedEvent = event instanceof BoundedEvent ? (BoundedEvent) event
078: : null;
079: if (event.type != XMLEvent.PREFIX_MAPPING
080: && boundedEvent != null && !boundedEvent.start) {
081: indent--;
082: }
083: printWithIndent(out, indent, "event.type="
084: + toString(event));
085: if (boundedEvent != null) {
086: printWithIndent(out, indent, " .start="
087: + boundedEvent.start);
088: }
089: switch (event.type) {
090: case XMLEvent.ELEMENT: {
091: ElementEvent elementEvent = (ElementEvent) event;
092: printWithIndent(out, indent, " .element="
093: + elementEvent.element);
094: printWithIndent(out, indent, " .start="
095: + elementEvent.start);
096: printWithIndent(out, indent, " .empty="
097: + elementEvent.empty);
098: XMLAttributes attrs = elementEvent.attributes;
099: int attrCount = attrs != null ? attrs.getLength() : 0;
100: for (int i = 0; i < attrCount; i++) {
101: printWithIndent(out, indent, " .attributes["
102: + i + "].qname=" + attrs.getQName(i));
103: printWithIndent(out, indent, " .attributes["
104: + i + "].value=\"" + attrs.getValue(i)
105: + '"');
106: }
107: break;
108: }
109: case XMLEvent.CHARACTERS: {
110: CharactersEvent charactersEvent = (CharactersEvent) event;
111: printWithIndent(out, indent, " .text=\""
112: + escape(charactersEvent.text.toString()) + '"');
113: printWithIndent(out, indent, " .ignorable="
114: + charactersEvent.ignorable);
115: break;
116: }
117: case XMLEvent.PREFIX_MAPPING: {
118: PrefixMappingEvent prefixMapEvent = (PrefixMappingEvent) event;
119: printWithIndent(out, indent, " .prefix=\""
120: + prefixMapEvent.prefix + '"');
121: if (prefixMapEvent.uri != null) {
122: printWithIndent(out, indent, " .uri=\""
123: + prefixMapEvent.uri + '"');
124: }
125: break;
126: }
127: case XMLEvent.COMMENT: {
128: CommentEvent commentEvent = (CommentEvent) event;
129: printWithIndent(out, indent, " .text=\""
130: + escape(commentEvent.text.toString()) + '"');
131: break;
132: }
133: case XMLEvent.PROCESSING_INSTRUCTION: {
134: ProcessingInstructionEvent piEvent = (ProcessingInstructionEvent) event;
135: printWithIndent(out, indent, " .target=\""
136: + piEvent.target + "'");
137: if (piEvent.data != null) {
138: printWithIndent(out, indent, " .data=\""
139: + piEvent.data + '"');
140: }
141: break;
142: }
143: case XMLEvent.DOCUMENT: {
144: DocumentEvent documentEvent = (DocumentEvent) event;
145: if (documentEvent.locator != null) {
146: printWithIndent(out, indent, " .locator="
147: + documentEvent.locator);
148: }
149: if (documentEvent.encoding != null) {
150: printWithIndent(out, indent, " .encoding=\""
151: + documentEvent.encoding + '"');
152: }
153: break;
154: }
155: case XMLEvent.DOCTYPE_DECL: {
156: DoctypeDeclEvent doctypeEvent = (DoctypeDeclEvent) event;
157: printWithIndent(out, indent, " .root=\""
158: + doctypeEvent.root + '"');
159: if (doctypeEvent.pubid != null) {
160: printWithIndent(out, indent, " .pubid=\""
161: + doctypeEvent.pubid + '"');
162: }
163: printWithIndent(out, indent, " .sysid=\""
164: + doctypeEvent.sysid + '"');
165: break;
166: }
167: case XMLEvent.GENERAL_ENTITY: {
168: GeneralEntityEvent geEvent = (GeneralEntityEvent) event;
169: printWithIndent(out, indent, " .name="
170: + geEvent.name);
171: if (geEvent.publicId != null) {
172: printWithIndent(out, indent, " .publicId=\""
173: + geEvent.publicId + '"');
174: }
175: if (geEvent.literalSystemId != null) {
176: printWithIndent(out, indent,
177: " .baseSystemId=\""
178: + geEvent.baseSystemId + '"');
179: printWithIndent(out, indent,
180: " .literalSystemId=\""
181: + geEvent.literalSystemId + '"');
182: printWithIndent(out, indent,
183: " .expandedSystemId=\""
184: + geEvent.expandedSystemId + '"');
185: }
186: if (geEvent.encoding != null) {
187: printWithIndent(out, indent, " .encoding=\""
188: + geEvent.encoding + '"');
189: }
190: break;
191: }
192: case XMLEvent.TEXT_DECL: {
193: TextDeclEvent textDeclEvent = (TextDeclEvent) event;
194: printWithIndent(out, indent, " .xmldecl="
195: + textDeclEvent.xmldecl);
196: if (textDeclEvent.version != null) {
197: printWithIndent(out, indent, " .version=\""
198: + textDeclEvent.version + '"');
199: }
200: if (textDeclEvent.encoding != null) {
201: printWithIndent(out, indent, " .encoding=\""
202: + textDeclEvent.encoding + '"');
203: }
204: if (textDeclEvent.standalone != null) {
205: printWithIndent(out, indent, " .standalone=\""
206: + textDeclEvent.standalone + '"');
207: }
208: break;
209: }
210: }
211: if (event.augs != null) {
212: printWithIndent(out, indent, " .augs=" + event.augs);
213: }
214: if (event.next != null) {
215: printWithIndent(out, indent, " .next=" + event.next);
216: }
217: if (event.type != XMLEvent.PREFIX_MAPPING
218: && boundedEvent != null && boundedEvent.start) {
219: indent++;
220: }
221: }
222:
223: } // trace(XMLPullParser,String)
224:
225: //
226: // Protected static methods
227: //
228:
229: /** Prints with the appropriate indent. */
230: protected static void printWithIndent(PrintWriter out, int indent,
231: String s) {
232: for (int i = 0; i < indent; i++) {
233: out.print(' ');
234: }
235: out.println(s);
236: } // printWithIndent(PrintWriter,String)
237:
238: /** Escapes a string. */
239: protected static String escape(String s) {
240: StringBuffer str = new StringBuffer();
241: int length = s.length();
242: for (int i = 0; i < length; i++) {
243: char c = s.charAt(i);
244: if (c == '"') {
245: str.append('\\');
246: }
247: str.append(c);
248: }
249: return str.toString();
250: } // escape(String):String
251:
252: /** Returns a string representation of the event type. */
253: protected static String toString(XMLEvent event) {
254: String name = event.getClass().getName();
255: switch (event.type) {
256: case XMLEvent.CDATA:
257: return "CDATA (" + name + ")";
258: case XMLEvent.CHARACTERS:
259: return "CHARACTERS (" + name + ")";
260: case XMLEvent.COMMENT:
261: return "COMMENT (" + name + ")";
262: case XMLEvent.DOCTYPE_DECL:
263: return "DOCTYPE_DECL (" + name + ")";
264: case XMLEvent.DOCUMENT:
265: return "DOCUMENT (" + name + ")";
266: case XMLEvent.ELEMENT:
267: return "ELEMENT (" + name + ")";
268: case XMLEvent.GENERAL_ENTITY:
269: return "GENERAL_ENTITY (" + name + ")";
270: case XMLEvent.PREFIX_MAPPING:
271: return "PREFIX_MAPPING (" + name + ")";
272: case XMLEvent.PROCESSING_INSTRUCTION:
273: return "PROCESSING_INSTRUCTION (" + name + ")";
274: case XMLEvent.TEXT_DECL:
275: return "TEXT_DECL (" + name + ")";
276: }
277: return "??? (" + name + "," + event.type + ")";
278: } // toString(short):String
279:
280: //
281: // MAIN
282: //
283:
284: /** Main program. */
285: public static void main(String[] argv) throws Exception {
286: XMLPullParser parser = new Xerces2();
287: for (int i = 0; i < argv.length; i++) {
288: DocumentTracer.trace(parser, argv[i]);
289: }
290: } // main(String[])
291:
292: } // class DocumentTracer
|