KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > ant > ReportTask


1 /*
2  * Copyright (c) 2003-2006, Franz-Josef Elmer, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */

25 package classycle.ant;
26
27 import java.io.File JavaDoc;
28 import java.io.FileWriter JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.util.HashSet JavaDoc;
31
32 import org.apache.tools.ant.BuildException;
33
34 import classycle.Analyser;
35
36 /**
37  * Ant Task for creating a Classycle Analyser report, either raw, CSV, or XML.
38  * <p>
39  * <table border="1" cellpadding="5" cellspacing="0">
40  * <tr><th>Attribute</th><th>Description</th><th>Required</th></tr>
41  * <tr><td valign="top">reportFile</td>
42  * <td valign="top">Path of the report file relative to the base
43  * directory.</td>
44  * <td valign="top">Yes</td>
45  * <tr><td valign="top">reportType</td>
46  * <td valign="top">Type of the report. Has to be either <tt>raw</tt>,
47  * <tt>csv</tt>, or <tt>xml</tt>.</td>
48  * <td valign="top">No. Default is <tt>xml</tt></td>
49  * </tr>
50  * <tr><td valign="top">title</td>
51  * <td valign="top">Title of the XML report.</td>
52  * <td valign="top">No. Default is the first file in the file set.</td>
53  * </tr>
54  * <tr><td valign="top">packagesOnly</td>
55  * <td valign="top">If <tt>true</tt> only packages and their dependencies
56  * are analysed and reported (only in XML report).</td>
57  * <td valign="top">No. Default is <tt>false</tt>.</td>
58  * </tr>
59  * <tr><td valign="top">includingClasses</td>
60  * <td>Comma or space separated list of wild-card patterns of
61  * fully-qualified class name which are included in the analysis.
62  * Only '*' are recognized as wild-card character.
63  * </td>
64  * <td valign="top">No. By default all classes defined in the file set
65  * are included.
66  * </td>
67  * </tr>
68  * <tr><td valign="top">excludingClasses</td>
69  * <td valign="top">Comma or space separated list of wild-card patterns of
70  * fully-qualified class name which are excluded from the analysis.
71  * Only '*' are recognized as wild-card character.
72  * </td>
73  * <td valign="top">No. By default no class defined in the file set is
74  * excluded.
75  * </td>
76  * </tr>
77  * <tr><td valign="top">mergeInnerClasses</td>
78  * <td valign="top">If <code>true</code> all class vertices are merged
79  * with the vertices of the corresponding inner classes.
80  * </td>
81  * <td valign="top">No. Default is <tt>false</tt>.</td>
82  * </tr>
83  * <tr><td valign="top">reflectionPattern</td>
84  * <td valign="top">Comma or space separated list of wild-card patterns of
85  * fully-qualified class name.
86  * Only '*' are recognized as wild-card character.
87  * <p>
88  * If in the code of a class an ordinary string constant matches
89  * one of these patterns and if this string constant
90  * has a valid syntax for a fully-qualified
91  * class name this constant will be treated as a class reference.
92  * </td>
93  * <td valign="top">No. By default ordinary string constants are not
94  * treated as class references.
95  * </td>
96  * </tr>
97  * </table>
98  *
99  * @author Boris Gruschko
100  * @author Franz-Josef Elmer
101  */

102 public class ReportTask extends ClassycleTask
103 {
104   public static final String JavaDoc TYPE_RAW = "raw",
105                              TYPE_CSV = "csv",
106                              TYPE_XML = "xml";
107   private static final HashSet JavaDoc TYPES = new HashSet JavaDoc();
108   
109   static
110   {
111     TYPES.add(TYPE_RAW);
112     TYPES.add(TYPE_CSV);
113     TYPES.add(TYPE_XML);
114   }
115   
116   private boolean _packagesOnly;
117   private String JavaDoc _reportFile;
118   private String JavaDoc _reportType = TYPE_XML;
119   private String JavaDoc _title;
120   
121   public void setPackagesOnly(boolean packagesOnly)
122   {
123     _packagesOnly = packagesOnly;
124   }
125
126   public void setReportFile(String JavaDoc xmlFile)
127   {
128     _reportFile = xmlFile;
129   }
130
131   public void setReportType(String JavaDoc csvFile)
132   {
133     _reportType = csvFile;
134   }
135
136   public void setTitle(String JavaDoc title)
137   {
138     _title = title;
139   }
140   
141   public void execute() throws BuildException
142   {
143     super.execute();
144
145     if (!TYPES.contains(_reportType))
146     {
147       throw new BuildException("invalid attribute 'reportType': "
148                                + _reportType);
149     }
150     if (_reportFile == null)
151     {
152       throw new BuildException("missing attribute 'reportFile'.");
153     }
154     
155     String JavaDoc[] classFiles = getClassFileNames();
156     if (classFiles.length > 0 && _title == null)
157     {
158       _title = classFiles[0];
159     }
160     Analyser analyser = new Analyser(classFiles, getPattern(),
161                                      getReflectionPattern(),
162                                      isMergeInnerClasses());
163     try
164     {
165       analyser.readAndAnalyse(_packagesOnly);
166       File JavaDoc f = new File JavaDoc(getOwningTarget().getProject().getBaseDir(),
167                         _reportFile);
168       PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(f));
169       if (_reportType.equals(TYPE_XML))
170       {
171         analyser.printXML(_title, _packagesOnly, writer);
172       } else if (_reportType.equals(TYPE_CSV))
173       {
174         analyser.printCSV(writer);
175       } else if (_reportType.equals(TYPE_RAW))
176       {
177         analyser.printRaw(writer);
178       }
179     } catch (Exception JavaDoc e)
180     {
181       throw new BuildException(e);
182     }
183   }
184 }
Popular Tags