1 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 69 70 public class CompilerConfiguration 71 { 72 public static final CompilerConfiguration DEFAULT = new CompilerConfiguration(); 73 74 private int warningLevel; private String sourceEncoding; private PrintWriter output; private File targetDirectory; private LinkedList classpath; private boolean verbose; private boolean debug; private int tolerance; private String scriptBaseClass; 84 85 86 89 90 public CompilerConfiguration() 91 { 92 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 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 129 130 public CompilerConfiguration( Properties configuration ) throws ConfigurationException 131 { 132 this(); 133 134 String text = null; 135 int numeric = 0; 136 137 138 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 177 text = configuration.getProperty( "groovy.source.encoding" ); 178 if( text != null ) 179 { 180 setSourceEncoding( text ); 181 } 182 183 184 187 text = configuration.getProperty( "groovy.target.directory" ); 188 if( text != null ) 189 { 190 setTargetDirectory( text ); 191 } 192 193 194 197 text = configuration.getProperty( "groovy.classpath" ); 198 if( text != null ) 199 { 200 setClasspath( text ); 201 } 202 203 204 207 text = configuration.getProperty( "groovy.output.verbose" ); 208 if( text != null && text.equals("true") ) 209 { 210 setVerbose( true ); 211 } 212 213 214 217 text = configuration.getProperty( "groovy.output.debug" ); 218 if( text != null && text.equals("true") ) 219 { 220 setDebug( true ); 221 } 222 223 224 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 245 text = configuration.getProperty( "groovy.script.base" ); 246 setScriptBaseClass( text ); 247 } 248 249 250 251 255 256 public int getWarningLevel() 257 { 258 return this.warningLevel; 259 } 260 261 262 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 283 284 public String getSourceEncoding() 285 { 286 return this.sourceEncoding; 287 } 288 289 290 293 294 public void setSourceEncoding( String encoding ) 295 { 296 this.sourceEncoding = encoding; 297 } 298 299 300 301 304 305 public PrintWriter getOutput() 306 { 307 return this.output; 308 } 309 310 311 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 332 333 public File getTargetDirectory() 334 { 335 return this.targetDirectory; 336 } 337 338 339 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 359 360 public void setTargetDirectory( File directory ) 361 { 362 this.targetDirectory = directory; 363 } 364 365 366 367 368 369 372 373 public List getClasspath() 374 { 375 return this.classpath; 376 } 377 378 379 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 399 400 public boolean getVerbose() 401 { 402 return this.verbose; 403 } 404 405 406 409 410 public void setVerbose( boolean verbose ) 411 { 412 this.verbose = verbose; 413 } 414 415 416 417 420 421 public boolean getDebug() 422 { 423 return this.debug; 424 } 425 426 427 430 431 public void setDebug( boolean debug ) 432 { 433 this.debug = debug; 434 } 435 436 437 438 439 442 443 public int getTolerance() 444 { 445 return this.tolerance; 446 } 447 448 449 454 455 public void setTolerance( int tolerance ) 456 { 457 this.tolerance = tolerance; 458 } 459 460 461 462 466 467 public String getScriptBaseClass() 468 { 469 return this.scriptBaseClass; 470 } 471 472 473 477 public void setScriptBaseClass( String scriptBaseClass ) 478 { 479 this.scriptBaseClass = scriptBaseClass; 480 } 481 482 483 } 484 485 486 487 488 | Popular Tags |