KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > SourceCodeGenerator


1 /* Copyright 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import nu.xom.Attribute;
25 import nu.xom.Builder;
26 import nu.xom.Comment;
27 import nu.xom.DocType;
28 import nu.xom.Document;
29 import nu.xom.Element;
30 import nu.xom.Node;
31 import nu.xom.ProcessingInstruction;
32 import nu.xom.Text;
33
34 /**
35  *
36  * <p>
37  * This program essentially serializes a XOM <code>Node</code>
38  * object into the Java statements necessary to build the
39  * <code>Node</code> using XOM. This may be useful for building
40  * unit tests.
41  * </p>
42  *
43  * @author Elliotte Rusty Harold
44  * @version 1.0
45  *
46  */

47 public class SourceCodeGenerator {
48
49     /**
50       * <p>
51       * The driver method for the SourceCodeGenerator program.
52       * </p>
53       *
54       * @param args <code>args[0]</code> contains the URL
55       * of the document to be processed.
56       */

57     public static void main(String JavaDoc[] args) {
58   
59         Builder builder = new Builder();
60         try {
61             Document input = builder.build(args[0]);
62             generateClass(input);
63         }
64         catch (Exception JavaDoc ex) {
65             System.err.println(ex);
66             ex.printStackTrace();
67         }
68   
69     }
70     
71     private static int elementCount = 1;
72     
73     public static void generateClass(Document doc) {
74         System.out.println("import nu.xom.*;");
75         System.out.println();
76         System.out.println();
77         System.out.println("public class CodeMaker {");
78         System.out.println();
79         System.out.println(" public static void main(String[] args) throws Exception {");
80         generateDoc(doc);
81         System.out.println(" Serializer serializer = new Serializer(System.out);");
82         System.out.println(" serializer.write(doc);");
83         System.out.println(" }");
84         System.out.println();
85         System.out.println("}");
86     }
87     
88     public static void generateSource(Node node, String JavaDoc parent) {
89         
90         if (node instanceof Element) {
91             Element element = (Element) node;
92             String JavaDoc name = "element" + elementCount;
93             System.out.println(" Element " + name + " = "
94              + "new Element(\"" + element.getQualifiedName()
95              + "\", \"" + element.getNamespaceURI() + "\");");
96             if ("doc".equals(parent)) {
97                 System.out.println(" doc.setRootElement(" + name + ");");
98             }
99             else {
100                 System.out.println(" " + parent + ".appendChild(" + name + ");");
101             }
102             
103             for (int i = 0; i < element.getAttributeCount(); i++) {
104                 Attribute a = element.getAttribute(i);
105                 System.out.println(" " + name + ".addAttribute(new Attribute(\""
106                   + a.getQualifiedName() + "\", \""
107                   + a.getNamespaceURI() + "\", \""
108                   + a.getValue() + "\"));");
109             }
110               
111             for (int i = 0; i < element.getNamespaceDeclarationCount(); i++) {
112                 String JavaDoc prefix = element.getNamespacePrefix(i);
113                 System.out.println(" " + name + ".addNamespaceDeclaration(\""
114                   + prefix + "\", \""
115                   + element.getNamespaceURI(prefix) + "\");");
116             }
117             
118             elementCount++;
119             for (int i = 0; i < element.getChildCount(); i++) {
120                 generateSource(element.getChild(i), name);
121             }
122               
123              
124         }
125         else if (node instanceof ProcessingInstruction) {
126             ProcessingInstruction pi = (ProcessingInstruction) node;
127             System.out.println(" pi = new ProcessingInstruction(\"" +
128               pi.getTarget() + "\", \"" + javaEscape(pi.getValue()) + "\");");
129             System.out.println(" " + parent + ".appendChild(pi);");
130         }
131         else if (node instanceof Comment) {
132             Comment comment = (Comment) node;
133             System.out.println(" comment = new Comment(\""
134              + javaEscape(comment.getValue()) + "\");");
135             System.out.println(" " + parent + ".appendChild(comment);");
136         }
137         else if (node instanceof Text) {
138             Text text = (Text) node;
139             System.out.println(" text = new Text(\""
140              + javaEscape(text.getValue()) + "\");");
141             System.out.println(" " + parent + ".appendChild(text);");
142         }
143         else if (node instanceof DocType) {
144             DocType doctype = (DocType) node;
145             String JavaDoc publicID = doctype.getPublicID();
146             String JavaDoc systemID = doctype.getSystemID();
147             System.out.println(" DocType doctype = new DocType(\""
148                 + doctype.getRootElementName() + "\");");
149             if (systemID != null) {
150                 System.out.println(" doctype.setSystemID(\""
151                  + systemID + "\");");
152             }
153             if (publicID != null) {
154                 System.out.println(" doctype.setPublicID(\""
155                  + publicID + "\");");
156             }
157             System.out.println(" doc.setDocType(doctype);");
158             
159           
160         }
161         
162     }
163     
164     private static String JavaDoc javaEscape(String JavaDoc text) {
165         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
166         for (int i = 0; i < text.length(); i++) {
167             char c = text.charAt(i);
168             switch (c) {
169                 case '"':
170                     result.append("\\\"");
171                     break;
172                 case '\n':
173                     result.append("\\n");
174                     break;
175                 case '\r':
176                     result.append("\\r");
177                     break;
178                 case '\t':
179                     result.append("\\t");
180                     break;
181                 case '\\':
182                     result.append("\\\\");
183                     break;
184                 default:
185                     result.append(c);
186             }
187             
188         }
189         return result.toString();
190     }
191
192     public static void generateDoc(Document doc) {
193         
194         System.out.println(" Comment comment;");
195         System.out.println(" Text text;");
196         System.out.println(" ProcessingInstruction pi;");
197         System.out.println(" Document doc = new Document(new Element(\"root\"));");
198         for (int i = 0; i < doc.getChildCount(); i++) {
199             generateSource(doc.getChild(i), "doc");
200         }
201         
202     }
203
204 }
205
Popular Tags