1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.issues;
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.Locale;
24 import java.util.Map;
25 import java.util.ResourceBundle;
26
27 import org.apache.commons.lang.LocaleUtils;
28 import org.apache.maven.artifact.factory.ArtifactFactory;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.artifact.resolver.ArtifactResolver;
31 import org.apache.maven.doxia.module.xhtml.decoration.render.RenderingContext;
32 import org.apache.maven.doxia.site.decoration.Body;
33 import org.apache.maven.doxia.site.decoration.DecorationModel;
34 import org.apache.maven.doxia.siterenderer.Renderer;
35 import org.apache.maven.doxia.siterenderer.RendererException;
36 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
37 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
38 import org.apache.maven.plugin.MojoExecutionException;
39 import org.apache.maven.plugin.logging.Log;
40 import org.apache.maven.project.MavenProject;
41 import org.apache.maven.reporting.AbstractMavenReport;
42 import org.apache.maven.reporting.MavenReportException;
43 import org.codehaus.plexus.util.StringUtils;
44
45 import de.smartics.maven.util.LoggingUtils;
46 import de.smartics.maven.util.report.ReportUtils;
47
48
49
50
51
52
53
54 public abstract class AbstractReportMojo extends AbstractMavenReport
55 {
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 protected MavenProject project;
72
73
74
75
76
77
78
79
80 protected Renderer siteRenderer;
81
82
83
84
85
86
87
88
89 protected ArtifactRepository localRepository;
90
91
92
93
94
95
96
97
98 protected ArtifactResolver resolver;
99
100
101
102
103
104
105
106
107 protected ArtifactFactory factory;
108
109
110
111
112
113
114
115
116
117
118 protected File outputDirectory;
119
120
121
122
123
124
125
126
127
128
129
130 protected String logLevel;
131
132
133
134
135
136
137
138
139
140 protected String locale;
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 @Override
162 protected MavenProject getProject()
163 {
164 return project;
165 }
166
167
168
169
170
171
172 @Override
173 protected Renderer getSiteRenderer()
174 {
175 return siteRenderer;
176 }
177
178
179
180
181
182
183
184
185
186
187 @Override
188 protected String getOutputDirectory()
189 {
190 return outputDirectory.getAbsolutePath();
191 }
192
193
194
195
196
197
198
199
200 public void execute() throws MojoExecutionException
201 {
202 final Log log = getLog();
203 if (!canGenerateReport())
204 {
205 if (log.isInfoEnabled())
206 {
207 log.info("Report '" + getName(Locale.getDefault())
208 + "' skipped due to offline mode.");
209 }
210 return;
211 }
212
213 LoggingUtils.configureLogger(log, logLevel);
214
215 provideSink();
216 }
217
218
219
220
221
222
223
224
225
226 @Override
227 protected void executeReport(final Locale locale) throws MavenReportException
228 {
229 final Log log = getLog();
230 LoggingUtils.configureLogger(log, logLevel);
231 }
232
233
234
235
236
237
238
239
240
241 protected void provideSink() throws MojoExecutionException
242 {
243 final Locale reportLocale = determineLocale();
244
245 try
246 {
247 final DecorationModel model = new DecorationModel();
248 model.setBody(new Body());
249 final Map<String, String> attributes = new HashMap<String, String>();
250 attributes.put("outputEncoding", "UTF-8");
251 final SiteRenderingContext siteContext =
252 siteRenderer.createContextForSkin(ReportUtils.getSkinArtifactFile(
253 project, localRepository, resolver, factory), attributes, model,
254 getName(reportLocale), reportLocale);
255
256 final RenderingContext context =
257 new RenderingContext(outputDirectory, getOutputName() + ".html");
258
259 final SiteRendererSink sink = new SiteRendererSink(context);
260 generate(sink, reportLocale);
261
262 if (!outputDirectory.mkdirs())
263 {
264 throw new IOException("Cannot generate directories '"
265 + outputDirectory.getPath() + "'");
266 }
267
268
269
270 final Writer writer =
271 new FileWriter(new File(outputDirectory, getOutputName() + ".html"));
272 siteRenderer.generateDocument(writer, sink, siteContext);
273
274 siteRenderer.copyResources(siteContext, new File(project.getBasedir(),
275 "src/site/resources"), outputDirectory);
276 }
277 catch (final RendererException e)
278 {
279 throw new MojoExecutionException(createErrorMessage(reportLocale), e);
280 }
281 catch (final IOException e)
282 {
283 throw new MojoExecutionException(createErrorMessage(reportLocale), e);
284 }
285 catch (final MavenReportException e)
286 {
287 throw new MojoExecutionException(createErrorMessage(reportLocale), e);
288 }
289 }
290
291
292
293
294
295
296
297 private String createErrorMessage(final Locale reportLocale)
298 {
299 return "An error has occurred in " + getName(reportLocale)
300 + " report generation.";
301 }
302
303
304
305
306
307
308
309 private Locale determineLocale()
310 {
311 return StringUtils.isNotBlank(this.locale) ? LocaleUtils
312 .toLocale(this.locale) : Locale.getDefault();
313 }
314
315
316
317
318
319
320
321 protected ResourceBundle getBundle(final Locale locale)
322 {
323 return ResourceBundle.getBundle("de.smartics.maven.issues.IssuesReport",
324 locale);
325 }
326
327
328
329 }