1 17 18 package org.apache.tools.ant.taskdefs.optional.sitraka; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.Vector ; 23 import javax.xml.transform.OutputKeys ; 24 import javax.xml.transform.Result ; 25 import javax.xml.transform.Source ; 26 import javax.xml.transform.Transformer ; 27 import javax.xml.transform.TransformerFactory ; 28 import javax.xml.transform.dom.DOMSource ; 29 import javax.xml.transform.stream.StreamResult ; 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.Project; 32 import org.apache.tools.ant.taskdefs.Execute; 33 import org.apache.tools.ant.taskdefs.LogStreamHandler; 34 import org.apache.tools.ant.types.Commandline; 35 import org.apache.tools.ant.types.EnumeratedAttribute; 36 import org.apache.tools.ant.types.Path; 37 import org.w3c.dom.Document ; 38 39 40 45 public class CovReport extends CovBase { 46 95 private String format = null; 96 97 98 private File tofile = null; 99 100 101 private String type = null; 102 103 104 private Integer percent = null; 105 106 107 private String filters = null; 108 109 110 private File snapshot = null; 111 112 113 private Path sourcePath = null; 114 115 116 private boolean includeSource = true; 117 118 private Path coveragePath = null; 119 120 121 private Reference reference = null; 122 123 124 public static class ReportFormat extends EnumeratedAttribute { 125 public String [] getValues() { 126 return new String []{"html", "text", "xml"}; 127 } 128 } 129 130 133 public void setFormat(ReportFormat value) { 134 this.format = value.getValue(); 135 } 136 137 public static class ReportType extends EnumeratedAttribute { 138 public String [] getValues() { 139 return new String []{"executive", "summary", "detailed", "verydetailed"}; 140 } 141 } 142 143 147 public void setType(ReportType value) { 148 this.type = value.getValue(); 149 } 150 151 155 public void setIncludesource(boolean value) { 156 this.includeSource = value; 157 } 158 159 163 public void setPercent(Integer value) { 164 this.percent = value; 165 } 166 167 171 public void setFilters(String values) { 172 this.filters = values; 173 } 174 175 178 public Path createSourcepath() { 179 if (sourcePath == null) { 180 sourcePath = new Path(getProject()); 181 } 182 return sourcePath.createPath(); 183 } 184 185 188 public void setSnapshot(File value) { 189 this.snapshot = value; 190 } 191 192 195 public void setTofile(File value) { 196 this.tofile = value; 197 } 198 199 203 public Path createCoveragepath() { 204 if (coveragePath == null) { 205 coveragePath = new Path(getProject()); 206 } 207 return coveragePath.createPath(); 208 } 209 210 214 public Reference createReference() { 215 if (reference == null) { 216 reference = new Reference(); 217 } 218 return reference; 219 } 220 221 222 public CovReport() { 223 } 224 225 226 protected void checkOptions() throws BuildException { 227 if (tofile == null) { 228 throw new BuildException("'tofile' attribute must be set."); 229 } 230 if (snapshot == null) { 231 throw new BuildException("'snapshot' attribute must be set."); 232 } 233 if (getHome() == null) { 234 throw new BuildException("'home' attribute must be set to JProbe home directory"); 235 } 236 File jar = findCoverageJar(); 237 if (!jar.exists()) { 238 throw new BuildException("Cannot find Coverage directory: " + getHome()); 239 } 240 if (reference != null && !"xml".equals(format)) { 241 log("Ignored reference. It cannot be used in non XML report."); 242 reference = null; } 244 245 } 246 247 public void execute() throws BuildException { 248 checkOptions(); 249 try { 250 Commandline cmdl = new Commandline(); 251 cmdl.setExecutable(findExecutable("jpcovreport")); 253 String [] params = getParameters(); 254 for (int i = 0; i < params.length; i++) { 255 cmdl.createArgument().setValue(params[i]); 256 } 257 258 LogStreamHandler handler 260 = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN); 261 Execute exec = new Execute(handler); 262 log(cmdl.describeCommand(), Project.MSG_VERBOSE); 263 exec.setCommandline(cmdl.getCommandline()); 264 int exitValue = exec.execute(); 265 if (Execute.isFailure(exitValue)) { 266 throw new BuildException("JProbe Coverage Report failed (" 267 + exitValue + ")"); 268 } 269 log("coveragePath: " + coveragePath, Project.MSG_VERBOSE); 270 log("format: " + format, Project.MSG_VERBOSE); 271 if (reference != null && "xml".equals(format)) { 272 reference.createEnhancedXMLReport(); 273 } 274 275 } catch (IOException e) { 276 throw new BuildException("Failed to execute JProbe Coverage Report.", e); 277 } 278 } 279 280 281 protected String [] getParameters() { 282 Vector v = new Vector (); 283 if (format != null) { 284 v.addElement("-format=" + format); 285 } 286 if (type != null) { 287 v.addElement("-type=" + type); 288 } 289 if (percent != null) { 290 v.addElement("-percent=" + percent); 291 } 292 if (filters != null) { 293 v.addElement("-filters=" + filters); 294 } 295 v.addElement("-output=" + getProject().resolveFile(tofile.getPath())); 296 v.addElement("-snapshot=" + getProject().resolveFile(snapshot.getPath())); 297 if (sourcePath == null) { 299 sourcePath = new Path(getProject()); 300 sourcePath.createPath().setLocation(getProject().resolveFile(".")); 301 } 302 v.addElement("-sourcepath=" + sourcePath); 303 304 if ("verydetailed".equalsIgnoreCase(format) && "xml".equalsIgnoreCase(type)) { 305 v.addElement("-inc_src_text=" + (includeSource ? "on" : "off")); 306 } 307 308 String [] params = new String [v.size()]; 309 v.copyInto(params); 310 return params; 311 } 312 313 314 public class Reference { 315 protected Path classPath; 316 protected ReportFilters filters; 317 318 public Path createClasspath() { 319 if (classPath == null) { 320 classPath = new Path(CovReport.this.getProject()); 321 } 322 return classPath.createPath(); 323 } 324 325 public ReportFilters createFilters() { 326 if (filters == null) { 327 filters = new ReportFilters(); 328 } 329 return filters; 330 } 331 332 protected void createEnhancedXMLReport() throws BuildException { 333 if (classPath == null) { 335 throw new BuildException("Need a 'classpath' element."); 336 } 337 String [] paths = classPath.list(); 339 if (paths.length == 0) { 340 throw new BuildException("Coverage path is invalid. It does not contain any existing path."); 341 } 342 if (filters == null || filters.size() == 0) { 344 createFilters(); 345 log("Adding default include filter to *.*()", Project.MSG_VERBOSE); 346 ReportFilters.Include include = new ReportFilters.Include(); 347 filters.addInclude(include); 348 } 349 try { 350 log("Creating enhanced XML report", Project.MSG_VERBOSE); 351 XMLReport report = new XMLReport(CovReport.this, tofile); 352 report.setReportFilters(filters); 353 report.setJProbehome(new File (getHome().getParent())); 354 Document doc = report.createDocument(paths); 355 TransformerFactory tfactory = TransformerFactory.newInstance(); 356 Transformer transformer = tfactory.newTransformer(); 357 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 358 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 359 Source src = new DOMSource (doc); 360 Result res = new StreamResult ("file:///" + tofile.toString()); 361 transformer.transform(src, res); 362 } catch (Exception e) { 363 throw new BuildException("Error while performing enhanced XML " 364 + "report from file " + tofile, e); 365 } 366 } 367 } 368 } 369 | Popular Tags |