1 20 21 package net.innig.macker.ant; 22 23 import java.util.*; 24 import java.io.File ; 25 import java.io.IOException ; 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 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 ) 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 threshold) 140 { 141 macker.setPrintThreshold(RuleSeverity.fromName(threshold)); 142 jvmArgs.add("--print"); 143 jvmArgs.add(threshold); 144 } 145 146 public void setAngerThreshold(String threshold) 147 { 148 macker.setAngerThreshold(RuleSeverity.fromName(threshold)); 149 jvmArgs.add("--anger"); 150 jvmArgs.add(threshold); 151 } 152 153 public void setAngerProperty(String 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 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 getName() { return name; } 184 public String getValue() { return value; } 185 public void setName(String name) { this.name = name; } 186 public void setValue(String value) { this.value = value; } 187 private String name, value; 188 } 189 190 public void addConfiguredClasses(FileSet classFiles) 191 throws IOException 192 { 193 DirectoryScanner classScanner = classFiles.getDirectoryScanner(getProject()); 194 String [] fileNames = classScanner.getIncludedFiles(); 195 File baseDir = classScanner.getBasedir(); 196 for(int n = 0; n < fileNames.length; n++) 197 { 198 File classFile = new File (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 216 { 217 DirectoryScanner rulesScanner = rulesFiles.getDirectoryScanner(getProject()); 218 String [] fileNames = rulesScanner.getIncludedFiles(); 219 File baseDir = rulesScanner.getBasedir(); 220 for(int n = 0; n < fileNames.length; n++) 221 { 222 File rulesFile = new File (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 jvmArgs; 249 private Macker macker; private Java jvm; private Path classPath; 252 private String angerProperty; 253 254 private static final String MACKER_CHOKED_MESSAGE = "Macker configuration failed"; 255 private static final String MACKER_IS_MAD_MESSAGE = "Macker rules checking failed"; 256 } 257 | Popular Tags |