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