588
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.quality.ant.taskdefs;
|
|
19 |
|
628
|
20 |
import java.io.BufferedReader;
|
|
21 |
import java.io.BufferedWriter;
|
|
22 |
import java.io.FileNotFoundException;
|
|
23 |
import java.io.FileOutputStream;
|
|
24 |
import java.io.FileReader;
|
|
25 |
import java.io.IOException;
|
|
26 |
import java.io.OutputStreamWriter;
|
|
27 |
import java.io.UnsupportedEncodingException;
|
|
28 |
import java.util.regex.Matcher;
|
|
29 |
import java.util.regex.Pattern;
|
|
30 |
|
588
|
31 |
import org.apache.tools.ant.Project;
|
|
32 |
import org.apache.tools.ant.Task;
|
628
|
33 |
|
|
34 |
/**
|
|
35 |
* This Task searches the index.html files created by CMT and looks for the summary values at the
|
|
36 |
* end of the file e.g. Files: 12 LOCphy: 137. and takes the info and places it in an XMl file ready
|
|
37 |
* to be sent to diamonds. The Xml file is placed in the diamonds folder in the output folder and
|
|
38 |
* these files are then sent to diamonds when the build is finished soall we have to do is create
|
|
39 |
* the file in the correct folder.
|
|
40 |
*
|
588
|
41 |
* <pre>
|
|
42 |
* <hlm:cmtmetadatainput
|
628
|
43 |
* diamondsHeaderFileName="C:\brtaylor\1\diamonds_header.xml"
|
|
44 |
* diamondsFooterFileName="C:\brtaylor\1\diamonds_footer.xml"
|
|
45 |
* outptuFile="Z:\output\diamonds/cmt_summary_componentName_1.xml/"
|
|
46 |
* inputFile="Z:\output\logs/minibuild_ido_0.0.03_test_cmt_componentName1_cmt/CMTHTML/index.html/>
|
588
|
47 |
* </pre>
|
|
48 |
*
|
|
49 |
* @ant.task name="cmtsummarytask" category="Quality"
|
|
50 |
*/
|
628
|
51 |
|
588
|
52 |
public class CMTSummaryTask extends Task {
|
|
53 |
|
628
|
54 |
// The following 2 variables are indexes to the SEARCH_TERMS_ARRAY
|
|
55 |
/** Index to the searched for text */
|
588
|
56 |
private static final int INPUT_TEXT_INDEX = 0;
|
628
|
57 |
/** Index to the text to write to the xml file */
|
588
|
58 |
private static final int OUTPUT_ELEMENT_INDEX = 1;
|
|
59 |
|
628
|
60 |
/**
|
|
61 |
* this array contains the text to be searched for in the inputFile (1st element) and the text
|
|
62 |
* to be written to the .xml output file (2nd element)
|
|
63 |
*/
|
588
|
64 |
private static final String[][] SEARCH_TERMS_ARRAY = {
|
628
|
65 |
// 1st elem 2nd elem
|
|
66 |
{ "Files:", "files" }, { "LOCphy:", "locphy" }, { "LOCbl:", "locbl" },
|
|
67 |
{ "LOCpro:", "locpro" }, { "LOCcom:", "loccom" }, { "v(G) :", "vg" },
|
|
68 |
{ "MI without comments :", "mi_wo_comments" },
|
|
69 |
{ "MI comment weight :", "mi_comment_weight" }, { "MI:", "mi" } };
|
588
|
70 |
|
628
|
71 |
/** always accesses the 1st group of the matcher */
|
|
72 |
private static int matcherGroupNum; // default init is 0
|
588
|
73 |
|
628
|
74 |
/** indexes to the SEARCH_TERMS_ARRAY */
|
588
|
75 |
private int arrayIndex;
|
628
|
76 |
|
588
|
77 |
private int sizeArray = SEARCH_TERMS_ARRAY.length;
|
|
78 |
|
628
|
79 |
/** the file containing the CMT summary data */
|
588
|
80 |
private String inputFile;
|
628
|
81 |
/** name of file to write the output to */
|
588
|
82 |
private String outputFile;
|
628
|
83 |
|
|
84 |
/** each line of the input file is read into this */
|
|
85 |
private String line;
|
|
86 |
/** the regex for a string of digits */
|
588
|
87 |
private Pattern digitPattern = Pattern.compile("\\d+");
|
|
88 |
|
628
|
89 |
/** file descriptor for the input file */
|
588
|
90 |
private BufferedReader inputFileReader;
|
|
91 |
|
628
|
92 |
/** file handler used to write the summary numbers to the output file **/
|
|
93 |
private BufferedWriter output; // default init = null
|
588
|
94 |
|
628
|
95 |
/** tells the main method whether it should be looking for digist or text */
|
|
96 |
private boolean lineStartsWithDigits; // default init = false
|
588
|
97 |
|
628
|
98 |
/**
|
|
99 |
* file name of the default diamonds XML file header part (1st few lines) used so not writing
|
|
100 |
* the XML text here
|
|
101 |
*/
|
588
|
102 |
private String diamondsHeaderFileName;
|
628
|
103 |
/**
|
|
104 |
* file name of the default diamonds XML file footer part (last line) used so not writing the
|
|
105 |
* XML text here
|
|
106 |
*/
|
588
|
107 |
private String diamondsFooterFileName;
|
|
108 |
|
|
109 |
/**
|
628
|
110 |
* @param outputFile set the output file name
|
588
|
111 |
* @ant.required
|
|
112 |
*/
|
|
113 |
public void setOutputFile(String outputFile) {
|
|
114 |
this.outputFile = outputFile;
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
628
|
118 |
* @return the outputFile the output file name
|
588
|
119 |
*/
|
|
120 |
public String getOutputFile() {
|
|
121 |
return outputFile;
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* @return the inputFile
|
|
126 |
*/
|
|
127 |
public String getinputFile() {
|
|
128 |
return inputFile;
|
|
129 |
}
|
|
130 |
|
|
131 |
/**
|
628
|
132 |
* @param inputFile the name of file to scan and extract data from
|
588
|
133 |
* @ant.required
|
|
134 |
*/
|
|
135 |
public void setinputFile(String inputFile) {
|
|
136 |
this.inputFile = inputFile;
|
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
628
|
140 |
* @param diamondsFooterFileName set the diamonds footer file name
|
588
|
141 |
* @ant.required
|
|
142 |
*/
|
|
143 |
public void setdiamondsFooterFileName(String diamondsFooterFileName) {
|
|
144 |
this.diamondsFooterFileName = diamondsFooterFileName;
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
628
|
148 |
* @return the diamondsFooterFileName the diamonds footer file name
|
588
|
149 |
*/
|
|
150 |
public String getdiamondsFooterFileName() {
|
|
151 |
return diamondsFooterFileName;
|
|
152 |
}
|
|
153 |
|
|
154 |
/**
|
628
|
155 |
* @param diamondsHeaderFileName set the diamonds header file name
|
588
|
156 |
* @ant.required
|
|
157 |
*/
|
|
158 |
public void setdiamondsHeaderFileName(String diamondsHeaderFileName) {
|
|
159 |
this.diamondsHeaderFileName = diamondsHeaderFileName;
|
|
160 |
}
|
|
161 |
|
|
162 |
/**
|
628
|
163 |
* @return the diamondsFooterFileName the diamonds footer file name
|
588
|
164 |
*/
|
|
165 |
public String getdiamondsHeaderFileName() {
|
|
166 |
return diamondsHeaderFileName;
|
|
167 |
}
|
|
168 |
|
628
|
169 |
/** the main part of the code - the method that is called */
|
588
|
170 |
public void execute() {
|
|
171 |
log("CMTSummaryTask execute method with input file : " + inputFile, Project.MSG_ERR);
|
|
172 |
boolean inputFileFound = true;
|
|
173 |
BufferedReader diamondsHeaderFile;
|
|
174 |
BufferedReader diamondsFooterFile;
|
628
|
175 |
|
588
|
176 |
log("output File is " + getOutputFile(), Project.MSG_ERR);
|
628
|
177 |
|
588
|
178 |
try {
|
628
|
179 |
// open the file with the CMT results init
|
588
|
180 |
inputFileReader = new BufferedReader(new FileReader(inputFile));
|
628
|
181 |
}
|
|
182 |
catch (FileNotFoundException exc) {
|
|
183 |
log("FileNotFoundException while getting the input file. : " + inputFile + " "
|
|
184 |
+ exc.getMessage(), Project.MSG_ERR);
|
|
185 |
inputFileFound = false; // stops an empty output file being created.
|
588
|
186 |
}
|
|
187 |
if (inputFileFound) {
|
|
188 |
try {
|
628
|
189 |
// write the title stuff for the XML diamonds schema
|
588
|
190 |
output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(getOutputFile()), "UTF8"));
|
628
|
191 |
}
|
|
192 |
catch (FileNotFoundException exc) {
|
|
193 |
log("FileNotFoundException while getting the output file. : " + getOutputFile()
|
|
194 |
+ " " + exc.getMessage(), Project.MSG_ERR);
|
|
195 |
}
|
|
196 |
catch (UnsupportedEncodingException exc) {
|
588
|
197 |
// We are Ignoring the errors as no need to fail the build.
|
628
|
198 |
log("UnsupportedEncodingException while creating the output file : "
|
|
199 |
+ getOutputFile() + " " + exc.getMessage(), Project.MSG_ERR);
|
588
|
200 |
}
|
628
|
201 |
catch (SecurityException exc) {
|
|
202 |
// We are Ignoring the errors as no need to fail the build.
|
|
203 |
log("SecurityException while creating the output file : " + getOutputFile() + " "
|
|
204 |
+ exc.getMessage(), Project.MSG_ERR);
|
|
205 |
}
|
|
206 |
|
588
|
207 |
if (output != null) {
|
628
|
208 |
// managed to open the output file
|
588
|
209 |
try {
|
628
|
210 |
// write the initial XML text to the file
|
588
|
211 |
String tempLine;
|
|
212 |
diamondsHeaderFile = null;
|
|
213 |
diamondsHeaderFile = new BufferedReader(new FileReader(getdiamondsHeaderFileName()));
|
|
214 |
while ((tempLine = diamondsHeaderFile.readLine()) != null) {
|
628
|
215 |
output.write(tempLine);
|
|
216 |
output.newLine();
|
588
|
217 |
}
|
|
218 |
diamondsHeaderFile.close();
|
|
219 |
output.write(" <cmt>\r\n");
|
628
|
220 |
|
588
|
221 |
// CheckStyle:InnerAssignment OFF
|
628
|
222 |
// process each of the searchterms
|
|
223 |
while ((arrayIndex < sizeArray)
|
|
224 |
&& ((line = inputFileReader.readLine()) != null)) {
|
|
225 |
findTextAndOutput(); // read finput file and write the output
|
588
|
226 |
}
|
|
227 |
// CheckStyle:InnerAssignment ON
|
628
|
228 |
|
|
229 |
// write the end of file text
|
588
|
230 |
output.write(" </cmt>");
|
|
231 |
output.newLine();
|
628
|
232 |
|
588
|
233 |
diamondsFooterFile = null;
|
|
234 |
diamondsFooterFile = new BufferedReader(new FileReader(getdiamondsFooterFileName()));
|
|
235 |
while ((tempLine = diamondsFooterFile.readLine()) != null) {
|
628
|
236 |
output.write(tempLine);
|
|
237 |
output.newLine();
|
588
|
238 |
}
|
628
|
239 |
output.close(); // close the output file
|
588
|
240 |
diamondsFooterFile.close();
|
628
|
241 |
}
|
|
242 |
catch (FileNotFoundException exc) {
|
|
243 |
log("FileNotFoundException while getting the diamonds header file : "
|
|
244 |
+ getdiamondsHeaderFileName() + " " + exc.getMessage(), Project.MSG_ERR);
|
|
245 |
}
|
|
246 |
catch (IOException exc) {
|
588
|
247 |
// We are Ignoring the errors as no need to fail the build.
|
628
|
248 |
log("IOException : " + getdiamondsHeaderFileName() + " output file = "
|
|
249 |
+ getOutputFile() + " " + exc.getMessage(), Project.MSG_ERR);
|
|
250 |
}
|
|
251 |
catch (IllegalArgumentException exc) {
|
588
|
252 |
// We are Ignoring the errors as no need to fail the build.
|
628
|
253 |
log("IllegalArgumentException : " + getdiamondsHeaderFileName()
|
|
254 |
+ " output file = " + getOutputFile() + " " + exc.getMessage(), Project.MSG_ERR);
|
588
|
255 |
}
|
628
|
256 |
}
|
|
257 |
else {
|
|
258 |
log("Error: no output File available ", Project.MSG_ERR);
|
588
|
259 |
}
|
628
|
260 |
}
|
|
261 |
else {
|
|
262 |
log("Error: no input File available ", Project.MSG_ERR);
|
|
263 |
}
|
588
|
264 |
}
|
|
265 |
|
|
266 |
/**
|
628
|
267 |
* This is the function that performs the actual file searches and writes the number following
|
|
268 |
* the searched for text to the output XML file
|
|
269 |
*/
|
|
270 |
private void findTextAndOutput() {
|
588
|
271 |
final int colonSkip = 2;
|
|
272 |
byte[] lineBytes = null;
|
|
273 |
int buffLen;
|
|
274 |
int lineLen;
|
|
275 |
int serchTermTextLen;
|
|
276 |
int spacesLen;
|
628
|
277 |
|
588
|
278 |
try {
|
|
279 |
lineLen = line.length();
|
|
280 |
while ((lineLen > 0) && (arrayIndex < sizeArray)) {
|
628
|
281 |
// Process each of the groups of chars in a line
|
588
|
282 |
if (line.startsWith(SEARCH_TERMS_ARRAY[arrayIndex][INPUT_TEXT_INDEX])) {
|
628
|
283 |
// Found the CMT data type we are looking for so look fo r the digits now
|
588
|
284 |
serchTermTextLen = SEARCH_TERMS_ARRAY[arrayIndex][INPUT_TEXT_INDEX].length();
|
|
285 |
buffLen = writeToOutput();
|
|
286 |
if ((buffLen > 0) && (arrayIndex < sizeArray)) {
|
628
|
287 |
// Skip over the digits so can get the next searchterm near start of line
|
588
|
288 |
line = line.substring(line.indexOf(':') + colonSkip + buffLen);
|
|
289 |
spacesLen = removeAnySpaces();
|
628
|
290 |
// Decrease line length for the while loop
|
|
291 |
lineLen = lineLen - (buffLen + serchTermTextLen + spacesLen);
|
|
292 |
}
|
|
293 |
else {
|
|
294 |
// Didn't find the digits so probably at the end of the line and the digits
|
|
295 |
// are on the next line
|
|
296 |
// Convert the line to bytes so we can check for a specific Character
|
588
|
297 |
lineBytes = line.getBytes("UTF-8");
|
|
298 |
int sbLen = lineBytes.length;
|
628
|
299 |
// The last real char (i.e. not the EOL) should be '=' if the digits are on
|
|
300 |
// the next line
|
588
|
301 |
if (lineBytes[sbLen - 1] == '=') {
|
628
|
302 |
// found the '=' so read the next line in
|
588
|
303 |
lineStartsWithDigits = true;
|
|
304 |
lineLen = 0;
|
628
|
305 |
}
|
|
306 |
else {
|
588
|
307 |
log("can't find the '=' at end of line ", Project.MSG_ERR);
|
|
308 |
lineLen = 0;
|
|
309 |
}
|
|
310 |
}
|
628
|
311 |
}
|
|
312 |
else if (lineStartsWithDigits) { // probably got a line with digits on
|
|
313 |
buffLen = writeToOutput(); // definitely got a line with digits on
|
588
|
314 |
lineStartsWithDigits = false;
|
628
|
315 |
if ((buffLen > 0) && (arrayIndex < sizeArray)) {
|
|
316 |
// Now need to get rid of the digits and get to the next
|
|
317 |
// none digits char
|
588
|
318 |
line = line.substring(buffLen);
|
|
319 |
spacesLen = removeAnySpaces();
|
628
|
320 |
// decrease line length for the while loop
|
|
321 |
lineLen = lineLen - (buffLen + spacesLen);
|
|
322 |
}
|
|
323 |
else {
|
588
|
324 |
lineLen = 0;
|
628
|
325 |
log("can't find the digits at beg of line ", Project.MSG_ERR);
|
588
|
326 |
}
|
628
|
327 |
}
|
|
328 |
else {
|
588
|
329 |
lineLen = 0;
|
|
330 |
}
|
|
331 |
}
|
628
|
332 |
}
|
|
333 |
catch (IOException exc) {
|
588
|
334 |
log("IOException Error searching : " + exc.getMessage(), Project.MSG_ERR);
|
|
335 |
}
|
|
336 |
}
|
|
337 |
|
628
|
338 |
private int writeToOutput() throws IOException {
|
588
|
339 |
String buffer;
|
|
340 |
int buffLen;
|
|
341 |
|
|
342 |
Matcher componentMatch = digitPattern.matcher(line);
|
|
343 |
if (componentMatch.find()) {
|
628
|
344 |
// Found the digits after the search term
|
588
|
345 |
buffer = componentMatch.group(matcherGroupNum);
|
628
|
346 |
// Write the XML formated <files>nn</files> to the output file (plus the other)
|
588
|
347 |
output.write(" <" + SEARCH_TERMS_ARRAY[arrayIndex][OUTPUT_ELEMENT_INDEX] + ">");
|
|
348 |
output.write(buffer);
|
|
349 |
output.write("</" + SEARCH_TERMS_ARRAY[arrayIndex++][OUTPUT_ELEMENT_INDEX] + ">");
|
|
350 |
output.newLine();
|
|
351 |
buffLen = buffer.length();
|
628
|
352 |
}
|
|
353 |
else {
|
588
|
354 |
buffLen = 0;
|
628
|
355 |
log("can't find digits may be '=' at end of line ", Project.MSG_ERR);
|
588
|
356 |
}
|
|
357 |
return buffLen;
|
|
358 |
}
|
628
|
359 |
|
588
|
360 |
private int removeAnySpaces() throws UnsupportedEncodingException {
|
|
361 |
byte[] lineBytes;
|
|
362 |
int num = 0;
|
|
363 |
int len = line.length();
|
628
|
364 |
// Convert the line to bytes so we can check for a specific Character
|
588
|
365 |
lineBytes = line.getBytes("UTF-8");
|
628
|
366 |
// While there are still spaces at the front of the line shuffle the line along to get rid
|
|
367 |
// of them
|
588
|
368 |
while ((lineBytes[num++] == ' ') && (len > 0)) {
|
628
|
369 |
// Remove 1 space at front of line
|
|
370 |
line = line.substring(1);
|
588
|
371 |
len--;
|
|
372 |
}
|
|
373 |
return num;
|
|
374 |
}
|
628
|
375 |
}
|