KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > ant > SimpleXslReportStyle


1 /*
2  * @(#)SimpleXslReportStyle.java
3  *
4  * Copyright (C) 2004 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.ant;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Project;
37 import org.w3c.dom.Document JavaDoc;
38
39
40
41 /**
42  * Describes a report style, used to generate readable reports from the
43  * XML output.
44  *
45  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
46  * @version $Date: 2004/04/15 05:48:25 $
47  * @since March 15, 2004
48  */

49 public class SimpleXslReportStyle implements IReportStyle
50 {
51     private File JavaDoc outdir;
52     private boolean removeEmpties = false;
53     private StyleTransformer styleRemove = null;
54     private StyleTransformer styleHtml = null;
55     private String JavaDoc prefix = "CoverageReport-";
56     private String JavaDoc suffix = ".html";
57     private File JavaDoc styleFile = null;
58     private String JavaDoc styleUrl = null;
59     private Vector JavaDoc params = new Vector JavaDoc();
60     
61     
62     public static final class ParamType
63     {
64         String JavaDoc name;
65         String JavaDoc expr;
66         String JavaDoc ifProp;
67         String JavaDoc elseProp;
68         
69         
70         public void setName( String JavaDoc n )
71         {
72             this.name = n;
73         }
74         
75         public void setExpression( String JavaDoc x )
76         {
77             this.expr = x;
78         }
79         
80         public void setIf( String JavaDoc i )
81         {
82             this.ifProp = i;
83         }
84         
85         public void setElse( String JavaDoc e )
86         {
87             this.elseProp = e;
88         }
89         
90         public void updateParameter( StyleTransformer st, Project p )
91         {
92             if (this.ifProp != null && p.getProperty( this.ifProp ) == null)
93             {
94                 p.log( "Ignoring parameter '"+this.name+"' because property '"+
95                     this.ifProp+"' was not set.", Project.MSG_VERBOSE );
96                 return;
97             }
98             if (this.elseProp != null && p.getProperty( this.elseProp ) != null)
99             {
100                 p.log( "Ignoring parameter '"+this.name+"' because property '"+
101                     this.elseProp+"' was set.", Project.MSG_VERBOSE );
102                 return;
103             }
104             
105             st.setParameter( this.name, this.expr );
106         }
107     }
108     
109     
110     
111     public void setPrefix( String JavaDoc p )
112     {
113         if (p == null)
114         {
115             p = "";
116         }
117         this.prefix = p;
118     }
119     
120     
121     public void setSuffix( String JavaDoc s )
122     {
123         if (s == null)
124         {
125             s = "";
126         }
127         this.suffix = s;
128     }
129     
130     
131     public void setDestDir( File JavaDoc dir )
132     {
133         this.outdir = dir;
134     }
135     
136     
137     public void setRemoveEmpty( boolean on )
138     {
139         this.removeEmpties = on;
140     }
141     
142     
143     public void setStyle( File JavaDoc f )
144     {
145         this.styleFile = f;
146     }
147     
148     
149     public void setStyleURL( String JavaDoc url )
150     {
151         this.styleUrl = url;
152     }
153     
154     
155     public void addParam( ParamType pt )
156     {
157         if (pt != null)
158         {
159             this.params.addElement( pt );
160         }
161     }
162     
163     
164     
165     /**
166      * Called when the task is finished generating all the reports. This
167      * may be useful for styles that join all the reports together.
168      */

169     public void reportComplete( Project project, Vector JavaDoc errors )
170             throws BuildException, IOException JavaDoc
171     {
172         // free up memory
173
this.styleHtml = null;
174         this.styleRemove = null;
175     }
176     
177     
178     public void generateReport( Project project, Document JavaDoc doc,
179             String JavaDoc moduleName )
180             throws BuildException, IOException JavaDoc
181     {
182         project.log( "Generating XSL report for module "+moduleName,
183             Project.MSG_VERBOSE );
184         if (this.removeEmpties)
185         {
186             project.log( "Removing empty methods and classes...",
187                 Project.MSG_DEBUG );
188             doc = getRemoveEmptiesStyle( project ).transform( doc );
189         }
190         project.log( "Transforming...",
191             Project.MSG_DEBUG );
192         this.outdir.mkdirs();
193         File JavaDoc outFile = new File JavaDoc( this.outdir,
194             this.prefix + moduleName + this.suffix );
195         getHtmlStyle( project ).transform( doc, outFile );
196     }
197     
198     
199     protected StyleTransformer getRemoveEmptiesStyle( Project project )
200             throws IOException JavaDoc
201     {
202         if (this.styleRemove == null && this.removeEmpties)
203         {
204             this.styleRemove = new StyleTransformer( project,
205                 getStylesheetSystemIdForClass( "remove-empty-classes.xsl" ),
206                 this.outdir );
207         }
208         return this.styleRemove;
209     }
210     
211     
212     protected StyleTransformer getHtmlStyle( Project project )
213             throws IOException JavaDoc
214     {
215         if (this.styleHtml == null)
216         {
217             this.styleHtml = new StyleTransformer( project,
218                 getStylesheetSystemId(),
219                 this.outdir );
220             Enumeration JavaDoc e = this.params.elements();
221             while (e.hasMoreElements())
222             {
223                 ((ParamType)e.nextElement()).updateParameter(
224                     this.styleHtml, project );
225             }
226         }
227         return this.styleHtml;
228     }
229     
230     
231     /**
232      * Get the systemid of the appropriate stylesheet based on its
233      * name and styledir. If no styledir is defined it will load
234      * it as a java resource in the xsl child package, otherwise it
235      * will get it from the given directory.
236      *
237      * @throws IOException thrown if the requested stylesheet does
238      * not exist.
239      */

240     protected String JavaDoc getStylesheetSystemId()
241             throws IOException JavaDoc
242     {
243         URL JavaDoc url = null;
244         if (this.styleFile == null)
245         {
246             if (this.styleUrl == null)
247             {
248                 throw new BuildException(
249                     "No URL or file defined for XSL style sheet." );
250             }
251             url = new URL JavaDoc( this.styleUrl );
252         }
253         else
254         {
255             if (!this.styleFile.exists())
256             {
257                 throw new java.io.FileNotFoundException JavaDoc(
258                     "Could not find file '" + this.styleFile + "'" );
259             }
260             url = new URL JavaDoc( "file", "", styleFile.getAbsolutePath() );
261         }
262         return url.toExternalForm();
263     }
264     
265     
266     protected String JavaDoc getStylesheetSystemIdForClass( String JavaDoc xslname )
267             throws IOException JavaDoc
268     {
269         URL JavaDoc url = getClass().getResource( "xsl/" + xslname );
270         if (url == null)
271         {
272             throw new java.io.FileNotFoundException JavaDoc(
273                 "Could not find jar resource " + xslname);
274         }
275         return url.toExternalForm();
276     }
277 }
278
279
Popular Tags