View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact tma@netspace.net.au. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: StatisticsReport.java,v 1.4 2004/02/03 21:52:08 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.report; 46 47 import java.io.File; 48 import java.io.FileWriter; 49 import java.io.IOException; 50 51 import javax.xml.transform.TransformerException; 52 53 import org.apache.commons.cli.CommandLine; 54 import org.apache.commons.cli.CommandLineParser; 55 import org.apache.commons.cli.GnuParser; 56 import org.apache.commons.cli.Options; 57 import org.apache.log4j.Category; 58 59 import org.exolab.castor.xml.MarshalException; 60 import org.exolab.castor.xml.ValidationException; 61 62 import org.exolab.jmscts.core.TestStatistics; 63 import org.exolab.jmscts.provider.ProviderLoader; 64 65 66 /*** 67 * This class generates a requirements coverage report in HTML from a 68 * {@link TestStatistics} instance 69 * 70 * @version $Revision: 1.4 $ $Date: 2004/02/03 21:52:08 $ 71 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 72 * @see TestStatistics 73 */ 74 public class StatisticsReport { 75 76 /*** 77 * The path to locate stylesheets 78 */ 79 private String _path; 80 81 /*** 82 * The statistics 83 */ 84 private TestStatistics _statistics; 85 86 /*** 87 * The provider configuration 88 */ 89 private ProviderLoader _provider; 90 91 /*** 92 * The logger 93 */ 94 private static final Category log = 95 Category.getInstance(StatisticsReport.class); 96 97 /*** 98 * The file name of the raw report 99 */ 100 private static final String STATISTICS_XML = "statistics.xml"; 101 102 /*** 103 * The file name of the xdoc report 104 */ 105 private static final String XDOC_XML = "statistics-xdoc.xml"; 106 107 /*** 108 * The file name of the HTML report 109 */ 110 private static final String STATISTICS_HTML = "statistics.html"; 111 112 /*** 113 * The file name of the statistics stylesheet 114 */ 115 private static final String STATS_STYLESHEET = 116 "resources/statistics.xsl"; 117 118 119 /*** 120 * Construct an instance to generate a report from an existing 121 * statistics.xml file 122 * 123 * @param path the path to locate stylesheets 124 */ 125 public StatisticsReport(String path) { 126 if (path == null) { 127 throw new IllegalArgumentException("Argument 'path' is null"); 128 } 129 _path = path; 130 } 131 132 /*** 133 * Construct an instance with the test suite's statistics of requirements 134 * 135 * @param path the path to locate stylesheets 136 * @param statistics the test suite's statistics of requirements 137 * @param provider the provider loader 138 */ 139 public StatisticsReport(String path, TestStatistics statistics, 140 ProviderLoader provider) { 141 if (path == null) { 142 throw new IllegalArgumentException("Argument 'path' is null"); 143 } 144 if (statistics == null) { 145 throw new IllegalArgumentException( 146 "Argument 'statistics' is null"); 147 } 148 if (provider == null) { 149 throw new IllegalArgumentException("Argument 'provider' is null"); 150 } 151 _path = path; 152 _statistics = statistics; 153 _provider = provider; 154 } 155 156 /*** 157 * Generate the statistics report 158 * 159 * @param dir the directory path to write the report to 160 * @throws IOException if the specified file is not found or if some other 161 * I/O error occurs 162 * @throws MarshalException if the statistics report cannot be written 163 * @throws ValidationException if the statistics report doesn't comply 164 * with the XML Schema definition 165 * @throws TransformerException if the transformation fails 166 */ 167 public void report(String dir) 168 throws IOException, MarshalException, TransformerException, 169 ValidationException { 170 if (_statistics != null) { 171 Statistics statistics = _statistics.getStatistics(); 172 statistics.setProvider(_provider.getName()); 173 String path = dir + "/" + STATISTICS_XML; 174 if (log.isDebugEnabled()) { 175 log.debug("Writing statistics report to " + path); 176 } 177 FileWriter stream = new FileWriter(path); 178 try { 179 statistics.marshal(stream); 180 } finally { 181 stream.close(); 182 } 183 } 184 transform(dir); 185 } 186 187 /*** 188 * Transform the xdoc report to HTML 189 * 190 * @param dir the base directory 191 * @throws IOException for any I/O error 192 * @throws TransformerException if transformation fails 193 */ 194 private void transform(String dir) 195 throws IOException, TransformerException { 196 197 File input = new File(dir, STATISTICS_XML); 198 File xdoc = new File(dir, XDOC_XML); 199 File baseDir = new File(_path); 200 File stylesheet = new File(baseDir, STATS_STYLESHEET); 201 File html = new File(dir, STATISTICS_HTML); 202 TransformerHelper.transform(input, xdoc, stylesheet); 203 TransformerHelper.transformXDoc2HTML(xdoc, html, baseDir); 204 } 205 206 /*** 207 * Main line 208 * 209 * @param args the command line arguments 210 * @throws Exception for any error 211 */ 212 public static void main(String[] args) throws Exception { 213 final String path = "path"; 214 215 Options options = new Options(); 216 options.addOption(path, true, "the output path"); 217 218 CommandLineParser parser = new GnuParser(); 219 CommandLine commands = parser.parse(options, args); 220 221 String dir = commands.getOptionValue(path); 222 if (dir == null) { 223 throw new Exception("-" + path + " <path> parameter not set"); 224 } 225 226 StatisticsReport report = new StatisticsReport("./"); 227 report.report(dir); 228 } 229 230 }

This page was automatically generated by Maven