KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > groovy > control > CompilerConfiguration


1 /*
2  $Id: CompilerConfiguration.java,v 1.2 2004/04/20 01:32:07 cpoirier Exp $
3
4  Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5
6  Redistribution and use of this software and associated documentation
7  ("Software"), with or without modification, are permitted provided
8  that the following conditions are met:
9
10  1. Redistributions of source code must retain copyright
11     statements and notices. Redistributions must also contain a
12     copy of this document.
13
14  2. Redistributions in binary form must reproduce the
15     above copyright notice, this list of conditions and the
16     following disclaimer in the documentation and/or other
17     materials provided with the distribution.
18
19  3. The name "groovy" must not be used to endorse or promote
20     products derived from this Software without prior written
21     permission of The Codehaus. For written permission,
22     please contact info@codehaus.org.
23
24  4. Products derived from this Software may not be called "groovy"
25     nor may "groovy" appear in their names without prior written
26     permission of The Codehaus. "groovy" is a registered
27     trademark of The Codehaus.
28
29  5. Due credit should be given to The Codehaus -
30     http://groovy.codehaus.org/
31
32  THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33  ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
36  THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43  OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  */

46
47 package org.codehaus.groovy.control;
48
49 import java.io.File JavaDoc;
50 import java.io.PrintWriter JavaDoc;
51 import java.util.LinkedList JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Properties JavaDoc;
54 import java.util.StringTokenizer JavaDoc;
55
56 import org.codehaus.groovy.control.io.NullWriter;
57 import org.codehaus.groovy.control.messages.WarningMessage;
58
59
60
61
62 /**
63  * Compilation control flags and coordination stuff.
64  *
65  * @author <a HREF="mailto:cpoirier@dreaming.org">Chris Poirier</a>
66  *
67  * @version $Id: CompilerConfiguration.java,v 1.2 2004/04/20 01:32:07 cpoirier Exp $
68  */

69
70 public class CompilerConfiguration
71 {
72     public static final CompilerConfiguration DEFAULT = new CompilerConfiguration();
73     
74     private int warningLevel; // See WarningMessage for levels
75
private String JavaDoc sourceEncoding; // Encoding for source files
76
private PrintWriter JavaDoc output; // A PrintWriter for communicating with the user
77
private File JavaDoc targetDirectory; // Directory into which to write classes
78
private LinkedList JavaDoc classpath; // Classpath for use during compilation
79
private boolean verbose; // If true, the compiler should produce action information
80
private boolean debug; // If true, debugging code should be activated
81
private int tolerance; // The number of non-fatal errors to allow before bailing
82
private String JavaDoc scriptBaseClass; // Base class name for scripts (must derive from Script)
83

84     
85     
86    /**
87     * Sets the Flags to defaults.
88     */

89     
90     public CompilerConfiguration()
91     {
92         //
93
// Set in safe defaults
94

95         setWarningLevel( WarningMessage.LIKELY_ERRORS );
96         setSourceEncoding( "US-ASCII" );
97         setOutput( null );
98         setTargetDirectory( (File JavaDoc)null );
99         setClasspath( "" );
100         setVerbose( false );
101         setDebug( false );
102         setTolerance( 10 );
103         setScriptBaseClass( null );
104
105         
106         //
107
// Try for better defaults, ignore errors.
108

109         try { setSourceEncoding( System.getProperty("file.encoding", "US-ASCII") ); } catch( Exception JavaDoc e ) {}
110         try { setOutput( new PrintWriter JavaDoc(System.err) ); } catch( Exception JavaDoc e ) {}
111         try { setClasspath( System.getProperty("java.class.path") ); } catch( Exception JavaDoc e ) {}
112
113         try
114         {
115             String JavaDoc target = System.getProperty( "groovy.target.directory" );
116             if( target != null )
117             {
118                 setTargetDirectory( target );
119             }
120         }
121         catch( Exception JavaDoc e ) {}
122     }
123     
124    
125    /**
126     * Sets the Flags to the specified configuration, with defaults
127     * for those not supplied.
128     */

129     
130     public CompilerConfiguration( Properties JavaDoc configuration ) throws ConfigurationException
131     {
132         this();
133         
134         String JavaDoc text = null;
135         int numeric = 0;
136
137         
138         //
139
// Warning level
140

141         numeric = getWarningLevel();
142         try
143         {
144             text = configuration.getProperty( "groovy.warnings", "likely errors" );
145             numeric = Integer.parseInt( text );
146         }
147         catch( NumberFormatException JavaDoc e )
148         {
149             if( text.equals("none") )
150             {
151                 numeric = WarningMessage.NONE;
152             }
153             else if( text.startsWith("likely") )
154             {
155                 numeric = WarningMessage.LIKELY_ERRORS;
156             }
157             else if( text.startsWith("possible") )
158             {
159                 numeric = WarningMessage.POSSIBLE_ERRORS;
160             }
161             else if( text.startsWith("paranoia") )
162             {
163                 numeric = WarningMessage.PARANOIA;
164             }
165             else
166             {
167                 throw new ConfigurationException( "unrecogized groovy.warnings: " + text );
168             }
169         }
170         
171         setWarningLevel( numeric );
172         
173         
174         //
175
// Source file encoding
176

177         text = configuration.getProperty( "groovy.source.encoding" );
178         if( text != null )
179         {
180             setSourceEncoding( text );
181         }
182         
183         
184         //
185
// Target directory for classes
186

187         text = configuration.getProperty( "groovy.target.directory" );
188         if( text != null )
189         {
190             setTargetDirectory( text );
191         }
192         
193         
194         //
195
// Classpath
196

197         text = configuration.getProperty( "groovy.classpath" );
198         if( text != null )
199         {
200             setClasspath( text );
201         }
202             
203         
204         //
205
// Verbosity
206

207         text = configuration.getProperty( "groovy.output.verbose" );
208         if( text != null && text.equals("true") )
209         {
210             setVerbose( true );
211         }
212         
213         
214         //
215
// Debugging
216

217         text = configuration.getProperty( "groovy.output.debug" );
218         if( text != null && text.equals("true") )
219         {
220             setDebug( true );
221         }
222         
223         
224         //
225
// Tolerance
226

227         numeric = 10;
228         
229         try
230         {
231             text = configuration.getProperty( "groovy.errors.tolerance", "10" );
232             numeric = Integer.parseInt( text );
233         }
234         catch( NumberFormatException JavaDoc e )
235         {
236             throw new ConfigurationException( e );
237         }
238         
239         setTolerance( numeric );
240         
241         
242         //
243
// Script Base Class
244

245         text = configuration.getProperty( "groovy.script.base" );
246         setScriptBaseClass( text );
247     }
248
249
250     
251    /**
252     * Gets the currently configured warning level. See WarningMessage
253     * for level details.
254     */

255     
256     public int getWarningLevel()
257     {
258         return this.warningLevel;
259     }
260     
261     
262    /**
263     * Sets the warning level. See WarningMessage for level details.
264     */

265     
266     public void setWarningLevel( int level )
267     {
268         if( level < WarningMessage.NONE || level > WarningMessage.PARANOIA )
269         {
270             this.warningLevel = WarningMessage.LIKELY_ERRORS;
271         }
272         else
273         {
274             this.warningLevel = level;
275         }
276     }
277
278     
279     
280    /**
281     * Gets the currently configured source file encoding.
282     */

283     
284     public String JavaDoc getSourceEncoding()
285     {
286         return this.sourceEncoding;
287     }
288     
289     
290    /**
291     * Sets the encoding to be used when reading source files.
292     */

293     
294     public void setSourceEncoding( String JavaDoc encoding )
295     {
296         this.sourceEncoding = encoding;
297     }
298     
299
300     
301    /**
302     * Gets the currently configured output writer.
303     */

304     
305     public PrintWriter JavaDoc getOutput()
306     {
307         return this.output;
308     }
309     
310     
311    /**
312     * Sets the output writer.
313     */

314     
315     public void setOutput( PrintWriter JavaDoc output )
316     {
317         if( this.output == null )
318         {
319             this.output = new PrintWriter JavaDoc( NullWriter.DEFAULT );
320         }
321         else
322         {
323             this.output = output;
324         }
325     }
326     
327
328     
329    /**
330     * Gets the target directory for writing classes.
331     */

332     
333     public File JavaDoc getTargetDirectory()
334     {
335         return this.targetDirectory;
336     }
337     
338     
339    /**
340     * Sets the target directory.
341     */

342     
343     public void setTargetDirectory( String JavaDoc directory )
344     {
345         if( directory != null && directory.length() > 0 )
346         {
347             this.targetDirectory = new File JavaDoc( directory );
348         }
349         else
350         {
351             this.targetDirectory = null;
352         }
353     }
354
355     
356    /**
357     * Sets the target directory.
358     */

359     
360     public void setTargetDirectory( File JavaDoc directory )
361     {
362         this.targetDirectory = directory;
363     }
364     
365     
366
367     
368
369    /**
370     * Gets the classpath.
371     */

372     
373     public List JavaDoc getClasspath()
374     {
375         return this.classpath;
376     }
377     
378     
379    /**
380     * Sets the output writer.
381     */

382     
383     public void setClasspath( String JavaDoc classpath )
384     {
385         this.classpath = new LinkedList JavaDoc();
386         
387         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( classpath, File.pathSeparator );
388         while( tokenizer.hasMoreTokens() )
389         {
390             this.classpath.add( tokenizer.nextToken() );
391         }
392     }
393     
394
395
396    /**
397     * Returns true if verbose operation has been requested.
398     */

399     
400     public boolean getVerbose()
401     {
402         return this.verbose;
403     }
404     
405     
406    /**
407     * Turns verbose operation on or off.
408     */

409     
410     public void setVerbose( boolean verbose )
411     {
412         this.verbose = verbose;
413     }
414
415
416
417    /**
418     * Returns true if debugging operation has been requested.
419     */

420     
421     public boolean getDebug()
422     {
423         return this.debug;
424     }
425     
426     
427    /**
428     * Turns debugging operation on or off.
429     */

430     
431     public void setDebug( boolean debug )
432     {
433         this.debug = debug;
434     }
435
436
437
438
439    /**
440     * Returns the requested error tolerance.
441     */

442     
443     public int getTolerance()
444     {
445         return this.tolerance;
446     }
447     
448     
449    /**
450     * Sets the error tolerance, which is the number of
451     * non-fatal errors (per unit) that should be tolerated before
452     * compilation is aborted.
453     */

454     
455     public void setTolerance( int tolerance )
456     {
457         this.tolerance = tolerance;
458     }
459
460
461     
462    /**
463     * Gets the name of the base class for scripts. It must be a subclass
464     * of Script.
465     */

466
467     public String JavaDoc getScriptBaseClass()
468     {
469         return this.scriptBaseClass;
470     }
471
472     
473    /**
474     * Sets the name of the base class for scripts. It must be a subclass
475     * of Script.
476     */

477     public void setScriptBaseClass( String JavaDoc scriptBaseClass )
478     {
479         this.scriptBaseClass = scriptBaseClass;
480     }
481
482
483 }
484
485
486
487
488
Popular Tags