1 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 47 public class SourceCodeGenerator { 48 49 57 public static void main(String [] args) { 58 59 Builder builder = new Builder(); 60 try { 61 Document input = builder.build(args[0]); 62 generateClass(input); 63 } 64 catch (Exception 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 parent) { 89 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 + ");"); 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 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 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 + "\");"); 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 javaEscape(String text) { 165 StringBuffer result = new StringBuffer (); 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 |