View Javadoc

1   package org.apache.maven.plugins.site;
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.artifact.factory.ArtifactFactory;
23  import org.apache.maven.artifact.resolver.ArtifactResolver;
24  import org.apache.maven.doxia.sink.render.RenderingContext;
25  import org.apache.maven.doxia.site.decoration.DecorationModel;
26  import org.apache.maven.doxia.site.decoration.inheritance.DecorationModelInheritanceAssembler;
27  import org.apache.maven.doxia.siterenderer.DocumentRenderer;
28  import org.apache.maven.doxia.siterenderer.Renderer;
29  import org.apache.maven.doxia.siterenderer.RendererException;
30  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
31  import org.apache.maven.doxia.tools.SiteToolException;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugin.MojoFailureException;
34  import org.apache.maven.reporting.MavenReport;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.util.ArrayList;
39  import java.util.Collection;
40  import java.util.HashMap;
41  import java.util.Iterator;
42  import java.util.List;
43  import java.util.Locale;
44  import java.util.Map;
45  
46  /**
47   * Base class for site rendering mojos.
48   *
49   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
50   * @version $Id: AbstractSiteRenderingMojo.html 816552 2012-05-08 11:54:37Z hboutemy $
51   */
52  public abstract class AbstractSiteRenderingMojo
53      extends AbstractSiteMojo
54  {
55      /**
56       * Module type exclusion mappings
57       * ex: <code>fml  -> **&#47;*-m1.fml</code>  (excludes fml files ending in '-m1.fml' recursively)
58       * <p/>
59       * The configuration looks like this:
60       * <pre>
61       *   &lt;moduleExcludes&gt;
62       *     &lt;moduleType&gt;filename1.ext,**&#47;*sample.ext&lt;/moduleType&gt;
63       *     &lt;!-- moduleType can be one of 'apt', 'fml' or 'xdoc'. --&gt;
64       *     &lt;!-- The value is a comma separated list of           --&gt;
65       *     &lt;!-- filenames or fileset patterns.                   --&gt;
66       *     &lt;!-- Here's an example:                               --&gt;
67       *     &lt;xdoc&gt;changes.xml,navigation.xml&lt;/xdoc&gt;
68       *   &lt;/moduleExcludes&gt;
69       * </pre>
70       *
71       * @parameter
72       */
73      protected Map moduleExcludes;
74  
75      /**
76       * The component for assembling inheritance.
77       *
78       * @component
79       */
80      protected DecorationModelInheritanceAssembler assembler;
81  
82      /**
83       * The component that is used to resolve additional artifacts required.
84       *
85       * @component
86       */
87      protected ArtifactResolver artifactResolver;
88  
89      /**
90       * Remote repositories used for the project.
91       *
92       * @todo this is used for site descriptor resolution - it should relate to the actual project but for some reason they are not always filled in
93       * @parameter expression="${project.remoteArtifactRepositories}"
94       */
95      protected List repositories;
96  
97      /**
98       * The component used for creating artifact instances.
99       *
100      * @component
101      */
102     protected ArtifactFactory artifactFactory;
103 
104     /**
105      * Directory containing the template page.
106      *
107      * @parameter expression="${templateDirectory}" default-value="src/site"
108      * @deprecated use templateFile or skinning instead
109      */
110     private File templateDirectory;
111 
112     /**
113      * Default template page.
114      *
115      * @parameter expression="${template}"
116      * @deprecated use templateFile or skinning instead
117      */
118     private String template;
119 
120     /**
121      * The location of a Velocity template file to use. When used, skins and the default templates, CSS and images
122      * are disabled. It is highly recommended that you package this as a skin instead.
123      *
124      * @parameter expression="${templateFile}"
125      * @since 2.0-beta-5
126      */
127     private File templateFile;
128 
129     /**
130      * The template properties for rendering the site.
131      *
132      * @parameter expression="${attributes}"
133      */
134     protected Map attributes;
135 
136     /**
137      * Site renderer.
138      *
139      * @component
140      */
141     protected Renderer siteRenderer;
142 
143     /**
144      * @parameter expression="${reports}"
145      * @required
146      * @readonly
147      */
148     protected List reports;
149 
150     /**
151      * Alternative directory for xdoc source, useful for m1 to m2 migration
152      *
153      * @parameter default-value="${basedir}/xdocs"
154      * @deprecated use the standard m2 directory layout
155      */
156     private File xdocDirectory;
157 
158     /**
159      * Directory containing generated documentation.
160      *
161      * @parameter alias="workingDirectory" expression="${project.build.directory}/generated-site"
162      * @required
163      * @todo should we deprecate in favour of reports?
164      */
165     protected File generatedSiteDirectory;
166 
167     protected List filterReports( List reports )
168     {
169         List filteredReports = new ArrayList( reports.size() );
170         for ( Iterator i = reports.iterator(); i.hasNext(); )
171         {
172             MavenReport report = (MavenReport) i.next();
173             //noinspection ErrorNotRethrown,UnusedCatchParameter
174             try
175             {
176                 if ( report.canGenerateReport() )
177                 {
178                     filteredReports.add( report );
179                 }
180             }
181             catch ( AbstractMethodError e )
182             {
183                 // the canGenerateReport() has been added just before the 2.0 release and will cause all the reporting
184                 // plugins with an earlier version to fail (most of the org.codehaus mojo now fails)
185                 // be nice with them, output a warning and don't let them break anything
186 
187                 getLog().warn(
188                                "Error loading report " + report.getClass().getName()
189                                    + " - AbstractMethodError: canGenerateReport()" );
190                 filteredReports.add( report );
191             }
192         }
193         return filteredReports;
194     }
195 
196     protected SiteRenderingContext createSiteRenderingContext( Locale locale )
197         throws MojoExecutionException, IOException, MojoFailureException
198     {
199         if ( attributes == null )
200         {
201             attributes = new HashMap();
202         }
203 
204         if ( attributes.get( "project" ) == null )
205         {
206             attributes.put( "project", project );
207         }
208 
209         if ( attributes.get( "inputEncoding" ) == null )
210         {
211             attributes.put( "inputEncoding", getInputEncoding() );
212         }
213 
214         if ( attributes.get( "outputEncoding" ) == null )
215         {
216             attributes.put( "outputEncoding", getOutputEncoding() );
217         }
218 
219         // Put any of the properties in directly into the Velocity context
220         attributes.putAll( project.getProperties() );
221 
222         DecorationModel decorationModel;
223         try
224         {
225             decorationModel = siteTool.getDecorationModel( project, reactorProjects, localRepository, repositories,
226                                                            toRelative( project.getBasedir(),
227                                                                        siteDirectory.getAbsolutePath() ),
228                                                            locale, getInputEncoding(), getOutputEncoding() );
229         }
230         catch ( SiteToolException e )
231         {
232             throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
233         }
234         if ( template != null )
235         {
236             if ( templateFile != null )
237             {
238                 getLog().warn( "'template' configuration is ignored when 'templateFile' is set" );
239             }
240             else
241             {
242                 templateFile = new File( templateDirectory, template );
243             }
244         }
245 
246         File skinFile;
247         try
248         {
249             skinFile = siteTool.getSkinArtifactFromRepository( localRepository, repositories, decorationModel )
250                 .getFile();
251         }
252         catch ( SiteToolException e )
253         {
254             throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
255         }
256         SiteRenderingContext context;
257         if ( templateFile != null )
258         {
259             if ( !templateFile.exists() )
260             {
261                 throw new MojoFailureException( "Template file '" + templateFile + "' does not exist" );
262             }
263             context = siteRenderer.createContextForTemplate( templateFile, skinFile, attributes, decorationModel,
264                                                              project.getName(), locale );
265         }
266         else
267         {
268             context = siteRenderer.createContextForSkin( skinFile, attributes, decorationModel, project.getName(),
269                                                          locale );
270         }
271 
272         // Generate static site
273         if ( !locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
274         {
275             context.addSiteDirectory( new File( siteDirectory, locale.getLanguage() ) );
276             context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "xdoc" );
277             context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "fml" );
278         }
279         else
280         {
281             context.addSiteDirectory( siteDirectory );
282             context.addModuleDirectory( xdocDirectory, "xdoc" );
283             context.addModuleDirectory( xdocDirectory, "fml" );
284         }
285 
286         if ( moduleExcludes != null )
287         {
288             context.setModuleExcludes( moduleExcludes );
289         }
290 
291         return context;
292     }
293 
294     /**
295      * Go through the list of reports and process each one like this:
296      * <ul>
297      * <li>Add the report to a map of reports keyed by filename having the report itself as value
298      * <li>If the report is not yet in the map of documents, add it together with a suitable renderer
299      * </ul>
300      *
301      * @param reports A List of MavenReports
302      * @param documents A Map of documents, keyed by filename
303      * @param locale the Locale the reports are processed for.
304      * @return A map with all reports keyed by filename having the report itself as value.
305      * The map will be used to populate a menu.
306      */
307     protected Map locateReports( List reports, Map documents, Locale locale )
308     {
309         Map reportsByOutputName = new HashMap();
310         for ( Iterator i = reports.iterator(); i.hasNext(); )
311         {
312             MavenReport report = (MavenReport) i.next();
313 
314             String outputName = report.getOutputName() + ".html";
315 
316             // Always add the report to the menu, see MSITE-150
317             reportsByOutputName.put( report.getOutputName(), report );
318 
319             if ( documents.containsKey( outputName ) )
320             {
321                 String displayLanguage = locale.getDisplayLanguage( Locale.ENGLISH );
322 
323                 getLog().info( "Skipped \"" + report.getName( locale ) + "\" report, file \"" + outputName
324                                    + "\" already exists for the " + displayLanguage + " version." );
325                 i.remove();
326             }
327             else
328             {
329                 RenderingContext renderingContext = new RenderingContext( siteDirectory, outputName );
330                 ReportDocumentRenderer renderer = new ReportDocumentRenderer( report, renderingContext, getLog() );
331                 documents.put( outputName, renderer );
332             }
333         }
334         return reportsByOutputName;
335     }
336 
337     /**
338      * Go through the collection of reports and put each report into a list for the appropriate category. The list is
339      * put into a map keyed by the name of the category.
340      *
341      * @param reports A Collection of MavenReports
342      * @return A map keyed category having the report itself as value
343      */
344     protected Map categoriseReports( Collection reports )
345     {
346         Map categories = new HashMap();
347         for ( Iterator i = reports.iterator(); i.hasNext(); )
348         {
349             MavenReport report = (MavenReport) i.next();
350             List categoryReports = (List) categories.get( report.getCategoryName() );
351             if ( categoryReports == null )
352             {
353                 categoryReports = new ArrayList();
354                 categories.put( report.getCategoryName(), categoryReports );
355             }
356             categoryReports.add( report );
357         }
358         return categories;
359     }
360 
361     protected Map locateDocuments( SiteRenderingContext context, List reports, Locale locale )
362         throws IOException, RendererException
363     {
364         Map documents = siteRenderer.locateDocumentFiles( context );
365 
366         Map reportsByOutputName = locateReports( reports, documents, locale );
367 
368         // TODO: I want to get rid of categories eventually. There's no way to add your own in a fully i18n manner
369         Map categories = categoriseReports( reportsByOutputName.values() );
370 
371         siteTool.populateReportsMenu( context.getDecoration(), locale, categories );
372         populateReportItems( context.getDecoration(), locale, reportsByOutputName );
373 
374         if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_INFORMATION ) )
375         {
376             List categoryReports = (List) categories.get( MavenReport.CATEGORY_PROJECT_INFORMATION );
377 
378             RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-info.html" );
379             String title = i18n.getString( "site-plugin", locale, "report.information.title" );
380             String desc1 = i18n.getString( "site-plugin", locale, "report.information.description1" );
381             String desc2 = i18n.getString( "site-plugin", locale, "report.information.description2" );
382             DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
383                                                                              i18n, categoryReports, getLog() );
384 
385             if ( !documents.containsKey( renderer.getOutputName() ) )
386             {
387                 documents.put( renderer.getOutputName(), renderer );
388             }
389             else
390             {
391                 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
392             }
393         }
394 
395         if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_REPORTS ) )
396         {
397             List categoryReports = (List) categories.get( MavenReport.CATEGORY_PROJECT_REPORTS );
398             RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-reports.html" );
399             String title = i18n.getString( "site-plugin", locale, "report.project.title" );
400             String desc1 = i18n.getString( "site-plugin", locale, "report.project.description1" );
401             String desc2 = i18n.getString( "site-plugin", locale, "report.project.description2" );
402             DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
403                                                                              i18n, categoryReports, getLog() );
404 
405             if ( !documents.containsKey( renderer.getOutputName() ) )
406             {
407                 documents.put( renderer.getOutputName(), renderer );
408             }
409             else
410             {
411                 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
412             }
413         }
414         return documents;
415     }
416 }