1 18 package org.apache.tools.ant.taskdefs.optional.junit; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 26 import java.net.URL ; 27 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Vector ; 31 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.Task; 38 import org.apache.tools.ant.taskdefs.XSLTProcess; 39 import org.apache.tools.ant.taskdefs.Delete; 40 import org.apache.tools.ant.taskdefs.TempFile; 41 import org.apache.tools.ant.util.JAXPUtils; 42 import org.apache.tools.ant.util.FileUtils; 43 import org.apache.tools.ant.types.EnumeratedAttribute; 44 import org.apache.tools.ant.types.Resource; 45 import org.apache.tools.ant.types.resources.URLResource; 46 import org.apache.tools.ant.types.resources.FileResource; 47 48 import org.w3c.dom.Document ; 49 50 58 public class AggregateTransformer { 59 62 public static final String FRAMES = "frames"; 63 64 67 public static final String NOFRAMES = "noframes"; 68 69 72 public static class Format extends EnumeratedAttribute { 73 77 public String [] getValues() { 78 return new String []{FRAMES, NOFRAMES}; 79 } 80 } 81 82 84 protected Task task; 85 86 87 protected Document document; 88 89 90 protected File styleDir; 91 92 93 protected File toDir; 94 95 100 private List params; 101 102 107 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 108 109 112 private static int counter = 0; 113 114 115 protected String format = FRAMES; 116 117 118 private static DocumentBuilderFactory privateDBFactory; 119 120 121 protected static DocumentBuilderFactory dbfactory; 122 123 static { 124 privateDBFactory = DocumentBuilderFactory.newInstance(); 125 dbfactory = privateDBFactory; 126 } 127 129 133 public AggregateTransformer(Task task) { 134 this.task = task; 135 params = new Vector (); 136 } 137 138 143 protected static DocumentBuilderFactory getDocumentBuilderFactory() { 144 return privateDBFactory; 145 } 146 147 151 public void setFormat(Format format) { 152 this.format = format.getValue(); 153 } 154 155 159 public void setXmlDocument(Document doc) { 160 this.document = doc; 161 } 162 163 169 protected void setXmlfile(File xmlfile) throws BuildException { 170 try { 171 DocumentBuilder builder = privateDBFactory.newDocumentBuilder(); 172 InputStream in = new FileInputStream (xmlfile); 173 try { 174 Document doc = builder.parse(in); 175 setXmlDocument(doc); 176 } finally { 177 in.close(); 178 } 179 } catch (Exception e) { 180 throw new BuildException("Error while parsing document: " + xmlfile, e); 181 } 182 } 183 184 190 public void setStyledir(File styledir) { 191 this.styleDir = styledir; 192 } 193 194 197 public void setTodir(File todir) { 198 this.toDir = todir; 199 } 200 201 204 public void setExtension(String ext) { 205 task.log("extension is not used anymore", Project.MSG_WARN); 206 } 207 208 214 public XSLTProcess.Param createParam() { 215 XSLTProcess.Param p = new XSLTProcess.Param(); 216 params.add(p); 217 return p; 218 } 219 220 224 public void transform() throws BuildException { 225 checkOptions(); 226 Project project = task.getProject(); 227 228 TempFile tempFileTask = new TempFile(); 229 tempFileTask.bindToOwner(task); 230 231 XSLTProcess xsltTask = new XSLTProcess(); 232 xsltTask.bindToOwner(task); 233 234 xsltTask.setXslResource(getStylesheet()); 235 236 xsltTask.setIn(((XMLResultAggregator) task).getDestinationFile()); 238 File outputFile = null; 239 if (format.equals(FRAMES)) { 240 String tempFileProperty = getClass().getName() + String.valueOf(counter++); 241 File tmp = FILE_UTILS.resolveFile(project.getBaseDir(), 242 project.getProperty("java.io.tmpdir")); 243 tempFileTask.setDestDir(tmp); 244 tempFileTask.setProperty(tempFileProperty); 245 tempFileTask.execute(); 246 outputFile = new File (project.getProperty(tempFileProperty)); 247 } else { 248 outputFile = new File (toDir, "junit-noframes.html"); 249 } 250 xsltTask.setOut(outputFile); 251 for (Iterator i = params.iterator(); i.hasNext();) { 252 XSLTProcess.Param param = (XSLTProcess.Param) i.next(); 253 XSLTProcess.Param newParam = xsltTask.createParam(); 254 newParam.setProject(task.getProject()); 255 newParam.setName(param.getName()); 256 newParam.setExpression(param.getExpression()); 257 } 258 XSLTProcess.Param paramx = xsltTask.createParam(); 259 paramx.setProject(task.getProject()); 260 paramx.setName("output.dir"); 261 paramx.setExpression(toDir.getAbsolutePath()); 262 final long t0 = System.currentTimeMillis(); 263 try { 264 xsltTask.execute(); 265 } catch (Exception e) { 266 throw new BuildException("Errors while applying transformations: " 267 + e.getMessage(), e); 268 } 269 final long dt = System.currentTimeMillis() - t0; 270 task.log("Transform time: " + dt + "ms"); 271 if (format.equals(FRAMES)) { 272 Delete delete = new Delete(); 273 delete.bindToOwner(task); 274 delete.setFile(outputFile); 275 delete.execute(); 276 } 277 } 278 279 283 protected Resource getStylesheet() { 284 String xslname = "junit-frames.xsl"; 285 if (NOFRAMES.equals(format)) { 286 xslname = "junit-noframes.xsl"; 287 } 288 if (styleDir == null) { 289 URLResource stylesheet = new URLResource(); 292 URL stylesheetURL = getClass().getClassLoader().getResource( 293 "org/apache/tools/ant/taskdefs/optional/junit/xsl/" + xslname); 294 stylesheet.setURL(stylesheetURL); 295 return stylesheet; 296 } 297 FileResource stylesheet = new FileResource(); 300 File stylesheetFile = new File (styleDir, xslname); 301 stylesheet.setFile(stylesheetFile); 302 return stylesheet; 303 } 304 305 306 309 protected void checkOptions() throws BuildException { 310 if (toDir == null) { 312 toDir = task.getProject().resolveFile("."); 313 } else if (!toDir.isAbsolute()) { 314 toDir = task.getProject().resolveFile(toDir.getPath()); 315 } 316 } 317 318 327 protected String getStylesheetSystemId() throws IOException { 328 String xslname = "junit-frames.xsl"; 329 if (NOFRAMES.equals(format)) { 330 xslname = "junit-noframes.xsl"; 331 } 332 if (styleDir == null) { 333 URL url = getClass().getResource("xsl/" + xslname); 334 if (url == null) { 335 throw new FileNotFoundException ("Could not find jar resource " + xslname); 336 } 337 return url.toExternalForm(); 338 } 339 File file = new File (styleDir, xslname); 340 if (!file.exists()) { 341 throw new FileNotFoundException ("Could not find file '" + file + "'"); 342 } 343 return JAXPUtils.getSystemId(file); 344 } 345 346 } 347 | Popular Tags |