Coverage Report - org.apache.maven.plugin.javadoc.JavadocReport
 
Classes in this File Line Coverage Branch Coverage Complexity
JavadocReport
45 %
21/47
18 %
4/22
2,417
 
 1  
 package org.apache.maven.plugin.javadoc;
 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 java.io.File;
 23  
 import java.util.List;
 24  
 import java.util.Locale;
 25  
 import java.util.ResourceBundle;
 26  
 
 27  
 import org.apache.maven.doxia.module.xhtml.decoration.render.RenderingContext;
 28  
 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
 29  
 import org.apache.maven.plugin.MojoExecutionException;
 30  
 import org.apache.maven.plugin.MojoFailureException;
 31  
 import org.apache.maven.reporting.MavenReport;
 32  
 import org.apache.maven.reporting.MavenReportException;
 33  
 import org.codehaus.doxia.sink.Sink;
 34  
 import org.codehaus.plexus.util.StringUtils;
 35  
 
 36  
 /**
 37  
  * Generates documentation for the <code>Java code</code> in the project using the standard
 38  
  * <a href="http://java.sun.com/j2se/javadoc/">Javadoc Tool</a>.
 39  
  *
 40  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 41  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 42  
  * @version $Id: org.apache.maven.plugin.javadoc.JavadocReport.html 829389 2012-08-19 17:23:07Z hboutemy $
 43  
  * @since 2.0
 44  
  * @goal javadoc
 45  
  * @execute phase="generate-sources"
 46  
  * @see <a href="http://java.sun.com/j2se/javadoc/">Javadoc Tool</a>
 47  
  * @see <a href="http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/javadoc.html#options">Javadoc Options</a>
 48  
  */
 49  24
 public class JavadocReport
 50  
     extends AbstractJavadocMojo
 51  
     implements MavenReport
 52  
 {
 53  
     // ----------------------------------------------------------------------
 54  
     // Report Mojo Parameters
 55  
     // ----------------------------------------------------------------------
 56  
 
 57  
     /**
 58  
      * Specifies the destination directory where javadoc saves the generated HTML files.
 59  
      *
 60  
      * @parameter expression="${project.reporting.outputDirectory}/apidocs"
 61  
      * @required
 62  
      */
 63  
     private File reportOutputDirectory;
 64  
 
 65  
     /**
 66  
      * The name of the destination directory.
 67  
      * <br/>
 68  
      *
 69  
      * @since 2.1
 70  
      * @parameter expression="${destDir}" default-value="apidocs"
 71  
      */
 72  
     private String destDir;
 73  
 
 74  
     /**
 75  
      * The name of the Javadoc report.
 76  
      *
 77  
      * @since 2.1
 78  
      * @parameter expression="${name}"
 79  
      */
 80  
     private String name;
 81  
 
 82  
     /**
 83  
      * The description of the Javadoc report.
 84  
      *
 85  
      * @since 2.1
 86  
      * @parameter expression="${description}"
 87  
      */
 88  
     private String description;
 89  
 
 90  
     // ----------------------------------------------------------------------
 91  
     // Report public methods
 92  
     // ----------------------------------------------------------------------
 93  
 
 94  
     /** {@inheritDoc} */
 95  
     public String getName( Locale locale )
 96  
     {
 97  2
         if ( StringUtils.isEmpty( name ) )
 98  
         {
 99  2
             return getBundle( locale ).getString( "report.javadoc.name" );
 100  
         }
 101  
 
 102  0
         return name;
 103  
     }
 104  
 
 105  
     /** {@inheritDoc} */
 106  
     public String getDescription( Locale locale )
 107  
     {
 108  0
         if ( StringUtils.isEmpty( description ) )
 109  
         {
 110  0
             return getBundle( locale ).getString( "report.javadoc.description" );
 111  
         }
 112  
 
 113  0
         return description;
 114  
     }
 115  
 
 116  
     /** {@inheritDoc} */
 117  
     public void generate( Sink sink, Locale locale )
 118  
         throws MavenReportException
 119  
     {
 120  23
         outputDirectory = getReportOutputDirectory();
 121  
 
 122  23
         executeReport( locale );
 123  21
     }
 124  
 
 125  
     /** {@inheritDoc} */
 126  
     public String getOutputName()
 127  
     {
 128  22
         return destDir + "/index";
 129  
     }
 130  
 
 131  
     /** {@inheritDoc} */
 132  
     public boolean isExternalReport()
 133  
     {
 134  0
         return true;
 135  
     }
 136  
 
 137  
     /** {@inheritDoc} */
 138  
     public boolean canGenerateReport()
 139  
     {
 140  
         boolean canGenerate;
 141  0
         if ( aggregate != isAggregator() )
 142  
         {
 143  0
             canGenerate = false;
 144  
         }
 145  0
         else if ( isAggregator() && !project.isExecutionRoot() )
 146  
         {
 147  0
             canGenerate = false;
 148  
         }
 149  
         else
 150  
         {
 151  0
             List sourcePaths = getSourcePaths();
 152  
 
 153  0
             List files = getFiles( sourcePaths );
 154  
 
 155  0
             canGenerate = canGenerateReport( files );
 156  
         }
 157  0
         return canGenerate;
 158  
     }
 159  
 
 160  
     /** {@inheritDoc} */
 161  
     public String getCategoryName()
 162  
     {
 163  0
         return CATEGORY_PROJECT_REPORTS;
 164  
     }
 165  
 
 166  
     /** {@inheritDoc} */
 167  
     public File getReportOutputDirectory()
 168  
     {
 169  22
         if ( reportOutputDirectory == null )
 170  
         {
 171  22
             return outputDirectory;
 172  
         }
 173  
 
 174  0
         return reportOutputDirectory;
 175  
     }
 176  
 
 177  
     /**
 178  
      * Method to set the directory where the generated reports will be put
 179  
      *
 180  
      * @param reportOutputDirectory the directory file to be set
 181  
      */
 182  
     public void setReportOutputDirectory( File reportOutputDirectory )
 183  
     {
 184  0
         if ( ( reportOutputDirectory != null ) && ( !reportOutputDirectory.getAbsolutePath().endsWith( destDir ) ) )
 185  
         {
 186  0
             this.reportOutputDirectory = new File( reportOutputDirectory, destDir );
 187  
         }
 188  
         else
 189  
         {
 190  0
             this.reportOutputDirectory = reportOutputDirectory;
 191  
         }
 192  0
     }
 193  
 
 194  
     /** {@inheritDoc} */
 195  
     public void execute()
 196  
         throws MojoExecutionException, MojoFailureException
 197  
     {
 198  23
         if ( skip )
 199  
         {
 200  0
             getLog().info( "Skipping javadoc generation" );
 201  0
             return;
 202  
         }
 203  
 
 204  
         try
 205  
         {
 206  23
             RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
 207  23
             SiteRendererSink sink = new SiteRendererSink( context );
 208  23
             Locale locale = Locale.getDefault();
 209  23
             generate( sink, locale );
 210  
         }
 211  2
         catch ( MavenReportException e )
 212  
         {
 213  2
             if ( failOnError )
 214  
             {
 215  2
                 throw new MojoExecutionException( "An error has occurred in " + getName( Locale.ENGLISH )
 216  
                     + " report generation:" + e.getMessage(), e );
 217  
             }
 218  
 
 219  0
             getLog().error( "An error has occurred in " + getName( Locale.ENGLISH )
 220  
                     + " report generation:" + e.getMessage(), e );
 221  
         }
 222  0
         catch ( RuntimeException e )
 223  
         {
 224  0
             if ( failOnError )
 225  
             {
 226  0
                 throw e;
 227  
             }
 228  
 
 229  0
             getLog().error( e.getMessage(), e );
 230  21
         }
 231  21
     }
 232  
 
 233  
     /** {@inheritDoc} */
 234  
     protected boolean isAggregator()
 235  
     {
 236  
         // only here for backward compatibility, this flag does not work reliably
 237  79
         return aggregate;
 238  
     }
 239  
 
 240  
     /**
 241  
      * Gets the resource bundle for the specified locale.
 242  
      *
 243  
      * @param locale The locale of the currently generated report.
 244  
      * @return The resource bundle for the requested locale.
 245  
      */
 246  
     private ResourceBundle getBundle( Locale locale )
 247  
     {
 248  2
         return ResourceBundle.getBundle( "javadoc-report", locale, getClass().getClassLoader() );
 249  
     }
 250  
 }