1 26 27 package net.sourceforge.groboutils.codecoverage.v2.ant; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.net.URL ; 32 import java.util.Enumeration ; 33 import java.util.Vector ; 34 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Project; 37 import org.w3c.dom.Document ; 38 39 40 41 49 public class SimpleXslReportStyle implements IReportStyle 50 { 51 private File outdir; 52 private boolean removeEmpties = false; 53 private StyleTransformer styleRemove = null; 54 private StyleTransformer styleHtml = null; 55 private String prefix = "CoverageReport-"; 56 private String suffix = ".html"; 57 private File styleFile = null; 58 private String styleUrl = null; 59 private Vector params = new Vector (); 60 61 62 public static final class ParamType 63 { 64 String name; 65 String expr; 66 String ifProp; 67 String elseProp; 68 69 70 public void setName( String n ) 71 { 72 this.name = n; 73 } 74 75 public void setExpression( String x ) 76 { 77 this.expr = x; 78 } 79 80 public void setIf( String i ) 81 { 82 this.ifProp = i; 83 } 84 85 public void setElse( String 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 p ) 112 { 113 if (p == null) 114 { 115 p = ""; 116 } 117 this.prefix = p; 118 } 119 120 121 public void setSuffix( String s ) 122 { 123 if (s == null) 124 { 125 s = ""; 126 } 127 this.suffix = s; 128 } 129 130 131 public void setDestDir( File 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 f ) 144 { 145 this.styleFile = f; 146 } 147 148 149 public void setStyleURL( String 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 169 public void reportComplete( Project project, Vector errors ) 170 throws BuildException, IOException 171 { 172 this.styleHtml = null; 174 this.styleRemove = null; 175 } 176 177 178 public void generateReport( Project project, Document doc, 179 String moduleName ) 180 throws BuildException, IOException 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 outFile = new File ( 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 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 214 { 215 if (this.styleHtml == null) 216 { 217 this.styleHtml = new StyleTransformer( project, 218 getStylesheetSystemId(), 219 this.outdir ); 220 Enumeration 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 240 protected String getStylesheetSystemId() 241 throws IOException 242 { 243 URL 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 ( this.styleUrl ); 252 } 253 else 254 { 255 if (!this.styleFile.exists()) 256 { 257 throw new java.io.FileNotFoundException ( 258 "Could not find file '" + this.styleFile + "'" ); 259 } 260 url = new URL ( "file", "", styleFile.getAbsolutePath() ); 261 } 262 return url.toExternalForm(); 263 } 264 265 266 protected String getStylesheetSystemIdForClass( String xslname ) 267 throws IOException 268 { 269 URL url = getClass().getResource( "xsl/" + xslname ); 270 if (url == null) 271 { 272 throw new java.io.FileNotFoundException ( 273 "Could not find jar resource " + xslname); 274 } 275 return url.toExternalForm(); 276 } 277 } 278 279 | Popular Tags |