1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package org.codehaus.groovy.control;
48
49 import java.io.File;
50 import java.io.PrintWriter;
51 import java.util.LinkedList;
52 import java.util.List;
53 import java.util.Properties;
54 import java.util.StringTokenizer;
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;
75 private String sourceEncoding;
76 private PrintWriter output;
77 private File targetDirectory;
78 private LinkedList classpath;
79 private boolean verbose;
80 private boolean debug;
81 private int tolerance;
82 private String scriptBaseClass;
83
84
85
86 /***
87 * Sets the Flags to defaults.
88 */
89
90 public CompilerConfiguration()
91 {
92
93
94
95 setWarningLevel( WarningMessage.LIKELY_ERRORS );
96 setSourceEncoding( "US-ASCII" );
97 setOutput( null );
98 setTargetDirectory( (File)null );
99 setClasspath( "" );
100 setVerbose( false );
101 setDebug( false );
102 setTolerance( 10 );
103 setScriptBaseClass( null );
104
105
106
107
108
109 try { setSourceEncoding( System.getProperty("file.encoding", "US-ASCII") ); } catch( Exception e ) {}
110 try { setOutput( new PrintWriter(System.err) ); } catch( Exception e ) {}
111 try { setClasspath( System.getProperty("java.class.path") ); } catch( Exception e ) {}
112
113 try
114 {
115 String target = System.getProperty( "groovy.target.directory" );
116 if( target != null )
117 {
118 setTargetDirectory( target );
119 }
120 }
121 catch( Exception 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 configuration ) throws ConfigurationException
131 {
132 this();
133
134 String text = null;
135 int numeric = 0;
136
137
138
139
140
141 numeric = getWarningLevel();
142 try
143 {
144 text = configuration.getProperty( "groovy.warnings", "likely errors" );
145 numeric = Integer.parseInt( text );
146 }
147 catch( NumberFormatException 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
176
177 text = configuration.getProperty( "groovy.source.encoding" );
178 if( text != null )
179 {
180 setSourceEncoding( text );
181 }
182
183
184
185
186
187 text = configuration.getProperty( "groovy.target.directory" );
188 if( text != null )
189 {
190 setTargetDirectory( text );
191 }
192
193
194
195
196
197 text = configuration.getProperty( "groovy.classpath" );
198 if( text != null )
199 {
200 setClasspath( text );
201 }
202
203
204
205
206
207 text = configuration.getProperty( "groovy.output.verbose" );
208 if( text != null && text.equals("true") )
209 {
210 setVerbose( true );
211 }
212
213
214
215
216
217 text = configuration.getProperty( "groovy.output.debug" );
218 if( text != null && text.equals("true") )
219 {
220 setDebug( true );
221 }
222
223
224
225
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 e )
235 {
236 throw new ConfigurationException( e );
237 }
238
239 setTolerance( numeric );
240
241
242
243
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 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 encoding )
295 {
296 this.sourceEncoding = encoding;
297 }
298
299
300
301 /***
302 * Gets the currently configured output writer.
303 */
304
305 public PrintWriter getOutput()
306 {
307 return this.output;
308 }
309
310
311 /***
312 * Sets the output writer.
313 */
314
315 public void setOutput( PrintWriter output )
316 {
317 if( this.output == null )
318 {
319 this.output = new PrintWriter( 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 getTargetDirectory()
334 {
335 return this.targetDirectory;
336 }
337
338
339 /***
340 * Sets the target directory.
341 */
342
343 public void setTargetDirectory( String directory )
344 {
345 if( directory != null && directory.length() > 0 )
346 {
347 this.targetDirectory = new File( 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 directory )
361 {
362 this.targetDirectory = directory;
363 }
364
365
366
367
368
369 /***
370 * Gets the classpath.
371 */
372
373 public List getClasspath()
374 {
375 return this.classpath;
376 }
377
378
379 /***
380 * Sets the output writer.
381 */
382
383 public void setClasspath( String classpath )
384 {
385 this.classpath = new LinkedList();
386
387 StringTokenizer tokenizer = new StringTokenizer( 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 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 scriptBaseClass )
478 {
479 this.scriptBaseClass = scriptBaseClass;
480 }
481
482
483 }
484
485
486
487