1 23 package org.enhydra.kelp.common.xmlc; 24 25 import org.enhydra.kelp.common.bridge.Generator; 27 import org.enhydra.kelp.common.bridge.Parser; 28 import org.enhydra.kelp.common.bridge.PrintInfo; 29 import org.enhydra.kelp.common.bridge.XMLCConnectionFactory; 30 import org.enhydra.kelp.common.node.OtterXMLCNode; 31 32 import org.enhydra.xml.xmlc.commands.xmlc.*; 34 import org.enhydra.xml.xmlc.commands.options.*; 35 import org.enhydra.xml.xmlc.compiler.EditDOM; 36 import org.enhydra.xml.xmlc.dom.XMLCDocument; 37 import org.enhydra.xml.xmlc.parsers.*; 38 import org.enhydra.xml.xmlc.XMLCUtil; 39 import org.enhydra.xml.xmlc.XMLCVersion; 40 import org.enhydra.xml.xmlc.XMLCException; 41 import org.w3c.dom.*; 42 import org.xml.sax.SAXException ; 43 44 import java.io.*; 46 import java.lang.reflect.Constructor ; 47 import java.lang.reflect.Method ; 48 import java.util.Enumeration ; 49 import java.util.Hashtable ; 50 import java.util.Vector ; 51 import java.util.ResourceBundle ; 52 53 57 public class XMLCRunner { 58 59 static ResourceBundle res = 61 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); private final String JAVA_LANG = "java.lang."; 64 67 private OtterXMLCNode node; 68 69 72 private Document document; 73 74 77 private PrintWriter traceWriter = new PrintWriter(System.err, true); 78 79 82 private XMLCDocument xmlcDoc; 83 84 90 public XMLCRunner() {} 91 92 100 public void setTraceWriter(PrintWriter tw) { 101 traceWriter = tw; 102 } 103 104 112 public PrintWriter getTraceWriter() { 113 return traceWriter; 114 } 115 116 122 123 131 private void printStackTrace(Throwable except) { 132 133 if (except instanceof SAXException ) { 135 Exception except2 = ((SAXException ) except).getException(); 136 137 if (except2 != null) { 138 except2.printStackTrace(); 139 return; 140 } 141 } 142 except.printStackTrace(); 143 } 144 145 149 150 158 public void printException(Throwable except) { 159 except.printStackTrace(); 160 161 if (except instanceof XMLCException) { 163 System.err.println(res.getString("Error_") + except.getMessage()); 164 if ((node.getMetaDataHandler() != null) 165 && (node.getMetaDataHandler().getVerbose())) { 166 printStackTrace(except); 167 } 168 169 Throwable cause = ((XMLCException) except).getCause(); 171 172 if ((cause != null) 173 && cause.getClass().getName().startsWith(JAVA_LANG)) { 174 printStackTrace(cause); 175 } 176 } else if ((except instanceof FileNotFoundException) 177 || (except instanceof IOException)) { 178 System.err.println(res.getString("Error_") 179 + except.getClass().getName() 180 + res.getString("LineEnd") 181 + except.getMessage()); 182 if ((node.getMetaDataHandler() != null) 183 && (node.getMetaDataHandler().getVerbose())) { 184 printStackTrace(except); 185 } 186 } else { 187 System.err.println(res.getString("Error_") 188 + except.getClass().getName() 189 + res.getString("LineEnd") 190 + except.getMessage()); 191 printStackTrace(except); 192 } 193 System.exit(1); 194 } 195 196 199 200 211 212 215 private void parsePage() 216 throws Throwable { 217 if (node.getMetaDataHandler().getVerbose()) { 218 traceWriter.println(res.getString("_parsing") 219 + node.getMetaDataHandler().getInputDocument()); 220 } 221 Parser parser = null; 222 223 parser = 224 XMLCConnectionFactory.createParser(traceWriter, 225 node.getMetaDataHandler()); 226 xmlcDoc = (XMLCDocument) parser.parse(); 227 document = xmlcDoc.getDocument(); 228 if (node.getMetaDataHandler().getPrintDocumentInfo()) { 229 if (node.getMetaDataHandler().getVerbose()) { 230 traceWriter.println(res.getString("_printing_document")); 231 } 232 PrintInfo info = XMLCConnectionFactory.createPrintInfo(document, 233 xmlcDoc); 234 235 info.printInfo(traceWriter); 236 } 237 EditDOM editDOM = null; 238 239 editDOM = 240 XMLCConnectionFactory.createEditDOM(node.getMetaDataHandler()); 241 editDOM.edit(xmlcDoc); 242 if (node.getMetaDataHandler().getPrintDOM()) { 243 if (node.getMetaDataHandler().getVerbose()) { 244 traceWriter.println(res.getString("_dumping_document")); 245 } 246 XMLCUtil.printNode(res.getString("DOM_hierarchy"), document, 247 XMLCUtil.PRINT_ALL, traceWriter); 248 } 249 } 250 251 254 255 264 private void generateJavaSource() throws XMLCException, IOException { 265 PrintWriter accessorWriter = null; 266 PrintWriter verboseWriter = null; 267 Generator codeGen = null; 268 269 if (node.getMetaDataHandler().getPrintAccessorInfo()) { 270 accessorWriter = traceWriter; 271 } 272 if (node.getMetaDataHandler().getVerbose()) { 273 traceWriter.println(res.getString("_generating_code")); 274 verboseWriter = traceWriter; 275 } 276 codeGen = 277 XMLCConnectionFactory.createGenerator(node.getMetaDataHandler(), 278 xmlcDoc, accessorWriter); 279 codeGen.generateJavaSource(verboseWriter); 280 } 281 282 285 286 295 private void generateJavaSourceInfo() throws XMLCException, IOException { 296 if (node.getMetaDataHandler().getVerbose()) { 297 traceWriter.println(res.getString("_generating_code_info")); 298 } 299 Generator codeGen = XMLCConnectionFactory.createGenerator(node.getMetaDataHandler(), 300 xmlcDoc, 301 (node.getMetaDataHandler().getPrintAccessorInfo() 302 ? traceWriter : null)); 303 304 codeGen.generateJavaSource(node.getMetaDataHandler().getVerbose() 305 ? traceWriter : null); 306 } 307 308 311 private void writeDOM() throws IOException { 312 File outputFile = 313 new File(node.getMetaDataHandler().getDocumentOutput()); 314 String dir = outputFile.getParent(); 315 316 if (dir != null) { 317 new File(dir).mkdirs(); 318 } 319 PrintWriter domOut = 320 new PrintWriter(new BufferedOutputStream(new FileOutputStream(node.getMetaDataHandler().getDocumentOutput()))); 321 322 try { 323 domOut.println(xmlcDoc.toDocument()); 324 } 325 finally { 326 domOut.close(); 327 } 328 } 329 330 333 334 344 private void compilePage() 345 throws XMLCException, FileNotFoundException, IOException { 346 generateJavaSource(); 347 348 if (node.getMetaDataHandler().getRecompilation()) { 350 File op = node.getOptionFileForRecomp(); 351 352 if (!op.getParentFile().exists()) { 353 op.getParentFile().mkdirs(); 354 } 355 node.getMetaDataHandler().save(node.getOptionFileForRecomp()); 356 357 } 359 360 } 363 364 367 private void printVersion() { 368 System.out.println(res.getString("Enhydra_XMLC_version") 369 + XMLCVersion.VERSION); 370 System.out.println(res.getString("See_http_www_enhydra")); 371 } 372 373 376 377 388 public void compile() 389 throws Throwable { 390 391 node.initProjectOptions(); 393 if (node.getMetaDataHandler().getPrintVersion()) { 394 printVersion(); 395 if (node.getMetaDataHandler().getInputDocument() == null) { 396 return; 397 } 398 } 399 parsePage(); 400 if (node.getMetaDataHandler().getDocumentOutput() == null) { 401 compilePage(); 402 } else { 403 if (node.getMetaDataHandler().getPrintAccessorInfo()) { 404 generateJavaSourceInfo(); 405 } 406 writeDOM(); 407 } 408 if (node.getMetaDataHandler().getVerbose()) { 409 traceWriter.println(res.getString("_completed")); 410 } 411 } 412 413 421 public void setNode(OtterXMLCNode n) { 422 node = n; 423 } 424 425 } 426 | Popular Tags |