/* * (C) Copyright IBM Corp. 1999 All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ package sax; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import sax.helpers.AttributeListImpl; import org.xml.sax.AttributeList; import org.xml.sax.HandlerBase; import org.xml.sax.Parser; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.ParserFactory; /** * A sample SAX counter. This sample program illustrates how to * register a SAX DocumentHandler and receive the callbacks in * order to print information about the document. * * @version Revision: 07 1.2 samples/sax/SAXCount.java, samples, xml4j2, xml4j2_0_5 */ public class SAXCount extends HandlerBase { // // Constants // /** Default parser name. */ private static final String DEFAULT_PARSER_NAME = "com.ibm.xml.parsers.SAXParser"; // // Data // /** Elements. */ private long elements; /** Attributes. */ private long attributes; /** Characters. */ private long characters; /** Ignorable whitespace. */ private long ignorableWhitespace; // // Public static methods // /** Prints the output from the SAX callbacks. */ public static void print(String parserName, String uri) { try { SAXCount counter = new SAXCount(); Parser parser = ParserFactory.makeParser(parserName); parser.setDocumentHandler(counter); parser.setErrorHandler(counter); long before = System.currentTimeMillis(); parser.parse(uri); long after = System.currentTimeMillis(); counter.printResults(uri, after - before); } catch (Exception e) { e.printStackTrace(System.err); } } // print(String,String) // // DocumentHandler methods // /** Start document. */ public void startDocument() { elements = 0; attributes = 0; characters = 0; ignorableWhitespace = 0; } // startDocument() /** Start element. */ public void startElement(String name, AttributeList attrs) { elements++; if (attrs != null) { attributes += attrs.getLength(); } } // startElement(String,AttributeList) /** Characters. */ public void characters(char ch[], int start, int length) { characters += length; } // characters(char[],int,int); /** Ignorable whitespace. */ public void ignorableWhitespace(char ch[], int start, int length) { ignorableWhitespace += length; } // ignorableWhitespace(char[],int,int); // // ErrorHandler methods // /** Warning. */ public void warning(SAXParseException ex) { System.err.println("[Warning] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Error. */ public void error(SAXParseException ex) { System.err.println("[Error] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Fatal error. */ public void fatalError(SAXParseException ex) throws SAXException { System.err.println("[Fatal Error] "+ getLocationString(ex)+": "+ ex.getMessage()); throw ex; } /** Returns a string of the location. */ private String getLocationString(SAXParseException ex) { StringBuffer str = new StringBuffer(); String systemId = ex.getSystemId(); if (systemId != null) { int index = systemId.lastIndexOf('/'); if (index != -1) systemId = systemId.substring(index + 1); str.append(systemId); } str.append(':'); str.append(ex.getLineNumber()); str.append(':'); str.append(ex.getColumnNumber()); return str.toString(); } // getLocationString(SAXParseException):String // // Public methods // /** Prints the results. */ public void printResults(String uri, long time) { // filename.xml: 631 ms (4 elems, 0 attrs, 78 spaces, 0 chars) System.out.print(uri); System.out.print(": "); System.out.print(time); System.out.print(" ms ("); System.out.print(elements); System.out.print(" elems, "); System.out.print(attributes); System.out.print(" attrs, "); System.out.print(ignorableWhitespace); System.out.print(" spaces, "); System.out.print(characters); System.out.print(" chars)"); System.out.println(); } // printResults(String,long) // // Main // /** Main program entry point. */ public static void main(String argv[]) { // is there anything to do? if (argv.length == 0) { printUsage(); System.exit(1); } // vars String parserName = DEFAULT_PARSER_NAME; boolean canonical = false; // check parameters for (int i = 0; i < argv.length; i++) { String arg = argv[i]; // options if (arg.startsWith("-")) { if (arg.equals("-p")) { if (i == argv.length - 1) { System.err.println("error: missing parser name"); System.exit(1); } parserName = argv[++i]; continue; } if (arg.equals("-h")) { printUsage(); System.exit(1); } } // print uri print(parserName, arg); } } // main(String[]) /** Prints the usage. */ private static void printUsage() { System.err.println("usage: java sax.SAXCount (options) uri ..."); System.err.println(); System.err.println("options:"); System.err.println(" -p name Specify SAX parser by name."); System.err.println(" Default parser: "+DEFAULT_PARSER_NAME); System.err.println(" -h This help screen."); } // printUsage() } // class SAXCount