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 1145180 2011-07-11 14:02:55Z bimargulies $
51   */
52  public class JxrReportUtil
53  {
54      
55      private static final String MAVEN_JAVADOC_PLUGIN_GROUP_ID = "org.apache.maven.plugins";
56      private static final String MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID = "maven-javadoc-plugin";
57  
58      /**
59       * Determine if javadoc is aggregated in this project, paying attention to both
60       * TODO: take cognizance of javadoc versus test-javadoc
61       * the old parameter and the new mojo.
62       * @param project
63       * @return
64       * @throws IOException
65       */
66      protected static boolean isJavadocAggregated( MavenProject project )
67          throws IOException
68      {
69          // first check conf for obsolete aggregate param.
70          boolean javadocAggregate = Boolean
71                          .valueOf( JxrReportUtil.getMavenJavadocPluginBasicOption( project, "aggregate", "false" ) )
72                          .booleanValue();
73          if ( javadocAggregate ) 
74          {
75              return true;
76          }
77          List plugins = getMavenJavadocPlugins ( project );
78          Iterator pi = plugins.iterator();
79          while ( pi.hasNext() )
80          {
81              Object pluginObject = pi.next();
82              
83              if ( pluginObject instanceof Plugin )
84              {
85                  Plugin plugin = (Plugin)pluginObject;
86                  List executions = plugin.getExecutions();
87                  Iterator ei = executions.iterator();
88                  while ( ei.hasNext() )
89                  {
90                      PluginExecution pe = (PluginExecution) ei.next();
91                      List goals = pe.getGoals();
92                      Iterator gi = goals.iterator();
93                      while ( gi.hasNext() )
94                      {
95                          String goal = (String) gi.next();
96                          if ( "aggregate".equals(goal) )
97                          {
98                              return true;
99                          }
100                     }
101                 }
102             } 
103         }
104         return false;
105     }
106     
107     /**
108      * Return the <code>optionName</code> value defined in a project for the "maven-javadoc-plugin" plugin.
109      *
110      * @param project not null
111      * @param optionName the option name wanted
112      * @param defaultValue a default value
113      * @return the value for the option name or the default value. Could be null if not found.
114      * @throws IOException if any
115      */
116     protected static String getMavenJavadocPluginBasicOption( MavenProject project, String optionName,
117                                                               String defaultValue )
118         throws IOException
119     {
120         List plugins = new ArrayList();
121         for ( Iterator it = project.getModel().getReporting().getPlugins().iterator(); it.hasNext(); )
122         {
123             plugins.add( it.next() );
124         }
125         for ( Iterator it = project.getModel().getBuild().getPlugins().iterator(); it.hasNext(); )
126         {
127             plugins.add( it.next() );
128         }
129 
130         String pluginArtifactId = MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID;
131         for ( Iterator it = plugins.iterator(); it.hasNext(); )
132         {
133             Object next = it.next();
134 
135             Xpp3Dom pluginConf = null;
136 
137             if ( next instanceof Plugin )
138             {
139                 Plugin plugin = (Plugin) next;
140 
141                 // using out-of-box Maven plugins
142                 if ( !( ( plugin.getGroupId().equals( MAVEN_JAVADOC_PLUGIN_GROUP_ID ) ) && ( plugin.getArtifactId()
143                     .equals( pluginArtifactId ) ) ) )
144                 {
145                     continue;
146                 }
147 
148                 pluginConf = (Xpp3Dom) plugin.getConfiguration();
149             }
150 
151             if ( next instanceof ReportPlugin )
152             {
153                 ReportPlugin reportPlugin = (ReportPlugin) next;
154 
155                 // using out-of-box Maven plugins
156                 if ( !( ( reportPlugin.getGroupId().equals( MAVEN_JAVADOC_PLUGIN_GROUP_ID ) ) && ( reportPlugin
157                     .getArtifactId().equals( pluginArtifactId ) ) ) )
158                 {
159                     continue;
160                 }
161 
162                 pluginConf = (Xpp3Dom) reportPlugin.getConfiguration();
163             }
164 
165             if ( pluginConf == null )
166             {
167                 continue;
168             }
169 
170             try
171             {
172                 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
173                     .parse( new StringInputStream( pluginConf.toString() ) );
174 
175                 XObject obj = XPathAPI.eval( doc, "//configuration/" + optionName );
176 
177                 if ( StringUtils.isNotEmpty( obj.toString() ) )
178                 {
179                     return obj.toString();
180                 }
181             }
182             catch ( SAXException e )
183             {
184                 throw new IOException( "SAXException: " + e.getMessage() );
185             }
186             catch ( ParserConfigurationException e )
187             {
188                 throw new IOException( "ParserConfigurationException: " + e.getMessage() );
189             }
190             catch ( FactoryConfigurationError e )
191             {
192                 throw new IOException( "FactoryConfigurationError: " + e.getMessage() );
193             }
194             catch ( TransformerException e )
195             {
196                 throw new IOException( "TransformerException: " + e.getMessage() );
197             }
198         }
199 
200         return defaultValue;
201     }
202     
203     /**
204      * Return the plugin references for the javadoc plugin in a project.
205      *
206      * @param project not null
207      * @throws IOException if any
208      */
209     protected static List getMavenJavadocPlugins( MavenProject project )
210         throws IOException
211     {
212         List plugins = new ArrayList();
213         for ( Iterator it = project.getModel().getReporting().getPlugins().iterator(); it.hasNext(); )
214         {
215             plugins.add( it.next() );
216         }
217         for ( Iterator it = project.getModel().getBuild().getPlugins().iterator(); it.hasNext(); )
218         {
219             plugins.add( it.next() );
220         }
221         
222         List result = new ArrayList();
223 
224         String pluginArtifactId = MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID;
225         for ( Iterator it = plugins.iterator(); it.hasNext(); )
226         {
227             Object next = it.next();
228 
229             if ( next instanceof Plugin )
230             {
231                 Plugin plugin = (Plugin) next;
232 
233                 // using out-of-box Maven plugins
234                 if ( !( ( plugin.getGroupId().equals( MAVEN_JAVADOC_PLUGIN_GROUP_ID ) ) && ( plugin.getArtifactId()
235                     .equals( pluginArtifactId ) ) ) )
236                 {
237                     continue;
238                 }
239 
240                 result.add( plugin );
241             }
242 
243             if ( next instanceof ReportPlugin )
244             {
245                 ReportPlugin reportPlugin = (ReportPlugin) next;
246 
247                 // using out-of-box Maven plugins
248                 if ( !( ( reportPlugin.getGroupId().equals( MAVEN_JAVADOC_PLUGIN_GROUP_ID ) ) && ( reportPlugin
249                     .getArtifactId().equals( pluginArtifactId ) ) ) )
250                 {
251                     continue;
252                 }
253                 result.add( reportPlugin );
254             }
255         }
256         return result;
257     }
258 
259     /**
260      * Generates the site structure using the project hierarchy (project and its modules) or using the
261      * distributionManagement elements from the pom.xml.
262      *
263      * @todo come from site plugin!
264      * @see org.apache.maven.plugins.site.SiteStageMojo#getStructure( MavenProject project, boolean ignoreMissingSiteUrl )
265      *
266      * @param project
267      * @param ignoreMissingSiteUrl
268      * @return the structure relative path
269      * @throws IOException if any
270      */
271     protected static String getStructure( MavenProject project, boolean ignoreMissingSiteUrl )
272         throws IOException
273     {
274         if ( project.getDistributionManagement() == null )
275         {
276             String hierarchy = project.getName();
277 
278             MavenProject parent = project.getParent();
279             while ( parent != null )
280             {
281                 hierarchy = parent.getName() + "/" + hierarchy;
282                 parent = parent.getParent();
283             }
284 
285             return hierarchy;
286         }
287 
288         Site site = project.getDistributionManagement().getSite();
289         if ( site == null )
290         {
291             if ( !ignoreMissingSiteUrl )
292             {
293                 throw new IOException(
294                                        "Missing site information in the distribution management element in the project: '"
295                                            + 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 }