View Javadoc
1   package org.apache.maven.reporting;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.doxia.sink.Sink;
23  import org.apache.maven.doxia.sink.SinkFactory;
24  import org.apache.maven.doxia.sink.render.RenderingContext;
25  import org.apache.maven.doxia.site.decoration.DecorationModel;
26  import org.apache.maven.doxia.siterenderer.Renderer;
27  import org.apache.maven.doxia.siterenderer.RendererException;
28  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
29  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
30  import org.apache.maven.plugin.AbstractMojo;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugins.annotations.Component;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.project.MavenProject;
35  import org.apache.maven.shared.utils.io.IOUtil;
36  import org.codehaus.plexus.util.ReaderFactory;
37  
38  import java.io.File;
39  import java.io.FileOutputStream;
40  import java.io.IOException;
41  import java.io.OutputStreamWriter;
42  import java.io.Writer;
43  import java.util.HashMap;
44  import java.util.Locale;
45  import java.util.Map;
46  
47  /**
48   * The basis for a Maven report which can be generated both as part of a site generation or
49   * as a direct standalone goal invocation.
50   * Both invocations are delegated to <code>abstract executeReport( Locale )</code> from:
51   * <ul> 
52   * <li>Mojo's <code>execute()</code> method, see maven-plugin-api</li>
53   * <li>MavenMultiPageReport's <code>generate( Sink, SinkFactory, Locale )</code>, see maven-reporting-api</li>
54   * </ul>
55   *
56   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
57   * @version $Id: AbstractMavenReport.java 1624794 2014-09-14 00:30:58Z hboutemy $
58   * @since 2.0
59   * @see #execute() <code>Mojo.execute()</code>, from maven-plugin-api 
60   * @see #generate(Sink, SinkFactory, Locale) <code>MavenMultiPageReport.generate( Sink, SinkFactory, Locale )</code>,
61   *  from maven-reporting-api
62   * @see #executeReport(Locale) <code>abstract executeReport( Locale )</code>
63   */
64  public abstract class AbstractMavenReport
65      extends AbstractMojo
66      implements MavenMultiPageReport
67  {
68      /**
69       * The output directory for the report. Note that this parameter is only evaluated if the goal is run directly from
70       * the command line. If the goal is run indirectly as part of a site generation, the output directory configured in
71       * the Maven Site Plugin is used instead.
72       */
73      @Parameter( defaultValue = "${project.reporting.outputDirectory}", readonly = true, required = true )
74      protected File outputDirectory;
75  
76      /**
77       * The Maven Project.
78       */
79      @Parameter( defaultValue = "${project}", readonly = true, required = true )
80      protected MavenProject project;
81  
82      /**
83       * Specifies the input encoding.
84       */
85      @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}", readonly = true )
86      private String inputEncoding;
87  
88      /**
89       * Specifies the output encoding.
90       */
91      @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}", readonly = true )
92      private String outputEncoding;
93  
94      /**
95       * Doxia Site Renderer component.
96       */
97      @Component
98      protected Renderer siteRenderer;
99  
100     /** The current sink to use */
101     private Sink sink;
102 
103     /** The sink factory to use */
104     private SinkFactory sinkFactory;
105 
106     /** The current report output directory to use */
107     private File reportOutputDirectory;
108 
109     /**
110      * This method is called when the report generation is invoked directly as a standalone Mojo.
111      *
112      * @throws MojoExecutionException if an error occurs when generating the report
113      * @see org.apache.maven.plugins.site.ReportDocumentRender
114      * @see org.apache.maven.plugin.Mojo#execute()
115      */
116     public void execute()
117         throws MojoExecutionException
118     {
119         if ( !canGenerateReport() )
120         {
121             return;
122         }
123 
124         Writer writer = null;
125         try
126         {
127             File outputDirectory = new File( getOutputDirectory() );
128 
129             String filename = getOutputName() + ".html";
130 
131             Locale locale = Locale.getDefault();
132 
133             SiteRenderingContext siteContext = new SiteRenderingContext();
134             siteContext.setDecoration( new DecorationModel() );
135             siteContext.setTemplateName( "org/apache/maven/doxia/siterenderer/resources/default-site.vm" );
136             siteContext.setLocale( locale );
137             siteContext.setTemplateProperties( getTemplateProperties() );
138 
139             RenderingContext context = new RenderingContext( outputDirectory, filename );
140 
141             SiteRendererSink sink = new SiteRendererSink( context );
142 
143             generate( sink, null, locale );
144 
145             if ( !isExternalReport() ) // MSHARED-204: only render Doxia sink if not an external report
146             {
147                 outputDirectory.mkdirs();
148 
149                 writer =
150                     new OutputStreamWriter( new FileOutputStream( new File( outputDirectory, filename ) ),
151                                             getOutputEncoding() );
152 
153                 getSiteRenderer().generateDocument( writer, sink, siteContext );
154 
155                 //getSiteRenderer().copyResources( siteContext, new File( project.getBasedir(), "src/site/resources" ),
156                 //                            outputDirectory );
157             }
158         }
159         catch ( RendererException e )
160         {
161             throw new MojoExecutionException(
162                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
163         }
164         catch ( IOException e )
165         {
166             throw new MojoExecutionException(
167                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
168         }
169         catch ( MavenReportException e )
170         {
171             throw new MojoExecutionException(
172                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
173         }
174         finally
175         {
176             IOUtil.close( writer );
177         }
178     }
179 
180     /**
181      * create template properties like done in maven-site-plugin's
182      * <code>AbstractSiteRenderingMojo.createSiteRenderingContext( Locale )</code>
183      * @return properties
184      */
185     private Map<String, Object> getTemplateProperties()
186     {
187         Map<String, Object> templateProperties = new HashMap<String, Object>();
188         templateProperties.put( "project", getProject() );
189         templateProperties.put( "inputEncoding", getInputEncoding() );
190         templateProperties.put( "outputEncoding", getOutputEncoding() );
191         // Put any of the properties in directly into the Velocity context
192         for ( Map.Entry<Object, Object> entry : getProject().getProperties().entrySet() )
193         {
194             templateProperties.put( (String) entry.getKey(), entry.getValue() );
195         }
196         return templateProperties;
197     }
198 
199     /**
200      * Generate a report.
201      *
202      * @param aSink the sink to use for the generation.
203      * @param aLocale the wanted locale to generate the report, could be null.
204      * @throws MavenReportException if any
205      * @deprecated use {@link #generate(Sink, SinkFactory, Locale)} instead.
206      */
207     public void generate( org.codehaus.doxia.sink.Sink aSink, Locale aLocale )
208         throws MavenReportException
209     {
210         generate( aSink, null, aLocale );
211     }
212 
213     /**
214      * Generate a report.
215      *
216      * @param aSink
217      * @param aLocale
218      * @throws MavenReportException
219      * @see org.apache.maven.reporting.MavenReport#generate(org.apache.maven.doxia.sink.Sink, java.util.Locale)
220      * @deprecated use {@link #generate(Sink, SinkFactory, Locale)} instead.
221      */
222     public void generate( Sink aSink, Locale aLocale )
223         throws MavenReportException
224     {
225         generate( aSink, null, aLocale );
226     }
227 
228     /**
229      * This method is called when the report generation is invoked by maven-site-plugin.
230      *
231      * @param aSink
232      * @param aSinkFactory
233      * @param aLocale
234      * @throws MavenReportException
235      */
236     public void generate( Sink aSink, SinkFactory aSinkFactory, Locale aLocale )
237         throws MavenReportException
238     {
239         if ( aSink == null )
240         {
241             throw new MavenReportException( "You must specify a sink." );
242         }
243 
244         if ( !canGenerateReport() )
245         {
246             getLog().info( "This report cannot be generated as part of the current build. "
247                            + "The report name should be referenced in this line of output." );
248             return;
249         }
250 
251         this.sink = aSink;
252 
253         this.sinkFactory = aSinkFactory;
254 
255         executeReport( aLocale );
256 
257         closeReport();
258     }
259 
260     /**
261      * {@inheritDoc}
262      * @return CATEGORY_PROJECT_REPORTS
263      */
264     public String getCategoryName()
265     {
266         return CATEGORY_PROJECT_REPORTS;
267     }
268 
269     /** {@inheritDoc} */
270     public File getReportOutputDirectory()
271     {
272         if ( reportOutputDirectory == null )
273         {
274             reportOutputDirectory = new File( getOutputDirectory() );
275         }
276 
277         return reportOutputDirectory;
278     }
279 
280     /** {@inheritDoc} */
281     public void setReportOutputDirectory( File reportOutputDirectory )
282     {
283         this.reportOutputDirectory = reportOutputDirectory;
284         this.outputDirectory = reportOutputDirectory;
285     }
286 
287     protected String getOutputDirectory()
288     {
289         return outputDirectory.getAbsolutePath();
290     }
291 
292     protected MavenProject getProject()
293     {
294         return project;
295     }
296 
297     protected Renderer getSiteRenderer()
298     {
299         return siteRenderer;
300     }
301 
302     /**
303      * Gets the input files encoding.
304      *
305      * @return The input files encoding, never <code>null</code>.
306      */
307     protected String getInputEncoding()
308     {
309         return ( inputEncoding == null ) ? ReaderFactory.ISO_8859_1 : inputEncoding;
310     }
311 
312     /**
313      * Gets the effective reporting output files encoding.
314      *
315      * @return The effective reporting output file encoding, never <code>null</code>.
316      */
317     protected String getOutputEncoding()
318     {
319         return ( outputEncoding == null ) ? ReaderFactory.UTF_8 : outputEncoding;
320     }
321 
322     /**
323      * Actions when closing the report.
324      */
325     protected void closeReport()
326     {
327         getSink().close();
328     }
329 
330     /**
331      * @return the sink used
332      */
333     public Sink getSink()
334     {
335         return sink;
336     }
337 
338     /**
339      * @return the sink factory used
340      */
341     public SinkFactory getSinkFactory()
342     {
343         return sinkFactory;
344     }
345 
346     /**
347      * @see org.apache.maven.reporting.MavenReport#isExternalReport()
348      * @return <tt>false</tt> by default.
349      */
350     public boolean isExternalReport()
351     {
352         return false;
353     }
354 
355     /** {@inheritDoc} */
356     public boolean canGenerateReport()
357     {
358         return true;
359     }
360 
361     /**
362      * Execute the generation of the report.
363      *
364      * @param locale the wanted locale to return the report's description, could be <code>null</code>.
365      * @throws MavenReportException if any
366      */
367     protected abstract void executeReport( Locale locale )
368         throws MavenReportException;
369 }