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 private static final String MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID = "maven-javadoc-plugin";
57
58
59
60
61
62
63
64
65
66 protected static boolean isJavadocAggregated( MavenProject project )
67 throws IOException
68 {
69
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
109
110
111
112
113
114
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
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
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
205
206
207
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
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
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
261
262
263
264
265
266
267
268
269
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 }