KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > metamata > MAudit


1 /*
2  * Copyright 2001-2002,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.tools.ant.taskdefs.optional.metamata;
18
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
27 import org.apache.tools.ant.taskdefs.LogStreamHandler;
28 import org.apache.tools.ant.types.FileSet;
29 import org.apache.tools.ant.types.Path;
30
31 /**
32  * Invokes the Metamata Audit/ Webgain Quality Analyzer on a set of Java files.
33  * <p>
34  * <i>maudit</i> performs static analysis of the Java source code and byte
35  * code files to find and report errors of style and potential problems related
36  * to performance, maintenance and robustness. As a convenience, a stylesheet
37  * is given in <tt>etc</tt> directory, so that an HTML report can be generated
38  * from the XML file.
39  *
40  */

41 public class MAudit extends AbstractMetamataTask {
42
43     /* As of Metamata 2.0, the command line of MAudit is as follows:
44     Usage
45         maudit <option>... <path>... [-unused <search-path>...]
46
47     Parameters
48         path File or directory to audit.
49         search-path File or directory to search for declaration uses.
50
51     Options
52         -arguments -A <file> Includes command line arguments from file.
53         -classpath -cp <path> Sets class path (also source path unless one
54                                       explicitly set). Overrides METAPATH/CLASSPATH.
55         -exit -x Exits after the first error.
56         -fix -f Automatically fixes certain errors.
57         -fullpath Prints full path for locations.
58         -help -h Prints help and exits.
59         -list -l Creates listing file for each audited file.
60         -offsets -off Offset and length for locations.
61         -output -o <file> Prints output to file.
62         -quiet -q Suppresses copyright and summary messages.
63         -sourcepath <path> Sets source path. Overrides SOURCEPATH.
64         -tab -t Prints a tab character after first argument.
65         -unused -u Finds declarations unused in search paths.
66         -verbose -v Prints all messages.
67         -version -V Prints version and exits.
68     */

69
70     //---------------------- PUBLIC METHODS ------------------------------------
71

72     /** pattern used by maudit to report the error for a file */
73     /** RE does not seems to support regexp pattern with comments so i'm stripping it*/
74     // (?:file:)?((?#filepath).+):((?#line)\\d+)\\s*:\\s+((?#message).*)
75
static final String JavaDoc AUDIT_PATTERN = "(?:file:)?(.+):(\\d+)\\s*:\\s+(.*)";
76
77     private File JavaDoc outFile = null;
78
79     private Path searchPath = null;
80
81     private Path rulesPath = null;
82
83     private boolean fix = false;
84
85     private boolean list = false;
86
87     private boolean unused = false;
88
89 // add a bunch of undocumented options for the task
90
private boolean quiet = false;
91     private boolean exit = false;
92     private boolean offsets = false;
93     private boolean verbose = false;
94     private boolean fullsemanticize = false;
95
96     /** default constructor */
97     public MAudit() {
98         super("com.metamata.gui.rc.MAudit");
99     }
100
101     /**
102      * The XML file to which the Audit result should be written to; required
103      */

104
105     public void setTofile(File JavaDoc outFile) {
106         this.outFile = outFile;
107     }
108
109     /**
110      * Automatically fix certain errors
111      * (those marked as fixable in the manual);
112      * optional, default=false
113      */

114     public void setFix(boolean flag) {
115         this.fix = flag;
116     }
117
118     /**
119      * Creates listing file for each audited file; optional, default false.
120      * When set, a .maudit file will be generated in the
121      * same location as the source file.
122      */

123     public void setList(boolean flag) {
124         this.list = flag;
125     }
126
127     /**
128      * Finds declarations unused in search paths; optional, default false.
129      * It will look for unused global declarations
130      * in the source code within a use domain specified by the
131      * <tt>searchpath</tt> element.
132      */

133     public void setUnused(boolean flag) {
134         this.unused = flag;
135     }
136
137     /**
138      * flag to suppress copyright and summary messages; default false.
139      * internal/testing only
140      * @ant.attribute ignore="true"
141      */

142     public void setQuiet(boolean flag) {
143         this.quiet = flag;
144     }
145
146     /**
147      * flag to tell the task to exit after the first error.
148      * internal/testing only
149      * @ant.attribute ignore="true"
150      */

151     public void setExit(boolean flag) {
152         this.exit = flag;
153     }
154
155     /**
156      * internal/testing only
157      * @ant.attribute ignore="true"
158      */

159     public void setOffsets(boolean flag) {
160         this.offsets = flag;
161     }
162
163     /**
164      * flag to print all messages; optional, default false.
165      * internal/testing only
166      * @ant.attribute ignore="true"
167      */

168     public void setVerbose(boolean flag) {
169         this.verbose = flag;
170     }
171
172     /**
173      * internal/testing only
174      * @ant.attribute ignore="true"
175      */

176     public void setFullsemanticize(boolean flag) {
177         this.fullsemanticize = flag;
178     }
179
180     /**
181      * classpath for additional audit rules
182      * these must be placed before metamata.jar !!
183      */

184     public Path createRulespath() {
185         if (rulesPath == null) {
186             rulesPath = new Path(getProject());
187         }
188         return rulesPath;
189     }
190
191     /**
192      * search path to use for unused global declarations;
193      * required when <tt>unused</tt> is set.
194      */

195     public Path createSearchpath() {
196         if (searchPath == null) {
197             searchPath = new Path(getProject());
198         }
199         return searchPath;
200     }
201
202     /**
203      * create the option vector for the command
204      */

205     protected Vector JavaDoc getOptions() {
206         Vector JavaDoc options = new Vector JavaDoc(512);
207         // add the source path automatically from the fileset.
208
// to avoid redundancy...
209
for (int i = 0; i < fileSets.size(); i++) {
210             FileSet fs = (FileSet) fileSets.elementAt(i);
211             Path path = createSourcepath();
212             File JavaDoc dir = fs.getDir(getProject());
213             path.setLocation(dir);
214         }
215
216         // there is a bug in Metamata 2.0 build 37. The sourcepath argument does
217
// not work. So we will use the sourcepath prepended to classpath. (order
218
// is important since Metamata looks at .class and .java)
219
if (sourcePath != null) {
220             sourcePath.append(classPath); // srcpath is prepended
221
classPath = sourcePath;
222             sourcePath = null; // prevent from using -sourcepath
223
}
224
225         // don't forget to modify the pattern if you change the options reporting
226
if (classPath != null) {
227             options.addElement("-classpath");
228             options.addElement(classPath.toString());
229         }
230         // suppress copyright msg when running, we will let it so that this
231
// will be the only output to the console if in xml mode
232
if (quiet) {
233             options.addElement("-quiet");
234         }
235         if (fullsemanticize) {
236             options.addElement("-full-semanticize");
237         }
238         if (verbose) {
239             options.addElement("-verbose");
240         }
241         if (offsets) {
242             options.addElement("-offsets");
243         }
244         if (exit) {
245             options.addElement("-exit");
246         }
247         if (fix) {
248             options.addElement("-fix");
249         }
250         options.addElement("-fullpath");
251
252         // generate .maudit files much more detailed than the report
253
// I don't like it very much, I think it could be interesting
254
// to get all .maudit files and include them in the XML.
255
if (list) {
256             options.addElement("-list");
257         }
258         if (sourcePath != null) {
259             options.addElement("-sourcepath");
260             options.addElement(sourcePath.toString());
261         }
262         addAllVector(options, includedFiles.keys());
263         if (unused) {
264             options.addElement("-unused");
265             options.addElement(searchPath.toString());
266         }
267         return options;
268     }
269
270     /**
271      * validate the settings
272      */

273     protected void checkOptions() throws BuildException {
274         super.checkOptions();
275         if (unused && searchPath == null) {
276             throw new BuildException("'searchpath' element must be set when "
277                 + "looking for 'unused' declarations.");
278         }
279         if (!unused && searchPath != null) {
280             log("'searchpath' element ignored. 'unused' attribute is disabled.",
281                 Project.MSG_WARN);
282         }
283         if (rulesPath != null) {
284             cmdl.createClasspath(getProject()).addExisting(rulesPath);
285         }
286     }
287
288     protected ExecuteStreamHandler createStreamHandler() throws BuildException {
289         // if we didn't specify a file, then use a screen report
290
if (outFile == null) {
291             return new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_ERR);
292         }
293         ExecuteStreamHandler handler = null;
294         OutputStream JavaDoc out = null;
295         try {
296             out = new FileOutputStream JavaDoc(outFile);
297             handler = new MAuditStreamHandler(this, out);
298         } catch (IOException JavaDoc e) {
299             throw new BuildException(e);
300         } finally {
301             if (out == null) {
302                 try {
303                     out.close();
304                 } catch (IOException JavaDoc e) {
305                 }
306             }
307         }
308         return handler;
309     }
310
311     protected void cleanUp() throws BuildException {
312         super.cleanUp();
313         // at this point if -list is used, we should move
314
// the .maudit file since we cannot choose their location :(
315
// the .maudit files match the .java files
316
// we'll use includedFiles to get the .maudit files.
317

318         /*if (out != null) {
319             // close it if not closed by the handler...
320         }*/

321     }
322
323 }
324
325
Popular Tags