1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.util.report;
17
18 import java.io.File;
19 import java.util.List;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.maven.model.ReportPlugin;
23 import org.apache.maven.plugin.logging.Log;
24 import org.apache.maven.project.MavenProject;
25 import org.codehaus.plexus.util.PathTool;
26
27 import de.smartics.maven.util.PathUtils;
28 import de.smartics.maven.util.report.link.ExternalReport;
29 import de.smartics.maven.util.report.link.ExternalReportFactory;
30 import de.smartics.maven.util.report.link.LinkConstructorStrategy;
31 import de.smartics.maven.util.report.link.LinkConstructorStrategyConfig;
32 import de.smartics.maven.util.report.link.ReportId;
33
34
35
36
37 public class MavenExternalReportFactory implements ExternalReportFactory
38 {
39
40
41
42
43
44
45
46
47
48
49 public static final String MAVEN_SUREFIRE_PLUGIN =
50 "maven-surefire-report-plugin";
51
52
53
54
55
56
57
58 public static final String MAVEN_JAVADOC_PLUGIN = "maven-javadoc-plugin";
59
60
61
62
63
64
65
66 public static final String MAVEN_JXR_PLUGIN = "maven-jxr-plugin";
67
68
69
70
71
72
73
74 public static final String MAVEN_COBERTURA_PLUGIN = "maven-cobertura-plugin";
75
76
77
78
79
80
81 private final MavenProject project;
82
83
84
85
86
87 private final Log log;
88
89
90
91
92 private final String upPath;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public MavenExternalReportFactory(final MavenProject project,
108 final String reportRelDir) throws IllegalArgumentException
109 {
110 this(null, project, reportRelDir);
111 }
112
113
114
115
116
117
118
119
120
121
122
123 public MavenExternalReportFactory(final Log log, final MavenProject project,
124 final String reportRelDir) throws IllegalArgumentException
125 {
126 checkArguments(project, reportRelDir);
127 this.log = log;
128 this.project = project;
129 this.upPath = PathUtils.getUpPath(reportRelDir);
130 }
131
132
133
134
135
136
137
138 private static void checkArguments(final MavenProject project,
139 final String reportRelDir) throws IllegalArgumentException
140 {
141 if (project == null)
142 {
143 throw new IllegalArgumentException("The project must not be 'null'.");
144 }
145 if (reportRelDir == null
146 || (StringUtils.isBlank(reportRelDir) && StringUtils
147 .isNotEmpty(reportRelDir)))
148 {
149 throw new IllegalArgumentException(
150 "The report relative directory within the project base"
151 + " directory must not be 'null' or contain only whitespaces.");
152 }
153 }
154
155
156
157
158
159 private String calcRelativePath(final File reportLocation)
160 {
161 final String reportBaseDir = project.getReporting().getOutputDirectory();
162 final String reportLocationAbsolutePath = reportLocation.getAbsolutePath();
163
164 final String relativePathBase;
165 if (reportLocationAbsolutePath.contains(".."))
166 {
167 relativePathBase = "..";
168 }
169 else
170 {
171 relativePathBase =
172 PathTool.getRelativePath(reportBaseDir, reportLocationAbsolutePath);
173 }
174 final String relativePath =
175 upPath + '/' + (getPathFragment(relativePathBase))
176 + reportLocation.getName();
177 return relativePath;
178 }
179
180 private static String getPathFragment(final String relativePathBase)
181 {
182 return isNotCurrentDir(relativePathBase) ? relativePathBase + '/' : "";
183 }
184
185 private static boolean isNotCurrentDir(final String relativePathBase)
186 {
187 return StringUtils.isNotEmpty(relativePathBase)
188 && !".".equals(relativePathBase);
189 }
190
191 @SuppressWarnings("unchecked")
192 private boolean isReportPluginRegistered(final String reportArtifactId)
193 {
194 final List<ReportPlugin> reportPlugins =
195 (List<ReportPlugin>) project.getReportPlugins();
196 for (final ReportPlugin report : reportPlugins)
197 {
198 final String artifactId = report.getArtifactId();
199 if (reportArtifactId.equals(artifactId))
200 {
201 return true;
202 }
203 }
204 return false;
205 }
206
207
208
209
210 public ExternalReport createExternalReport(final ReportId reportId,
211 final LinkConstructorStrategy strategy)
212 {
213 if (reportId == null)
214 {
215 throw new IllegalArgumentException("The report ID is required.");
216 }
217 if (strategy == null)
218 {
219 throw new IllegalArgumentException(
220 "The link construction strategy is required.");
221 }
222
223 final String reportBasePath =
224 createRelativeReportLocationLink(reportId, strategy);
225 if (reportBasePath != null)
226 {
227 return new ExternalReport(reportId, reportBasePath, strategy);
228 }
229 else
230 {
231 return null;
232 }
233 }
234
235 private String createRelativeReportLocationLink(final ReportId reportId,
236 final LinkConstructorStrategy strategy)
237 {
238 final LinkConstructorStrategyConfig config = strategy.getConfig();
239
240 final File reportLocation = config.getReportLocation();
241 final String reportArtifactId = reportId.getReportArtifactId();
242
243 final String location;
244 if (reportLocation.exists() || isReportPluginRegistered(reportArtifactId))
245 {
246 location = calcRelativePath(reportLocation);
247 }
248 else
249 {
250 location = null;
251 if (log != null && log.isWarnEnabled())
252 {
253 log.warn("Cannot locate report of '" + reportArtifactId
254 + "'. Disabling references to that report.");
255 }
256 }
257
258 return location;
259 }
260
261
262
263 }