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.CharactersEvent;
013: import org.cyberneko.pull.event.ElementEvent;
014: import org.cyberneko.pull.parsers.Xerces2;
015:
016: import java.io.IOException;
017:
018: import org.apache.xerces.xni.XNIException;
019: import org.apache.xerces.xni.parser.XMLInputSource;
020:
021: /**
022: * A simple counter program to show the use of the pull parsing API.
023: *
024: * @author Andy Clark
025: *
026: * @version $Id$
027: */
028: public class Counter {
029:
030: //
031: // Constructors
032: //
033:
034: /** This class cannot be constructed. */
035: private Counter() {
036: } // <init>()
037:
038: //
039: // Public static methods
040: //
041:
042: /**
043: * Counts the elements, attributes, characters, and ignorable
044: * whitespace in a document.
045: *
046: * @param parser The pull parser to use.
047: * @param sysid The system id of the document to read.
048: */
049: public static void count(XMLPullParser parser, String sysid)
050: throws XNIException, IOException {
051:
052: // variables
053: int elements = 0;
054: int attributes = 0;
055: int whitespaces = 0;
056: int characters = 0;
057:
058: // initialize parser
059: long timeBefore = System.currentTimeMillis();
060: XMLInputSource source = new XMLInputSource(null, sysid, null);
061: parser.setInputSource(source);
062:
063: // parse document
064: XMLEvent event;
065: while ((event = parser.nextEvent()) != null) {
066: switch (event.type) {
067: case XMLEvent.ELEMENT: {
068: ElementEvent elementEvent = (ElementEvent) event;
069: if (elementEvent.start) {
070: elements++;
071: if (elementEvent.attributes != null) {
072: attributes += elementEvent.attributes
073: .getLength();
074: }
075: }
076: break;
077: }
078: case XMLEvent.CHARACTERS: {
079: CharactersEvent charactersEvent = (CharactersEvent) event;
080: if (charactersEvent.ignorable) {
081: whitespaces += charactersEvent.text.length;
082: } else {
083: characters += charactersEvent.text.length;
084: }
085: break;
086: }
087: }
088: }
089: long timeAfter = System.currentTimeMillis();
090:
091: // print results
092: System.out.print(sysid);
093: System.out.print(": ");
094: System.out.print(timeAfter - timeBefore);
095: System.out.print(" ms (");
096: System.out.print(elements);
097: System.out.print(" elems, ");
098: System.out.print(attributes);
099: System.out.print(" attrs, ");
100: System.out.print(whitespaces);
101: System.out.print(" spaces, ");
102: System.out.print(characters);
103: System.out.print(" chars)");
104: System.out.println();
105:
106: } // count(XMLPullParser,String)
107:
108: //
109: // MAIN
110: //
111:
112: /** Main program. */
113: public static void main(String[] argv) throws Exception {
114: XMLPullParser parser = new Xerces2();
115: for (int i = 0; i < argv.length; i++) {
116: Counter.count(parser, argv[i]);
117: }
118: } // main(String[])
119:
120: } // class Counter
|