1   package org.apache.maven.plugin.jxr;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  
36  
37  
38  
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  
49  
50  
51  
52  
53  
54  
55      protected static boolean isJavadocAggregated( MavenProject project )
56          throws IOException
57      {
58          
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  
90  
91  
92  
93  
94  
95  
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                 
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                 
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 
153 
154 
155 
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                 
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                 
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 
211 
212 
213 
214 
215 
216 
217 
218     protected static String getStructure( MavenProject project, boolean ignoreMissingSiteUrl )
219         throws IOException
220     {
221         
222         
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 }