1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.apidoc;
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 protected boolean skip;
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 @Override
171 protected MavenProject getProject()
172 {
173 return project;
174 }
175
176
177
178
179
180
181 @Override
182 protected Renderer getSiteRenderer()
183 {
184 return siteRenderer;
185 }
186
187
188
189
190
191
192
193
194
195
196 @Override
197 protected String getOutputDirectory()
198 {
199 return outputDirectory.getAbsolutePath();
200 }
201
202
203
204
205
206
207
208
209 public void execute() throws MojoExecutionException
210 {
211 final Log log = getLog();
212 if (!canGenerateReport())
213 {
214 if (log.isInfoEnabled())
215 {
216 log.info("Report '" + getName(Locale.getDefault())
217 + "' skipped due to offline mode.");
218 }
219 return;
220 }
221
222 LoggingUtils.configureLogger(log, logLevel);
223
224 provideSink();
225 }
226
227
228
229
230
231
232
233
234
235 @Override
236 protected void executeReport(final Locale locale) throws MavenReportException
237 {
238 final Log log = getLog();
239 LoggingUtils.configureLogger(log, logLevel);
240 }
241
242
243
244
245
246
247
248
249
250
251 @Override
252 public boolean canGenerateReport()
253 {
254 return !skip;
255 }
256
257
258
259
260
261
262
263
264
265 protected void provideSink() throws MojoExecutionException
266 {
267 final Locale reportLocale = determineLocale();
268
269 try
270 {
271 final DecorationModel model = new DecorationModel();
272 model.setBody(new Body());
273 final Map<String, String> attributes = new HashMap<String, String>();
274 attributes.put("outputEncoding", "UTF-8");
275 final SiteRenderingContext siteContext =
276 siteRenderer.createContextForSkin(ReportUtils.getSkinArtifactFile(
277 project, localRepository, resolver, factory), attributes, model,
278 getName(reportLocale), reportLocale);
279
280 final RenderingContext context =
281 new RenderingContext(outputDirectory, getOutputName() + ".html");
282
283 final SiteRendererSink sink = new SiteRendererSink(context);
284 generate(sink, reportLocale);
285
286 if (!outputDirectory.exists() && !outputDirectory.mkdirs())
287 {
288 throw new IOException("Cannot generate directories '"
289 + outputDirectory.getPath() + "'");
290 }
291
292
293
294 final Writer writer =
295 new FileWriter(new File(outputDirectory, getOutputName() + ".html"));
296 siteRenderer.generateDocument(writer, sink, siteContext);
297
298 siteRenderer.copyResources(siteContext, new File(project.getBasedir(),
299 "src/site/resources"), outputDirectory);
300 }
301 catch (final RendererException e)
302 {
303 throw new MojoExecutionException(createErrorMessage(reportLocale), e);
304 }
305 catch (final IOException e)
306 {
307 throw new MojoExecutionException(createErrorMessage(reportLocale), e);
308 }
309 catch (final MavenReportException e)
310 {
311 throw new MojoExecutionException(createErrorMessage(reportLocale), e);
312 }
313 }
314
315
316
317
318
319
320
321 private String createErrorMessage(final Locale reportLocale)
322 {
323 return "An error has occurred in " + getName(reportLocale)
324 + " report generation.";
325 }
326
327
328
329
330
331
332
333 private Locale determineLocale()
334 {
335 return StringUtils.isNotBlank(this.locale) ? LocaleUtils
336 .toLocale(this.locale) : Locale.getDefault();
337 }
338
339
340
341
342
343
344
345 protected ResourceBundle getBundle(final Locale locale)
346 {
347 return ResourceBundle.getBundle("de.smartics.maven.apidoc.ApidocReport",
348 locale);
349 }
350
351
352
353 }