KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > driver > ExecXmlc


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: ExecXmlc.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.driver;
25
26 import java.io.File JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
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 /**
43  * Run XMLC for tests. This calls XMLC with command line options to
44  * test parsing. Also has various constants useful for calling XMLC.
45  */

46 public class ExecXmlc {
47     /** Compile-time force verbose XMLC output, for debugging */
48     private static final boolean DEBUG_FORCE_VERBOSE = false;
49
50     /** Constants for XMLC options */
51     public static final String JavaDoc OPT_PARSER = "-parser";
52     public static final String JavaDoc OPT_DOM_FACTORY = "-domfactory";
53     public static final String JavaDoc OPT_DOM = "-dom";
54     public static final String JavaDoc OPT_FOR_RECOMP = "-for-recomp";
55     public static final String JavaDoc OPT_FOR_DEFERRED_PARSING = "-for-deferred-parsing";
56     public static final String JavaDoc OPT_DEST_DIR = "-d";
57     public static final String JavaDoc OPT_SOURCE_OUT = "-sourceout";
58     public static final String JavaDoc OPT_KEEP = "-keep";
59     public static final String JavaDoc OPT_CLASS = "-class";
60     public static final String JavaDoc OPT_DUMP = "-dump";
61     public static final String JavaDoc OPT_METHODS = "-methods";
62     public static final String JavaDoc OPT_NO_COMPILE = "-nocompile";
63     public static final String JavaDoc OPT_DOC_OUT = "-docout";
64     public static final String JavaDoc OPT_URL_MAPPING = "-urlmapping";
65     public static final String JavaDoc OPT_URL_REGEXP_MAPPING = "-urlregexpmapping";
66     public static final String JavaDoc OPT_URL_SETTING = "-urlsetting";
67     public static final String JavaDoc OPT_JAVAC_G = "-g";
68     public static final String JavaDoc OPT_SSI = "-ssi";
69     public static final String JavaDoc OPT_WARNINGS = "-warnings";
70     public static final String JavaDoc OPT_DELETE_CLASS = "-delete-class";
71     public static final String JavaDoc OPT_JAVAC_FLAG = "-javacflag";
72     public static final String JavaDoc OPT_JAVAC_OPT = "-javacopt";
73     public static final String JavaDoc OPT_VERBOSE = "-verbose";
74     public static final String JavaDoc OPT_CREATE_GET_TAG_METHODS
75         = "-create-get-tag-methods";
76     public static final String JavaDoc OPT_EXTENDS = "-extends";
77     public static final String JavaDoc OPT_IMPLEMENTS = "-implements";
78     public static final String JavaDoc OPT_GENERATE = "-generate";
79
80     public static final String JavaDoc OPT_HTML_ENCODING = "-html:encoding";
81     public static final String JavaDoc OPT_ADD_TAG_SET = "-html:addtagset";
82     public static final String JavaDoc OPT_VALIDATE = "-validate";
83     public static final String JavaDoc OPT_XCATALOG = "-xcatalog";
84     public static final String JavaDoc OPT_PARSEINFO = "-parseinfo";
85
86     /** Constants for various argument values */
87     public static final String JavaDoc XERCES_DOM = "xerces";
88     public static final String JavaDoc XERCES_HTML_DOM_FACTORY
89         = "org.enhydra.xml.xmlc.dom.xerces.XercesHTMLDomFactory";
90     public static final String JavaDoc XERCES_DOM_FACTORY
91         = "org.enhydra.xml.xmlc.dom.xerces.XercesDomFactory";
92     public static final String JavaDoc LAZY_DOM = "lazydom";
93     public static final String JavaDoc LAZY_HTML_DOM_FACTORY
94         = "org.enhydra.xml.xmlc.dom.lazydom.LazyHTMLDomFactory";
95     public static final String JavaDoc LAZY_DOM_FACTORY
96         = "org.enhydra.xml.xmlc.dom.lazydom.LazyDomFactory";
97     public static final String JavaDoc CYBERSTUDIO_TAG_SET = "cyberstudio";
98     public static final String JavaDoc SWING_PARSER = "swing";
99     public static final String JavaDoc TIDY_PARSER = "tidy";
100     public static final String JavaDoc XERCES_PARSER = "xerces";
101     public static final String JavaDoc GENERATE_BOTH = "both";
102     public static final String JavaDoc GENERATE_INTERFACE = "interface";
103     public static final String JavaDoc GENERATE_IMPLEMENTATION = "implementation";
104     public static final String JavaDoc YES = "yes";
105     public static final String JavaDoc NO = "no";
106     
107     /** Test case we are assocaited with. */
108     private TestCaseBase fTest;
109
110     /** Object containing test parameters */
111     private XmlcTestParams fParams;
112
113     /** List of options and arguments */
114     private ArrayList JavaDoc fOpts = new ArrayList JavaDoc();
115
116     /** If not null, value to pass to javac -classpath */
117     private String JavaDoc fClassPath;
118
119     /** Destination directory */
120     private File JavaDoc fDestDir;
121
122     /** Class name */
123     private String JavaDoc fClassName;
124
125     /** Option files (created on first use) */
126     private ArrayList JavaDoc fOptFiles;
127
128     /** Source file to compile */
129     private File JavaDoc fSrcFile;
130
131     /* Automatically set the DOM */
132     private boolean fAutoSetDom = true;
133
134     /** Use -for-recomp and copy source to class dir */
135     private boolean fForRecomp = false;
136
137     /** Use -for-deferred-parsing and copy source to class dir */
138     private boolean fForDeferredParsing = false;
139
140     /** was -verbose set? */
141     private boolean fSetVerbose = false;
142
143     /**
144      * List of additional source files that are to be copied to class
145      * directory if recompilation is being tested. null if none have been
146      * added.
147      */

148     private ArrayList JavaDoc fExtraSrc;
149
150     /** Constructor */
151     public ExecXmlc(XmlcTestCaseBase test) {
152     this(test, test.getParams());
153     }
154
155     /** Constructor */
156     public ExecXmlc(TestCaseBase testCase,
157                     XmlcTestParams params) {
158         fTest = testCase;
159         fParams = params;
160     fForDeferredParsing = fParams.getIsDeferredParsing();
161     }
162
163     /** Specify the -dom or -domfactory option, preventing automatic setting */
164     public void setDomOpt(String JavaDoc opt,
165                           String JavaDoc arg) {
166         addOpt(opt, arg);
167         fAutoSetDom = false;
168     }
169
170     /**
171      * Add a element to the -classpath passed to javac. If this has not been
172      * called, then the option is not passed.
173      */

174     public void addClassPath(File JavaDoc element) {
175         if (fClassPath == null) {
176             fClassPath = element.getPath();
177         } else {
178             fClassPath = element.getPath() + File.pathSeparator + fClassPath;
179         }
180     }
181
182     /** Create classpath to pass to javac, or null if none should be passed */
183     private String JavaDoc makeJavacClassPath() {
184         String JavaDoc propPath = TestProperties.getJavacClassPath();
185         if ((propPath == null) && (fClassPath == null)) {
186             return null; // no path to add
187
}
188         StringBuffer JavaDoc path = new StringBuffer JavaDoc(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         // must add system classpath, as CLASSPATH environment is ignored
200
// if -classpath is specified
201
path.append(File.pathSeparator);
202         path.append(System.getProperty("java.class.path"));
203
204         return path.toString();
205     }
206
207     /** Add an option */
208     public void addOpt(String JavaDoc opt) {
209         fOpts.add(opt);
210
211         // Detect options that need special handling
212
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     /** Add and option and argument */
222     public void addOpt(String JavaDoc opt,
223                        String JavaDoc arg) {
224         fOpts.add(opt);
225         fOpts.add(arg);
226
227         // Detect options that need special handling
228
if (opt.equals(OPT_CLASS)) {
229             fClassName = arg;
230         } else if (opt.equals(OPT_DEST_DIR)) {
231             fDestDir = new File JavaDoc(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     /** Add and option and file argument */
240     public void addOpt(String JavaDoc opt,
241                        File JavaDoc arg) {
242         addOpt(opt, arg.getPath());
243     }
244
245     /** Add an option and two arguments */
246     public void addOpt(String JavaDoc opt,
247                        String JavaDoc arg1,
248                        String JavaDoc arg2) {
249         fOpts.add(opt);
250         fOpts.add(arg1);
251         fOpts.add(arg2);
252     }
253
254     /** Add an option or metadata file */
255     public void addOptionFile(File JavaDoc optFile) {
256         if (fOptFiles == null) {
257             fOptFiles = new ArrayList JavaDoc();
258         }
259         fOptFiles.add(optFile);
260     }
261
262     /**
263      * Add the cyberstudio tag set.
264      */

265     public void addCyberStudioTags() {
266         addOpt(ExecXmlc.OPT_ADD_TAG_SET, ExecXmlc.CYBERSTUDIO_TAG_SET);
267     }
268
269     /** Set the source file */
270     public void setSrcFile(File JavaDoc srcFile) {
271         fSrcFile = srcFile;
272     }
273
274     /**
275      * Add an additional source file to are to be copied to class directory if
276      * recompilation is being tested.
277      */

278     public void addExtraSrcFile(File JavaDoc extraSrcFile) {
279         if (fExtraSrc == null) {
280             fExtraSrc = new ArrayList JavaDoc();
281         }
282         fExtraSrc.add(extraSrcFile);
283     }
284
285     /** Build arguments */
286     private String JavaDoc[] buildArgs() {
287         if (fSrcFile == null) {
288             throw new TestError("bug: no source file specified");
289         }
290         
291         ArrayList JavaDoc args = new ArrayList JavaDoc();
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         // class path, if specified
312
String JavaDoc path = makeJavacClassPath();
313         if (path != null) {
314             args.add(OPT_JAVAC_OPT);
315             args.add("-classpath");
316             args.add(path);
317         }
318
319         // Add specified opts
320
args.addAll(fOpts);
321
322         if (fSetVerbose || DEBUG_FORCE_VERBOSE) {
323             args.add(OPT_VERBOSE);
324         }
325
326         // Options or metadata files
327
if (fOptFiles != null) {
328             Iterator JavaDoc iter = fOptFiles.iterator();
329             while(iter.hasNext()) {
330                 args.add(((File JavaDoc)iter.next()).getPath());
331             }
332         }
333         args.add(fSrcFile.getPath());
334         return (String JavaDoc[])args.toArray(new String JavaDoc[args.size()]);
335     }
336
337     /** Figure out the package directory */
338     private File JavaDoc getPkgDir() {
339         // If dest dir is not specified, it's current dir
340
File JavaDoc dir = fDestDir;
341         if (dir == null) {
342             dir = new File JavaDoc(".");
343         }
344         // If class isn't specified, it's the dest dir
345
if (fClassName != null) {
346             String JavaDoc classNamePath = fClassName.replace('.', '/');
347             dir = new File JavaDoc(dir, classNamePath).getParentFile();
348         }
349         return dir;
350     }
351
352     /** copy source files to package directory */
353     private void copySrcToPkgDir() {
354         File JavaDoc pkgDir = getPkgDir();
355         TestFileOps.copyFileToDir(fSrcFile, pkgDir);
356         if (fExtraSrc != null) {
357             for (int idx = 0; idx < fExtraSrc.size(); idx++) {
358                 TestFileOps.copyFileToDir((File JavaDoc)fExtraSrc.get(idx), pkgDir);
359             }
360         }
361     }
362
363     /** Do work of running xmlc */
364     private void doCompile(PrintWriter JavaDoc out)
365         throws XMLCException, IOException JavaDoc {
366         if (fForRecomp || fForDeferredParsing) {
367             copySrcToPkgDir();
368         }
369         String JavaDoc[] args = buildArgs();
370         PrintWriter JavaDoc 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     /** Execute XMLC */
389     public void compile(File JavaDoc outFile) throws TestException {
390         try {
391             TestFileOps.ensureFileDir(outFile);
392             if (fDestDir != null) {
393                 fDestDir.mkdirs();
394             }
395
396             // Force consistent encoding
397
PrintWriter JavaDoc out
398                 = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(
399                                     new FileOutputStream JavaDoc(outFile), "UTF-8"));
400             try {
401                 doCompile(out);
402             } catch (XMLCException except) {
403                 // Write exception that may or may not be normal to file
404
out.println(except.toString());
405                 throw except;
406             } finally {
407                 out.close();
408             }
409         } catch (Throwable JavaDoc err) {
410             throw new TestException("xmlc compile failed", err);
411         }
412     }
413     
414 }
415
Popular Tags