1 28 29 34 package net.sf.jasperreports.ant; 35 36 import java.io.File ; 37 import java.util.Collection ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 import java.util.Map ; 41 42 import net.sf.jasperreports.engine.JRException; 43 import net.sf.jasperreports.engine.JasperCompileManager; 44 import net.sf.jasperreports.engine.util.JRProperties; 45 46 import org.apache.tools.ant.BuildException; 47 import org.apache.tools.ant.DirectoryScanner; 48 import org.apache.tools.ant.taskdefs.MatchingTask; 49 import org.apache.tools.ant.types.Path; 50 import org.apache.tools.ant.util.RegexpPatternMapper; 51 import org.apache.tools.ant.util.SourceFileScanner; 52 53 54 78 public class JRAntCompileTask extends MatchingTask 79 { 80 81 82 85 private Path src = null; 86 private File destdir = null; 87 private File tempdir = null; 88 private boolean keepjava = false; 89 private String compiler = null; 90 private Path classpath = null; 91 private boolean xmlvalidation = true; 92 93 private Map reportFilesMap = null; 94 95 96 101 public void setSrcdir(Path srcdir) 102 { 103 if (src == null) 104 { 105 src = srcdir; 106 } 107 else 108 { 109 src.append(srcdir); 110 } 111 } 112 113 114 119 public Path createSrc() 120 { 121 if (src == null) 122 { 123 src = new Path(getProject()); 124 } 125 126 return src.createPath(); 127 } 128 129 130 135 public void setDestdir(File destdir) 136 { 137 this.destdir = destdir; 138 } 139 140 141 152 public void setTempdir(File tempdir) 153 { 154 this.tempdir = tempdir; 155 } 156 157 158 165 public void setKeepjava(boolean keepjava) 166 { 167 this.keepjava = keepjava; 168 } 169 170 171 183 public void setCompiler(String compiler) 184 { 185 this.compiler = compiler; 186 } 187 188 189 194 public Path createClasspath() 195 { 196 if (classpath == null) 197 { 198 classpath = new Path(getProject()); 199 } 200 201 return classpath.createPath(); 202 } 203 204 205 210 public void setXmlvalidation(boolean xmlvalidation) 211 { 212 this.xmlvalidation = xmlvalidation; 213 } 214 215 216 219 public void execute() throws BuildException 220 { 221 checkParameters(); 222 223 reportFilesMap = new HashMap (); 224 225 JRProperties.backupProperties(); 226 227 try 228 { 229 if (tempdir != null) 230 { 231 JRProperties.setProperty(JRProperties.COMPILER_TEMP_DIR, String.valueOf(tempdir)); 232 } 233 234 JRProperties.setProperty(JRProperties.COMPILER_KEEP_JAVA_FILE, keepjava); 235 236 if (compiler != null) 237 { 238 JRProperties.setProperty(JRProperties.COMPILER_CLASS, compiler); 239 } 240 241 if (classpath != null) 242 { 243 JRProperties.setProperty(JRProperties.COMPILER_CLASSPATH, String.valueOf(classpath)); 244 } 245 246 JRProperties.setProperty(JRProperties.COMPILER_XML_VALIDATION, xmlvalidation); 247 248 249 scanSrc(); 250 251 252 compile(); 253 } 254 finally 255 { 256 JRProperties.restoreProperties(); 257 } 258 } 259 260 261 264 protected void checkParameters() throws BuildException 265 { 266 if (src == null || src.size() == 0) 267 { 268 throw 269 new BuildException( 270 "The srcdir attribute must be set.", 271 location 272 ); 273 } 274 275 if (destdir != null && !destdir.isDirectory()) 276 { 277 throw 278 new BuildException( 279 "The destination directory \"" 280 + destdir 281 + "\" does not exist " 282 + "or is not a directory.", 283 location 284 ); 285 } 286 287 if (tempdir != null && !tempdir.isDirectory()) 288 { 289 throw 290 new BuildException( 291 "The temporary directory \"" 292 + tempdir 293 + "\" does not exist " 294 + "or is not a directory.", 295 location 296 ); 297 } 298 } 299 300 301 304 protected void scanSrc() throws BuildException 305 { 306 String [] list = src.list(); 307 for (int i = 0; i < list.length; i++) 308 { 309 File srcdir = project.resolveFile(list[i]); 310 if (!srcdir.exists()) 311 { 312 throw 313 new BuildException( 314 "The srcdir \"" 315 + srcdir.getPath() 316 + "\" does not exist.", 317 location 318 ); 319 } 320 321 if (srcdir.isDirectory()) 322 { 323 DirectoryScanner ds = getDirectoryScanner(srcdir); 324 String [] files = ds.getIncludedFiles(); 325 326 scanDir(srcdir, destdir != null ? destdir : srcdir, files); 327 } 328 else 329 { 330 String [] files = new String []{srcdir.getName()}; 331 332 scanDir(srcdir.getParentFile(), destdir != null ? destdir : srcdir.getParentFile(), files); 333 } 334 } 335 } 336 337 338 346 protected void scanDir(File srcdir, File destdir, String [] files) 347 { 348 RegexpPatternMapper mapper = new RegexpPatternMapper(); 349 mapper.setFrom("^(.*)\\.(.*)$"); 350 mapper.setTo("\\1.jasper"); 351 352 SourceFileScanner scanner = new SourceFileScanner(this); 353 String [] newFiles = scanner.restrict(files, srcdir, destdir, mapper); 354 355 if (newFiles != null && newFiles.length > 0) 356 { 357 for (int i = 0; i < newFiles.length; i++) 358 { 359 reportFilesMap.put( 360 (new File (srcdir, newFiles[i])).getAbsolutePath(), 361 (new File (destdir, mapper.mapFileName(newFiles[i])[0])).getAbsolutePath() 362 ); 363 } 364 } 365 } 366 367 368 371 protected void compile() throws BuildException 372 { 373 Collection files = reportFilesMap.keySet(); 374 375 if (files != null && files.size() > 0) 376 { 377 boolean isError = false; 378 379 System.out.println("Compiling " + files.size() + " report design files."); 380 381 String srcFileName = null; 382 String destFileName = null; 383 File destFileParent = null; 384 385 for (Iterator it = files.iterator(); it.hasNext();) 386 { 387 srcFileName = (String )it.next(); 388 destFileName = (String )reportFilesMap.get(srcFileName); 389 destFileParent = new File (destFileName).getParentFile(); 390 if(!destFileParent.exists()) 391 { 392 destFileParent.mkdirs(); 393 } 394 395 try 396 { 397 System.out.print("File : " + srcFileName + " ... "); 398 JasperCompileManager.compileReportToFile(srcFileName, destFileName); 399 System.out.println("OK."); 400 } 401 catch(JRException e) 402 { 403 System.out.println("FAILED."); 404 System.out.println("Error compiling report design : " + srcFileName); 405 e.printStackTrace(System.out); 406 isError = true; 407 } 408 } 409 410 if(isError) 411 { 412 throw new BuildException("Errors were encountered when compiling report designs."); 413 } 414 } 415 } 416 417 418 } 419 | Popular Tags |