KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > ant > JRAntCompileTask


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28
29 /*
30  * Contributors:
31  * Henri Chen - henrichen@users.sourceforge.net
32  * Kees Kuip - keeskuip@users.sourceforge.net
33  */

34 package net.sf.jasperreports.ant;
35
36 import java.io.File JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Map JavaDoc;
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 /**
55  * Ant task for batch-compiling XML report design files.
56  * Works like the built-in <code>javac</code> Ant task.
57  * <p>
58  * This task can take the following arguments:
59  * <ul>
60  * <li>src
61  * <li>destdir
62  * <li>compiler
63  * <li>classpath
64  * <li>tempdir
65  * <li>keepjava
66  * <li>xmlvalidation
67  * </ul>
68  * Of these arguments, the <code>src</code> and <code>destdir</code> are required.
69  * When this task executes, it will recursively scan the <code>src</code> and
70  * <code>destdir</code> looking for XML report design files to compile.
71  * This task makes its compile decision based on timestamp and only XML files
72  * that have no corresponding .jasper file or where the compiled report design file
73  * is older than the XML file will be compiled.
74  *
75  * @author Teodor Danciu (teodord@users.sourceforge.net)
76  * @version $Id: JRAntCompileTask.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
77  */

78 public class JRAntCompileTask extends MatchingTask
79 {
80
81
82     /**
83      *
84      */

85     private Path src = null;
86     private File JavaDoc destdir = null;
87     private File JavaDoc tempdir = null;
88     private boolean keepjava = false;
89     private String JavaDoc compiler = null;
90     private Path classpath = null;
91     private boolean xmlvalidation = true;
92
93     private Map JavaDoc reportFilesMap = null;
94
95
96     /**
97      * Sets the source directories to find the XML report design files.
98      *
99      * @param srcdir source path
100      */

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     /**
115      * Adds a path for source compilation.
116      *
117      * @return source path
118      */

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     /**
131      * Sets the destination directory into which the XML report design files should be compiled.
132      *
133      * @param destdir destination directory
134      */

135     public void setDestdir(File JavaDoc destdir)
136     {
137         this.destdir = destdir;
138     }
139
140
141     /**
142      * Sets the temporary working directory into which to store the temporary files
143      * generated during XML report design file compilation. This is only used by the
144      * Java bytecode report compilers that need to have the Java source files stored
145      * on disk in order to compile them.
146      * <p>
147      * If not set, the temporary working directory will be the current working directory,
148      * as specified by the <code>user.dir</code> system property.
149      *
150      * @param tempdir temporary working directory
151      */

152     public void setTempdir(File JavaDoc tempdir)
153     {
154         this.tempdir = tempdir;
155     }
156
157
158     /**
159      * Sets a boolean flag that will instruct the Java bytecode report compilers
160      * to avoid deletion of the Java source files generated in the temporary working
161      * directory during report generation. This is useful when debugging.
162      *
163      * @param keepjava flag for preventing the deletion of generated Java source files
164      */

165     public void setKeepjava(boolean keepjava)
166     {
167         this.keepjava = keepjava;
168     }
169
170
171     /**
172      * Sets the name of the report compiler class to use when compiling the XML
173      * report design files.
174      * <p>
175      * The specified class should be an implementation of the
176      * {@link net.sf.jasperreports.engine.design.JRCompiler} interface.
177      * When specified, this value will temporarily override the value of the
178      * <code>jasper.reports.compiler.class</code> system property which in turn
179      * is used by the {@link net.sf.jasperreports.engine.design.JRDefaultCompiler}.
180      *
181      * @param compiler report compiler class name
182      */

183     public void setCompiler(String JavaDoc compiler)
184     {
185         this.compiler = compiler;
186     }
187
188
189     /**
190      * Adds a path to the classpath.
191      *
192      * @return classpath to use when compiling the report associated Java expressions class
193      */

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     /**
206      * Instructs the XML parser to validate the XML report design file during compilation.
207      *
208      * @param xmlvalidation flag for enabling/disabling the validation feature of the XML parser
209      */

210     public void setXmlvalidation(boolean xmlvalidation)
211     {
212         this.xmlvalidation = xmlvalidation;
213     }
214
215
216     /**
217      * Executes the task.
218      */

219     public void execute() throws BuildException
220     {
221         checkParameters();
222
223         reportFilesMap = new HashMap JavaDoc();
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     /**
262      * Checks that all required attributes have been set and that the supplied values are valid.
263      */

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     /**
302      * Scans the source directories looking for source files to be compiled.
303      */

304     protected void scanSrc() throws BuildException
305     {
306         String JavaDoc[] list = src.list();
307         for (int i = 0; i < list.length; i++)
308         {
309             File JavaDoc 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 JavaDoc[] files = ds.getIncludedFiles();
325                 
326                 scanDir(srcdir, destdir != null ? destdir : srcdir, files);
327             }
328             else
329             {
330                 String JavaDoc[] files = new String JavaDoc[]{srcdir.getName()};
331                 
332                 scanDir(srcdir.getParentFile(), destdir != null ? destdir : srcdir.getParentFile(), files);
333             }
334         }
335     }
336     
337     
338     /**
339      * Scans the directory looking for source files to be compiled.
340      * The results are returned in the instance variable <code>reportFilesMap</code>.
341      *
342      * @param srcdir source directory
343      * @param destdir destination directory
344      * @param files included file names
345      */

346     protected void scanDir(File JavaDoc srcdir, File JavaDoc destdir, String JavaDoc[] files)
347     {
348         RegexpPatternMapper mapper = new RegexpPatternMapper();
349         mapper.setFrom("^(.*)\\.(.*)$");
350         mapper.setTo("\\1.jasper");
351
352         SourceFileScanner scanner = new SourceFileScanner(this);
353         String JavaDoc[] 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 JavaDoc(srcdir, newFiles[i])).getAbsolutePath(),
361                     (new File JavaDoc(destdir, mapper.mapFileName(newFiles[i])[0])).getAbsolutePath()
362                     );
363             }
364         }
365     }
366     
367     
368     /**
369      * Performs the compilation of the selected report design files.
370      */

371     protected void compile() throws BuildException
372     {
373         Collection JavaDoc 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 JavaDoc srcFileName = null;
382             String JavaDoc destFileName = null;
383             File JavaDoc destFileParent = null;
384
385             for (Iterator JavaDoc it = files.iterator(); it.hasNext();)
386             {
387                 srcFileName = (String JavaDoc)it.next();
388                 destFileName = (String JavaDoc)reportFilesMap.get(srcFileName);
389                 destFileParent = new File JavaDoc(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