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