1 package org.apache.velocity.texen.ant; 2 3 18 19 import java.util.StringTokenizer ; 20 import java.util.Date ; 21 import java.util.Hashtable ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import java.io.File ; 26 import java.io.Writer ; 27 import java.io.FileInputStream ; 28 import java.io.InputStream ; 29 import java.io.IOException ; 30 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Task; 33 import org.apache.velocity.VelocityContext; 34 import org.apache.velocity.app.VelocityEngine; 35 import org.apache.velocity.context.Context; 36 import org.apache.velocity.texen.Generator; 37 import org.apache.velocity.util.StringUtils; 38 import org.apache.velocity.exception.MethodInvocationException; 39 import org.apache.velocity.exception.ParseErrorException; 40 import org.apache.velocity.exception.ResourceNotFoundException; 41 import org.apache.commons.collections.ExtendedProperties; 42 43 50 public class TexenTask 51 extends Task 52 { 53 58 private final static String ERR_MSG_FRAGMENT = 59 ". For more information consult the velocity log, or invoke ant " + 60 "with the -debug flag."; 61 62 67 protected String controlTemplate; 68 69 73 protected String templatePath; 74 75 79 protected String outputDirectory; 80 81 85 protected String outputFile; 86 87 90 protected String outputEncoding; 91 92 96 protected String inputEncoding; 97 98 130 protected ExtendedProperties contextProperties; 131 132 136 protected boolean useClasspath; 137 138 141 private String fileSeparator = System.getProperty("file.separator"); 142 143 147 public void setControlTemplate (String controlTemplate) 148 { 149 this.controlTemplate = controlTemplate; 150 } 151 152 156 public String getControlTemplate() 157 { 158 return controlTemplate; 159 } 160 161 166 167 public void setTemplatePath(String templatePath) throws Exception 168 { 169 StringBuffer resolvedPath = new StringBuffer (); 170 StringTokenizer st = new StringTokenizer (templatePath, ","); 171 while ( st.hasMoreTokens() ) 172 { 173 File fullPath = project.resolveFile(st.nextToken()); 176 resolvedPath.append(fullPath.getCanonicalPath()); 177 if ( st.hasMoreTokens() ) 178 { 179 resolvedPath.append(","); 180 } 181 } 182 this.templatePath = resolvedPath.toString(); 183 184 System.out.println(templatePath); 185 } 186 187 192 public String getTemplatePath() 193 { 194 return templatePath; 195 } 196 197 201 public void setOutputDirectory(File outputDirectory) 202 { 203 try 204 { 205 this.outputDirectory = outputDirectory.getCanonicalPath(); 206 } 207 catch (java.io.IOException ioe) 208 { 209 throw new BuildException(ioe); 210 } 211 } 212 213 216 public String getOutputDirectory() 217 { 218 return outputDirectory; 219 } 220 221 225 public void setOutputFile(String outputFile) 226 { 227 this.outputFile = outputFile; 228 } 229 230 233 public void setOutputEncoding(String outputEncoding) 234 { 235 this.outputEncoding = outputEncoding; 236 } 237 238 241 public void setInputEncoding(String inputEncoding) 242 { 243 this.inputEncoding = inputEncoding; 244 } 245 246 250 public String getOutputFile() 251 { 252 return outputFile; 253 } 254 255 260 public void setContextProperties( String file ) 261 { 262 String [] sources = StringUtils.split(file,","); 263 contextProperties = new ExtendedProperties(); 264 265 for (int i = 0; i < sources.length; i++) 272 { 273 ExtendedProperties source = new ExtendedProperties(); 274 275 try 276 { 277 File fullPath = project.resolveFile(sources[i]); 280 log("Using contextProperties file: " + fullPath); 281 source.load(new FileInputStream (fullPath)); 282 } 283 catch (Exception e) 284 { 285 ClassLoader classLoader = this.getClass().getClassLoader(); 286 287 try 288 { 289 InputStream inputStream = classLoader.getResourceAsStream(sources[i]); 290 291 if (inputStream == null) 292 { 293 throw new BuildException("Context properties file " + sources[i] + 294 " could not be found in the file system or on the classpath!"); 295 } 296 else 297 { 298 source.load(inputStream); 299 } 300 } 301 catch (IOException ioe) 302 { 303 source = null; 304 } 305 } 306 307 Iterator j = source.getKeys(); 308 309 while (j.hasNext()) 310 { 311 String name = (String ) j.next(); 312 String value = source.getString(name); 313 contextProperties.setProperty(name,value); 314 } 315 } 316 } 317 318 323 public ExtendedProperties getContextProperties() 324 { 325 return contextProperties; 326 } 327 328 333 public void setUseClasspath(boolean useClasspath) 334 { 335 this.useClasspath = useClasspath; 336 } 337 338 345 public Context initControlContext() 346 throws Exception 347 { 348 return new VelocityContext(); 349 } 350 351 358 public void execute () 359 throws BuildException 360 { 361 if (templatePath == null && useClasspath == false) 363 { 364 throw new BuildException( 365 "The template path needs to be defined if you are not using " + 366 "the classpath for locating templates!"); 367 } 368 369 if (controlTemplate == null) 371 { 372 throw new BuildException("The control template needs to be defined!"); 373 } 374 375 if (outputDirectory == null) 377 { 378 throw new BuildException("The output directory needs to be defined!"); 379 } 380 381 if (outputFile == null) 383 { 384 throw new BuildException("The output file needs to be defined!"); 385 } 386 387 VelocityEngine ve = new VelocityEngine(); 388 389 try 390 { 391 if (templatePath != null) 393 { 394 log("Using templatePath: " + templatePath, project.MSG_VERBOSE); 395 ve.setProperty( 396 ve.FILE_RESOURCE_LOADER_PATH, templatePath); 397 } 398 399 if (useClasspath) 400 { 401 log("Using classpath"); 402 ve.addProperty( 403 VelocityEngine.RESOURCE_LOADER, "classpath"); 404 405 ve.setProperty( 406 "classpath." + VelocityEngine.RESOURCE_LOADER + ".class", 407 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 408 409 ve.setProperty( 410 "classpath." + VelocityEngine.RESOURCE_LOADER + 411 ".cache", "false"); 412 413 ve.setProperty( 414 "classpath." + VelocityEngine.RESOURCE_LOADER + 415 ".modificationCheckInterval", "2"); 416 } 417 418 ve.init(); 419 420 Generator generator = Generator.getInstance(); 422 generator.setVelocityEngine(ve); 423 generator.setOutputPath(outputDirectory); 424 generator.setInputEncoding(inputEncoding); 425 generator.setOutputEncoding(outputEncoding); 426 427 if (templatePath != null) 428 { 429 generator.setTemplatePath(templatePath); 430 } 431 432 File file = new File (outputDirectory); 435 if (! file.exists()) 436 { 437 file.mkdirs(); 438 } 439 440 String path = outputDirectory + File.separator + outputFile; 441 log("Generating to file " + path, project.MSG_INFO); 442 Writer writer = generator.getWriter(path, outputEncoding); 443 444 Context c = initControlContext(); 448 449 populateInitialContext(c); 455 456 if (contextProperties != null) 460 { 461 Iterator i = contextProperties.getKeys(); 462 463 while (i.hasNext()) 464 { 465 String property = (String ) i.next(); 466 String value = contextProperties.getString(property); 467 468 try 472 { 473 c.put(property, new Integer (value)); 474 } 475 catch (NumberFormatException nfe) 476 { 477 String booleanString = 481 contextProperties.testBoolean(value); 482 483 if (booleanString != null) 484 { 485 c.put(property, new Boolean (booleanString)); 486 } 487 else 488 { 489 if (property.endsWith("file.contents")) 503 { 504 value = StringUtils.fileContentsToString( 507 project.resolveFile(value).getCanonicalPath()); 508 509 property = property.substring( 510 0, property.indexOf("file.contents") - 1); 511 } 512 513 c.put(property, value); 514 } 515 } 516 } 517 } 518 519 writer.write(generator.parse(controlTemplate, c)); 520 writer.flush(); 521 writer.close(); 522 generator.shutdown(); 523 cleanup(); 524 } 525 catch( BuildException e) 526 { 527 throw e; 528 } 529 catch( MethodInvocationException e ) 530 { 531 throw new BuildException( 532 "Exception thrown by '" + e.getReferenceName() + "." + 533 e.getMethodName() +"'" + ERR_MSG_FRAGMENT, 534 e.getWrappedThrowable()); 535 } 536 catch( ParseErrorException e ) 537 { 538 throw new BuildException("Velocity syntax error" + ERR_MSG_FRAGMENT ,e); 539 } 540 catch( ResourceNotFoundException e ) 541 { 542 throw new BuildException("Resource not found" + ERR_MSG_FRAGMENT,e); 543 } 544 catch( Exception e ) 545 { 546 throw new BuildException("Generation failed" + ERR_MSG_FRAGMENT ,e); 547 } 548 } 549 550 567 protected void populateInitialContext(Context context) 568 throws Exception 569 { 570 context.put("now", new Date ().toString()); 571 } 572 573 581 protected void cleanup() 582 throws Exception 583 { 584 } 585 } 586 | Popular Tags |