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