1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.issues.util;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.maven.artifact.versioning.ArtifactVersion;
25 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
26 import org.apache.maven.artifact.versioning.VersionRange;
27 import org.apache.maven.model.ReportPlugin;
28 import org.apache.maven.model.ReportSet;
29 import org.apache.maven.model.Reporting;
30 import org.codehaus.plexus.util.xml.Xpp3Dom;
31
32
33
34
35
36
37
38
39 public class ReportReferenceExtractor
40 {
41
42
43
44
45
46
47
48 private final Log log = LogFactory.getLog(ReportReferenceExtractor.class);
49
50
51
52
53 private final Reporting reporting;
54
55
56
57
58
59
60 private final String id;
61
62
63
64
65
66 private final ArtifactVersion version;
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public ReportReferenceExtractor(final String id,
85 final ArtifactVersion version, final Reporting reporting)
86 {
87 this.id = id;
88 this.version = version;
89 this.reporting = reporting;
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 @SuppressWarnings("unchecked")
110 public List<ReportReference> readReportReferences()
111 {
112 final List<ReportReference> references = new ArrayList<ReportReference>();
113 final List<ReportPlugin> reportPlugins = reporting.getPlugins();
114 for (ReportPlugin plugin : reportPlugins)
115 {
116 if (isIssueReportPlugin(plugin))
117 {
118 if (log.isTraceEnabled())
119 {
120 log.trace("Issue report found: " + plugin.getKey());
121 }
122
123 final List<ReportSet> reportSets = plugin.getReportSets();
124 for (ReportSet reportSet : reportSets)
125 {
126 final Object object = reportSet.getConfiguration();
127 if (object instanceof Xpp3Dom)
128 {
129 parseAndAddReportReferenceIfRelevant(references, (Xpp3Dom) object);
130 }
131 }
132 }
133 }
134 Collections.sort(references);
135 return references;
136 }
137
138
139
140
141
142
143
144 @SuppressWarnings("unchecked")
145 private void parseAndAddReportReferenceIfRelevant(
146 final List<ReportReference> references, final Xpp3Dom dom)
147 {
148 final String giConf = dom.getName();
149 if ("configuration".equals(giConf))
150 {
151 final ReportReference ref = createReportReference(dom);
152 if (ref != null && version.compareTo(ref.getVersionRepresentative()) > 0)
153 {
154 references.add(ref);
155 }
156 }
157 }
158
159
160
161
162
163
164
165
166 private ReportReference createReportReference(final Xpp3Dom dom)
167 {
168 try
169 {
170 return createReportReferenceInstance(dom);
171 }
172 catch (final IllegalArgumentException e)
173 {
174 return null;
175 }
176 catch (final InvalidVersionSpecificationException e)
177 {
178 return null;
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193 private ReportReference createReportReferenceInstance(final Xpp3Dom dom)
194 throws InvalidVersionSpecificationException, IllegalArgumentException
195 {
196 String outputName = null;
197 String title = null;
198 String versionRangeSpec = null;
199 for (Xpp3Dom child : dom.getChildren())
200 {
201 final String name = child.getName();
202 if ("outputName".equals(name))
203 {
204 outputName = child.getValue();
205 if (isNotReportOfSameType(outputName))
206 {
207 return null;
208 }
209 }
210 else if ("title".equals(name))
211 {
212 title = child.getValue();
213 }
214 else if ("versionRange".equals(name))
215 {
216 versionRangeSpec = child.getValue();
217 }
218 }
219
220 return createReportReference(outputName, title, versionRangeSpec);
221 }
222
223
224
225
226
227
228
229
230
231 private boolean isNotReportOfSameType(final String outputName)
232 {
233 return !outputName.startsWith(id) || outputName.equals(id);
234 }
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249 private ReportReference createReportReference(final String outputName,
250 final String title, final String versionRangeSpec)
251 throws InvalidVersionSpecificationException, IllegalArgumentException
252 {
253 if (versionRangeSpec == null)
254 {
255 return null;
256 }
257
258 final VersionRange range =
259 VersionRange.createFromVersionSpec(versionRangeSpec);
260 final ArtifactVersion representative =
261 Utils.findVersionRepresentative(range);
262 final ReportReference ref =
263 new ReportReference(title, outputName, representative);
264 return ref;
265 }
266
267
268
269
270
271
272
273
274 private boolean isIssueReportPlugin(final ReportPlugin plugin)
275 {
276 return "issues-maven-plugin".equals(plugin.getArtifactId())
277 && "de.smartics.maven.plugin".equals(plugin.getGroupId());
278 }
279
280
281
282 }