KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > codegen > JavaCompile


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: JavaCompile.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.codegen;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 import org.enhydra.xml.io.ErrorReporter;
30 import org.enhydra.xml.xmlc.XMLCException;
31 import org.enhydra.xml.xmlc.misc.ProcessRunner;
32
33
34 //FIXME: This could be integrated with JavaClass.
35

36 /**
37  * Run a Java compiler as a child process.
38  */

39 public class JavaCompile extends ProcessRunner {
40     /**
41      * Default options.
42      */

43     private static final int DEFAULT_OPTIONS = PASS_STDOUT|PASS_STDERR;
44
45     /**
46      * Default java compiler command.
47      */

48     private static final String JavaDoc DEFAULT_JAVAC = "javac";
49
50     /**
51      * Error handler to use.
52      */

53     private ErrorReporter fErrorReporter;
54
55     /**
56      * Java program.
57      */

58     private String JavaDoc fJavac;
59
60     /**
61      * Class output root directory.
62      */

63     private String JavaDoc fClassOutputRoot;
64
65     /**
66      * General argument command to execute.
67      */

68     private ArrayList JavaDoc fArgs = new ArrayList JavaDoc();
69
70     /**
71      * Source files.
72      */

73     private ArrayList JavaDoc fSrcs = new ArrayList JavaDoc();
74
75     /**
76      * Construct a javac runner, specifying a java compiler.
77      */

78     public JavaCompile(ErrorReporter errorReporter,
79                        String JavaDoc javac) {
80         super(DEFAULT_OPTIONS);
81         fErrorReporter = errorReporter;
82         if (javac != null) {
83             fJavac = javac;
84         } else {
85             fJavac = DEFAULT_JAVAC;
86         }
87     }
88
89     /**
90      * Set the class output root.
91      */

92     public void setClassOutputRoot(String JavaDoc root) {
93         fClassOutputRoot = root;
94     }
95
96     /**
97      * Add an argument.
98      */

99     public void addArg(String JavaDoc arg) {
100         fArgs.add(arg);
101     }
102
103     /**
104      * Add an argument and associate value.
105      */

106     public void addArg(String JavaDoc arg,
107                        String JavaDoc value) {
108         fArgs.add(arg);
109         fArgs.add(value);
110     }
111
112     /**
113      * Add an array of arguments.
114      */

115     public void addArgs(String JavaDoc[] args) {
116         for (int i = 0; i < args.length; i++) {
117             fArgs.add(args[i]);
118         }
119     }
120
121     /**
122      * Add a source file to compile.
123      */

124     public void addSrc(String JavaDoc srcFile) {
125         fSrcs.add(srcFile);
126     }
127
128     /**
129      * Run the javac process. Stdout/stderr are written to the standard
130      * descriptors.
131      *
132      * @param metaData Document metadata.
133      * @param errorReporter Write errors to this object.
134      * @param verboseOut Write verbose message to this file if not null.
135      */

136     public void compile(PrintWriter JavaDoc verboseOut) throws XMLCException {
137         ArrayList JavaDoc cmd = new ArrayList JavaDoc();
138         cmd.add(fJavac);
139         if (fClassOutputRoot != null) {
140             cmd.add("-d");
141             cmd.add(fClassOutputRoot);
142         }
143         cmd.addAll(fArgs);
144         cmd.addAll(fSrcs);
145         // Run process
146
run((String JavaDoc[])cmd.toArray(new String JavaDoc[cmd.size()]),
147             fErrorReporter, verboseOut,
148             "compile of generated java code failed");
149     }
150 }
151
Popular Tags