KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > perf > DOMConverter


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: DOMConverter.java,v 1.2 2005/01/26 08:29:25 jkjome Exp $
22  */

23 package org.enhydra.xml.xmlc.perf;
24
25 import java.io.PrintStream JavaDoc;
26
27 import org.enhydra.xml.xmlc.XMLCFactory;
28 import org.enhydra.xml.xmlc.XMLCStdFactory;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.DocumentType JavaDoc;
31 import org.w3c.dom.NamedNodeMap JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34
35
36 /**
37  * Will convert a document into a format suitable for testing.
38  * All text nodes are converted into elements marked up with the
39  * 'span' tag. Each 'span' tag has an id of the format {Test[0-9]+}.
40  * These span elements can be directly accessed via test classes.
41  * The string representation of the converted document if printed out.
42  */

43 public class DOMConverter {
44
45     private int testIndex;
46     private PrintStream JavaDoc out = System.out;
47     private PrintStream JavaDoc err = System.err;
48
49     /**
50      * Converts the file.
51      *
52      * @param docClassName the class name of the DOM to be converted.
53      */

54     public void convert(String JavaDoc docClassName) {
55         try {
56         Class JavaDoc docClass = Class.forName(docClassName);
57         XMLCFactory factory = new XMLCStdFactory(null, null);
58         Document JavaDoc doc = factory.create(docClass);
59         testIndex = 1;
60             printNode(doc, "");
61         } catch (Exception JavaDoc e) {
62             err.println("Error: " + e.getMessage());
63         }
64     }
65
66     /**
67      * This will print a DOM <code>Node</code> and then recurse
68      * on its children.
69      *
70      * @param node <code>Node</code> object to print.
71      * @param indent <code>String</code> spacing to insert
72      * before this <code>Node</code>
73      */

74     public void printNode(Node JavaDoc node, String JavaDoc indent) {
75
76         switch (node.getNodeType()) {
77     case Node.DOCUMENT_NODE:
78         NodeList JavaDoc 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 JavaDoc name = node.getNodeName();
88         out.print(indent + "<" + name);
89         NamedNodeMap JavaDoc attributes = node.getAttributes();
90         for (int i=0; i<attributes.getLength(); i++) {
91         Node JavaDoc current = attributes.item(i);
92         out.print(" " + current.getNodeName() +
93                  "=\"" + current.getNodeValue() +
94                  "\"");
95         }
96         out.println(">");
97                 
98         NodeList JavaDoc 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 JavaDoc docType = (DocumentType JavaDoc)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     /**
144      * Usage: java DOMConverter domClassName
145      *
146      * @param args[0] the dom class name.
147      */

148     public static void main(String JavaDoc[] 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