View Javadoc

1   package org.codehaus.mojo.ounce;
2   
3   import java.io.File;
4   import java.util.HashMap;
5   import java.util.Locale;
6   import java.util.ResourceBundle;
7   
8   import org.apache.maven.doxia.siterenderer.Renderer;
9   import org.apache.maven.plugin.MojoExecutionException;
10  import org.apache.maven.plugin.MojoFailureException;
11  import org.apache.maven.project.MavenProject;
12  import org.apache.maven.reporting.MavenReport;
13  import org.apache.maven.reporting.MavenReportException;
14  import org.codehaus.doxia.sink.Sink;
15  
16  /**
17   * Generate the scan results as part of the site.
18   * 
19   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
20   * @goal report
21   * @phase site
22   */
23  public class ReportMojo
24      extends ScanMojo
25      implements MavenReport
26  {
27      /**
28       * Directory where reports will go.
29       * 
30       * @parameter expression="${project.reporting.outputDirectory}/ounce"
31       * @required
32       * @readonly
33       */
34      private File reportOutputDirectory;
35  
36      /**
37       * Specify the name of an existing assessment for which to generate a report. If not specified, Ounce/Maven scans
38       * the application and generates the report from that assessment.
39       * 
40       * @parameter expression="${ounce.existingAssessmentFile}"
41       */
42      String existingAssessmentFile;
43  
44      /**
45       * The current Project.
46       * 
47       * @parameter expression="${project}"
48       * @readonly
49       */
50      protected MavenProject project;
51  
52      /**
53       * For internal use only.
54       * 
55       * @component
56       * @required
57       * @readonly
58       */
59      private Renderer siteRenderer;
60  
61      protected Renderer getSiteRenderer()
62      {
63          return siteRenderer;
64      }
65  
66      public String getDescription( Locale locale )
67      {
68          return getBundle( locale ).getString( "report.description" );
69      }
70  
71      public String getName( Locale locale )
72      {
73          return getBundle( locale ).getString( "report.name" );
74      }
75  
76      public String getOutputName()
77      {
78          return "Ounce-Analysis/index";
79      }
80  
81      /**
82       * @see org.apache.maven.reporting.MavenReport#isExternalReport()
83       */
84      public boolean isExternalReport()
85      {
86          return true;
87      }
88  
89      private ResourceBundle getBundle( Locale locale )
90      {
91          return ResourceBundle.getBundle( "ounce-report", locale, this.getClass().getClassLoader() );
92      }
93  
94      public void execute()
95          throws MojoExecutionException, MojoFailureException
96      {
97          this.getLog().warn( "Generating..." );
98  
99          if ( existingAssessmentFile != null )
100         {
101             if ( options == null )
102             {
103                 options = new HashMap();
104             }
105             options.put( "existingAssessmentFile", existingAssessmentFile );
106         }
107 
108         super.execute();
109     }
110 
111     public boolean canGenerateReport()
112     {
113         return true;
114     }
115 
116     public void generate( Sink sink, Locale locale )
117         throws MavenReportException
118     {
119         this.getLog().warn( "Generating..." );
120         this.waitForScan = true;
121         if ( this.reportType == null )
122         {
123             this.reportType = "Findings";
124         }
125         if ( this.reportOutputType == null )
126         {
127             this.reportOutputType = "html";
128         }
129         this.reportOutputPath = reportOutputDirectory + File.separator + getOutputName() + ".html";
130 
131         try
132         {
133             super.execute();
134         }
135         catch ( MojoExecutionException e )
136         {
137             throw new MavenReportException( "Execption generating report:", e );
138         }
139         catch ( MojoFailureException e )
140         {
141             throw new MavenReportException( "Execption generating report:", e );
142         }
143     }
144 
145     /**
146      * @see org.apache.maven.reporting.MavenReport#getCategoryName()
147      */
148     public String getCategoryName()
149     {
150         return CATEGORY_PROJECT_REPORTS;
151     }
152 
153     /**
154      * @see org.apache.maven.reporting.MavenReport#getReportOutputDirectory()
155      */
156     public File getReportOutputDirectory()
157     {
158         return this.reportOutputDirectory;
159     }
160 
161     /**
162      * @see org.apache.maven.reporting.MavenReport#setReportOutputDirectory()
163      */
164     public void setReportOutputDirectory( File outputDirectory )
165     {
166         reportOutputDirectory = outputDirectory;
167     }
168 }