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