001: package org.igfay.util;
002:
003: import javax.xml.parsers.DocumentBuilder;
004: import javax.xml.parsers.DocumentBuilderFactory;
005: import javax.xml.parsers.ParserConfigurationException;
006:
007: import org.w3c.dom.Document;
008: import org.w3c.dom.Element;
009: import org.w3c.dom.NamedNodeMap;
010: import org.w3c.dom.Node;
011: import org.w3c.dom.NodeList;
012:
013: public class PrettyPrinter {
014: public static void printDoc(String url)
015: throws ParserConfigurationException {
016: System.out.println("Pretty printing XML file: " + url + ".");
017: // Creating a parser factory (JAXP)
018: // It can be configured at runtime, which implementation
019: // (Xerces, Crimson, ...) is to be used,
020: // for example via the Java systems property.
021: DocumentBuilderFactory parserFactory = DocumentBuilderFactory
022: .newInstance();
023: // Configuring the parsers that the factory will create (JAXP)
024: parserFactory.setValidating(true);
025: parserFactory.setNamespaceAware(true);
026: parserFactory.setExpandEntityReferences(true);
027: // Creating a parser (JAXP)
028: DocumentBuilder parser = parserFactory.newDocumentBuilder();
029: try {
030: // Using the parser to build the DOM document (JAXP)
031: Document doc = parser.parse(url);
032: // All further computations are based on DOM, no longer on JAXP.
033: printNode(doc); // print nodes recursively
034: } catch (Exception e) {
035: System.out.println("Error in parsing: " + e.getMessage());
036: }
037: };
038:
039: public static void printNode(Node node) {
040: // This method uses only DOM functionality.
041: switch (node.getNodeType()) {
042: case Node.DOCUMENT_NODE:
043: System.out
044: .println("<?xml version=\"1.0\" encoding=\"UTF8\"?>");
045: Document doc = (Document) node;
046: printNode(doc.getDocumentElement());
047: break;
048: case Node.ELEMENT_NODE:
049: Element element = (Element) node;
050: element.normalize();
051: String name = node.getNodeName();
052: System.out.print("<" + name);
053: NamedNodeMap attributes = node.getAttributes();
054: for (int i = 0; i < attributes.getLength(); i++) {
055: Node attr = attributes.item(i);
056: System.out.print(" " + attr.getNodeName() + "=\""
057: + attr.getNodeValue() + "\"");
058: }
059: ;
060: System.out.println(">");
061: NodeList children = node.getChildNodes();
062: if (children != null) {
063: for (int i = 0; i < children.getLength(); i++) {
064: printNode(children.item(i));
065: }
066: ;
067: }
068: ;
069: System.out.println("</" + name + ">");
070: break;
071: case Node.TEXT_NODE:
072: case Node.CDATA_SECTION_NODE:
073: System.out.println(node.getNodeValue() + ""); // needs filtering!
074: break;
075: case Node.PROCESSING_INSTRUCTION_NODE:
076: break;
077: case Node.ENTITY_REFERENCE_NODE:
078: System.out.println("&" + node.getNodeName() + ";");
079: break;
080: case Node.DOCUMENT_TYPE_NODE:
081: break;
082: }
083: ;
084: };
085:
086: public static void main(String[] args) {
087: if (args.length != 1) {
088: System.out.println("Usage: java PrettyPrinter [XML URL]");
089: System.exit(0);
090: }
091: ;
092:
093: String url = args[0];
094:
095: try {
096: PrettyPrinter.printDoc(url);
097: } catch (ParserConfigurationException e) {
098: System.out.println("Pretty printer failed due to parser"
099: + "configuration error.");
100: System.exit(0);
101: }
102: };
103: }
|