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