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