KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > report > AggregateTransformer


1 package csdl.jblanket.report;
2
3 import csdl.jblanket.JBlanketException;
4 import csdl.jblanket.report.element.ElementContentHandler;
5 import csdl.jblanket.report.element.ElementErrorHandler;
6 import csdl.jblanket.report.element.MethodSetsElement;
7
8 import java.io.ByteArrayOutputStream JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.OutputStream JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 //import javax.xml.parsers.SAXParser;
22
//import javax.xml.parsers.SAXParserFactory;
23
import javax.xml.transform.Result JavaDoc;
24 import javax.xml.transform.Source JavaDoc;
25 import javax.xml.transform.Transformer JavaDoc;
26 import javax.xml.transform.TransformerFactory JavaDoc;
27 import javax.xml.transform.dom.DOMSource JavaDoc;
28 import javax.xml.transform.stream.StreamResult JavaDoc;
29 import javax.xml.transform.stream.StreamSource JavaDoc;
30
31 import org.apache.tools.ant.types.EnumeratedAttribute;
32 import org.apache.xerces.parsers.SAXParser;
33 import org.w3c.dom.Document JavaDoc;
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37
38 /**
39  * Aggregates the output from JBlanket and transforms the aggregate file from XML to HTML.
40  * <p>
41  * First this class combines all methods in the system into one XML file, 'COVER-MethodSets.xml',
42  * according to the following types:
43  * <p>
44  * 'constructor' - constructors.<br>
45  * 'oneline' - methods that contain only one line of code.<br>
46  * 'excludedindividual' - excluded methods chose though ExcludedIndividualMethodsApp.<br>
47  * 'tested' - methods invoked during testing that are not 'oneline' methods.<br>
48  * 'untested' - methods not invoked during testing that are not 'oneline' methods.<br>
49  * <p>
50  * Then the aggregate XML file can be transformed into HTML pages, mimicking the behavior found
51  * in Apache Ant's junitreport task.
52  * <p>
53  * Note that the <code>excluded individual methods</code> take priority over all the other
54  * categories of methods. I.e., if a one-line method was individually excluded by the application,
55  * then it is considered to be an individually excluded methods instead of a one-line method.
56  * <p>
57  * Code is based upon Ant's XMLResultAggregator class.
58  *
59  * @author Joy M. Agustin
60  * @version $Id: AggregateTransformer.java,v 1.1 2004/11/07 00:32:44 timshadel Exp $
61  */

62 public class AggregateTransformer {
63   
64   /** Collection of 'type of method stored' mapped to 'name of file to aggregate' */
65   private Map JavaDoc reportCategories;
66   /** Path of to the JBlanket output directory */
67   private String JavaDoc jblanketDir;
68   /** Path of to the HTML output directory */
69   private File JavaDoc toDir;
70   /** Holds the value from Format class */
71   private String JavaDoc format;
72
73   /** Use to specify frames in output */
74   public static final String JavaDoc FRAMES = "frames";
75   /** Use to specify no frames in output */
76   public static final String JavaDoc NOFRAMES = "noframes";
77
78   /** Set of MethodSet's */
79   private MethodSetsElement methodSets = new MethodSetsElement();
80
81   /**
82    * Provides an attempt at enumeration for HTML formats FRAMES or NOFRAMES
83    *
84    * @author see AggregateTransformer class in Jakarta Ant
85    * @version $Id: AggregateTransformer.java,v 1.1 2004/11/07 00:32:44 timshadel Exp $
86    */

87   public static class Format extends EnumeratedAttribute {
88
89     /**
90      * Gets an array of possible frame values.
91      *
92      * @return possible frame values.
93      */

94     public String JavaDoc[] getValues() {
95       return new String JavaDoc[]{FRAMES, NOFRAMES};
96     }
97   }
98   
99   /**
100    * Constructs a new instance of this class.
101    *
102    * @param reportCategories the method categories found, i.e., 'tested', 'untested', 'oneline'.
103    * @param reportFormat the format of report, either 'frames' or 'noframes'.
104    * @param jblanketDir the directory for JBlanket output.
105    * @param toDir the directory for HTML output.
106    */

107   public AggregateTransformer(Map JavaDoc reportCategories, String JavaDoc reportFormat,
108     String JavaDoc jblanketDir, File JavaDoc toDir) {
109     super();
110
111     this.reportCategories = reportCategories;
112
113     // check that reportFormat is a legal value
114
Format newFormat = new Format();
115     newFormat.setValue(reportFormat);
116     this.format = newFormat.getValue();
117
118     // set the JBlanket output directory
119
this.jblanketDir = jblanketDir;
120
121     // set the HTML output directory
122
this.toDir = toDir;
123   }
124
125   /**
126    * Creates an aggregate file containing all of the methods that were 'tested', 'untested',
127    * 'oneline', or 'constructor' from the files in 'jblanketDir'. The methods are sorted according
128    * to individual classes.
129    *
130    * @param outFile name of resulting XML aggregate file.
131    * @throws JBlanketException if cannot find <code>outFile</code>, cannot find one of the
132    * files corresponding to a method type, or write to
133    * <code>outFile</code>.
134    */

135   protected void createAggregateFile(File JavaDoc outFile) throws JBlanketException {
136
137     // combine all tested and untested methods under one root
138
Iterator JavaDoc i = this.reportCategories.keySet().iterator();
139     while (i.hasNext()) {
140       String JavaDoc key = (String JavaDoc) i.next();
141       File JavaDoc nextFile = new File JavaDoc((String JavaDoc) this.reportCategories.get(key));
142       // file for individually excluded files may not exist yet
143
if (!(key.equals("excludedIndividual") && !nextFile.exists())) {
144         parseXmlFile(nextFile, key);
145       }
146     }
147
148     writeXmlFile();
149   }
150
151   /**
152    * Parses <code>fileName</code> and aggregates its contained methods into a common repository.
153    *
154    * @param file the XML file to parse.
155    * @param methodCategory the category of methods in <code>fileName</code>.
156    * @throws JBlanketException if unable to open, read, or parse <code>fileName</code>.
157    */

158   private void parseXmlFile(File JavaDoc file, String JavaDoc methodCategory) throws JBlanketException {
159
160     // create instances needed for parsing
161
XMLReader JavaDoc reader = new SAXParser();
162     /* Attempt at replacing Xerces with Java 1.4 Standard Library
163     XMLReader reader = null;
164     try {
165       // throws SAXException, ParserConfigurationException
166       reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
167     }
168     catch (Exception e) {
169        throw new JBlanketException("Unable to create an XMLReader to read " + fileName, e);
170     }
171     */

172     // register content handler
173
reader.setContentHandler(new ElementContentHandler(this.methodSets, methodCategory));
174     // register error handler
175
reader.setErrorHandler(new ElementErrorHandler());
176     // parse
177

178     InputSource JavaDoc inputSource = null;
179     try {
180       inputSource = new InputSource JavaDoc(new FileInputStream JavaDoc(file));
181     }
182     catch (IOException JavaDoc e) {
183       throw new JBlanketException("Unable to open file " + file);
184     }
185
186     inputSource.setSystemId(file.getAbsolutePath());
187     try {
188       reader.parse(inputSource);
189     }
190     catch (SAXException JavaDoc e) {
191       throw new JBlanketException("Unable to parse file " + file, e);
192     }
193     catch (IOException JavaDoc e) {
194       throw new JBlanketException("Unable to read file " + file, e);
195     }
196   }
197   
198   /**
199    * Write all aggregated methods to output XML file 'COVER-MethodSets.xml'.
200    *
201    * @throws JBlanketException if unable to write to output file.
202    */

203   private void writeXmlFile() throws JBlanketException {
204
205     PrintWriter JavaDoc writer = null;
206     File JavaDoc file = new File JavaDoc(jblanketDir, "COVER-MethodSets.xml");
207     try {
208       writer = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(file));
209     }
210     catch (IOException JavaDoc e) {
211       throw new JBlanketException("Unable to write to file " + file.getAbsoluteFile(), e);
212     }
213
214     this.methodSets.write(writer);
215
216     writer.close();
217   }
218
219   /**
220    * Transforms the XML file <code>xmlFileName</code> to HTML. The default format is with frames.
221    *
222    * @param xmlFileName the name of XML file to transform.
223    * @throws JBlanketException if unable to transform XML file to HTML using XSL.
224    */

225   protected void transformXmlToHtml(String JavaDoc xmlFileName) throws JBlanketException {
226
227     OutputStream JavaDoc ostream = null;
228     
229     // get the XSL file, default "frames"
230
String JavaDoc xslFileName = "jblanket-frames.xsl";
231     if (NOFRAMES.equals(this.format)) {
232       xslFileName = "jblanket-noframes.xsl";
233     }
234
235     URL JavaDoc xslUrl = getClass().getResource(xslFileName);
236     if (xslUrl == null) {
237       String JavaDoc message = "Could not find jar resource " + xslFileName + ";\n"
238                        + "Add jblanket.jar to classpath";
239       throw new JBlanketException(message);
240     }
241
242     // transform XML file xmlFileName to HTML
243
try {
244
245       TransformerFactory JavaDoc tfactory = TransformerFactory.newInstance();
246       Source JavaDoc xslSource = new StreamSource JavaDoc(xslUrl.toExternalForm());
247       // throws javax.xml.transform.TransformerConfigurationExeption
248
Transformer JavaDoc tformer = tfactory.newTransformer(xslSource);
249       DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
250       // throws javax.xml.parsers.ParserConfigurationException
251
DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
252       // throws org.xml.sax.SAXException
253
Document JavaDoc doc = builder.parse(new File JavaDoc(xmlFileName));
254       Source JavaDoc xmlSource = new DOMSource JavaDoc(doc);
255
256       if (FRAMES.equals(this.format)) {
257         ostream = new ByteArrayOutputStream JavaDoc();
258       }
259       else {
260         ostream = new FileOutputStream JavaDoc(new File JavaDoc(toDir.getAbsolutePath(),
261             "jblanket-noframes.html"));
262       }
263
264       tformer.setParameter("output.dir", toDir.getAbsolutePath());
265       Result JavaDoc result = new StreamResult JavaDoc(ostream);
266       // following throws javax.xml.transform.TransformerException
267
tformer.transform(xmlSource, result);
268     }
269     catch (Exception JavaDoc e) {
270       throw new JBlanketException("Unable to transform XML to HTML", e);
271     }
272     finally {
273       try {
274       if (ostream != null) {
275           ostream.close();
276       }
277       }
278       catch (IOException JavaDoc e) {
279         throw new JBlanketException("Could not close OutputStream for " + xmlFileName, e);
280       }
281     }
282   }
283 }
284
Popular Tags