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.ReportPlugin;
34  import org.apache.maven.model.Site;
35  import org.apache.maven.project.MavenProject;
36  import org.apache.maven.wagon.repository.Repository;
37  import org.apache.xpath.XPathAPI;
38  import org.apache.xpath.objects.XObject;
39  import org.codehaus.plexus.util.StringInputStream;
40  import org.codehaus.plexus.util.StringUtils;
41  import org.codehaus.plexus.util.xml.Xpp3Dom;
42  import org.w3c.dom.Document;
43  import org.xml.sax.SAXException;
44  
45  /**
46   * Utility class for the jxr report.
47   *
48   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
49   * @version $Id: JxrReportUtil.java 692686 2008-09-06 17:13:41Z hboutemy $
50   */
51  public class JxrReportUtil
52  {
53      /**
54       * Return the <code>optionName</code> value defined in a project for the "maven-javadoc-plugin" plugin.
55       *
56       * @param project not null
57       * @param optionName the option name wanted
58       * @param defaultValue a default value
59       * @return the value for the option name or the default value. Could be null if not found.
60       * @throws IOException if any
61       */
62      protected static String getMavenJavadocPluginBasicOption( MavenProject project, String optionName,
63                                                                String defaultValue )
64          throws IOException
65      {
66          List plugins = new ArrayList();
67          for ( Iterator it = project.getModel().getReporting().getPlugins().iterator(); it.hasNext(); )
68          {
69              plugins.add( it.next() );
70          }
71          for ( Iterator it = project.getModel().getBuild().getPlugins().iterator(); it.hasNext(); )
72          {
73              plugins.add( it.next() );
74          }
75  
76          String pluginArtifactId = "maven-javadoc-plugin";
77          for ( Iterator it = plugins.iterator(); it.hasNext(); )
78          {
79              Object next = it.next();
80  
81              Xpp3Dom pluginConf = null;
82  
83              if ( next instanceof Plugin )
84              {
85                  Plugin plugin = (Plugin) next;
86  
87                  // using out-of-box Maven plugins
88                  if ( !( ( plugin.getGroupId().equals( "org.apache.maven.plugins" ) ) && ( plugin.getArtifactId()
89                      .equals( pluginArtifactId ) ) ) )
90                  {
91                      continue;
92                  }
93  
94                  pluginConf = (Xpp3Dom) plugin.getConfiguration();
95              }
96  
97              if ( next instanceof ReportPlugin )
98              {
99                  ReportPlugin reportPlugin = (ReportPlugin) next;
100 
101                 // using out-of-box Maven plugins
102                 if ( !( ( reportPlugin.getGroupId().equals( "org.apache.maven.plugins" ) ) && ( reportPlugin
103                     .getArtifactId().equals( pluginArtifactId ) ) ) )
104                 {
105                     continue;
106                 }
107 
108                 pluginConf = (Xpp3Dom) reportPlugin.getConfiguration();
109             }
110 
111             if ( pluginConf == null )
112             {
113                 continue;
114             }
115 
116             try
117             {
118                 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
119                     .parse( new StringInputStream( pluginConf.toString() ) );
120 
121                 XObject obj = XPathAPI.eval( doc, "//configuration/" + optionName );
122 
123                 if ( StringUtils.isNotEmpty( obj.toString() ) )
124                 {
125                     return obj.toString();
126                 }
127             }
128             catch ( SAXException e )
129             {
130                 throw new IOException( "SAXException: " + e.getMessage() );
131             }
132             catch ( ParserConfigurationException e )
133             {
134                 throw new IOException( "ParserConfigurationException: " + e.getMessage() );
135             }
136             catch ( FactoryConfigurationError e )
137             {
138                 throw new IOException( "FactoryConfigurationError: " + e.getMessage() );
139             }
140             catch ( TransformerException e )
141             {
142                 throw new IOException( "TransformerException: " + e.getMessage() );
143             }
144         }
145 
146         return defaultValue;
147     }
148 
149     /**
150      * Generates the site structure using the project hierarchy (project and its modules) or using the
151      * distributionManagement elements from the pom.xml.
152      *
153      * @todo come from site plugin!
154      * @see org.apache.maven.plugins.site.SiteStageMojo#getStructure( MavenProject project, boolean ignoreMissingSiteUrl )
155      *
156      * @param project
157      * @param ignoreMissingSiteUrl
158      * @return the structure relative path
159      * @throws IOException if any
160      */
161     protected static String getStructure( MavenProject project, boolean ignoreMissingSiteUrl )
162         throws IOException
163     {
164         if ( project.getDistributionManagement() == null )
165         {
166             String hierarchy = project.getName();
167 
168             MavenProject parent = project.getParent();
169             while ( parent != null )
170             {
171                 hierarchy = parent.getName() + "/" + hierarchy;
172                 parent = parent.getParent();
173             }
174 
175             return hierarchy;
176         }
177 
178         Site site = project.getDistributionManagement().getSite();
179         if ( site == null )
180         {
181             if ( !ignoreMissingSiteUrl )
182             {
183                 throw new IOException(
184                                        "Missing site information in the distribution management element in the project: '"
185                                            + project.getName() + "'." );
186             }
187 
188             return null;
189         }
190 
191         if ( StringUtils.isEmpty( site.getUrl() ) )
192         {
193             if ( !ignoreMissingSiteUrl )
194             {
195                 throw new IOException( "The URL in the site is missing in the project descriptor." );
196             }
197 
198             return null;
199         }
200 
201         Repository repository = new Repository( site.getId(), site.getUrl() );
202         if ( StringUtils.isEmpty( repository.getBasedir() ) )
203         {
204             return repository.getHost();
205         }
206 
207         if ( repository.getBasedir().startsWith( "/" ) )
208         {
209             return repository.getHost() + repository.getBasedir();
210         }
211 
212         return repository.getHost() + "/" + repository.getBasedir();
213     }
214 }