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