/* * XmlDebug.java * * Created on February 7, 2002, 9:46 AM */ package org.ninjasoft.xml; import java.util.*; import java.io.*; import java.text.SimpleDateFormat; import org.w3c.dom.*; import javax.xml.parsers.*; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.ninjasoft.util.Text; /** * XmlDebug (written for the older XML libraries (xerces 1.3.1), for use with * the Rosettanet code. * * The useful public functions you will want to call are: * logDocument * printDocument * validateDocument * */ public class XmlDebug { public static final int INDENT = 3; private StringBuffer errorMessages = new StringBuffer(); /** No need to instantiate, everything pubilc is static */ private XmlDebug() {} /** * Take an XML document and log it to c:\xml_log.txt with a timestamp * and comment. * *@param d XML DOM document *@param comment comment to put in log file */ public static void logDocument(Document d, String comment) { try{ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); FileOutputStream fos = new FileOutputStream("c:/xml_log.txt", true); PrintStream ps = new PrintStream(fos); ps.println(formatter.format(new Date()) + " " + comment); printDocument(ps, d); ps.println("\n\n\n"); }catch(IOException e){System.out.println(e.toString());} } /** * Print an XML DOM document to stdout. * *@param d XML DOM document */ public static void printDocument(Document d) {printDocument(System.out, d);} /** * Print an XML document to stdout, parsing it into an XML DOM first. * *@param filename path+name of XML text file to read in */ public static void printDocument(String filename) { try{ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true); printDocument(dbf.newDocumentBuilder().parse(filename)); }catch(Exception e){System.out.println(e.toString());} } /** * Print an XML DOM document to the given PrintStream (instead of stdout). * *@param out the print stream to place the document *@param d the XML DOM document */ public static void printDocument(PrintStream out, Document d) { DocumentType dt = d.getDoctype(); if (dt != null) { out.println("doctype name: " + Text.normalizeString(dt.getName())); out.println("doctype public id: " + Text.normalizeString(dt.getPublicId())); out.println("doctype system id: " + Text.normalizeString(dt.getSystemId())); } else { out.println("No doctype specified in XML"); } printNodeRecursive(out, d.getDocumentElement(), 0); } public static void printNode(PrintStream out, Node node) { printNodeRecursive(out, node, 0); } public static void printNode(Node node) { printNodeRecursive(System.out, node, 0); } /** * Given a node, recursively print it and its children. * *@param out the PrintStream to print the output *@param n the node to print *@depth the indent level--initially pass in zero, as the function gets * recusively more deep, this number increments by steps of one */ private static void printNodeRecursive(PrintStream out, Node n, int depth) { StringBuffer b = new StringBuffer(); NamedNodeMap map; int max; Node tempNode; NodeList children; String value; // Get name and value for (int i=0; i