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.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
48
49
50
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
61
62
63
64
65
66
67 protected static boolean isJavadocAggregated( MavenProject project )
68 throws IOException
69 {
70
71
72 boolean javadocAggregate =
73 Boolean.valueOf( JxrReportUtil.getMavenJavadocPluginBasicOption( project, "aggregate", "false" ) ).booleanValue();
74
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
104
105
106
107
108
109
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
135
136 if ( !isReportPluginMavenJavadoc( pluginArtifactId, plugin ) )
137
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
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
198
199
200
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
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
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
262
263
264
265
266
267
268
269 protected static String getStructure( MavenProject project, boolean ignoreMissingSiteUrl )
270 throws IOException
271 {
272
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( "Missing site information in the distribution management "
294 + "element in the project: '" + project.getName() + "'." );
295 }
296
297 return null;
298 }
299
300 if ( StringUtils.isEmpty( site.getUrl() ) )
301 {
302 if ( !ignoreMissingSiteUrl )
303 {
304 throw new IOException( "The URL in the site is missing in the project descriptor." );
305 }
306
307 return null;
308 }
309
310 Repository repository = new Repository( site.getId(), site.getUrl() );
311 if ( StringUtils.isEmpty( repository.getBasedir() ) )
312 {
313 return repository.getHost();
314 }
315
316 if ( repository.getBasedir().startsWith( "/" ) )
317 {
318 return repository.getHost() + repository.getBasedir();
319 }
320
321 return repository.getHost() + '/' + repository.getBasedir();
322 }
323 }