/* * (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 dom; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * A sample DOM counter. This sample program illustrates how to * traverse a DOM tree in order to information about the document. * * @version Revision: 11 1.1 samples/dom/DOMCount.java, samples, xml4j2, xml4j2_0_5 */ public class DOMCount { // // Constants // /** Default parser name. */ private static final String DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser"; // // Data // /** Elements. */ private long elements; /** Attributes. */ private long attributes; /** Characters. */ private long characters; /** Ignorable whitespace. */ private long ignorableWhitespace; // // Public static methods // /** Counts the resulting document tree. */ public static void count(String parserWrapperName, String uri) { try { DOMParserWrapper parser = (DOMParserWrapper)Class.forName(parserWrapperName).newInstance(); Document document = parser.parse(uri); DOMCount counter = new DOMCount(); long before = System.currentTimeMillis(); counter.traverse(document); long after = System.currentTimeMillis(); counter.printResults(uri, after - before); } catch (Exception e) { e.printStackTrace(System.err); } } // print(String,String,boolean) // // Public methods // /** Traverses the specified node, recursively. */ public void traverse(Node node) { // is there anything to do? if (node == null) { return; } int type = node.getNodeType(); switch (type) { // print document case Node.DOCUMENT_NODE: { elements = 0; attributes = 0; characters = 0; ignorableWhitespace = 0; traverse(((Document)node).getDocumentElement()); break; } // print element with attributes case Node.ELEMENT_NODE: { elements++; NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { attributes += attrs.getLength(); } NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { traverse(children.item(i)); } } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { traverse(children.item(i)); } } break; } // print text case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: { characters += node.getNodeValue().length(); break; } } } // traverse(Node) /** 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; // 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); } } // count uri count(parserName, arg); } } // main(String[]) /** Prints the usage. */ private static void printUsage() { System.err.println("usage: java dom.DOMCount (options) uri ..."); System.err.println(); System.err.println("options:"); System.err.println(" -p name Specify DOM parser wrapper by name."); System.err.println(" Default parser: "+DEFAULT_PARSER_NAME); System.err.println(" -h This help screen."); } // printUsage() } // class DOMCount