Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BuildStatChartTask |
|
| 1.5925925925925926;1.593 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // | |
3 | // ObjectLab is sponsoring QALab | |
4 | // | |
5 | // Based in London, we are world leaders in the design and development | |
6 | // of bespoke applications for the Securities Financing markets. | |
7 | // | |
8 | // <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> | |
9 | // ___ _ _ _ _ _ | |
10 | // / _ \| |__ (_) ___ ___| |_| | __ _| |__ | |
11 | // | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ | |
12 | // | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | | |
13 | // \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ | |
14 | // |__/ | |
15 | // | |
16 | // http://www.ObjectLab.co.uk | |
17 | // --------------------------------------------------------------------------- | |
18 | // | |
19 | //QALab is released under the GNU General Public License. | |
20 | // | |
21 | //QALab: Collects QA Statistics from your build over time. | |
22 | //2005+, ObjectLab Ltd | |
23 | // | |
24 | //This library is free software; you can redistribute it and/or | |
25 | //modify it under the terms of the GNU General Public | |
26 | //License as published by the Free Software Foundation; either | |
27 | //version 2.1 of the License, or (at your option) any later version. | |
28 | // | |
29 | //This library is distributed in the hope that it will be useful, | |
30 | //but WITHOUT ANY WARRANTY; without even the implied warranty of | |
31 | //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
32 | //General Public License for more details. | |
33 | // | |
34 | //You should have received a copy of the GNU General Public | |
35 | //License along with this library; if not, write to the Free Software | |
36 | //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
37 | // | |
38 | //////////////////////////////////////////////////////////////////////////////// | |
39 | package net.objectlab.qalab.ant; | |
40 | ||
41 | import java.io.File; | |
42 | import java.io.FileInputStream; | |
43 | import java.io.FileNotFoundException; | |
44 | import java.io.IOException; | |
45 | ||
46 | import javax.xml.parsers.ParserConfigurationException; | |
47 | import javax.xml.parsers.SAXParser; | |
48 | import javax.xml.parsers.SAXParserFactory; | |
49 | ||
50 | import net.objectlab.qalab.parser.BuildStatForChartParser; | |
51 | ||
52 | import org.apache.tools.ant.BuildException; | |
53 | import org.apache.tools.ant.Task; | |
54 | import org.xml.sax.InputSource; | |
55 | import org.xml.sax.SAXException; | |
56 | ||
57 | /** | |
58 | * @author Benoit Xhenseval | |
59 | * @version $Revision: 189 $ | |
60 | */ | |
61 | 0 | public class BuildStatChartTask extends Task { |
62 | // ~ Static fields/initializers | |
63 | // ------------------------------------------------------------------------ | |
64 | ||
65 | /** Default SAX parser. * */ | |
66 | // protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser"; | |
67 | ||
68 | /** Default Chart width. * */ | |
69 | private static final int DEFAULT_WIDTH = 950; | |
70 | ||
71 | /** Default Chart height. * */ | |
72 | private static final int DEFAULT_HEIGHT = 600; | |
73 | ||
74 | // ~ Instance fields | |
75 | // ------------------------------------------------------------------------ | |
76 | ||
77 | /** the base properties file. */ | |
78 | 0 | private File inputFile = null; |
79 | ||
80 | /** the merged properties file. */ | |
81 | 0 | private File toDir = null; |
82 | ||
83 | /** if true, logging output will be suppressed. */ | |
84 | 0 | private boolean quiet = false; |
85 | ||
86 | /** Chart width. * */ | |
87 | 0 | private int width = DEFAULT_WIDTH; |
88 | ||
89 | /** Chart height. * */ | |
90 | 0 | private int height = DEFAULT_HEIGHT; |
91 | ||
92 | /** moving Average for second line on Chart. * */ | |
93 | 0 | private int movingAverage = 0; |
94 | ||
95 | /** Summary only (1 chart only). * */ | |
96 | 0 | private boolean summaryOnly = true; |
97 | ||
98 | /** statistic type, defaulted to 'checkstyle'. * */ | |
99 | 0 | private String type = "checkstyle"; |
100 | ||
101 | /** statistic type for the summary chart, defaulted to 'checkstyle'. * */ | |
102 | 0 | private String summaryType = "checkstyle"; |
103 | ||
104 | /** Prefix for chart file names. * */ | |
105 | 0 | private String filePrefix = ""; |
106 | ||
107 | /** X Axis Title */ | |
108 | 0 | private String xAxisTitle = "Dates"; |
109 | ||
110 | /** Y Axis Title */ | |
111 | 0 | private String yAxisTitle = "Violations"; |
112 | ||
113 | /** X Axis Title for Summary */ | |
114 | 0 | private String xAxisSummaryTitle = "Dates"; |
115 | ||
116 | /** Y Axis Title for Summary */ | |
117 | 0 | private String yAxisSummaryTitle = "Violations"; |
118 | ||
119 | ||
120 | // ~ Methods | |
121 | // ------------------------------------------------------------------------ | |
122 | ||
123 | /** | |
124 | * Method invoked by the ant or maven framework to execute the action | |
125 | * associated with this task. | |
126 | */ | |
127 | public final void execute() { | |
128 | // validate the provided parameters | |
129 | 0 | validate(); |
130 | ||
131 | // Display the files being processed | |
132 | 0 | if (!quiet) { |
133 | 0 | log("inputFile='" + inputFile.getPath() + "', toDir='" + toDir.getPath() + "'"); |
134 | 0 | log("width='" + width + "' height='" + height + "' summaryOnly='" + summaryOnly + "'"); |
135 | 0 | log("type='" + type + "' summaryType='" + summaryType + "'"); |
136 | } | |
137 | ||
138 | 0 | generateCharts(); |
139 | 0 | } |
140 | ||
141 | /** | |
142 | * Generate the charts by creating a SAX parser specialised in finding the | |
143 | * elements of given type and creating charts in a given directory. | |
144 | */ | |
145 | private void generateCharts() { | |
146 | // define a sax stuff... | |
147 | 0 | BuildStatForChartParser myForChartParser = new BuildStatForChartParser(new AntTaskLogger(this)); |
148 | ||
149 | 0 | myForChartParser.setChartHeight(getHeight()); |
150 | 0 | myForChartParser.setChartWidth(getWidth()); |
151 | 0 | myForChartParser.setToDir(toDir.getAbsolutePath() + "/"); |
152 | 0 | myForChartParser.setMovingAverage(movingAverage); |
153 | 0 | myForChartParser.setSummaryOnly(summaryOnly); |
154 | 0 | myForChartParser.setSummaryStyle(summaryType); |
155 | 0 | myForChartParser.setAcceptedStyle(type); |
156 | 0 | myForChartParser.setQuiet(quiet); |
157 | 0 | myForChartParser.setFilePrefix(getFilePrefix()); |
158 | 0 | myForChartParser.setXAxisSummaryTitle(xAxisSummaryTitle); |
159 | 0 | myForChartParser.setYAxisSummaryTitle(yAxisSummaryTitle); |
160 | 0 | myForChartParser.setXAxisTitle(xAxisTitle); |
161 | 0 | myForChartParser.setYAxisTitle(yAxisTitle); |
162 | ||
163 | // XMLReader parser = null; | |
164 | ||
165 | try { | |
166 | 0 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
167 | 0 | SAXParser saxParser = factory.newSAXParser(); |
168 | ||
169 | 0 | if (!quiet) { |
170 | 0 | log("Parsing " + inputFile); |
171 | } | |
172 | ||
173 | 0 | saxParser.parse(new InputSource(new FileInputStream(inputFile)), myForChartParser); |
174 | // parser = XMLReaderFactory.createXMLReader(); | |
175 | // parser.setContentHandler(myForChartParser); | |
176 | // parser.setErrorHandler(myForChartParser); | |
177 | // | |
178 | // if (!quiet) { | |
179 | // log("Parsing " + inputFile); | |
180 | // } | |
181 | // | |
182 | // parser.parse(new InputSource(new FileInputStream(inputFile))); | |
183 | 0 | } catch (SAXException e) { |
184 | 0 | log(e.toString()); |
185 | 0 | } catch (FileNotFoundException e) { |
186 | 0 | log(e.toString()); |
187 | 0 | } catch (IOException e) { |
188 | 0 | log(e.toString()); |
189 | 0 | } catch (ParserConfigurationException e) { |
190 | 0 | log(e.toString()); |
191 | 0 | } |
192 | 0 | } |
193 | ||
194 | /** | |
195 | * Validates the parameters supplied by ant/maven. | |
196 | */ | |
197 | private void validate() { | |
198 | 0 | if (inputFile == null) { |
199 | 0 | throw new BuildException("inputFile is mandatory"); |
200 | } | |
201 | ||
202 | 0 | if (toDir == null) { |
203 | 0 | throw new BuildException("toDir is mandatory"); |
204 | } | |
205 | ||
206 | 0 | if (!inputFile.canRead()) { |
207 | 0 | final String message = "Unable to read from " + inputFile + "."; |
208 | ||
209 | 0 | throw new BuildException(message); |
210 | } | |
211 | ||
212 | 0 | if (toDir.exists() && !toDir.isDirectory()) { |
213 | 0 | final String message = "toDir must be a directory (" + toDir + ")"; |
214 | ||
215 | 0 | throw new BuildException(message); |
216 | } | |
217 | ||
218 | 0 | if (!toDir.exists()) { |
219 | 0 | if (!quiet) { |
220 | 0 | log("Creating directory: " + toDir.toString()); |
221 | } | |
222 | ||
223 | 0 | toDir.mkdir(); |
224 | } | |
225 | 0 | } |
226 | ||
227 | /** | |
228 | * @param statisticsType | |
229 | * the statistics type to take into account. | |
230 | */ | |
231 | public final void setType(final String statisticsType) { | |
232 | 0 | this.type = statisticsType; |
233 | 0 | } |
234 | ||
235 | /** | |
236 | * Sets the base file (qalab.xml). | |
237 | * | |
238 | * @param statisticsFile | |
239 | * the qalab.xml | |
240 | */ | |
241 | public final void setInputFile(final File statisticsFile) { | |
242 | 0 | this.inputFile = statisticsFile; |
243 | 0 | } |
244 | ||
245 | /** | |
246 | * Set the to directory where to output the charts. | |
247 | * | |
248 | * @param targetDirectory | |
249 | * the destination directory where the charts are written. The | |
250 | * directory will be created if it does not exist. | |
251 | */ | |
252 | public final void setToDir(final File targetDirectory) { | |
253 | 0 | this.toDir = targetDirectory; |
254 | 0 | } |
255 | ||
256 | /** | |
257 | * Sets the quiet mode. | |
258 | * | |
259 | * @param beQuiet | |
260 | * if true, no messages are displayed while generating charts. | |
261 | */ | |
262 | public final void setQuiet(final boolean beQuiet) { | |
263 | 0 | this.quiet = beQuiet; |
264 | 0 | } |
265 | ||
266 | /** | |
267 | * @return the chart width in pixels. | |
268 | */ | |
269 | public final int getWidth() { | |
270 | 0 | return width; |
271 | } | |
272 | ||
273 | /** | |
274 | * set the chart width in pixels. | |
275 | * | |
276 | * @param chartWidth | |
277 | * number of pixels. | |
278 | */ | |
279 | public final void setWidth(final int chartWidth) { | |
280 | 0 | this.width = chartWidth; |
281 | 0 | } |
282 | ||
283 | /** | |
284 | * @return the chart height in pixels. | |
285 | */ | |
286 | public final int getHeight() { | |
287 | 0 | return height; |
288 | } | |
289 | ||
290 | /** | |
291 | * set the chart height in pixels. | |
292 | * | |
293 | * @param chartHeight | |
294 | * number of pixels. | |
295 | */ | |
296 | public final void setHeight(final int chartHeight) { | |
297 | 0 | this.height = chartHeight; |
298 | 0 | } |
299 | ||
300 | /** | |
301 | * @return number of points taken for form the moving average line. | |
302 | */ | |
303 | public final int getMovingAverage() { | |
304 | 0 | return movingAverage; |
305 | } | |
306 | ||
307 | /** | |
308 | * Set the number of points taken to form the moving average line, no moving | |
309 | * average line will be created if value <=0. | |
310 | * | |
311 | * @param aMovingAverage | |
312 | * no average if <=0 | |
313 | */ | |
314 | public final void setMovingAverage(final int aMovingAverage) { | |
315 | 0 | this.movingAverage = aMovingAverage; |
316 | 0 | } |
317 | ||
318 | /** | |
319 | * @return if true, only a summary chart will be generated (default). | |
320 | */ | |
321 | public final boolean isSummaryOnly() { | |
322 | 0 | return summaryOnly; |
323 | } | |
324 | ||
325 | /** | |
326 | * set the boolean summary only, if true, only a summary chart will be | |
327 | * generated. | |
328 | * | |
329 | * @param onlySummary | |
330 | * true will generate only 1 chart. | |
331 | */ | |
332 | public final void setSummaryOnly(final boolean onlySummary) { | |
333 | 0 | this.summaryOnly = onlySummary; |
334 | 0 | } |
335 | ||
336 | /** | |
337 | * @return Returns the filePrefix. | |
338 | */ | |
339 | public final String getFilePrefix() { | |
340 | 0 | return filePrefix; |
341 | } | |
342 | ||
343 | /** | |
344 | * @param prefix | |
345 | * The filePrefix to set. | |
346 | */ | |
347 | public final void setFilePrefix(final String prefix) { | |
348 | 0 | this.filePrefix = prefix; |
349 | 0 | } |
350 | ||
351 | /** | |
352 | * @return Returns the summaryType. | |
353 | */ | |
354 | public String getSummaryType() { | |
355 | 0 | return summaryType; |
356 | } | |
357 | ||
358 | /** | |
359 | * @param theSummaryType | |
360 | * The summaryType to set. | |
361 | */ | |
362 | public void setSummaryType(final String theSummaryType) { | |
363 | 0 | this.summaryType = theSummaryType; |
364 | 0 | } |
365 | ||
366 | /** | |
367 | * @return Returns the xAxisSummaryTitle. | |
368 | */ | |
369 | public String getXAxisSummaryTitle() { | |
370 | 0 | return xAxisSummaryTitle; |
371 | } | |
372 | ||
373 | /** | |
374 | * @param axisSummaryTitle The xAxisSummaryTitle to set. | |
375 | */ | |
376 | public void setXAxisSummaryTitle(String axisSummaryTitle) { | |
377 | 0 | xAxisSummaryTitle = axisSummaryTitle; |
378 | 0 | } |
379 | ||
380 | /** | |
381 | * @return Returns the xAxisTitle. | |
382 | */ | |
383 | public String getXAxisTitle() { | |
384 | 0 | return xAxisTitle; |
385 | } | |
386 | ||
387 | /** | |
388 | * @param axisTitle The xAxisTitle to set. | |
389 | */ | |
390 | public void setXAxisTitle(String axisTitle) { | |
391 | 0 | xAxisTitle = axisTitle; |
392 | 0 | } |
393 | ||
394 | /** | |
395 | * @return Returns the yAxisSummaryTitle. | |
396 | */ | |
397 | public String getYAxisSummaryTitle() { | |
398 | 0 | return yAxisSummaryTitle; |
399 | } | |
400 | ||
401 | /** | |
402 | * @param axisSummaryTitle The yAxisSummaryTitle to set. | |
403 | */ | |
404 | public void setYAxisSummaryTitle(String axisSummaryTitle) { | |
405 | 0 | yAxisSummaryTitle = axisSummaryTitle; |
406 | 0 | } |
407 | ||
408 | /** | |
409 | * @return Returns the yAxisTitle. | |
410 | */ | |
411 | public String getYAxisTitle() { | |
412 | 0 | return yAxisTitle; |
413 | } | |
414 | ||
415 | /** | |
416 | * @param axisTitle The yAxisTitle to set. | |
417 | */ | |
418 | public void setYAxisTitle(String axisTitle) { | |
419 | 0 | yAxisTitle = axisTitle; |
420 | 0 | } |
421 | } | |
422 | /* | |
423 | * ObjectLab is sponsoring QALab | |
424 | * | |
425 | * Based in London, we are world leaders in the design and development | |
426 | * of bespoke applications for the securities financing markets. | |
427 | * | |
428 | * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a> | |
429 | * ___ _ _ _ _ _ | |
430 | * / _ \| |__ (_) ___ ___| |_| | __ _| |__ | |
431 | * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ | |
432 | * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | | |
433 | * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ | |
434 | * |__/ | |
435 | * | |
436 | * www.ObjectLab.co.uk | |
437 | */ |