View Javadoc

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