1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.jxr;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.maven.model.Plugin;
26 import org.apache.maven.model.PluginExecution;
27 import org.apache.maven.model.ReportPlugin;
28 import org.apache.maven.model.Site;
29 import org.apache.maven.project.MavenProject;
30 import org.apache.maven.wagon.repository.Repository;
31 import org.codehaus.plexus.util.StringUtils;
32 import org.codehaus.plexus.util.xml.Xpp3Dom;
33
34
35
36
37
38
39 public class JxrReportUtil {
40
41 private static final String MAVEN_JAVADOC_PLUGIN_GROUP_ID = "org.apache.maven.plugins";
42
43 private static final String MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID = "maven-javadoc-plugin";
44
45
46
47
48
49
50
51
52 protected static boolean isJavadocAggregated(MavenProject project) {
53
54 boolean javadocAggregate =
55 Boolean.parseBoolean(JxrReportUtil.getMavenJavadocPluginBasicOption(project, "aggregate", "false"));
56
57 if (javadocAggregate) {
58 return true;
59 }
60 for (Object pluginObject : getMavenJavadocPlugins(project)) {
61 if (pluginObject instanceof Plugin) {
62 Plugin plugin = (Plugin) pluginObject;
63 List<PluginExecution> executions = plugin.getExecutions();
64 for (PluginExecution pe : executions) {
65 List<String> goals = pe.getGoals();
66 for (String goal : goals) {
67 if ("aggregate".equals(goal)) {
68 return true;
69 }
70 }
71 }
72 }
73 }
74 return false;
75 }
76
77
78
79
80
81
82
83
84
85 protected static String getMavenJavadocPluginBasicOption(
86 MavenProject project, String optionName, String defaultValue) {
87 List<Object> plugins = new ArrayList<>();
88 plugins.addAll(project.getModel().getReporting().getPlugins());
89 plugins.addAll(project.getModel().getBuild().getPlugins());
90
91 String pluginArtifactId = MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID;
92 for (Object next : plugins) {
93 Xpp3Dom pluginConf = null;
94
95 if (next instanceof Plugin) {
96 Plugin plugin = (Plugin) next;
97
98
99 if (!isReportPluginMavenJavadoc(pluginArtifactId, plugin)) {
100 continue;
101 }
102
103 pluginConf = (Xpp3Dom) plugin.getConfiguration();
104 }
105
106 if (next instanceof ReportPlugin) {
107 ReportPlugin reportPlugin = (ReportPlugin) next;
108
109
110 if (!isReportPluginJavaDocPlugin(pluginArtifactId, reportPlugin)) {
111 continue;
112 }
113
114 pluginConf = (Xpp3Dom) reportPlugin.getConfiguration();
115 }
116
117 if (pluginConf == null) {
118 continue;
119 }
120
121 String attribute = pluginConf.getAttribute(optionName);
122 if (attribute != null && !attribute.isEmpty()) {
123 return attribute;
124 }
125 }
126
127 return defaultValue;
128 }
129
130
131
132
133
134
135
136 protected static List<?> getMavenJavadocPlugins(MavenProject project) {
137 List<Object> plugins = new ArrayList<>();
138 plugins.addAll(project.getModel().getReporting().getPlugins());
139 plugins.addAll(project.getModel().getBuild().getPlugins());
140
141 List<Object> result = new ArrayList<>();
142
143 String pluginArtifactId = MAVEN_JAVADOC_PLUGIN_ARTIFACT_ID;
144 for (Object next : plugins) {
145 if (next instanceof Plugin) {
146 Plugin plugin = (Plugin) next;
147
148
149 if (!isReportPluginMavenJavadoc(pluginArtifactId, plugin)) {
150 continue;
151 }
152
153 result.add(plugin);
154 }
155
156 if (next instanceof ReportPlugin) {
157 ReportPlugin reportPlugin = (ReportPlugin) next;
158
159
160 if (!isReportPluginJavaDocPlugin(pluginArtifactId, reportPlugin)) {
161 continue;
162 }
163 result.add(reportPlugin);
164 }
165 }
166 return result;
167 }
168
169 private static boolean isReportPluginMavenJavadoc(String pluginArtifactId, Plugin plugin) {
170 return (plugin.getGroupId().equals(MAVEN_JAVADOC_PLUGIN_GROUP_ID))
171 && (plugin.getArtifactId().equals(pluginArtifactId));
172 }
173
174 private static boolean isReportPluginJavaDocPlugin(String pluginArtifactId, ReportPlugin reportPlugin) {
175 return (reportPlugin.getGroupId().equals(MAVEN_JAVADOC_PLUGIN_GROUP_ID))
176 && (reportPlugin.getArtifactId().equals(pluginArtifactId));
177 }
178
179
180
181
182
183
184
185
186
187
188 protected static String getStructure(MavenProject project, boolean ignoreMissingSiteUrl) throws IOException {
189
190
191 if (project.getDistributionManagement() == null) {
192 String hierarchy = project.getName();
193
194 MavenProject parent = project.getParent();
195 while (parent != null) {
196 hierarchy = parent.getName() + '/' + hierarchy;
197 parent = parent.getParent();
198 }
199
200 return hierarchy;
201 }
202
203 Site site = project.getDistributionManagement().getSite();
204 if (site == null) {
205 if (!ignoreMissingSiteUrl) {
206 throw new IOException("Missing site information in the distribution management "
207 + "element in the project: '" + project.getName() + "'.");
208 }
209
210 return null;
211 }
212
213 if (StringUtils.isEmpty(site.getUrl())) {
214 if (!ignoreMissingSiteUrl) {
215 throw new IOException("The URL in the site is missing in the project descriptor.");
216 }
217
218 return null;
219 }
220
221 Repository repository = new Repository(site.getId(), site.getUrl());
222 if (StringUtils.isEmpty(repository.getBasedir())) {
223 return repository.getHost();
224 }
225
226 if (repository.getBasedir().startsWith("/")) {
227 return repository.getHost() + repository.getBasedir();
228 }
229
230 return repository.getHost() + '/' + repository.getBasedir();
231 }
232 }