1 23 24 package org.enhydra.xml.xmlc.driver; 25 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.OutputStreamWriter ; 30 import java.io.PrintWriter ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 34 import org.enhydra.xml.driver.TestCaseBase; 35 import org.enhydra.xml.driver.TestError; 36 import org.enhydra.xml.driver.TestException; 37 import org.enhydra.xml.driver.TestFileOps; 38 import org.enhydra.xml.driver.TestProperties; 39 import org.enhydra.xml.xmlc.XMLCException; 40 import org.enhydra.xml.xmlc.commands.xmlc.XMLC; 41 42 46 public class ExecXmlc { 47 48 private static final boolean DEBUG_FORCE_VERBOSE = false; 49 50 51 public static final String OPT_PARSER = "-parser"; 52 public static final String OPT_DOM_FACTORY = "-domfactory"; 53 public static final String OPT_DOM = "-dom"; 54 public static final String OPT_FOR_RECOMP = "-for-recomp"; 55 public static final String OPT_FOR_DEFERRED_PARSING = "-for-deferred-parsing"; 56 public static final String OPT_DEST_DIR = "-d"; 57 public static final String OPT_SOURCE_OUT = "-sourceout"; 58 public static final String OPT_KEEP = "-keep"; 59 public static final String OPT_CLASS = "-class"; 60 public static final String OPT_DUMP = "-dump"; 61 public static final String OPT_METHODS = "-methods"; 62 public static final String OPT_NO_COMPILE = "-nocompile"; 63 public static final String OPT_DOC_OUT = "-docout"; 64 public static final String OPT_URL_MAPPING = "-urlmapping"; 65 public static final String OPT_URL_REGEXP_MAPPING = "-urlregexpmapping"; 66 public static final String OPT_URL_SETTING = "-urlsetting"; 67 public static final String OPT_JAVAC_G = "-g"; 68 public static final String OPT_SSI = "-ssi"; 69 public static final String OPT_WARNINGS = "-warnings"; 70 public static final String OPT_DELETE_CLASS = "-delete-class"; 71 public static final String OPT_JAVAC_FLAG = "-javacflag"; 72 public static final String OPT_JAVAC_OPT = "-javacopt"; 73 public static final String OPT_VERBOSE = "-verbose"; 74 public static final String OPT_CREATE_GET_TAG_METHODS 75 = "-create-get-tag-methods"; 76 public static final String OPT_EXTENDS = "-extends"; 77 public static final String OPT_IMPLEMENTS = "-implements"; 78 public static final String OPT_GENERATE = "-generate"; 79 80 public static final String OPT_HTML_ENCODING = "-html:encoding"; 81 public static final String OPT_ADD_TAG_SET = "-html:addtagset"; 82 public static final String OPT_VALIDATE = "-validate"; 83 public static final String OPT_XCATALOG = "-xcatalog"; 84 public static final String OPT_PARSEINFO = "-parseinfo"; 85 86 87 public static final String XERCES_DOM = "xerces"; 88 public static final String XERCES_HTML_DOM_FACTORY 89 = "org.enhydra.xml.xmlc.dom.xerces.XercesHTMLDomFactory"; 90 public static final String XERCES_DOM_FACTORY 91 = "org.enhydra.xml.xmlc.dom.xerces.XercesDomFactory"; 92 public static final String LAZY_DOM = "lazydom"; 93 public static final String LAZY_HTML_DOM_FACTORY 94 = "org.enhydra.xml.xmlc.dom.lazydom.LazyHTMLDomFactory"; 95 public static final String LAZY_DOM_FACTORY 96 = "org.enhydra.xml.xmlc.dom.lazydom.LazyDomFactory"; 97 public static final String CYBERSTUDIO_TAG_SET = "cyberstudio"; 98 public static final String SWING_PARSER = "swing"; 99 public static final String TIDY_PARSER = "tidy"; 100 public static final String XERCES_PARSER = "xerces"; 101 public static final String GENERATE_BOTH = "both"; 102 public static final String GENERATE_INTERFACE = "interface"; 103 public static final String GENERATE_IMPLEMENTATION = "implementation"; 104 public static final String YES = "yes"; 105 public static final String NO = "no"; 106 107 108 private TestCaseBase fTest; 109 110 111 private XmlcTestParams fParams; 112 113 114 private ArrayList fOpts = new ArrayList (); 115 116 117 private String fClassPath; 118 119 120 private File fDestDir; 121 122 123 private String fClassName; 124 125 126 private ArrayList fOptFiles; 127 128 129 private File fSrcFile; 130 131 132 private boolean fAutoSetDom = true; 133 134 135 private boolean fForRecomp = false; 136 137 138 private boolean fForDeferredParsing = false; 139 140 141 private boolean fSetVerbose = false; 142 143 148 private ArrayList fExtraSrc; 149 150 151 public ExecXmlc(XmlcTestCaseBase test) { 152 this(test, test.getParams()); 153 } 154 155 156 public ExecXmlc(TestCaseBase testCase, 157 XmlcTestParams params) { 158 fTest = testCase; 159 fParams = params; 160 fForDeferredParsing = fParams.getIsDeferredParsing(); 161 } 162 163 164 public void setDomOpt(String opt, 165 String arg) { 166 addOpt(opt, arg); 167 fAutoSetDom = false; 168 } 169 170 174 public void addClassPath(File element) { 175 if (fClassPath == null) { 176 fClassPath = element.getPath(); 177 } else { 178 fClassPath = element.getPath() + File.pathSeparator + fClassPath; 179 } 180 } 181 182 183 private String makeJavacClassPath() { 184 String propPath = TestProperties.getJavacClassPath(); 185 if ((propPath == null) && (fClassPath == null)) { 186 return null; } 188 StringBuffer path = new StringBuffer (256); 189 if (fClassPath != null) { 190 path.append(fClassPath); 191 } 192 if (propPath != null) { 193 if (path.length() > 0) { 194 path.append(File.pathSeparator); 195 } 196 path.append(propPath); 197 } 198 199 path.append(File.pathSeparator); 202 path.append(System.getProperty("java.class.path")); 203 204 return path.toString(); 205 } 206 207 208 public void addOpt(String opt) { 209 fOpts.add(opt); 210 211 if (opt.equals(OPT_FOR_RECOMP)) { 213 fForRecomp = true; 214 } else if (opt.equals(OPT_FOR_DEFERRED_PARSING)) { 215 fForDeferredParsing = true; 216 } else if (opt.equals(OPT_VERBOSE)) { 217 fSetVerbose = true; 218 } 219 } 220 221 222 public void addOpt(String opt, 223 String arg) { 224 fOpts.add(opt); 225 fOpts.add(arg); 226 227 if (opt.equals(OPT_CLASS)) { 229 fClassName = arg; 230 } else if (opt.equals(OPT_DEST_DIR)) { 231 fDestDir = new File (arg); 232 } else if (opt.equals(OPT_DOM)) { 233 fAutoSetDom = false; 234 } else if (opt.equals(OPT_DOM_FACTORY)) { 235 fAutoSetDom = false; 236 } 237 } 238 239 240 public void addOpt(String opt, 241 File arg) { 242 addOpt(opt, arg.getPath()); 243 } 244 245 246 public void addOpt(String opt, 247 String arg1, 248 String arg2) { 249 fOpts.add(opt); 250 fOpts.add(arg1); 251 fOpts.add(arg2); 252 } 253 254 255 public void addOptionFile(File optFile) { 256 if (fOptFiles == null) { 257 fOptFiles = new ArrayList (); 258 } 259 fOptFiles.add(optFile); 260 } 261 262 265 public void addCyberStudioTags() { 266 addOpt(ExecXmlc.OPT_ADD_TAG_SET, ExecXmlc.CYBERSTUDIO_TAG_SET); 267 } 268 269 270 public void setSrcFile(File srcFile) { 271 fSrcFile = srcFile; 272 } 273 274 278 public void addExtraSrcFile(File extraSrcFile) { 279 if (fExtraSrc == null) { 280 fExtraSrc = new ArrayList (); 281 } 282 fExtraSrc.add(extraSrcFile); 283 } 284 285 286 private String [] buildArgs() { 287 if (fSrcFile == null) { 288 throw new TestError("bug: no source file specified"); 289 } 290 291 ArrayList args = new ArrayList (); 292 if (fAutoSetDom && fParams.getDom().equals(ExecXmlc.XERCES_DOM)) { 293 args.add(OPT_DOM_FACTORY); 294 if (fParams.getIsXml()) { 295 args.add(XERCES_DOM_FACTORY); 296 } else { 297 args.add(XERCES_HTML_DOM_FACTORY); 298 } 299 } 300 if (fForRecomp) { 301 args.add(ExecXmlc.OPT_FOR_RECOMP); 302 } 303 304 if (fForDeferredParsing) { 305 args.add(ExecXmlc.OPT_FOR_DEFERRED_PARSING); 306 } 307 308 args.add(OPT_PARSER); 309 args.add(fParams.getParser()); 310 311 String path = makeJavacClassPath(); 313 if (path != null) { 314 args.add(OPT_JAVAC_OPT); 315 args.add("-classpath"); 316 args.add(path); 317 } 318 319 args.addAll(fOpts); 321 322 if (fSetVerbose || DEBUG_FORCE_VERBOSE) { 323 args.add(OPT_VERBOSE); 324 } 325 326 if (fOptFiles != null) { 328 Iterator iter = fOptFiles.iterator(); 329 while(iter.hasNext()) { 330 args.add(((File )iter.next()).getPath()); 331 } 332 } 333 args.add(fSrcFile.getPath()); 334 return (String [])args.toArray(new String [args.size()]); 335 } 336 337 338 private File getPkgDir() { 339 File dir = fDestDir; 341 if (dir == null) { 342 dir = new File ("."); 343 } 344 if (fClassName != null) { 346 String classNamePath = fClassName.replace('.', '/'); 347 dir = new File (dir, classNamePath).getParentFile(); 348 } 349 return dir; 350 } 351 352 353 private void copySrcToPkgDir() { 354 File pkgDir = getPkgDir(); 355 TestFileOps.copyFileToDir(fSrcFile, pkgDir); 356 if (fExtraSrc != null) { 357 for (int idx = 0; idx < fExtraSrc.size(); idx++) { 358 TestFileOps.copyFileToDir((File )fExtraSrc.get(idx), pkgDir); 359 } 360 } 361 } 362 363 364 private void doCompile(PrintWriter out) 365 throws XMLCException, IOException { 366 if (fForRecomp || fForDeferredParsing) { 367 copySrcToPkgDir(); 368 } 369 String [] args = buildArgs(); 370 PrintWriter verbOut = fTest.getVerboseOut(); 371 if (verbOut != null) { 372 verbOut.print("xmlc compile: xmlc "); 373 for (int iArg = 0; iArg < args.length; iArg++) { 374 verbOut.print(" "); 375 verbOut.print(args[iArg]); 376 } 377 verbOut.println(); 378 } 379 380 if (verbOut != null) { 381 new XMLC(out, verbOut).compile(args); 382 } else { 383 new XMLC(out, out).compile(args); 384 } 385 386 } 387 388 389 public void compile(File outFile) throws TestException { 390 try { 391 TestFileOps.ensureFileDir(outFile); 392 if (fDestDir != null) { 393 fDestDir.mkdirs(); 394 } 395 396 PrintWriter out 398 = new PrintWriter (new OutputStreamWriter ( 399 new FileOutputStream (outFile), "UTF-8")); 400 try { 401 doCompile(out); 402 } catch (XMLCException except) { 403 out.println(except.toString()); 405 throw except; 406 } finally { 407 out.close(); 408 } 409 } catch (Throwable err) { 410 throw new TestException("xmlc compile failed", err); 411 } 412 } 413 414 } 415 | Popular Tags |