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 1038048 2010-11-23 10:52:14Z vsiveton $
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                     }
132                 }
133                 else
134                 {
135                     getLog().warn( "No filesystem POM found for module " + module );
136 
137                     moduleModel = new Model();
138                     moduleModel.setName( module );
139                 }
140 
141                 tableRow( new String[] {linkedName( moduleModel.getName(), module ), moduleModel.getDescription()} );
142             }
143 
144             endTable();
145 
146             endSection();
147         }
148     }
149 
150     private String linkedName( String name, String link )
151     {
152         return "{" + name + ", ./" + link + "/}";
153     }
154 
155     /**
156      * Gets the pom model for this file.
157      *
158      * @param pom the pom
159      *
160      * @return the model
161      */
162     private Model readModel ( File pom )
163     {
164         MavenXpp3Reader xpp3 = new MavenXpp3Reader();
165         Reader reader = null;
166 
167         try
168         {
169             reader = ReaderFactory.newXmlReader( pom );
170             return xpp3.read( reader );
171         }
172         catch ( IOException io )
173         {
174             getLog().debug( io );
175             return null;
176         }
177         catch ( XmlPullParserException xe )
178         {
179             getLog().debug( xe );
180             return null;
181         }
182         finally
183         {
184             IOUtil.close( reader );
185         }
186     }
187 }