1 23 24 package org.enhydra.xml.xmlc.compiler; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.PrintWriter ; 29 30 import org.enhydra.xml.dom.DOMInfo; 31 import org.enhydra.xml.io.DOMFormatter; 32 import org.enhydra.xml.io.ErrorReporter; 33 import org.enhydra.xml.io.InputSourceOps; 34 import org.enhydra.xml.io.OutputOptions; 35 import org.enhydra.xml.xmlc.XMLCException; 36 import org.enhydra.xml.xmlc.dom.XMLCDocument; 37 import org.enhydra.xml.xmlc.metadata.CompileOptions; 38 import org.enhydra.xml.xmlc.metadata.DocumentClass; 39 import org.enhydra.xml.xmlc.metadata.MetaData; 40 import org.w3c.dom.Document ; 41 import org.xml.sax.InputSource ; 42 43 45 50 public class Compiler { 51 54 private MetaData fMetaData; 55 56 59 private CompileOptions fCompileOptions; 60 61 64 private boolean fVerbose = false; 65 66 69 private InputSource fInputSource; 70 71 74 private Document fDocument; 75 76 79 ErrorReporter fErrorReporter; 80 81 84 private PrintWriter fTraceOut; 85 86 89 private XMLCDocument fXmlcDoc; 90 91 96 public Compiler(ErrorReporter errorReporter, 97 PrintWriter traceOut) { 98 fErrorReporter = errorReporter; 99 fTraceOut = traceOut; 100 } 101 102 105 private void parsePage() throws XMLCException, IOException { 106 if (fVerbose) { 107 fTraceOut.println(">>> parsing " + InputSourceOps.getName(fInputSource)); 108 } 109 110 Parse parser = new Parse(fErrorReporter, fTraceOut); 111 fXmlcDoc = parser.parse(fMetaData); 112 fDocument = fXmlcDoc.getDocument(); 113 114 if (fCompileOptions.getPrintDocumentInfo()) { 115 if (fVerbose) { 116 fTraceOut.println(">>> printing document info"); 117 } 118 PrintInfo info = new PrintInfo(fDocument, fXmlcDoc); 119 info.printInfo(fTraceOut); 120 } 121 122 EditDOM domEditor = new EditDOM(fMetaData); 123 domEditor.edit(fXmlcDoc); 124 125 if (fCompileOptions.getPrintDOM()) { 126 if (fVerbose) { 127 fTraceOut.println(">>> dumping document hierarchy"); 128 } 129 DOMInfo.printTree("DOM hierarchy", fDocument, fTraceOut); 130 } 131 } 132 133 136 private void generateJavaSource() throws XMLCException, IOException { 137 if (fVerbose) { 138 fTraceOut.println(">>> generating code"); 139 } 140 ClassGenerator codeGen = 141 new ClassGenerator(fMetaData, fXmlcDoc, 142 (fCompileOptions.getPrintAccessorInfo() 143 ? fTraceOut : null)); 144 codeGen.generateJavaSource(fVerbose ? fTraceOut : null); 145 } 146 147 150 private void compileJavaSource() throws XMLCException { 151 if (fVerbose) { 152 fTraceOut.println(">>> compiling code"); 153 } 154 Javac javac = new Javac(); 155 javac.compile(fMetaData, fErrorReporter, 156 (fVerbose ? fTraceOut : null)); 157 } 158 159 162 private void writeDOM() throws IOException { 163 File docOut = new File (fCompileOptions.getDocumentOutput()); 164 String dir = docOut.getParent(); 165 if (dir != null) { 166 new File (dir).mkdirs(); 167 } 168 169 OutputOptions options = DOMFormatter.getDefaultOutputOptions(fXmlcDoc.getDocument()); 170 options.setEncoding(fXmlcDoc.getEncoding()); 171 DOMFormatter formatter = new DOMFormatter(options); 172 formatter.write(fXmlcDoc.getDocument(), docOut); 173 } 174 175 178 private void compileDocument() 179 throws XMLCException, IOException { 180 DocumentClass documentClass = fMetaData.getDocumentClass(); 181 boolean saveWarnings = fErrorReporter.getPrintWarnings(); 182 fErrorReporter.setPrintWarnings(fCompileOptions.getWarnings()); 183 try { 184 generateJavaSource(); 185 186 if (fCompileOptions.getCompileSource()) { 187 compileJavaSource(); 188 } 189 190 191 if (documentClass.getRecompilation() || documentClass.getDeferredParsing()) { 192 if (fVerbose) { 193 fTraceOut.println(">>> output metadata to " + fMetaData.getDocument().getMetadataOutputFile().getAbsolutePath()); 194 } 195 fMetaData.getDocument().serialize(); 196 } 197 } finally { 198 fErrorReporter.setPrintWarnings(saveWarnings); 199 if (!fCompileOptions.getKeepGeneratedSource()) { 200 if (documentClass.getJavaClassSource() != null) { 201 documentClass.getJavaClassSource().delete(); 202 } 203 if (documentClass.getJavaInterfaceSource() != null) { 204 documentClass.getJavaInterfaceSource().delete(); 205 } 206 } 207 } 208 } 209 210 213 public void compile(MetaData metaData) 214 throws XMLCException, IOException { 215 216 fMetaData = metaData; 217 fCompileOptions = fMetaData.getCompileOptions(); 218 fVerbose = fCompileOptions.getVerbose(); 219 220 String inputDocument = fMetaData.getInputDocument().getUrl(); 221 if (inputDocument == null) { 222 throw new XMLCException("input document not specified"); 223 } 224 fInputSource = new InputSource (inputDocument); 225 226 parsePage(); 227 if (fCompileOptions.getCreateSource()) { 228 compileDocument(); 229 } 230 231 if (fCompileOptions.getDocumentOutput() != null) { 233 writeDOM(); 234 } 235 if (fVerbose) { 236 fTraceOut.println(">>> completed"); 237 } 238 } 239 } 240 | Popular Tags |