View Javadoc
1   package org.apache.maven.plugin.jxr;
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.IOException;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import javax.xml.parsers.DocumentBuilderFactory;
28  import javax.xml.parsers.FactoryConfigurationError;
29  import javax.xml.parsers.ParserConfigurationException;
30  import javax.xml.transform.TransformerException;
31  
32  import org.apache.maven.model.Plugin;
33  import org.apache.maven.model.PluginExecution;
34  import org.apache.maven.model.ReportPlugin;
35  import org.apache.maven.model.Site;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.wagon.repository.Repository;
38  import org.apache.xpath.XPathAPI;
39  import org.apache.xpath.objects.XObject;
40  import org.codehaus.plexus.util.StringInputStream;
41  import org.codehaus.plexus.util.StringUtils;
42  import org.codehaus.plexus.util.xml.Xpp3Dom;
43  import org.w3c.dom.Document;
44  import org.xml.sax.SAXException;
45  
46  /**
47   * Utility class for the jxr report.
48   *
49   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
50   * @version $Id: JxrReportUtil.java 1634366 2014-10-26 17:53:17Z khmarbaise $
51   */
52  public class JxrReportUtil
53  {
54  
55      private static final String MAVEN_JAVADOC_PLUGIN_GROUP_ID = "org.apache.maven.plugins";
56  
57      private static final String MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID = "maven-javadoc-plugin";
58  
59      /**
60       * Determine if javadoc is aggregated in this project, paying attention to both TODO: take cognizance of javadoc
61       * versus test-javadoc the old parameter and the new mojo.
62       * 
63       * @param project
64       * @return
65       * @throws IOException
66       */
67      protected static boolean isJavadocAggregated( MavenProject project )
68          throws IOException
69      {
70          // first check conf for obsolete aggregate param.
71          // CHECKSTYLE_OFF: LineLength
72          boolean javadocAggregate =
73              Boolean.valueOf( JxrReportUtil.getMavenJavadocPluginBasicOption( project, "aggregate", "false" ) ).booleanValue();
74          // CHECKSTYLE_ON: LineLength
75  
76          if ( javadocAggregate )
77          {
78              return true;
79          }
80          for ( Object pluginObject : getMavenJavadocPlugins( project ) )
81          {
82              if ( pluginObject instanceof Plugin )
83              {
84                  Plugin plugin = (Plugin) pluginObject;
85                  List<PluginExecution> executions = plugin.getExecutions();
86                  for ( PluginExecution pe : executions )
87                  {
88                      List<String> goals = pe.getGoals();
89                      for ( String goal : goals )
90                      {
91                          if ( "aggregate".equals( goal ) )
92                          {
93                              return true;
94                          }
95                      }
96                  }
97              }
98          }
99          return false;
100     }
101 
102     /**
103      * Return the <code>optionName</code> value defined in a project for the "maven-javadoc-plugin" plugin.
104      *
105      * @param project not null
106      * @param optionName the option name wanted
107      * @param defaultValue a default value
108      * @return the value for the option name or the default value. Could be null if not found.
109      * @throws IOException if any
110      */
111     protected static String getMavenJavadocPluginBasicOption( MavenProject project, String optionName,
112                                                               String defaultValue )
113         throws IOException
114     {
115         List<Object> plugins = new ArrayList<Object>();
116         for ( Iterator<?> it = project.getModel().getReporting().getPlugins().iterator(); it.hasNext(); )
117         {
118             plugins.add( it.next() );
119         }
120         for ( Iterator<?> it = project.getModel().getBuild().getPlugins().iterator(); it.hasNext(); )
121         {
122             plugins.add( it.next() );
123         }
124 
125         String pluginArtifactId = MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID;
126         for ( Object next : plugins )
127         {
128             Xpp3Dom pluginConf = null;
129 
130             if ( next instanceof Plugin )
131             {
132                 Plugin plugin = (Plugin) next;
133 
134                 // CHECKSTYLE_OFF: LineLength
135                 // using out-of-box Maven plugins
136                 if ( !isReportPluginMavenJavadoc( pluginArtifactId, plugin ) )
137                 // CHECKSTYLE_ON: LineLength
138                 {
139                     continue;
140                 }
141 
142                 pluginConf = (Xpp3Dom) plugin.getConfiguration();
143             }
144 
145             if ( next instanceof ReportPlugin )
146             {
147                 ReportPlugin reportPlugin = (ReportPlugin) next;
148 
149                 // using out-of-box Maven plugins
150                 if ( !isReportPluginJavaDocPlugin( pluginArtifactId, reportPlugin ) )
151                 {
152                     continue;
153                 }
154 
155                 pluginConf = (Xpp3Dom) reportPlugin.getConfiguration();
156             }
157 
158             if ( pluginConf == null )
159             {
160                 continue;
161             }
162 
163             try
164             {
165                 StringInputStream stringInputStream = new StringInputStream( pluginConf.toString() );
166                 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( stringInputStream );
167 
168                 XObject obj = XPathAPI.eval( doc, "//configuration/" + optionName );
169 
170                 if ( StringUtils.isNotEmpty( obj.toString() ) )
171                 {
172                     return obj.toString();
173                 }
174             }
175             catch ( SAXException e )
176             {
177                 throw new IOException( "SAXException: " + e.getMessage() );
178             }
179             catch ( ParserConfigurationException e )
180             {
181                 throw new IOException( "ParserConfigurationException: " + e.getMessage() );
182             }
183             catch ( FactoryConfigurationError e )
184             {
185                 throw new IOException( "FactoryConfigurationError: " + e.getMessage() );
186             }
187             catch ( TransformerException e )
188             {
189                 throw new IOException( "TransformerException: " + e.getMessage() );
190             }
191         }
192 
193         return defaultValue;
194     }
195 
196     /**
197      * Return the plugin references for the javadoc plugin in a project.
198      *
199      * @param project not null
200      * @throws IOException if any
201      */
202     protected static List<?> getMavenJavadocPlugins( MavenProject project )
203         throws IOException
204     {
205         List<Object> plugins = new ArrayList<Object>();
206         for ( Iterator<?> it = project.getModel().getReporting().getPlugins().iterator(); it.hasNext(); )
207         {
208             plugins.add( it.next() );
209         }
210         for ( Iterator<?> it = project.getModel().getBuild().getPlugins().iterator(); it.hasNext(); )
211         {
212             plugins.add( it.next() );
213         }
214 
215         List<Object> result = new ArrayList<Object>();
216 
217         String pluginArtifactId = MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID;
218         for ( Object next : plugins )
219         {
220             if ( next instanceof Plugin )
221             {
222                 Plugin plugin = (Plugin) next;
223 
224                 // using out-of-box Maven plugins
225                 if ( !isReportPluginMavenJavadoc( pluginArtifactId, plugin ) )
226                 {
227                     continue;
228                 }
229 
230                 result.add( plugin );
231             }
232 
233             if ( next instanceof ReportPlugin )
234             {
235                 ReportPlugin reportPlugin = (ReportPlugin) next;
236 
237                 // using out-of-box Maven plugins
238                 if ( !isReportPluginJavaDocPlugin( pluginArtifactId, reportPlugin ) )
239                 {
240                     continue;
241                 }
242                 result.add( reportPlugin );
243             }
244         }
245         return result;
246     }
247 
248     private static boolean isReportPluginMavenJavadoc( String pluginArtifactId, Plugin plugin )
249     {
250         return ( plugin.getGroupId().equals( MAVEN_JAVADOC_PLUGIN_GROUP_ID ) )
251             && ( plugin.getArtifactId().equals( pluginArtifactId ) );
252     }
253 
254     private static boolean isReportPluginJavaDocPlugin( String pluginArtifactId, ReportPlugin reportPlugin )
255     {
256         return ( reportPlugin.getGroupId().equals( MAVEN_JAVADOC_PLUGIN_GROUP_ID ) )
257             && ( reportPlugin.getArtifactId().equals( pluginArtifactId ) );
258     }
259 
260     /**
261      * Generates the site structure using the project hierarchy (project and its modules) or using the
262      * distributionManagement elements from the pom.xml.
263      *
264      * @todo come from site plugin!
265      * @see org.apache.maven.plugins.site.SiteStageMojo#getStructure(MavenProject project, boolean ignoreMissingSiteUrl
266      *      )
267      * @param project
268      * @param ignoreMissingSiteUrl
269      * @return the structure relative path
270      * @throws IOException if any
271      */
272     protected static String getStructure( MavenProject project, boolean ignoreMissingSiteUrl )
273         throws IOException
274     {
275         if ( project.getDistributionManagement() == null )
276         {
277             String hierarchy = project.getName();
278 
279             MavenProject parent = project.getParent();
280             while ( parent != null )
281             {
282                 hierarchy = parent.getName() + "/" + hierarchy;
283                 parent = parent.getParent();
284             }
285 
286             return hierarchy;
287         }
288 
289         Site site = project.getDistributionManagement().getSite();
290         if ( site == null )
291         {
292             if ( !ignoreMissingSiteUrl )
293             {
294                 throw new IOException( "Missing site information in the distribution management "
295                     + "element in the project: '" + project.getName() + "'." );
296             }
297 
298             return null;
299         }
300 
301         if ( StringUtils.isEmpty( site.getUrl() ) )
302         {
303             if ( !ignoreMissingSiteUrl )
304             {
305                 throw new IOException( "The URL in the site is missing in the project descriptor." );
306             }
307 
308             return null;
309         }
310 
311         Repository repository = new Repository( site.getId(), site.getUrl() );
312         if ( StringUtils.isEmpty( repository.getBasedir() ) )
313         {
314             return repository.getHost();
315         }
316 
317         if ( repository.getBasedir().startsWith( "/" ) )
318         {
319             return repository.getHost() + repository.getBasedir();
320         }
321 
322         return repository.getHost() + "/" + repository.getBasedir();
323     }
324 }