1
/* Copyright 2003 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.samples;
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;
31
import nu.xom.ProcessingInstruction;
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
43
* @author Elliotte Rusty Harold
47
public class SourceCodeGenerator {
51
* The driver method for the SourceCodeGenerator program.
54
* @param args <code>args[0]</code> contains the URL
55
* of the document to be processed.
57
public static void main(String[] args) {
59
Builder builder = new Builder();
61
Document input = builder.build(args[0]);
64
catch (Exception ex) {
65
System.err.println(ex);
71
private static int elementCount = 1;
73
public static void generateClass(Document doc) {
74
System.out.println("import nu.xom.*;");
77
System.out.println("public class CodeMaker {");
79
System.out.println(" public static void main(String[] args) throws Exception {");
81
System.out.println(" Serializer serializer = new Serializer(System.out);");
82
System.out.println(" serializer.write(doc);");
83
System.out.println(" }");
85
System.out.println("}");
88
public static void generateSource(Node node, String parent) {
90
if (node instanceof Element) {
91
Element element = (Element) node;
92
String 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 + ");");
100
System.out.println(" " + parent + ".appendChild(" + name + ");");
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() + "\"));");
111
for (int i = 0; i < element.getNamespaceDeclarationCount(); i++) {
112
String prefix = element.getNamespacePrefix(i);
113
System.out.println(" " + name + ".addNamespaceDeclaration(\""
115
+ element.getNamespaceURI(prefix) + "\");");
119
for (int i = 0; i < element.getChildCount(); i++) {
120
generateSource(element.getChild(i), name);
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);");
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);");
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);");
143
else if (node instanceof DocType) {
144
DocType doctype = (DocType) node;
145
String publicID = doctype.getPublicID();
146
String 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 + "\");");
153
if (publicID != null) {
154
System.out.println(" doctype.setPublicID(\""
155
+ publicID + "\");");
157
System.out.println(" doc.setDocType(doctype);");
164
private static String javaEscape(String text) {
165
StringBuffer result = new StringBuffer();
166
for (int i = 0; i < text.length(); i++) {
167
char c = text.charAt(i);
170
result.append("\\\"");
173
result.append("\\n");
176
result.append("\\r");
179
result.append("\\t");
182
result.append("\\\\");
189
return result.toString();
192
public static void generateDoc(Document doc) {
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");