/* Copyright (c) 2003 Mark T. Hayes; All Rights Reserved $Id: Traverse.java,v 1.10 2003/04/12 03:18:31 mark Exp $ */ import java.io.FileInputStream; import java.io.InputStream; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.greybird.xmliter.DomIterator; import org.greybird.xmliter.SaxIterator; import org.greybird.xmliter.XmlIterator; import org.greybird.xmliter.XmlIteratorException; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class Traverse { static void usage(String err) { if (err != null) { System.out.println("Error: " + err); } System.out.println("usage: java " + Traverse.class.getName() + " -in XML_FILE" + "\n [-h | -help] # print this message" + "\n [-mixed] # use advanceMixed()" + "\n [-dom] # use DomIterator"); System.exit(2); } public static void main(String[] args) throws IOException, XmlIteratorException, SAXException, ParserConfigurationException { boolean mixed = false; boolean dom = false; InputStream in = null; for (int i = 0; i < args.length; i += 1) { if (args[i].equals("-h") || args[i].equals("-help")) { usage(null); } else if (args[i].equals("-mixed")) { mixed = true; } else if (args[i].equals("-dom")) { dom = true; } else if (args[i].equals("-in") && i + 1 < args.length) { i += 1; in = new FileInputStream(args[i]); } else { usage("Unknown arg: " + args[i]); } } if (in == null) { usage("No -in file was specified"); } XmlIterator iter; if (dom) { iter = new DomIterator(parseDom(new InputSource(in))); } else { iter = new SaxIterator(new InputSource(in)); } if (mixed) { traverseMixed(iter, ""); } else { traverse(iter, ""); } } static void traverse(XmlIterator iter, String indent) throws IOException, XmlIteratorException { while (iter.advance()) { System.out.println(indent + "- " + iter.name() + " (" + iter.namespace() + ')'); XmlIterator children = iter.children(); if (children != null) { traverse(children, indent + " "); } else { String value = iter.value(); if (value.length() > 0) { System.out.println(indent + " = " + value); } } } } static void traverseMixed(XmlIterator iter, String indent) throws IOException, XmlIteratorException { while (iter.advanceMixed()) { if (iter.name() != null) { System.out.println(indent + "- " + iter.name() + " (" + iter.namespace() + ')'); XmlIterator children = iter.children(); if (children != null) { traverseMixed(children, indent + " "); } else { String value = iter.value(); if (value.length() > 0) { System.out.println(indent + " = " + value); } } } else { System.out.println(indent + "= " + iter.value()); } } } static Document parseDom(InputSource input) throws IOException, SAXException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(input); } }