1 23 24 package org.enhydra.xml.lazydom; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.PrintWriter ; 29 import java.lang.reflect.Constructor ; 30 import java.lang.reflect.InvocationTargetException ; 31 32 import org.enhydra.xml.driver.TestException; 33 import org.enhydra.xml.io.ErrorReporter; 34 import org.enhydra.xml.xmlc.XMLCException; 35 import org.enhydra.xml.xmlc.compiler.Parse; 36 import org.enhydra.xml.xmlc.dom.XMLCDocument; 37 import org.enhydra.xml.xmlc.metadata.DocumentClass; 38 import org.enhydra.xml.xmlc.metadata.InputDocument; 39 import org.enhydra.xml.xmlc.metadata.MetaData; 40 import org.enhydra.xml.xmlc.metadata.MetaDataDocument; 41 import org.w3c.dom.DocumentType ; 42 43 50 class LazyDOMBuilder { 51 52 private static final boolean DEBUG = false; 53 54 55 private static final Class [] XML_CONSTR_SIGN 56 = new Class []{DocumentType .class, TemplateDOM.class}; 57 private static final Class [] HTML_CONSTR_SIGN 58 = new Class []{TemplateDOM.class}; 59 60 61 private PrintWriter fTraceOut; 62 63 64 private Class fDomFactoryClass; 65 66 67 private PrintWriter fMsgWriter; 68 69 70 public LazyDOMBuilder(PrintWriter msgWriter, 71 Class domFactoryClass) { 72 if (DEBUG) { 73 fTraceOut = msgWriter; 74 } 75 fMsgWriter = msgWriter; 76 fDomFactoryClass = domFactoryClass; 77 } 78 79 82 private LazyDocument parseFile(File srcFile) 83 throws IOException , XMLCException { 84 Parse parser = new Parse(new ErrorReporter(fMsgWriter), fTraceOut); 85 MetaData metaData = MetaDataDocument.newInstance().getMetaData(); 86 InputDocument inputDoc = metaData.getInputDocument(); 87 inputDoc.setUrl(srcFile.getPath()); 88 DocumentClass docClass = metaData.getDocumentClass(); 89 docClass.setDomFactory(fDomFactoryClass.getName()); 90 91 metaData.getParser().setWarnings(false); 93 94 if (DEBUG) { 95 metaData.getCompileOptions().setVerbose(true); 96 metaData.getCompileOptions().setPrintDocumentInfo(true); 97 } 98 99 XMLCDocument xmlcDoc = parser.parse(metaData); 100 return (LazyDocument)xmlcDoc.getDocument(); 101 } 102 103 107 private LazyDocument newXmlLazyDoc(Class lazyDocumentClass, 108 TemplateDOM templateDOM) 109 throws NoSuchMethodException , InstantiationException , 110 IllegalAccessException , InvocationTargetException { 111 Constructor constr 112 = lazyDocumentClass.getConstructor(XML_CONSTR_SIGN); 113 return (LazyDocument)constr.newInstance(new Object []{null, templateDOM}); 114 } 115 116 120 private LazyDocument newHtmlLazyDoc(Class lazyDocumentClass, 121 TemplateDOM templateDOM) 122 throws NoSuchMethodException , InstantiationException , 123 IllegalAccessException , InvocationTargetException { 124 Constructor constr 125 = lazyDocumentClass.getConstructor(HTML_CONSTR_SIGN); 126 return (LazyDocument)constr.newInstance(new Object []{templateDOM}); 127 } 128 129 132 private LazyDocument newLazyDoc(Class lazyDocumentClass, 133 TemplateDOM templateDOM) { 134 try { 136 try { 137 return newXmlLazyDoc(lazyDocumentClass, templateDOM); 138 } catch (NoSuchMethodException except) { 139 } 141 return newHtmlLazyDoc(lazyDocumentClass, templateDOM); 142 } catch (Exception except) { 143 throw new TestException("failed to create instance of: " + lazyDocumentClass, 144 except); 145 } 146 } 147 148 151 public LazyDocument parseUnexpanded(File srcFile) { 152 try { 153 LazyDocument expandedDoc = parseFile(srcFile); 154 TemplateDOM templateDom = new TemplateDOM(expandedDoc); 155 return newLazyDoc(expandedDoc.getClass(), templateDom); 156 } catch (IOException except) { 157 throw new TestException(srcFile.getPath(), except); 158 } catch (XMLCException except) { 159 throw new TestException(srcFile.getPath(), except); 160 } 161 } 162 163 } 164 165 166 | Popular Tags |