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.Iterator;
26  import java.util.List;
27  import java.util.Locale;
28  
29  import org.apache.maven.doxia.sink.Sink;
30  import org.apache.maven.model.Model;
31  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
32  
33  import org.codehaus.plexus.i18n.I18N;
34  import org.codehaus.plexus.util.IOUtil;
35  import org.codehaus.plexus.util.ReaderFactory;
36  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37  
38  /**
39   * Generates the Project Modules report.
40   *
41   * @author ltheussl
42   * @version $Id: ModulesReport.java 940663 2010-05-03 22:58:15Z hboutemy $
43   * @since 2.2
44   * @goal modules
45   */
46  public class ModulesReport
47      extends AbstractProjectInfoReport
48  {
49      // ----------------------------------------------------------------------
50      // Public methods
51      // ----------------------------------------------------------------------
52  
53      /** {@inheritDoc} */
54      public void executeReport( Locale locale )
55      {
56          new ModulesRenderer( getSink(), getProject().getModel(), i18n, locale ).render();
57      }
58  
59      /** {@inheritDoc} */
60      public String getOutputName()
61      {
62          return "modules";
63      }
64  
65      protected String getI18Nsection()
66      {
67          return "modules";
68      }
69  
70      /** {@inheritDoc} */
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          protected String getI18Nsection()
96          {
97              return "modules";
98          }
99  
100         /** {@inheritDoc} */
101         public void renderBody()
102         {
103             List modules = model.getModules();
104 
105             startSection( getTitle() );
106 
107             paragraph( getI18nString( "intro" ) );
108 
109             startTable();
110 
111             String name = getI18nString( "header.name" );
112             String description = getI18nString( "header.description" );
113             tableHeader( new String[] {name, description} );
114 
115             for ( Iterator it = modules.iterator(); it.hasNext(); )
116             {
117                 String module = (String) it.next();
118 
119                 Model moduleModel;
120                 File f = new File( project.getBasedir(), module + "/pom.xml" );
121 
122                 if ( f.exists() )
123                 {
124                     moduleModel = readModel( f );
125 
126                     if ( moduleModel == null )
127                     {
128                         getLog().warn( "Unable to read filesystem POM for module " + module );
129 
130                         moduleModel = new Model();
131                         moduleModel.setName( 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                 }
141 
142                 tableRow( new String[] {linkedName( moduleModel.getName(), module ), moduleModel.getDescription()} );
143             }
144 
145             endTable();
146 
147             endSection();
148         }
149     }
150 
151     private String linkedName( String name, String link )
152     {
153         return "{" + name + ", ./" + link + "/}";
154     }
155 
156     /**
157      * Gets the pom model for this file.
158      *
159      * @param pom the pom
160      *
161      * @return the model
162      */
163     private Model readModel ( File pom )
164     {
165         MavenXpp3Reader xpp3 = new MavenXpp3Reader();
166         Model model = null;
167         Reader reader = null;
168 
169         try
170         {
171             reader = ReaderFactory.newXmlReader( pom );
172             model = xpp3.read( reader );
173         }
174         catch ( IOException io )
175         {
176             getLog().debug( io );
177             return null;
178         }
179         catch ( XmlPullParserException xe )
180         {
181             getLog().debug( xe );
182             return null;
183         }
184         finally
185         {
186             IOUtil.close( reader );
187             reader = null;
188         }
189 
190         return model;
191     }
192 }