1 package org.apache.maven.jcoveragereport;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.FileReader;
24 import java.io.PrintWriter;
25
26 /**
27 * @author Emmanuel Venisse
28 * @version $Id: CoverageReportGenerator.java 170200 2005-05-15 06:24:19Z brett $
29 */
30 public class CoverageReportGenerator
31 {
32 private String dataFile;
33 private String outputDir;
34
35 public void setDataFile(String dataFile)
36 {
37 this.dataFile = dataFile;
38 }
39
40 public String getDataFile()
41 {
42 return dataFile;
43 }
44
45 public void setOutputDir(String dir)
46 {
47 this.outputDir = dir;
48 }
49
50 public String getOutputDir()
51 {
52 return outputDir;
53 }
54
55 public void execute() throws Exception
56 {
57 System.out.println("Generate report for " + dataFile + " file.");
58 System.out.println("OutputDir = " + outputDir);
59 FileReader fr = null;
60 try
61 {
62 prepareFile();
63 fr = new FileReader(dataFile);
64 CoverageUnmarshaller cum = new CoverageUnmarshaller();
65 Coverage coverage = cum.parse(fr);
66
67 CoverageReport cr = new CoverageReport(coverage);
68 cr.generate(outputDir);
69 }
70 catch (Exception e)
71 {
72 e.printStackTrace();
73 }
74 finally
75 {
76 if (fr != null)
77 {
78 fr.close();
79 }
80 }
81 }
82
83
84
85
86 private void prepareFile() throws Exception
87 {
88 BufferedReader br = new BufferedReader(new FileReader(dataFile));
89 PrintWriter pw = new PrintWriter(new FileOutputStream(dataFile + ".new"));
90 String line;
91 while ((line = br.readLine()) != null)
92 {
93 line = replace(line, "<init>", "<init>", 0);
94 line = replace(line, "<clinit>", "<clinit>", 0);
95 line = replace(line, "<Unknown>", "[Unknown]", 0);
96 pw.println(line);
97 }
98 pw.close();
99 br.close();
100 File fSrc = new File(dataFile);
101 File fNew = new File(dataFile + ".new");
102 fSrc.delete();
103 fNew.renameTo(fSrc);
104 }
105
106 private String replace( String line, String oldString, String newString, int startAt )
107 {
108 int i = startAt;
109 while ((i = line.indexOf(oldString, i)) >= 0)
110 {
111 line = (new StringBuffer().append(line.substring(0, i))
112 .append(newString)
113 .append(line.substring(i + oldString.length()))).toString();
114 i += newString.length();
115 }
116 return line;
117 }
118 }