1 23 package org.enhydra.xml.xmlc.perf; 24 25 import java.io.PrintStream ; 26 27 import org.enhydra.xml.xmlc.XMLCFactory; 28 import org.enhydra.xml.xmlc.XMLCStdFactory; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.DocumentType ; 31 import org.w3c.dom.NamedNodeMap ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.NodeList ; 34 35 36 43 public class DOMConverter { 44 45 private int testIndex; 46 private PrintStream out = System.out; 47 private PrintStream err = System.err; 48 49 54 public void convert(String docClassName) { 55 try { 56 Class docClass = Class.forName(docClassName); 57 XMLCFactory factory = new XMLCStdFactory(null, null); 58 Document doc = factory.create(docClass); 59 testIndex = 1; 60 printNode(doc, ""); 61 } catch (Exception e) { 62 err.println("Error: " + e.getMessage()); 63 } 64 } 65 66 74 public void printNode(Node node, String indent) { 75 76 switch (node.getNodeType()) { 77 case Node.DOCUMENT_NODE: 78 NodeList nodes = node.getChildNodes(); 79 if (nodes != null) { 80 for (int i=0; i<nodes.getLength(); i++) { 81 printNode(nodes.item(i), ""); 82 } 83 } 84 break; 85 86 case Node.ELEMENT_NODE: 87 String name = node.getNodeName(); 88 out.print(indent + "<" + name); 89 NamedNodeMap attributes = node.getAttributes(); 90 for (int i=0; i<attributes.getLength(); i++) { 91 Node current = attributes.item(i); 92 out.print(" " + current.getNodeName() + 93 "=\"" + current.getNodeValue() + 94 "\""); 95 } 96 out.println(">"); 97 98 NodeList children = node.getChildNodes(); 99 if (children != null) { 100 for (int i=0; i<children.getLength(); i++) { 101 printNode(children.item(i), indent + " "); 102 } 103 } 104 105 out.println(indent + "</" + name + ">"); 106 break; 107 108 case Node.TEXT_NODE: 109 out.print("<SPAN ID=\"Test" + testIndex + "\">"); 110 out.print(node.getNodeValue()); 111 out.print("</SPAN>"); 112 testIndex++; 113 break; 114 115 case Node.CDATA_SECTION_NODE: 116 out.print(node.getNodeValue()); 117 break; 118 119 case Node.PROCESSING_INSTRUCTION_NODE: 120 out.println("<?" + node.getNodeName() + 121 " " + node.getNodeValue() + 122 "?>"); 123 break; 124 125 case Node.ENTITY_REFERENCE_NODE: 126 out.println("&" + node.getNodeName() + ";"); 127 break; 128 129 case Node.DOCUMENT_TYPE_NODE: 130 DocumentType docType = (DocumentType )node; 131 out.print("<!DOCTYPE " + docType.getName()); 132 if (docType.getPublicId() != null) { 133 out.print(" PUBLIC \"" + 134 docType.getPublicId() + "\" "); 135 } else { 136 out.print(" SYSTEM "); 137 } 138 out.println("\"" + docType.getSystemId() + "\">"); 139 break; 140 } 141 } 142 143 148 public static void main(String [] args) { 149 if (args.length != 1) { 150 System.err.println("Usage: java DOMConverter domClassName"); 151 System.exit(0); 152 } 153 154 DOMConverter converter = new DOMConverter(); 155 converter.convert(args[0]); 156 } 157 158 } 159 160 161 | Popular Tags |