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