1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.exceptions;
17
18 import java.io.File;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.io.Writer;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.Map;
26 import java.util.ResourceBundle;
27
28 import org.apache.commons.lang.LocaleUtils;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.artifact.factory.ArtifactFactory;
32 import org.apache.maven.artifact.repository.ArtifactRepository;
33 import org.apache.maven.artifact.resolver.ArtifactResolver;
34 import org.apache.maven.doxia.sink.render.RenderingContext;
35 import org.apache.maven.doxia.site.decoration.Body;
36 import org.apache.maven.doxia.site.decoration.DecorationModel;
37 import org.apache.maven.doxia.siterenderer.Renderer;
38 import org.apache.maven.doxia.siterenderer.RendererException;
39 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
40 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
41 import org.apache.maven.execution.MavenSession;
42 import org.apache.maven.plugin.MojoExecutionException;
43 import org.apache.maven.plugin.logging.Log;
44 import org.apache.maven.project.MavenProject;
45 import org.apache.maven.reporting.AbstractMavenReport;
46 import org.apache.maven.reporting.MavenReportException;
47
48 import de.smartics.maven.util.LoggingUtils;
49 import de.smartics.maven.util.PathUtils;
50 import de.smartics.maven.util.report.ReportUtils;
51
52
53
54
55
56
57
58 public abstract class AbstractElementReport extends AbstractMavenReport
59 {
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 protected MavenProject project;
76
77
78
79
80
81
82
83
84 protected MavenSession session;
85
86
87
88
89
90
91
92
93 protected List<Artifact> pluginArtifacts;
94
95
96
97
98
99
100
101
102 protected Renderer siteRenderer;
103
104
105
106
107
108
109
110
111 protected ArtifactRepository localRepository;
112
113
114
115
116
117
118
119
120 protected ArtifactResolver resolver;
121
122
123
124
125
126
127
128
129 protected ArtifactFactory factory;
130
131
132
133
134
135
136
137
138
139
140
141 protected File javadocDir;
142
143
144
145
146
147
148
149
150
151
152
153
154 protected File outputDirectory;
155
156
157
158
159
160
161
162
163
164
165 protected String logLevel;
166
167
168
169
170
171
172
173
174 protected String locale;
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public String getDescription(final Locale locale)
196 {
197 return getBundle(locale).getString("report.description");
198 }
199
200
201
202
203
204
205 public String getName(final Locale locale)
206 {
207 return getBundle(locale).getString("report.name");
208 }
209
210
211
212
213
214
215
216
217 public MavenProject getProject()
218 {
219 return project;
220 }
221
222
223
224
225
226
227 public void setProject(final MavenProject project)
228 {
229 this.project = project;
230 }
231
232
233
234
235
236
237 protected String getOutputDirectory()
238 {
239 return outputDirectory.getAbsolutePath();
240 }
241
242
243
244
245
246
247 protected Renderer getSiteRenderer()
248 {
249 return siteRenderer;
250 }
251
252
253
254
255
256
257
258
259 public void execute() throws MojoExecutionException
260 {
261 final Log log = getLog();
262 if (!canGenerateReport())
263 {
264 if (log.isInfoEnabled())
265 {
266 log.info("Nothing to generate a properties report. Skipping...");
267 }
268 return;
269 }
270
271 LoggingUtils.configureLogger(log, logLevel);
272
273 provideSink();
274 }
275
276
277
278
279
280
281
282
283
284 protected void provideSink() throws MojoExecutionException
285 {
286 final Locale reportLocale = determineLocale();
287 try
288 {
289 final SiteRenderingContext siteContext =
290 createSiteRenderingContext(reportLocale);
291
292 final RenderingContext context =
293 new RenderingContext(outputDirectory, getOutputName() + ".html");
294
295 final SiteRendererSink sink = new SiteRendererSink(context);
296 generate(sink, null, reportLocale);
297
298 outputDirectory.mkdirs();
299
300 final Writer writer =
301 new FileWriter(new File(outputDirectory, getOutputName() + ".html"));
302
303
304
305 siteRenderer.generateDocument(writer, sink, siteContext);
306
307 siteRenderer.copyResources(siteContext, new File(project.getBasedir(),
308 "src/site/resources"), outputDirectory);
309
310 }
311 catch (final RendererException e)
312 {
313 throw new MojoExecutionException("An error has occurred in "
314 + getName(reportLocale)
315 + " report generation.", e);
316 }
317 catch (final IOException e)
318 {
319 throw new MojoExecutionException("An error has occurred in "
320 + getName(reportLocale)
321 + " report generation.", e);
322 }
323 catch (final MavenReportException e)
324 {
325 throw new MojoExecutionException("An error has occurred in "
326 + getName(reportLocale)
327 + " report generation.", e);
328 }
329 }
330
331 private SiteRenderingContext createSiteRenderingContext(
332 final Locale reportLocale) throws IOException, MojoExecutionException
333 {
334 final DecorationModel model = new DecorationModel();
335 model.setBody(new Body());
336 final Map<String, String> attributes = new HashMap<String, String>();
337 attributes.put("outputEncoding", "UTF-8");
338 final SiteRenderingContext siteContext =
339 siteRenderer.createContextForSkin(ReportUtils.getSkinArtifactFile(
340 project, localRepository, resolver, factory), attributes, model,
341 getName(reportLocale), reportLocale);
342 return siteContext;
343 }
344
345
346
347
348
349
350
351 private Locale determineLocale()
352 {
353 return StringUtils.isNotBlank(this.locale) ? LocaleUtils
354 .toLocale(this.locale) : Locale.getDefault();
355 }
356
357
358
359
360
361
362
363 protected ResourceBundle getBundle(final Locale locale)
364 {
365 return ResourceBundle.getBundle(getBundleName(), locale,
366 AbstractElementReport.class.getClassLoader());
367 }
368
369
370
371
372
373
374
375 protected abstract String getBundleName();
376
377 @Override
378 @SuppressWarnings("unchecked")
379 public boolean canGenerateReport()
380 {
381 final String packaging = project.getPackaging();
382 return !("pom".equals(packaging) || !PathUtils.exists(project
383 .getCompileSourceRoots()));
384 }
385
386
387
388 }