View Javadoc

1   package org.apache.maven.jcoveragereport;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
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       * Rewrite all lines of coverage.xml file for obtain a xml well formed
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>", "&lt;init&gt;", 0);
94              line = replace(line, "<clinit>", "&lt;clinit&gt;", 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 }