Coverage Report - org.apache.maven.report.projectinfo.ModulesReport
 
Classes in this File Line Coverage Branch Coverage Complexity
ModulesReport
82 %
23/28
50 %
5/10
2,3
ModulesReport$ModulesRenderer
75 %
27/36
62 %
5/8
2,3
 
 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.doxia.sink.Sink;
 23  
 import org.apache.maven.model.Model;
 24  
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 25  
 import org.apache.maven.plugins.annotations.Mojo;
 26  
 import org.codehaus.plexus.i18n.I18N;
 27  
 import org.codehaus.plexus.util.IOUtil;
 28  
 import org.codehaus.plexus.util.ReaderFactory;
 29  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 30  
 
 31  
 import java.io.File;
 32  
 import java.io.IOException;
 33  
 import java.io.Reader;
 34  
 import java.util.List;
 35  
 import java.util.Locale;
 36  
 
 37  
 /**
 38  
  * Generates the Project Modules report.
 39  
  *
 40  
  * @author ltheussl
 41  
  * @version $Id: ModulesReport.java 1367255 2012-07-30 20:01:21Z hboutemy $
 42  
  * @since 2.2
 43  
  */
 44  
 @Mojo( name = "modules" )
 45  9
 public class ModulesReport
 46  
     extends AbstractProjectInfoReport
 47  
 {
 48  
     // ----------------------------------------------------------------------
 49  
     // Public methods
 50  
     // ----------------------------------------------------------------------
 51  
 
 52  
     @Override
 53  
     public void executeReport( Locale locale )
 54  
     {
 55  1
         new ModulesRenderer( getSink(), getProject().getModel(), getI18N( locale ), locale ).render();
 56  1
     }
 57  
 
 58  
     /** {@inheritDoc} */
 59  
     public String getOutputName()
 60  
     {
 61  2
         return "modules";
 62  
     }
 63  
 
 64  
     @Override
 65  
     protected String getI18Nsection()
 66  
     {
 67  1
         return "modules";
 68  
     }
 69  
 
 70  
     @Override
 71  
     public boolean canGenerateReport()
 72  
     {
 73  2
         return ( getProject().getModel().getModules() != null && !getProject().getModel().getModules().isEmpty() );
 74  
     }
 75  
 
 76  
     // ----------------------------------------------------------------------
 77  
     // Private
 78  
     // ----------------------------------------------------------------------
 79  
 
 80  
     /**
 81  
      * Internal renderer class
 82  
      */
 83  1
     private class ModulesRenderer
 84  
         extends AbstractProjectInfoRenderer
 85  
     {
 86  
         private Model model;
 87  
 
 88  
         ModulesRenderer( Sink sink, Model model, I18N i18n, Locale locale )
 89  1
         {
 90  1
             super( sink, i18n, locale );
 91  
 
 92  1
             this.model = model;
 93  1
         }
 94  
 
 95  
         @Override
 96  
         protected String getI18Nsection()
 97  
         {
 98  5
             return "modules";
 99  
         }
 100  
 
 101  
         @Override
 102  
         public void renderBody()
 103  
         {
 104  1
             List<String> modules = model.getModules();
 105  
 
 106  1
             startSection( getTitle() );
 107  
 
 108  1
             paragraph( getI18nString( "intro" ) );
 109  
 
 110  1
             startTable();
 111  
 
 112  1
             String name = getI18nString( "header.name" );
 113  1
             String description = getI18nString( "header.description" );
 114  1
             tableHeader( new String[] {name, description} );
 115  
 
 116  1
             for ( String module : modules )
 117  
             {
 118  
                 Model moduleModel;
 119  2
                 File f = new File( project.getBasedir(), module + "/pom.xml" );
 120  
 
 121  2
                 if ( f.exists() )
 122  
                 {
 123  2
                     moduleModel = readModel( f );
 124  
 
 125  2
                     if ( moduleModel == null )
 126  
                     {
 127  0
                         getLog().warn( "Unable to read filesystem POM for module " + module );
 128  
 
 129  0
                         moduleModel = new Model();
 130  0
                         moduleModel.setName( module );
 131  0
                         moduleModel.setArtifactId( module );
 132  
                     }
 133  
                 }
 134  
                 else
 135  
                 {
 136  0
                     getLog().warn( "No filesystem POM found for module " + module );
 137  
 
 138  0
                     moduleModel = new Model();
 139  0
                     moduleModel.setName( module );
 140  0
                     moduleModel.setArtifactId( module );
 141  
                 }
 142  
 
 143  2
                 final String moduleName = moduleModel.getName();
 144  
 
 145  2
                 String baseURL = model.getUrl();
 146  
 
 147  
                 // re-read the parent URL because the above gives some relative link (why?)
 148  2
                 final Model parentModel = readModel( new File( project.getBasedir(), "pom.xml" ) );
 149  2
                 if ( parentModel != null )
 150  
                 {
 151  0
                     baseURL = parentModel.getUrl();
 152  
                 }
 153  
 
 154  2
                 final String moduleHref = getRelativeLink( baseURL, moduleModel.getUrl(), moduleModel.getArtifactId() );
 155  
 
 156  2
                 tableRow( new String[] {linkedName( moduleName, moduleHref ), moduleModel.getDescription()} );
 157  2
             }
 158  
 
 159  1
             endTable();
 160  
 
 161  1
             endSection();
 162  1
         }
 163  
     }
 164  
 
 165  
     // adapted from DefaultSiteTool#appendMenuItem
 166  
     private String getRelativeLink( String baseUrl, String href, String defaultHref )
 167  
     {
 168  2
         String selectedHref = href;
 169  
 
 170  2
         if ( selectedHref == null )
 171  
         {
 172  2
             selectedHref = defaultHref;
 173  
         }
 174  
 
 175  2
         if ( baseUrl != null )
 176  
         {
 177  0
             selectedHref = siteTool.getRelativePath( selectedHref, baseUrl );
 178  
         }
 179  
 
 180  2
         if ( selectedHref.endsWith( "/" ) )
 181  
         {
 182  0
             selectedHref = selectedHref.concat( "index.html" );
 183  
         }
 184  
         else
 185  
         {
 186  2
             selectedHref = selectedHref.concat( "/index.html" );
 187  
         }
 188  
 
 189  2
         return selectedHref;
 190  
     }
 191  
 
 192  
     private String linkedName( String name, String link )
 193  
     {
 194  2
         return "{" + name + ", ./" + link + "}";
 195  
     }
 196  
 
 197  
     /**
 198  
      * Gets the pom model for this file.
 199  
      *
 200  
      * @param pom the pom
 201  
      *
 202  
      * @return the model
 203  
      */
 204  
     private Model readModel ( File pom )
 205  
     {
 206  4
         MavenXpp3Reader xpp3 = new MavenXpp3Reader();
 207  4
         Reader reader = null;
 208  
 
 209  
         try
 210  
         {
 211  4
             reader = ReaderFactory.newXmlReader( pom );
 212  2
             return xpp3.read( reader );
 213  
         }
 214  2
         catch ( IOException io )
 215  
         {
 216  2
             getLog().debug( io );
 217  2
             return null;
 218  
         }
 219  0
         catch ( XmlPullParserException xe )
 220  
         {
 221  0
             getLog().debug( xe );
 222  0
             return null;
 223  
         }
 224  
         finally
 225  
         {
 226  4
             IOUtil.close( reader );
 227  
         }
 228  
     }
 229  
 }