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.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 public class BuildStatChartTask extends Task {
62
63
64
65 /** Default SAX parser. * */
66
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
75
76
77 /** the base properties file. */
78 private File inputFile = null;
79
80 /** the merged properties file. */
81 private File toDir = null;
82
83 /** if true, logging output will be suppressed. */
84 private boolean quiet = false;
85
86 /** Chart width. * */
87 private int width = DEFAULT_WIDTH;
88
89 /** Chart height. * */
90 private int height = DEFAULT_HEIGHT;
91
92 /** moving Average for second line on Chart. * */
93 private int movingAverage = 0;
94
95 /** Summary only (1 chart only). * */
96 private boolean summaryOnly = true;
97
98 /** statistic type, defaulted to 'checkstyle'. * */
99 private String type = "checkstyle";
100
101 /** statistic type for the summary chart, defaulted to 'checkstyle'. * */
102 private String summaryType = "checkstyle";
103
104 /** Prefix for chart file names. * */
105 private String filePrefix = "";
106
107 /** X Axis Title */
108 private String xAxisTitle = "Dates";
109
110 /** Y Axis Title */
111 private String yAxisTitle = "Violations";
112
113 /** X Axis Title for Summary */
114 private String xAxisSummaryTitle = "Dates";
115
116 /** Y Axis Title for Summary */
117 private String yAxisSummaryTitle = "Violations";
118
119
120
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
129 validate();
130
131
132 if (!quiet) {
133 log("inputFile='" + inputFile.getPath() + "', toDir='" + toDir.getPath() + "'");
134 log("width='" + width + "' height='" + height + "' summaryOnly='" + summaryOnly + "'");
135 log("type='" + type + "' summaryType='" + summaryType + "'");
136 }
137
138 generateCharts();
139 }
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
147 BuildStatForChartParser myForChartParser = new BuildStatForChartParser(new AntTaskLogger(this));
148
149 myForChartParser.setChartHeight(getHeight());
150 myForChartParser.setChartWidth(getWidth());
151 myForChartParser.setToDir(toDir.getAbsolutePath() + "/");
152 myForChartParser.setMovingAverage(movingAverage);
153 myForChartParser.setSummaryOnly(summaryOnly);
154 myForChartParser.setSummaryStyle(summaryType);
155 myForChartParser.setAcceptedStyle(type);
156 myForChartParser.setQuiet(quiet);
157 myForChartParser.setFilePrefix(getFilePrefix());
158 myForChartParser.setXAxisSummaryTitle(xAxisSummaryTitle);
159 myForChartParser.setYAxisSummaryTitle(yAxisSummaryTitle);
160 myForChartParser.setXAxisTitle(xAxisTitle);
161 myForChartParser.setYAxisTitle(yAxisTitle);
162
163
164
165 try {
166 SAXParserFactory factory = SAXParserFactory.newInstance();
167 SAXParser saxParser = factory.newSAXParser();
168
169 if (!quiet) {
170 log("Parsing " + inputFile);
171 }
172
173 saxParser.parse(new InputSource(new FileInputStream(inputFile)), myForChartParser);
174
175
176
177
178
179
180
181
182
183 } catch (SAXException e) {
184 log(e.toString());
185 } catch (FileNotFoundException e) {
186 log(e.toString());
187 } catch (IOException e) {
188 log(e.toString());
189 } catch (ParserConfigurationException e) {
190 log(e.toString());
191 }
192 }
193
194 /**
195 * Validates the parameters supplied by ant/maven.
196 */
197 private void validate() {
198 if (inputFile == null) {
199 throw new BuildException("inputFile is mandatory");
200 }
201
202 if (toDir == null) {
203 throw new BuildException("toDir is mandatory");
204 }
205
206 if (!inputFile.canRead()) {
207 final String message = "Unable to read from " + inputFile + ".";
208
209 throw new BuildException(message);
210 }
211
212 if (toDir.exists() && !toDir.isDirectory()) {
213 final String message = "toDir must be a directory (" + toDir + ")";
214
215 throw new BuildException(message);
216 }
217
218 if (!toDir.exists()) {
219 if (!quiet) {
220 log("Creating directory: " + toDir.toString());
221 }
222
223 toDir.mkdir();
224 }
225 }
226
227 /**
228 * @param statisticsType
229 * the statistics type to take into account.
230 */
231 public final void setType(final String statisticsType) {
232 this.type = statisticsType;
233 }
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 this.inputFile = statisticsFile;
243 }
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 this.toDir = targetDirectory;
254 }
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 this.quiet = beQuiet;
264 }
265
266 /**
267 * @return the chart width in pixels.
268 */
269 public final int getWidth() {
270 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 this.width = chartWidth;
281 }
282
283 /**
284 * @return the chart height in pixels.
285 */
286 public final int getHeight() {
287 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 this.height = chartHeight;
298 }
299
300 /**
301 * @return number of points taken for form the moving average line.
302 */
303 public final int getMovingAverage() {
304 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 this.movingAverage = aMovingAverage;
316 }
317
318 /**
319 * @return if true, only a summary chart will be generated (default).
320 */
321 public final boolean isSummaryOnly() {
322 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 this.summaryOnly = onlySummary;
334 }
335
336 /**
337 * @return Returns the filePrefix.
338 */
339 public final String getFilePrefix() {
340 return filePrefix;
341 }
342
343 /**
344 * @param prefix
345 * The filePrefix to set.
346 */
347 public final void setFilePrefix(final String prefix) {
348 this.filePrefix = prefix;
349 }
350
351 /**
352 * @return Returns the summaryType.
353 */
354 public String getSummaryType() {
355 return summaryType;
356 }
357
358 /**
359 * @param theSummaryType
360 * The summaryType to set.
361 */
362 public void setSummaryType(final String theSummaryType) {
363 this.summaryType = theSummaryType;
364 }
365
366 /**
367 * @return Returns the xAxisSummaryTitle.
368 */
369 public String getXAxisSummaryTitle() {
370 return xAxisSummaryTitle;
371 }
372
373 /**
374 * @param axisSummaryTitle The xAxisSummaryTitle to set.
375 */
376 public void setXAxisSummaryTitle(String axisSummaryTitle) {
377 xAxisSummaryTitle = axisSummaryTitle;
378 }
379
380 /**
381 * @return Returns the xAxisTitle.
382 */
383 public String getXAxisTitle() {
384 return xAxisTitle;
385 }
386
387 /**
388 * @param axisTitle The xAxisTitle to set.
389 */
390 public void setXAxisTitle(String axisTitle) {
391 xAxisTitle = axisTitle;
392 }
393
394 /**
395 * @return Returns the yAxisSummaryTitle.
396 */
397 public String getYAxisSummaryTitle() {
398 return yAxisSummaryTitle;
399 }
400
401 /**
402 * @param axisSummaryTitle The yAxisSummaryTitle to set.
403 */
404 public void setYAxisSummaryTitle(String axisSummaryTitle) {
405 yAxisSummaryTitle = axisSummaryTitle;
406 }
407
408 /**
409 * @return Returns the yAxisTitle.
410 */
411 public String getYAxisTitle() {
412 return yAxisTitle;
413 }
414
415 /**
416 * @param axisTitle The yAxisTitle to set.
417 */
418 public void setYAxisTitle(String axisTitle) {
419 yAxisTitle = axisTitle;
420 }
421 }
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437