Coverage Report - org.apache.maven.plugins.site.AbstractSiteRenderingMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSiteRenderingMojo
1%
1/100
0%
0/42
6,4
 
 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: org.apache.maven.plugins.site.AbstractSiteRenderingMojo.html 816550 2012-05-08 11:52:49Z hboutemy $
 52  
  */
 53  1
 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  0
         List filteredReports = new ArrayList( reports.size() );
 171  0
         for ( Iterator i = reports.iterator(); i.hasNext(); )
 172  
         {
 173  0
             MavenReport report = (MavenReport) i.next();
 174  
             //noinspection ErrorNotRethrown,UnusedCatchParameter
 175  
             try
 176  
             {
 177  0
                 if ( report.canGenerateReport() )
 178  
                 {
 179  0
                     filteredReports.add( report );
 180  
                 }
 181  
             }
 182  0
             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  0
                 getLog().warn(
 189  
                                "Error loading report " + report.getClass().getName()
 190  
                                    + " - AbstractMethodError: canGenerateReport()" );
 191  0
                 filteredReports.add( report );
 192  0
             }
 193  
         }
 194  0
         return filteredReports;
 195  
     }
 196  
 
 197  
     protected SiteRenderingContext createSiteRenderingContext( Locale locale )
 198  
         throws MojoExecutionException, IOException, MojoFailureException
 199  
     {
 200  0
         if ( attributes == null )
 201  
         {
 202  0
             attributes = new HashMap();
 203  
         }
 204  
 
 205  0
         if ( attributes.get( "project" ) == null )
 206  
         {
 207  0
             attributes.put( "project", project );
 208  
         }
 209  
 
 210  0
         if ( attributes.get( "inputEncoding" ) == null )
 211  
         {
 212  0
             attributes.put( "inputEncoding", getInputEncoding() );
 213  
         }
 214  
 
 215  0
         if ( attributes.get( "outputEncoding" ) == null )
 216  
         {
 217  0
             attributes.put( "outputEncoding", getOutputEncoding() );
 218  
         }
 219  
 
 220  
         // Put any of the properties in directly into the Velocity context
 221  0
         attributes.putAll( project.getProperties() );
 222  
 
 223  
         DecorationModel decorationModel;
 224  
         try
 225  
         {
 226  0
             decorationModel = siteTool.getDecorationModel( project, reactorProjects, localRepository, repositories,
 227  
                                                            toRelative( project.getBasedir(),
 228  
                                                                        siteDirectory.getAbsolutePath() ),
 229  
                                                            locale, getInputEncoding(), getOutputEncoding() );
 230  
         }
 231  0
         catch ( SiteToolException e )
 232  
         {
 233  0
             throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
 234  0
         }
 235  0
         if ( template != null )
 236  
         {
 237  0
             if ( templateFile != null )
 238  
             {
 239  0
                 getLog().warn( "'template' configuration is ignored when 'templateFile' is set" );
 240  
             }
 241  
             else
 242  
             {
 243  0
                 templateFile = new File( templateDirectory, template );
 244  
             }
 245  
         }
 246  
 
 247  
         File skinFile;
 248  
         try
 249  
         {
 250  0
             skinFile = siteTool.getSkinArtifactFromRepository( localRepository, repositories, decorationModel )
 251  
                 .getFile();
 252  
         }
 253  0
         catch ( SiteToolException e )
 254  
         {
 255  0
             throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
 256  0
         }
 257  
         SiteRenderingContext context;
 258  0
         if ( templateFile != null )
 259  
         {
 260  0
             if ( !templateFile.exists() )
 261  
             {
 262  0
                 throw new MojoFailureException( "Template file '" + templateFile + "' does not exist" );
 263  
             }
 264  0
             context = siteRenderer.createContextForTemplate( templateFile, skinFile, attributes, decorationModel,
 265  
                                                              project.getName(), locale );
 266  
         }
 267  
         else
 268  
         {
 269  0
             context = siteRenderer.createContextForSkin( skinFile, attributes, decorationModel, project.getName(),
 270  
                                                          locale );
 271  
         }
 272  
 
 273  
         // Generate static site
 274  0
         if ( !locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
 275  
         {
 276  0
             context.addSiteDirectory( new File( siteDirectory, locale.getLanguage() ) );
 277  0
             context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "xdoc" );
 278  0
             context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "fml" );
 279  
         }
 280  
         else
 281  
         {
 282  0
             context.addSiteDirectory( siteDirectory );
 283  0
             context.addModuleDirectory( xdocDirectory, "xdoc" );
 284  0
             context.addModuleDirectory( xdocDirectory, "fml" );
 285  
         }
 286  
 
 287  0
         if ( moduleExcludes != null )
 288  
         {
 289  0
             context.setModuleExcludes( moduleExcludes );
 290  
         }
 291  
 
 292  0
         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  0
         Map reportsByOutputName = new HashMap();
 310  0
         for ( Iterator i = reports.iterator(); i.hasNext(); )
 311  
         {
 312  0
             MavenReport report = (MavenReport) i.next();
 313  
 
 314  0
             String outputName = report.getOutputName() + ".html";
 315  
 
 316  
             // Always add the report to the menu, see MSITE-150
 317  0
             reportsByOutputName.put( report.getOutputName(), report );
 318  
 
 319  0
             if ( documents.containsKey( outputName ) )
 320  
             {
 321  0
                 String displayLanguage = locale.getDisplayLanguage( Locale.ENGLISH );
 322  
 
 323  0
                 getLog().info( "Skipped \"" + report.getName( locale ) + "\" report, file \"" + outputName
 324  
                                    + "\" already exists for the " + displayLanguage + " version." );
 325  0
                 i.remove();
 326  
             }
 327  
             else
 328  
             {
 329  0
                 RenderingContext renderingContext = new RenderingContext( siteDirectory, outputName );
 330  0
                 ReportDocumentRenderer renderer = new ReportDocumentRenderer( report, renderingContext, getLog() );
 331  0
                 documents.put( outputName, renderer );
 332  
             }
 333  
         }
 334  0
         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  0
         Map categories = new HashMap();
 347  0
         for ( Iterator i = reports.iterator(); i.hasNext(); )
 348  
         {
 349  0
             MavenReport report = (MavenReport) i.next();
 350  0
             List categoryReports = (List) categories.get( report.getCategoryName() );
 351  0
             if ( categoryReports == null )
 352  
             {
 353  0
                 categoryReports = new ArrayList();
 354  0
                 categories.put( report.getCategoryName(), categoryReports );
 355  
             }
 356  0
             categoryReports.add( report );
 357  
         }
 358  0
         return categories;
 359  
     }
 360  
 
 361  
     protected Map locateDocuments( SiteRenderingContext context, List reports, Locale locale )
 362  
         throws IOException, RendererException
 363  
     {
 364  0
         Map documents = siteRenderer.locateDocumentFiles( context );
 365  
 
 366  
         // TODO: temporary solution for MSITE-289. We need to upgrade doxia site tools
 367  0
         Map tmp = new HashMap();
 368  0
         for ( Iterator it = documents.keySet().iterator(); it.hasNext(); )
 369  
         {
 370  0
             String key = (String) it.next();
 371  0
             tmp.put( StringUtils.replace( key, "\\", "/" ), documents.get( key ) );
 372  
         }
 373  0
         documents = tmp;
 374  
 
 375  0
         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  0
         Map categories = categoriseReports( reportsByOutputName.values() );
 379  
 
 380  0
         siteTool.populateReportsMenu( context.getDecoration(), locale, categories );
 381  0
         populateReportItems( context.getDecoration(), locale, reportsByOutputName );
 382  
 
 383  0
         if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_INFORMATION ) )
 384  
         {
 385  0
             List categoryReports = (List) categories.get( MavenReport.CATEGORY_PROJECT_INFORMATION );
 386  
 
 387  0
             RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-info.html" );
 388  0
             String title = i18n.getString( "site-plugin", locale, "report.information.title" );
 389  0
             String desc1 = i18n.getString( "site-plugin", locale, "report.information.description1" );
 390  0
             String desc2 = i18n.getString( "site-plugin", locale, "report.information.description2" );
 391  0
             DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
 392  
                                                                              i18n, categoryReports );
 393  
 
 394  0
             if ( !documents.containsKey( renderer.getOutputName() ) )
 395  
             {
 396  0
                 documents.put( renderer.getOutputName(), renderer );
 397  
             }
 398  
             else
 399  
             {
 400  0
                 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
 401  
             }
 402  
         }
 403  
 
 404  0
         if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_REPORTS ) )
 405  
         {
 406  0
             List categoryReports = (List) categories.get( MavenReport.CATEGORY_PROJECT_REPORTS );
 407  0
             RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-reports.html" );
 408  0
             String title = i18n.getString( "site-plugin", locale, "report.project.title" );
 409  0
             String desc1 = i18n.getString( "site-plugin", locale, "report.project.description1" );
 410  0
             String desc2 = i18n.getString( "site-plugin", locale, "report.project.description2" );
 411  0
             DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
 412  
                                                                              i18n, categoryReports );
 413  
 
 414  0
             if ( !documents.containsKey( renderer.getOutputName() ) )
 415  
             {
 416  0
                 documents.put( renderer.getOutputName(), renderer );
 417  
             }
 418  
             else
 419  
             {
 420  0
                 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
 421  
             }
 422  
         }
 423  0
         return documents;
 424  
     }
 425  
 }