Coverage Report - org.apache.maven.report.projectinfo.AbstractProjectInfoReport
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractProjectInfoReport
63%
22/35
50%
1/2
2,429
 
 1  
 package org.apache.maven.report.projectinfo;
 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.Artifact;
 23  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 24  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 25  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 26  
 import org.apache.maven.doxia.module.xhtml.decoration.render.RenderingContext;
 27  
 import org.apache.maven.doxia.site.decoration.Body;
 28  
 import org.apache.maven.doxia.site.decoration.DecorationModel;
 29  
 import org.apache.maven.doxia.siterenderer.Renderer;
 30  
 import org.apache.maven.doxia.siterenderer.RendererException;
 31  
 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
 32  
 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
 33  
 import org.apache.maven.doxia.tools.SiteTool;
 34  
 import org.apache.maven.doxia.tools.SiteToolException;
 35  
 import org.apache.maven.plugin.MojoExecutionException;
 36  
 import org.apache.maven.project.MavenProject;
 37  
 import org.apache.maven.reporting.AbstractMavenReport;
 38  
 import org.apache.maven.reporting.MavenReportException;
 39  
 import org.codehaus.plexus.i18n.I18N;
 40  
 
 41  
 import java.io.File;
 42  
 import java.io.FileOutputStream;
 43  
 import java.io.IOException;
 44  
 import java.io.OutputStreamWriter;
 45  
 import java.io.Writer;
 46  
 import java.util.HashMap;
 47  
 import java.util.Locale;
 48  
 import java.util.Map;
 49  
 
 50  
 /**
 51  
  * Base class with the things that should be in AbstractMavenReport anyway.
 52  
  *
 53  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 54  
  * @version $Id: AbstractProjectInfoReport.java 728546 2008-12-21 22:56:51Z bentmann $
 55  
  * @since 2.0
 56  
  */
 57  16
 public abstract class AbstractProjectInfoReport
 58  
     extends AbstractMavenReport
 59  
 {
 60  
     // ----------------------------------------------------------------------
 61  
     // Mojo components
 62  
     // ----------------------------------------------------------------------
 63  
 
 64  
     /**
 65  
      * SiteTool component.
 66  
      *
 67  
      * @since 2.1
 68  
      * @component
 69  
      */
 70  
     protected SiteTool siteTool;
 71  
 
 72  
     /**
 73  
      * Doxia Site Renderer component.
 74  
      *
 75  
      * @component
 76  
      */
 77  
     protected Renderer siteRenderer;
 78  
 
 79  
     /**
 80  
      * Artifact Resolver component.
 81  
      *
 82  
      * @component
 83  
      */
 84  
     protected ArtifactResolver resolver;
 85  
 
 86  
     /**
 87  
      * Artifact Factory component.
 88  
      *
 89  
      * @component
 90  
      */
 91  
     protected ArtifactFactory factory;
 92  
 
 93  
     /**
 94  
      * Internationalization component.
 95  
      *
 96  
      * @component
 97  
      */
 98  
     protected I18N i18n;
 99  
 
 100  
     // ----------------------------------------------------------------------
 101  
     // Mojo parameters
 102  
     // ----------------------------------------------------------------------
 103  
 
 104  
     /**
 105  
      * The output directory for the report. Note that this parameter is only evaluated if the goal is run directly from
 106  
      * the command line. If the goal is run indirectly as part of a site generation, the output directory configured in
 107  
      * the Maven Site Plugin is used instead.
 108  
      *
 109  
      * @parameter expression="${project.reporting.outputDirectory}"
 110  
      * @required
 111  
      */
 112  
     protected File outputDirectory;
 113  
 
 114  
     /**
 115  
      * The Maven Project.
 116  
      *
 117  
      * @parameter expression="${project}"
 118  
      * @required
 119  
      * @readonly
 120  
      */
 121  
     protected MavenProject project;
 122  
 
 123  
     /**
 124  
      * Local Repository.
 125  
      *
 126  
      * @parameter expression="${localRepository}"
 127  
      * @required
 128  
      * @readonly
 129  
      */
 130  
     protected ArtifactRepository localRepository;
 131  
 
 132  
     // ----------------------------------------------------------------------
 133  
     // Public methods
 134  
     // ----------------------------------------------------------------------
 135  
 
 136  
     /** {@inheritDoc} */
 137  
     public void execute()
 138  
         throws MojoExecutionException
 139  
     {
 140  16
         if ( !canGenerateReport() )
 141  
         {
 142  0
             return;
 143  
         }
 144  
 
 145  
         // TODO: push to a helper? Could still be improved by taking more of the site information from the site plugin
 146  
         try
 147  
         {
 148  16
             DecorationModel model = new DecorationModel();
 149  16
             model.setBody( new Body() );
 150  16
             Map attributes = new HashMap();
 151  16
             attributes.put( "outputEncoding", "UTF-8" );
 152  16
             attributes.put( "project", project );
 153  16
             Locale locale = Locale.getDefault();
 154  16
             Artifact defaultSkin =
 155  
                 siteTool.getDefaultSkinArtifact( localRepository, project.getRemoteArtifactRepositories() );
 156  16
             SiteRenderingContext siteContext = siteRenderer.createContextForSkin( defaultSkin.getFile(), attributes,
 157  
                                                                                   model, getName( locale ), locale );
 158  
 
 159  16
             RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
 160  
 
 161  16
             SiteRendererSink sink = new SiteRendererSink( context );
 162  16
             generate( sink, locale );
 163  
 
 164  13
             outputDirectory.mkdirs();
 165  
 
 166  13
             Writer writer =
 167  
                 new OutputStreamWriter( new FileOutputStream( new File( outputDirectory, getOutputName() + ".html" ) ),
 168  
                                         "UTF-8" );
 169  
 
 170  13
             siteRenderer.generateDocument( writer, sink, siteContext );
 171  
 
 172  13
             siteRenderer.copyResources( siteContext, new File( project.getBasedir(), "src/site/resources" ),
 173  
                                         outputDirectory );
 174  
         }
 175  0
         catch ( RendererException e )
 176  
         {
 177  0
             throw new MojoExecutionException(
 178  
                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
 179  
         }
 180  0
         catch ( IOException e )
 181  
         {
 182  0
             throw new MojoExecutionException(
 183  
                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
 184  
         }
 185  0
         catch ( SiteToolException e )
 186  
         {
 187  0
             throw new MojoExecutionException(
 188  
                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
 189  
         }
 190  0
         catch ( MavenReportException e )
 191  
         {
 192  0
             throw new MojoExecutionException(
 193  
                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
 194  13
         }
 195  13
     }
 196  
 
 197  
     /** {@inheritDoc} */
 198  
     public String getCategoryName()
 199  
     {
 200  0
         return CATEGORY_PROJECT_INFORMATION;
 201  
     }
 202  
 
 203  
     // ----------------------------------------------------------------------
 204  
     // Protected methods
 205  
     // ----------------------------------------------------------------------
 206  
 
 207  
     /** {@inheritDoc} */
 208  
     protected String getOutputDirectory()
 209  
     {
 210  1
         return outputDirectory.getAbsolutePath();
 211  
     }
 212  
 
 213  
     /** {@inheritDoc} */
 214  
     public File getReportOutputDirectory()
 215  
     {
 216  13
         return outputDirectory;
 217  
     }
 218  
 
 219  
     /** {@inheritDoc} */
 220  
     public void setReportOutputDirectory( File reportOutputDirectory )
 221  
     {
 222  0
         this.outputDirectory = reportOutputDirectory;
 223  0
     }
 224  
 
 225  
     /** {@inheritDoc} */
 226  
     protected MavenProject getProject()
 227  
     {
 228  9
         return project;
 229  
     }
 230  
 
 231  
     /** {@inheritDoc} */
 232  
     protected Renderer getSiteRenderer()
 233  
     {
 234  0
         return siteRenderer;
 235  
     }
 236  
 }