e.g. Calendar Search Help
You must enter a value before pressing Search
ant

Class: org.apache.tools.ant.taskdefs.optional.junit.AggregateTransformer   ©

 OK to copy?
001 /*
002  * Copyright  2001-2002,2004 The Apache Software Foundation
003  *
004  *  Licensed under the Apache License, Version 2.0 (the "License");
005  *  you may not use this file except in compliance with the License.
006  *  You may obtain a copy of the License at
007  *
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  *
010  *  Unless required by applicable law or agreed to in writing, software
011  *  distributed under the License is distributed on an "AS IS" BASIS,
012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  *  See the License for the specific language governing permissions and
014  *  limitations under the License.
015  *
016  */
017 package org.apache.tools.ant.taskdefs.optional.junit;
018 
019 import java.io.File;
020 import java.io.FileInputStream;
021 import java.io.FileNotFoundException;
022 import java.io.IOException;
023 import java.io.InputStream;
024 import java.net.URL;
025 import javax.xml.parsers.DocumentBuilder;
026 import javax.xml.parsers.DocumentBuilderFactory;
027 import org.apache.tools.ant.BuildException;
028 import org.apache.tools.ant.Project;
029 import org.apache.tools.ant.Task;
030 import org.apache.tools.ant.util.JAXPUtils;
031 import org.apache.tools.ant.types.EnumeratedAttribute;
032 import org.w3c.dom.Document;
033 
034 /**
035  * Transform a JUnit xml report.
036  * The default transformation generates an html report in either framed or non-framed
037  * style. The non-framed style is convenient to have a concise report via mail, the
038  * framed report is much more convenient if you want to browse into different
039  * packages or testcases since it is a Javadoc like report.
040  *
041  */
042 public class AggregateTransformer {
043 
044     public static final String FRAMES = "frames";
045 
046     public static final String NOFRAMES = "noframes";
047 
048     public static class Format extends EnumeratedAttribute {
049         public String[] getValues() {
050             return new String[]{FRAMES, NOFRAMES};
051         }
052     }
053 
054     /** Task */
055     protected Task task;
056 
057     /** the xml document to process */
058     protected Document document;
059 
060     /** the style directory. XSLs should be read from here if necessary */
061     protected File styleDir;
062 
063     /** the destination directory, this is the root from where html should be generated */
064     protected File toDir;
065 
066     /** the format to use for the report. Must be <tt>FRAMES</tt> or <tt>NOFRAMES</tt> */
067     protected String format = FRAMES;
068 
069     /** XML Parser factory */
070     private static DocumentBuilderFactory privateDBFactory;
071 
072     /** XML Parser factory accessible to subclasses */
073     protected static DocumentBuilderFactory dbfactory;
074 
075     static {
076        privateDBFactory = DocumentBuilderFactory.newInstance();
077        dbfactory = privateDBFactory;
078     }
079 
080     public AggregateTransformer(Task task) {
081         this.task = task;
082     }
083 
084     /**
085      * Get the Document Builder Factory
086      *
087      * @return the DocumentBuilderFactory instance in use
088      */
089     protected static DocumentBuilderFactory getDocumentBuilderFactory() {
090         return privateDBFactory;
091     }
092 
093     public void setFormat(Format format) {
094         this.format = format.getValue();
095     }
096 
097     public void setXmlDocument(Document doc) {
098         this.document = doc;
099     }
100 
101     /**
102      * Set the xml file to be processed. This is a helper if you want
103      * to set the file directly. Much more for testing purposes.
104      * @param xmlfile xml file to be processed
105      */
106     protected void setXmlfile(File xmlfile) throws BuildException {
107         try {
108             DocumentBuilder builder = privateDBFactory.newDocumentBuilder();
109             InputStream in = new FileInputStream(xmlfile);
110             try {
111                 Document doc = builder.parse(in);
112                 setXmlDocument(doc);
113             } finally {
114                 in.close();
115             }
116         } catch (Exception e) {
117             throw new BuildException("Error while parsing document: " + xmlfile, e);
118         }
119     }
120 
121     /**
122      * set the style directory. It is optional and will override the
123      * default xsl used.
124      * @param styledir  the directory containing the xsl files if the user
125      * would like to override with its own style.
126      */
127     public void setStyledir(File styledir) {
128         this.styleDir = styledir;
129     }
130 
131     /** set the destination directory */
132     public void setTodir(File todir) {
133         this.toDir = todir;
134     }
135 
136     /** set the extension of the output files */
137     public void setExtension(String ext) {
138         task.log("extension is not used anymore", Project.MSG_WARN);
139     }
140 
141     public void transform() throws BuildException {
142         checkOptions();
143         final long t0 = System.currentTimeMillis();
144         XalanExecutor executor = XalanExecutor.newInstance(this);
145         try {
146             executor.execute();
147         } catch (Exception e) {
148             throw new BuildException("Errors while applying transformations: "
149                 + e.getMessage(), e);
150         }
151         final long dt = System.currentTimeMillis() - t0;
152         task.log("Transform time: " + dt + "ms");
153     }
154 
155     /** check for invalid options */
156     protected void checkOptions() throws BuildException {
157         // set the destination directory relative from the project if needed.
158         if (toDir == null) {
159             toDir = task.getProject().resolveFile(".");
160         } else if (!toDir.isAbsolute()) {
161             toDir = task.getProject().resolveFile(toDir.getPath());
162         }
163     }
164 
165     /**
166      * Get the systemid of the appropriate stylesheet based on its
167      * name and styledir. If no styledir is defined it will load
168      * it as a java resource in the xsl child package, otherwise it
169      * will get it from the given directory.
170      * @throws IOException thrown if the requested stylesheet does
171      * not exist.
172      */
173     protected String getStylesheetSystemId() throws IOException {
174         String xslname = "junit-frames.xsl";
175         if (NOFRAMES.equals(format)) {
176             xslname = "junit-noframes.xsl";
177         }
178         if (styleDir == null) {
179             URL url = getClass().getResource("xsl/" + xslname);
180             if (url == null) {
181                 throw new FileNotFoundException("Could not find jar resource " + xslname);
182             }
Rate183             return url.toExternalForm();
184         }
185         File file = new File(styleDir, xslname);
186         if (!file.exists()) {
187             throw new FileNotFoundException("Could not find file '" + file + "'");
188         }
189         return JAXPUtils.getSystemId(file);
190     }
191 
192 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
183 0% of 0