1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 package net.objectlab.qalab.m2.report;
40
41 import java.io.File;
42 import java.io.IOException;
43 import java.util.Locale;
44 import java.util.ResourceBundle;
45 import java.io.FileInputStream;
46 import java.io.InputStream;
47
48 import net.objectlab.qalab.m2.util.Utils;
49 import net.objectlab.qalab.m2.util.XmlTransformer;
50
51 import org.apache.maven.project.MavenProject;
52 import org.apache.maven.reporting.AbstractMavenReport;
53 import org.apache.maven.reporting.MavenReportException;
54
55 import org.codehaus.doxia.site.renderer.SiteRenderer;
56 import javax.xml.transform.TransformerException;
57
58 /**
59 * Goal that handles the generation of a site report based on the data in the
60 * qalab.xml file.
61 *
62 * @author Dave Sag and Benoit Xhenseval.
63 * @goal report
64 * @phase deploy
65 */
66 public class MainReport extends AbstractMavenReport {
67
68
69 /**
70 * The generated qalab.xml file.
71 *
72 * @parameter expression="${project.basedir}/qalab.xml"
73 */
74 private File inputFile = null;
75
76 /**
77 * The xslt stylesheet bundled, either qalab-chart-xdoc.xsl or
78 * qalab-chart-html.xsl.
79 *
80 * @parameter default-value="qalab-chart-xdoc.xsl";
81 */
82 private String moversBundledXsl = null;
83
84 /**
85 * The directory where the generated html report will go. @ parameter
86 * expression="${project.reporting.outputDirectory}/qalab"
87 * @parameter expression="${project.build.directory}/generated-site/xdoc/qalab"
88 * @required
89 */
90 private String outputDirectory;
91
92 /**
93 * Statistic types, defaulted to
94 * 'checkstyle,pmd,pmd-cpd,findbugs,simian,cobertura-line,cobertura-branch'.
95 *
96 * @parameter default-value="checkstyle,pmd,pmd-cpd,findbugs,simian,cobertura-line,cobertura-branch"
97 */
98 private String types = "checkstyle,pmd,pmd-cpd,findbugs,simian,cobertura-line,cobertura-branch";
99
100 /**
101 * The xml input stream.
102 */
103 private InputStream theXmlStream = null;
104
105 /**
106 * The xslt style sheet.
107 *
108 * @parameter
109 */
110 private File styleSheet = null;
111
112 /**
113 * The xslt style sheet input stream.
114 */
115 private InputStream theStyleSheetStream = null;
116
117 /**
118 * The number of hours to define the time window from now.
119 *
120 * @parameter default-value="48"
121 */
122 private String startTimeHoursOffset = "48";
123
124 /**
125 * The string version of the actual date computed by the offset set above.
126 */
127 private String theOffset;
128
129 /**
130 * Not sure what this is.
131 *
132 * @component
133 */
134 private SiteRenderer siteRenderer;
135
136 /**
137 * The maven project.
138 *
139 * @parameter expression="${project}"
140 * @required
141 * @readonly
142 */
143 private MavenProject project;
144
145 /**
146 * generate the actual report.
147 *
148 * @param aLocale
149 * ignored.
150 * @throws MavenReportException
151 * if anything goes wrong.
152 */
153 protected final void executeReport(final Locale aLocale) throws MavenReportException {
154
155 validate();
156 final File outdir = new File(getOutputDirectory());
157 if (!outdir.exists()) {
158 outdir.mkdirs();
159 }
160 assert outdir.isDirectory() : "the output directory was not a directory";
161
162 final File output;
163 try {
164 output = File.createTempFile("qalab", null, outdir);
165 assert output != null : "the temp file is null.";
166
167 } catch (IOException ioex) {
168 throw new MavenReportException("could not create temp file.", ioex);
169 }
170
171 final XmlTransformer t = new XmlTransformer(theXmlStream, theStyleSheetStream, output);
172 t.addParameter("targetdir", getOutputDirectory());
173 t.addParameter("type", types);
174 t.addParameter("offset", theOffset);
175 try {
176 t.transform();
177 } catch (TransformerException tex) {
178 throw new MavenReportException("transformation failed.", tex);
179 }
180
181
182 output.deleteOnExit();
183 }
184
185 /**
186 * @return "qalab/index"
187 * @see org.apache.maven.reporting.MavenReport#getOutputName()
188 */
189 public final String getOutputName() {
190 return "qalab/index";
191 }
192
193 /**
194 * @return The output directory as configured in your <code>pom.xml</code>.
195 * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
196 */
197 protected final String getOutputDirectory() {
198 return outputDirectory;
199 }
200
201 /**
202 * @return The Maven Project itself. Used internally to get access to Config
203 * params etc.
204 * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
205 */
206 protected final MavenProject getProject() {
207 return project;
208 }
209
210 /**
211 * @return a direct reference to the site renderer.
212 * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
213 */
214 protected final SiteRenderer getSiteRenderer() {
215 return siteRenderer;
216 }
217
218 /**
219 * @param aLocale
220 * The locale.
221 * @return The locale specific report name.
222 * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
223 */
224 public final String getName(final Locale aLocale) {
225 return getBundle(aLocale).getString("report.qalab.name");
226 }
227
228 /**
229 * @param aLocale
230 * The locale.
231 * @return The locale specific report description.
232 * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
233 */
234 public final String getDescription(final Locale aLocale) {
235 return getBundle(aLocale).getString("report.qalab.description");
236 }
237
238 /**
239 * @return true.
240 * @see org.apache.maven.reporting.MavenReport#isExternalReport
241 */
242 public final boolean isExternalReport() {
243 return true;
244 }
245
246 /**
247 * @param aLocale
248 * The locale.
249 * @return the resource bundle for this report.
250 */
251 private static ResourceBundle getBundle(final Locale aLocale) {
252 return ResourceBundle.getBundle("qalab-report", aLocale, MainReport.class.getClassLoader());
253 }
254
255 /**
256 * Validates the parameters supplied by maven 2.
257 *
258 * @throws MavenReportException
259 * if any supplied params are wrong.
260 */
261 private void validate() throws MavenReportException {
262 try {
263 Utils.checkFile(inputFile, "inputFile");
264 theXmlStream = new FileInputStream(inputFile);
265 } catch (IOException ioex) {
266 throw new MavenReportException(ioex.getMessage(), ioex);
267 }
268
269 if (styleSheet == null) {
270 try {
271 theStyleSheetStream = Utils.extractAsInputStream(moversBundledXsl);
272 } catch (IOException ioex) {
273 throw new MavenReportException("Could not find the default stylesheet. " + ioex.getMessage(), ioex);
274 }
275 } else {
276 try {
277 Utils.checkFile(styleSheet, "styleSheet");
278 theStyleSheetStream = new FileInputStream(styleSheet);
279 } catch (IOException ioex) {
280 throw new MavenReportException(ioex.getMessage(), ioex);
281 }
282 }
283
284 theOffset = Utils.formatDateBasedOnOffset(startTimeHoursOffset);
285
286 }
287 }
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303