1 package org.apache.maven.reporting;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.doxia.sink.Sink;
23 import org.apache.maven.doxia.sink.SinkFactory;
24 import org.apache.maven.doxia.site.decoration.DecorationModel;
25 import org.apache.maven.doxia.siterenderer.Renderer;
26 import org.apache.maven.doxia.siterenderer.RendererException;
27 import org.apache.maven.doxia.siterenderer.RenderingContext;
28 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
29 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugins.annotations.Component;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.shared.utils.WriterFactory;
36 import org.apache.maven.shared.utils.io.IOUtil;
37 import org.codehaus.plexus.util.ReaderFactory;
38
39 import java.io.File;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.io.OutputStreamWriter;
43 import java.io.Writer;
44 import java.util.HashMap;
45 import java.util.Locale;
46 import java.util.Map;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public abstract class AbstractMavenReport
66 extends AbstractMojo
67 implements MavenMultiPageReport
68 {
69
70
71
72
73
74 @Parameter( defaultValue = "${project.reporting.outputDirectory}", readonly = true, required = true )
75 protected File outputDirectory;
76
77
78
79
80 @Parameter( defaultValue = "${project}", readonly = true, required = true )
81 protected MavenProject project;
82
83
84
85
86 @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}", readonly = true )
87 private String inputEncoding;
88
89
90
91
92 @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}", readonly = true )
93 private String outputEncoding;
94
95
96
97
98 @Component
99 protected Renderer siteRenderer;
100
101
102 private Sink sink;
103
104
105 private SinkFactory sinkFactory;
106
107
108 private File reportOutputDirectory;
109
110
111
112
113
114
115
116
117 public void execute()
118 throws MojoExecutionException
119 {
120 if ( !canGenerateReport() )
121 {
122 return;
123 }
124
125 Writer writer = null;
126 try
127 {
128 File outputDirectory = new File( getOutputDirectory() );
129
130 String filename = getOutputName() + ".html";
131
132 Locale locale = Locale.getDefault();
133
134 SiteRenderingContext siteContext = new SiteRenderingContext();
135 siteContext.setDecoration( new DecorationModel() );
136 siteContext.setTemplateName( "org/apache/maven/doxia/siterenderer/resources/default-site.vm" );
137 siteContext.setLocale( locale );
138 siteContext.setTemplateProperties( getTemplateProperties() );
139
140 RenderingContext context = new RenderingContext( outputDirectory, filename );
141
142 SiteRendererSink sink = new SiteRendererSink( context );
143
144 generate( sink, null, locale );
145
146 if ( !isExternalReport() )
147 {
148 outputDirectory.mkdirs();
149
150 writer =
151 new OutputStreamWriter( new FileOutputStream( new File( outputDirectory, filename ) ),
152 getOutputEncoding() );
153
154 getSiteRenderer().generateDocument( writer, sink, siteContext );
155
156
157
158 }
159 }
160 catch ( RendererException e )
161 {
162 throw new MojoExecutionException(
163 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
164 }
165 catch ( IOException e )
166 {
167 throw new MojoExecutionException(
168 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
169 }
170 catch ( MavenReportException e )
171 {
172 throw new MojoExecutionException(
173 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
174 }
175 finally
176 {
177 IOUtil.close( writer );
178 }
179 }
180
181
182
183
184
185
186 private Map<String, Object> getTemplateProperties()
187 {
188 Map<String, Object> templateProperties = new HashMap<String, Object>();
189 templateProperties.put( "project", getProject() );
190 templateProperties.put( "inputEncoding", getInputEncoding() );
191 templateProperties.put( "outputEncoding", getOutputEncoding() );
192
193 for ( Map.Entry<Object, Object> entry : getProject().getProperties().entrySet() )
194 {
195 templateProperties.put( (String) entry.getKey(), entry.getValue() );
196 }
197 return templateProperties;
198 }
199
200
201
202
203
204
205
206
207
208 public void generate( org.codehaus.doxia.sink.Sink sink, Locale locale )
209 throws MavenReportException
210 {
211 generate( sink, null, locale );
212 }
213
214
215
216
217
218
219
220
221
222
223 public void generate( Sink sink, Locale locale )
224 throws MavenReportException
225 {
226 generate( sink, null, locale );
227 }
228
229
230
231
232
233
234
235
236
237 public void generate( Sink sink, SinkFactory sinkFactory, Locale locale )
238 throws MavenReportException
239 {
240 if ( !canGenerateReport() )
241 {
242 getLog().info( "This report cannot be generated as part of the current build. "
243 + "The report name should be referenced in this line of output." );
244 return;
245 }
246
247 this.sink = sink;
248
249 this.sinkFactory = sinkFactory;
250
251 executeReport( locale );
252
253 closeReport();
254 }
255
256
257
258
259
260 public String getCategoryName()
261 {
262 return CATEGORY_PROJECT_REPORTS;
263 }
264
265
266 public File getReportOutputDirectory()
267 {
268 if ( reportOutputDirectory == null )
269 {
270 reportOutputDirectory = new File( getOutputDirectory() );
271 }
272
273 return reportOutputDirectory;
274 }
275
276
277 public void setReportOutputDirectory( File reportOutputDirectory )
278 {
279 this.reportOutputDirectory = reportOutputDirectory;
280 this.outputDirectory = reportOutputDirectory;
281 }
282
283 protected String getOutputDirectory()
284 {
285 return outputDirectory.getAbsolutePath();
286 }
287
288 protected MavenProject getProject()
289 {
290 return project;
291 }
292
293 protected Renderer getSiteRenderer()
294 {
295 return siteRenderer;
296 }
297
298
299
300
301
302
303 protected String getInputEncoding()
304 {
305 return ( inputEncoding == null ) ? ReaderFactory.FILE_ENCODING : inputEncoding;
306 }
307
308
309
310
311
312
313 protected String getOutputEncoding()
314 {
315 return ( outputEncoding == null ) ? WriterFactory.UTF_8 : outputEncoding;
316 }
317
318
319
320
321 protected void closeReport()
322 {
323 getSink().close();
324 }
325
326
327
328
329 public Sink getSink()
330 {
331 return sink;
332 }
333
334
335
336
337 public SinkFactory getSinkFactory()
338 {
339 return sinkFactory;
340 }
341
342
343
344
345
346 public boolean isExternalReport()
347 {
348 return false;
349 }
350
351
352 public boolean canGenerateReport()
353 {
354 return true;
355 }
356
357
358
359
360
361
362
363 protected abstract void executeReport( Locale locale )
364 throws MavenReportException;
365 }