KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > ant > MackerAntTask


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2002-2003 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

20  
21 package net.innig.macker.ant;
22
23 import java.util.*;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import net.innig.macker.Macker;
28 import net.innig.macker.event.MackerIsMadException;
29 import net.innig.macker.event.ListenerException;
30 import net.innig.macker.structure.ClassParseException;
31 import net.innig.macker.structure.IncompleteClassInfoException;
32 import net.innig.macker.rule.RulesException;
33 import net.innig.macker.rule.RuleSeverity;
34
35 import org.apache.tools.ant.Task;
36 import org.apache.tools.ant.BuildException;
37 import org.apache.tools.ant.DirectoryScanner;
38 import org.apache.tools.ant.AntClassLoader;
39 import org.apache.tools.ant.types.FileSet;
40 import org.apache.tools.ant.types.Path;
41 import org.apache.tools.ant.taskdefs.Java;
42
43 /**
44     A task which allows access to Macker from Ant build files.
45     
46     @see <a HREF="http://ant.apache.org/manual/">The Ant manual</a>
47 */

48 public class MackerAntTask extends Task
49     {
50     public MackerAntTask()
51         {
52         macker = new Macker();
53         jvmArgs = new ArrayList();
54         }
55         
56     public void execute()
57         throws BuildException
58         {
59         if(verbose)
60             System.out.println("Macker (verbose mode enabled)");
61         if(failOnError && angerProperty != null)
62             System.out.println("WARNING: failOnError is set, so angerProperty will have no effect");
63
64         try
65             {
66             if(!fork)
67                 {
68                 if(classPath != null)
69                     macker.setClassLoader(new AntClassLoader(getProject(), classPath, false));
70                 
71                 macker.check();
72                 }
73             else
74                 {
75                 if(classPath == null)
76                     throw new BuildException("nested <classpath> element is required when fork=true");
77                 
78                 getJvm().setTaskName("macker");
79                 getJvm().setClassname("net.innig.macker.Macker");
80                 getJvm().setFork(fork);
81                 getJvm().setFailonerror(false);
82                 getJvm().clearArgs();
83                 
84                 for(Iterator i = jvmArgs.iterator(); i.hasNext(); )
85                     getJvm().createArg().setValue((String JavaDoc) i.next());
86                     
87                 int resultCode = getJvm().executeJava();
88                 if(resultCode == 2)
89                     throw new MackerIsMadException();
90                 if(resultCode != 0)
91                     throw new BuildException(MACKER_CHOKED_MESSAGE);
92                 }
93             }
94         catch(MackerIsMadException mime)
95             {
96             if(mime.getMessage() != null)
97                 {
98                 System.out.println();
99                 System.out.println(mime.getMessage());
100                 }
101             if(angerProperty != null)
102                 getProject().setProperty(angerProperty, "true");
103             if(failOnError)
104                 throw new BuildException(MACKER_IS_MAD_MESSAGE);
105             }
106         catch(ListenerException lie)
107             {
108             System.out.println();
109             System.out.println(lie.getMessage());
110             throw new BuildException(MACKER_CHOKED_MESSAGE);
111             }
112         catch(RulesException rue)
113             {
114             System.out.println();
115             System.out.println(rue.getMessage());
116             throw new BuildException(MACKER_CHOKED_MESSAGE);
117             }
118         catch(IncompleteClassInfoException icie)
119             {
120             System.out.println();
121             System.out.println(icie.getMessage());
122             throw new BuildException(MACKER_CHOKED_MESSAGE);
123             }
124         }
125
126     public void setFork(boolean fork)
127         { this.fork = fork; }
128
129     public void setFailOnError(boolean failOnError)
130         { this.failOnError = failOnError; }
131     
132     public void setMaxMessages(int printMaxMessages)
133         {
134         macker.setPrintMaxMessages(printMaxMessages);
135         jvmArgs.add("--print-max");
136         jvmArgs.add(String.valueOf(printMaxMessages));
137         }
138
139     public void setPrintThreshold(String JavaDoc threshold)
140         {
141         macker.setPrintThreshold(RuleSeverity.fromName(threshold));
142         jvmArgs.add("--print");
143         jvmArgs.add(threshold);
144         }
145
146     public void setAngerThreshold(String JavaDoc threshold)
147         {
148         macker.setAngerThreshold(RuleSeverity.fromName(threshold));
149         jvmArgs.add("--anger");
150         jvmArgs.add(threshold);
151         }
152
153     public void setAngerProperty(String JavaDoc property)
154         { this.angerProperty = property; }
155
156     public void setVerbose(boolean verbose)
157         {
158         this.verbose = verbose;
159         macker.setVerbose(verbose);
160         if(verbose)
161             jvmArgs.add("-v");
162         }
163
164     public Path createClasspath()
165         { return classPath = getJvm().createClasspath(); }
166
167     public void addConfiguredVar(Var var)
168         {
169         macker.setVariable(var.getName(), var.getValue());
170         jvmArgs.add("-D");
171         jvmArgs.add(var.getName() + "=" + var.getValue());
172         }
173
174     public void setXmlReportFile(File JavaDoc xmlReportFile)
175         {
176         macker.setXmlReportFile(xmlReportFile);
177         jvmArgs.add("-o");
178         jvmArgs.add(xmlReportFile.getPath());
179         }
180     
181     static public class Var
182         {
183         public String JavaDoc getName() { return name; }
184         public String JavaDoc getValue() { return value; }
185         public void setName(String JavaDoc name) { this.name = name; }
186         public void setValue(String JavaDoc value) { this.value = value; }
187         private String JavaDoc name, value;
188         }
189
190     public void addConfiguredClasses(FileSet classFiles)
191         throws IOException JavaDoc
192         {
193         DirectoryScanner classScanner = classFiles.getDirectoryScanner(getProject());
194         String JavaDoc[] fileNames = classScanner.getIncludedFiles();
195         File JavaDoc baseDir = classScanner.getBasedir();
196         for(int n = 0; n < fileNames.length; n++)
197             {
198             File JavaDoc classFile = new File JavaDoc(baseDir, fileNames[n]);
199             if(!classFile.getName().endsWith(".class"))
200                 System.out.println("WARNING: " + fileNames[n]
201                     + " is not a .class file; ignoring");
202             jvmArgs.add(classFile.getPath());
203             try
204                 { macker.addClass(classFile); }
205             catch(ClassParseException cpe)
206                 {
207                 System.out.println("Unable to parse class file: " + classFile.getPath());
208                 System.out.println(cpe.getMessage());
209                 throw new BuildException(MACKER_CHOKED_MESSAGE);
210                 }
211             }
212         }
213
214     public void addConfiguredRules(FileSet rulesFiles)
215         throws IOException JavaDoc
216         {
217         DirectoryScanner rulesScanner = rulesFiles.getDirectoryScanner(getProject());
218         String JavaDoc[] fileNames = rulesScanner.getIncludedFiles();
219         File JavaDoc baseDir = rulesScanner.getBasedir();
220         for(int n = 0; n < fileNames.length; n++)
221             {
222             File JavaDoc rulesFile = new File JavaDoc(baseDir, fileNames[n]);
223             jvmArgs.add("-r");
224             jvmArgs.add(rulesFile.getPath());
225             try
226                 { macker.addRulesFile(rulesFile); }
227             catch(RulesException re)
228                 {
229                 System.out.println(re.getMessage());
230                 throw new BuildException(MACKER_CHOKED_MESSAGE);
231                 }
232             }
233         }
234     
235     private Java getJvm()
236         {
237         if(jvm == null)
238             {
239             jvm = new Java();
240             jvm.setProject(getProject());
241             }
242         return jvm;
243         }
244
245     private boolean fork = false;
246     private boolean failOnError = true;
247     private boolean verbose = false;
248     private List/*<String>*/ jvmArgs;
249     private Macker macker; // for non-forked
250
private Java jvm; // for forked
251
private Path classPath;
252     private String JavaDoc angerProperty;
253     
254     private static final String JavaDoc MACKER_CHOKED_MESSAGE = "Macker configuration failed";
255     private static final String JavaDoc MACKER_IS_MAD_MESSAGE = "Macker rules checking failed";
256     }
257
Popular Tags