1 package org.apache.velocity.anakia; 2 3 18 19 import java.io.BufferedWriter ; 20 import java.io.File ; 21 import java.io.FileOutputStream ; 22 import java.io.OutputStreamWriter ; 23 import java.io.Writer ; 24 25 import java.util.StringTokenizer ; 26 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.DirectoryScanner; 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.taskdefs.MatchingTask; 31 32 import org.xml.sax.SAXParseException ; 33 34 import org.jdom.Document; 35 import org.jdom.JDOMException; 36 import org.jdom.input.SAXBuilder; 37 38 import org.apache.velocity.Template; 39 import org.apache.velocity.app.VelocityEngine; 40 import org.apache.velocity.runtime.RuntimeConstants; 41 import org.apache.velocity.util.StringUtils; 42 43 import org.apache.velocity.VelocityContext; 44 45 60 public class AnakiaTask extends MatchingTask 61 { 62 63 private SAXBuilder builder; 64 65 66 private File destDir = null; 67 68 69 private File baseDir = null; 70 71 72 private String style = null; 73 74 75 private File styleFile = null; 76 77 78 private long styleSheetLastModified = 0; 79 80 81 private String projectAttribute = null; 82 83 84 private File projectFile = null; 85 86 87 private long projectFileLastModified = 0; 88 89 90 private boolean lastModifiedCheck = true; 91 92 93 private String extension = ".html"; 94 95 96 private String templatePath = null; 97 98 99 private File velocityPropertiesFile = null; 100 101 102 private VelocityEngine ve = new VelocityEngine(); 103 104 107 public AnakiaTask() 108 { 109 builder = new SAXBuilder(); 110 builder.setFactory(new AnakiaJDOMFactory()); 111 } 112 113 116 public void setBasedir(File dir) 117 { 118 baseDir = dir; 119 } 120 121 126 public void setDestdir(File dir) 127 { 128 destDir = dir; 129 } 130 131 134 public void setExtension(String extension) 135 { 136 this.extension = extension; 137 } 138 139 142 public void setStyle(String style) 143 { 144 this.style = style; 145 } 146 147 150 public void setProjectFile(String projectAttribute) 151 { 152 this.projectAttribute = projectAttribute; 153 } 154 155 164 165 public void setTemplatePath(File templatePath) 166 { 167 try 168 { 169 this.templatePath = templatePath.getCanonicalPath(); 170 } 171 catch (java.io.IOException ioe) 172 { 173 throw new BuildException(ioe); 174 } 175 } 176 177 184 public void setVelocityPropertiesFile(File velocityPropertiesFile) 185 { 186 this.velocityPropertiesFile = velocityPropertiesFile; 187 } 188 189 192 public void setLastModifiedCheck(String lastmod) 193 { 194 if (lastmod.equalsIgnoreCase("false") || lastmod.equalsIgnoreCase("no") 195 || lastmod.equalsIgnoreCase("off")) 196 { 197 this.lastModifiedCheck = false; 198 } 199 } 200 201 204 public void execute () throws BuildException 205 { 206 DirectoryScanner scanner; 207 String [] list; 208 String [] dirs; 209 210 if (baseDir == null) 211 { 212 baseDir = project.resolveFile("."); 213 } 214 if (destDir == null ) 215 { 216 String msg = "destdir attribute must be set!"; 217 throw new BuildException(msg); 218 } 219 if (style == null) 220 { 221 throw new BuildException("style attribute must be set!"); 222 } 223 224 if (velocityPropertiesFile == null) 225 { 226 velocityPropertiesFile = new File ("velocity.properties"); 227 } 228 229 233 if ( !velocityPropertiesFile.exists() && templatePath == null ) 234 { 235 throw new BuildException ("No template path and could not " + 236 "locate velocity.properties file: " + 237 velocityPropertiesFile.getAbsolutePath()); 238 } 239 240 log("Transforming into: " + destDir.getAbsolutePath(), Project.MSG_INFO); 241 242 if (projectAttribute != null && projectAttribute.length() > 0) 244 { 245 projectFile = new File (baseDir, projectAttribute); 246 if (projectFile.exists()) 247 { 248 projectFileLastModified = projectFile.lastModified(); 249 } 250 else 251 { 252 log ("Project file is defined, but could not be located: " + 253 projectFile.getAbsolutePath(), Project.MSG_INFO ); 254 projectFile = null; 255 } 256 } 257 258 Document projectDocument = null; 259 try 260 { 261 if ( velocityPropertiesFile.exists() ) 262 { 263 ve.init(velocityPropertiesFile.getAbsolutePath()); 264 } 265 else if (templatePath != null && templatePath.length() > 0) 266 { 267 ve.setProperty( RuntimeConstants.FILE_RESOURCE_LOADER_PATH, 268 templatePath); 269 ve.init(); 270 } 271 272 styleSheetLastModified = ve.getTemplate( style ).getLastModified(); 274 275 if (projectFile != null) 277 { 278 projectDocument = builder.build(projectFile); 279 } 280 } 281 catch (Exception e) 282 { 283 log("Error: " + e.toString(), Project.MSG_INFO); 284 throw new BuildException(e); 285 } 286 287 scanner = getDirectoryScanner(baseDir); 289 290 list = scanner.getIncludedFiles(); 292 for (int i = 0;i < list.length; ++i) 293 { 294 process( baseDir, list[i], destDir, projectDocument ); 295 } 296 } 297 298 301 private void process(File baseDir, String xmlFile, File destDir, 302 Document projectDocument) 303 throws BuildException 304 { 305 File outFile=null; 306 File inFile=null; 307 Writer writer = null; 308 try 309 { 310 inFile = new File (baseDir,xmlFile); 312 outFile = new File (destDir, 314 xmlFile.substring(0, 315 xmlFile.lastIndexOf('.')) + extension); 316 317 if (lastModifiedCheck == false || 319 (inFile.lastModified() > outFile.lastModified() || 320 styleSheetLastModified > outFile.lastModified() || 321 projectFileLastModified > outFile.lastModified())) 322 { 323 ensureDirectoryFor( outFile ); 324 325 log("Input: " + xmlFile, Project.MSG_INFO ); 327 328 Document root = builder.build(inFile); 330 331 VelocityContext context = new VelocityContext(); 333 334 338 String encoding = (String ) ve.getProperty( RuntimeConstants.OUTPUT_ENCODING ); 339 if (encoding == null || encoding.length() == 0 340 || encoding.equals("8859-1") || encoding.equals("8859_1")) 341 { 342 encoding = "ISO-8859-1"; 343 } 344 345 OutputWrapper ow = new OutputWrapper(); 346 ow.setEncoding (encoding); 347 348 context.put ("root", root.getRootElement()); 349 context.put ("xmlout", ow ); 350 context.put ("relativePath", getRelativePath(xmlFile)); 351 context.put ("treeWalk", new TreeWalker()); 352 context.put ("xpath", new XPathTool() ); 353 context.put ("escape", new Escape() ); 354 context.put ("date", new java.util.Date () ); 355 356 if (projectDocument != null) 358 { 359 context.put ("project", projectDocument.getRootElement()); 360 } 361 362 writer = new BufferedWriter (new OutputStreamWriter ( 365 new FileOutputStream (outFile), 366 encoding)); 367 Template template = ve.getTemplate(style); 369 template.merge(context, writer); 370 371 log("Output: " + outFile, Project.MSG_INFO ); 372 } 373 } 374 catch (JDOMException e) 375 { 376 if (outFile != null ) outFile.delete(); 377 if (e.getCause() != null) 378 { 379 Throwable rootCause = e.getCause(); 380 if (rootCause instanceof SAXParseException ) 381 { 382 System.out.println(""); 383 System.out.println("Error: " + rootCause.getMessage()); 384 System.out.println( 385 " Line: " + 386 ((SAXParseException )rootCause).getLineNumber() + 387 " Column: " + 388 ((SAXParseException )rootCause).getColumnNumber()); 389 System.out.println(""); 390 } 391 else 392 { 393 rootCause.printStackTrace(); 394 } 395 } 396 else 397 { 398 e.printStackTrace(); 399 } 400 } 402 catch (Throwable e) 403 { 404 if (outFile != null) 406 { 407 outFile.delete(); 408 } 409 e.printStackTrace(); 410 } 411 finally 412 { 413 if (writer != null) 414 { 415 try 416 { 417 writer.flush(); 418 writer.close(); 419 } 420 catch (Exception e) 421 { 422 } 423 } 424 } 425 } 426 427 432 private String getRelativePath(String file) 433 { 434 if (file == null || file.length()==0) 435 return ""; 436 StringTokenizer st = new StringTokenizer (file, "/\\"); 437 int slashCount = st.countTokens() - 1; 439 StringBuffer sb = new StringBuffer (); 440 for (int i=0;i<slashCount ;i++ ) 441 { 442 sb.append ("../"); 443 } 444 445 if (sb.toString().length() > 0) 446 { 447 return StringUtils.chop(sb.toString(), 1); 448 } 449 else 450 { 451 return "."; 452 } 453 } 454 455 458 private void ensureDirectoryFor( File targetFile ) throws BuildException 459 { 460 File directory = new File ( targetFile.getParent() ); 461 if (!directory.exists()) 462 { 463 if (!directory.mkdirs()) 464 { 465 throw new BuildException("Unable to create directory: " 466 + directory.getAbsolutePath() ); 467 } 468 } 469 } 470 } 471 | Popular Tags |