1 | |
package org.apache.maven.plugins.site; |
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.artifact.factory.ArtifactFactory; |
23 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
24 | |
import org.apache.maven.doxia.module.xhtml.decoration.render.RenderingContext; |
25 | |
import org.apache.maven.doxia.site.decoration.DecorationModel; |
26 | |
import org.apache.maven.doxia.site.decoration.inheritance.DecorationModelInheritanceAssembler; |
27 | |
import org.apache.maven.doxia.siterenderer.DocumentRenderer; |
28 | |
import org.apache.maven.doxia.siterenderer.Renderer; |
29 | |
import org.apache.maven.doxia.siterenderer.RendererException; |
30 | |
import org.apache.maven.doxia.siterenderer.SiteRenderingContext; |
31 | |
import org.apache.maven.doxia.tools.SiteToolException; |
32 | |
import org.apache.maven.plugin.MojoExecutionException; |
33 | |
import org.apache.maven.plugin.MojoFailureException; |
34 | |
import org.apache.maven.reporting.MavenReport; |
35 | |
import org.codehaus.plexus.util.StringUtils; |
36 | |
|
37 | |
import java.io.File; |
38 | |
import java.io.IOException; |
39 | |
import java.util.ArrayList; |
40 | |
import java.util.Collection; |
41 | |
import java.util.HashMap; |
42 | |
import java.util.Iterator; |
43 | |
import java.util.List; |
44 | |
import java.util.Locale; |
45 | |
import java.util.Map; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 2 | public abstract class AbstractSiteRenderingMojo |
54 | |
extends AbstractSiteMojo |
55 | |
{ |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
protected Map moduleExcludes; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
protected DecorationModelInheritanceAssembler assembler; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
protected ArtifactResolver artifactResolver; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
protected List repositories; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
protected ArtifactFactory artifactFactory; |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
private File templateDirectory; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
private String template; |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
private File templateFile; |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
protected Map attributes; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
protected Renderer siteRenderer; |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
protected List reports; |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
private File xdocDirectory; |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
protected File generatedSiteDirectory; |
167 | |
|
168 | |
protected List filterReports( List reports ) |
169 | |
{ |
170 | 0 | List filteredReports = new ArrayList( reports.size() ); |
171 | 0 | for ( Iterator i = reports.iterator(); i.hasNext(); ) |
172 | |
{ |
173 | 0 | MavenReport report = (MavenReport) i.next(); |
174 | |
|
175 | |
try |
176 | |
{ |
177 | 0 | if ( report.canGenerateReport() ) |
178 | |
{ |
179 | 0 | filteredReports.add( report ); |
180 | |
} |
181 | |
} |
182 | 0 | catch ( AbstractMethodError e ) |
183 | |
{ |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | 0 | getLog().warn( |
189 | |
"Error loading report " + report.getClass().getName() |
190 | |
+ " - AbstractMethodError: canGenerateReport()" ); |
191 | 0 | filteredReports.add( report ); |
192 | 0 | } |
193 | |
} |
194 | 0 | return filteredReports; |
195 | |
} |
196 | |
|
197 | |
protected SiteRenderingContext createSiteRenderingContext( Locale locale ) |
198 | |
throws MojoExecutionException, IOException, MojoFailureException |
199 | |
{ |
200 | 0 | if ( attributes == null ) |
201 | |
{ |
202 | 0 | attributes = new HashMap(); |
203 | |
} |
204 | |
|
205 | 0 | if ( attributes.get( "project" ) == null ) |
206 | |
{ |
207 | 0 | attributes.put( "project", project ); |
208 | |
} |
209 | |
|
210 | 0 | if ( attributes.get( "inputEncoding" ) == null ) |
211 | |
{ |
212 | 0 | attributes.put( "inputEncoding", getInputEncoding() ); |
213 | |
} |
214 | |
|
215 | 0 | if ( attributes.get( "outputEncoding" ) == null ) |
216 | |
{ |
217 | 0 | attributes.put( "outputEncoding", getOutputEncoding() ); |
218 | |
} |
219 | |
|
220 | |
|
221 | 0 | attributes.putAll( project.getProperties() ); |
222 | |
|
223 | |
DecorationModel decorationModel; |
224 | |
try |
225 | |
{ |
226 | 0 | decorationModel = siteTool.getDecorationModel( project, reactorProjects, localRepository, repositories, |
227 | |
toRelative( project.getBasedir(), |
228 | |
siteDirectory.getAbsolutePath() ), |
229 | |
locale, getInputEncoding(), getOutputEncoding() ); |
230 | |
} |
231 | 0 | catch ( SiteToolException e ) |
232 | |
{ |
233 | 0 | throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e ); |
234 | 0 | } |
235 | 0 | if ( template != null ) |
236 | |
{ |
237 | 0 | if ( templateFile != null ) |
238 | |
{ |
239 | 0 | getLog().warn( "'template' configuration is ignored when 'templateFile' is set" ); |
240 | |
} |
241 | |
else |
242 | |
{ |
243 | 0 | templateFile = new File( templateDirectory, template ); |
244 | |
} |
245 | |
} |
246 | |
|
247 | |
File skinFile; |
248 | |
try |
249 | |
{ |
250 | 0 | skinFile = siteTool.getSkinArtifactFromRepository( localRepository, repositories, decorationModel ) |
251 | |
.getFile(); |
252 | |
} |
253 | 0 | catch ( SiteToolException e ) |
254 | |
{ |
255 | 0 | throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e ); |
256 | 0 | } |
257 | |
SiteRenderingContext context; |
258 | 0 | if ( templateFile != null ) |
259 | |
{ |
260 | 0 | if ( !templateFile.exists() ) |
261 | |
{ |
262 | 0 | throw new MojoFailureException( "Template file '" + templateFile + "' does not exist" ); |
263 | |
} |
264 | 0 | context = siteRenderer.createContextForTemplate( templateFile, skinFile, attributes, decorationModel, |
265 | |
project.getName(), locale ); |
266 | |
} |
267 | |
else |
268 | |
{ |
269 | 0 | context = siteRenderer.createContextForSkin( skinFile, attributes, decorationModel, project.getName(), |
270 | |
locale ); |
271 | |
} |
272 | |
|
273 | |
|
274 | 0 | if ( !locale.getLanguage().equals( Locale.getDefault().getLanguage() ) ) |
275 | |
{ |
276 | 0 | context.addSiteDirectory( new File( siteDirectory, locale.getLanguage() ) ); |
277 | 0 | context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "xdoc" ); |
278 | 0 | context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "fml" ); |
279 | |
} |
280 | |
else |
281 | |
{ |
282 | 0 | context.addSiteDirectory( siteDirectory ); |
283 | 0 | context.addModuleDirectory( xdocDirectory, "xdoc" ); |
284 | 0 | context.addModuleDirectory( xdocDirectory, "fml" ); |
285 | |
} |
286 | |
|
287 | 0 | if ( moduleExcludes != null ) |
288 | |
{ |
289 | 0 | context.setModuleExcludes( moduleExcludes ); |
290 | |
} |
291 | |
|
292 | 0 | return context; |
293 | |
} |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
protected Map locateReports( List reports, Map documents, Locale locale ) |
308 | |
{ |
309 | 0 | Map reportsByOutputName = new HashMap(); |
310 | 0 | for ( Iterator i = reports.iterator(); i.hasNext(); ) |
311 | |
{ |
312 | 0 | MavenReport report = (MavenReport) i.next(); |
313 | |
|
314 | 0 | String outputName = report.getOutputName() + ".html"; |
315 | |
|
316 | |
|
317 | 0 | reportsByOutputName.put( report.getOutputName(), report ); |
318 | |
|
319 | 0 | if ( documents.containsKey( outputName ) ) |
320 | |
{ |
321 | 0 | String displayLanguage = locale.getDisplayLanguage( Locale.ENGLISH ); |
322 | |
|
323 | 0 | getLog().info( "Skipped \"" + report.getName( locale ) + "\" report, file \"" + outputName |
324 | |
+ "\" already exists for the " + displayLanguage + " version." ); |
325 | 0 | i.remove(); |
326 | |
} |
327 | |
else |
328 | |
{ |
329 | 0 | RenderingContext renderingContext = new RenderingContext( siteDirectory, outputName ); |
330 | 0 | ReportDocumentRenderer renderer = new ReportDocumentRenderer( report, renderingContext, getLog() ); |
331 | 0 | documents.put( outputName, renderer ); |
332 | |
} |
333 | |
} |
334 | 0 | return reportsByOutputName; |
335 | |
} |
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
protected Map categoriseReports( Collection reports ) |
345 | |
{ |
346 | 0 | Map categories = new HashMap(); |
347 | 0 | for ( Iterator i = reports.iterator(); i.hasNext(); ) |
348 | |
{ |
349 | 0 | MavenReport report = (MavenReport) i.next(); |
350 | 0 | List categoryReports = (List) categories.get( report.getCategoryName() ); |
351 | 0 | if ( categoryReports == null ) |
352 | |
{ |
353 | 0 | categoryReports = new ArrayList(); |
354 | 0 | categories.put( report.getCategoryName(), categoryReports ); |
355 | |
} |
356 | 0 | categoryReports.add( report ); |
357 | |
} |
358 | 0 | return categories; |
359 | |
} |
360 | |
|
361 | |
protected Map locateDocuments( SiteRenderingContext context, List reports, Locale locale ) |
362 | |
throws IOException, RendererException |
363 | |
{ |
364 | 0 | Map documents = siteRenderer.locateDocumentFiles( context ); |
365 | |
|
366 | |
|
367 | 0 | Map tmp = new HashMap(); |
368 | 0 | for ( Iterator it = documents.keySet().iterator(); it.hasNext(); ) |
369 | |
{ |
370 | 0 | String key = (String) it.next(); |
371 | 0 | tmp.put( StringUtils.replace( key, "\\", "/" ), documents.get( key ) ); |
372 | |
} |
373 | 0 | documents = tmp; |
374 | |
|
375 | 0 | Map reportsByOutputName = locateReports( reports, documents, locale ); |
376 | |
|
377 | |
|
378 | 0 | Map categories = categoriseReports( reportsByOutputName.values() ); |
379 | |
|
380 | 0 | siteTool.populateReportsMenu( context.getDecoration(), locale, categories ); |
381 | 0 | populateReportItems( context.getDecoration(), locale, reportsByOutputName ); |
382 | |
|
383 | 0 | if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_INFORMATION ) ) |
384 | |
{ |
385 | 0 | List categoryReports = (List) categories.get( MavenReport.CATEGORY_PROJECT_INFORMATION ); |
386 | |
|
387 | 0 | RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-info.html" ); |
388 | 0 | String title = i18n.getString( "site-plugin", locale, "report.information.title" ); |
389 | 0 | String desc1 = i18n.getString( "site-plugin", locale, "report.information.description1" ); |
390 | 0 | String desc2 = i18n.getString( "site-plugin", locale, "report.information.description2" ); |
391 | 0 | DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2, |
392 | |
i18n, categoryReports ); |
393 | |
|
394 | 0 | if ( !documents.containsKey( renderer.getOutputName() ) ) |
395 | |
{ |
396 | 0 | documents.put( renderer.getOutputName(), renderer ); |
397 | |
} |
398 | |
else |
399 | |
{ |
400 | 0 | getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" ); |
401 | |
} |
402 | |
} |
403 | |
|
404 | 0 | if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_REPORTS ) ) |
405 | |
{ |
406 | 0 | List categoryReports = (List) categories.get( MavenReport.CATEGORY_PROJECT_REPORTS ); |
407 | 0 | RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-reports.html" ); |
408 | 0 | String title = i18n.getString( "site-plugin", locale, "report.project.title" ); |
409 | 0 | String desc1 = i18n.getString( "site-plugin", locale, "report.project.description1" ); |
410 | 0 | String desc2 = i18n.getString( "site-plugin", locale, "report.project.description2" ); |
411 | 0 | DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2, |
412 | |
i18n, categoryReports ); |
413 | |
|
414 | 0 | if ( !documents.containsKey( renderer.getOutputName() ) ) |
415 | |
{ |
416 | 0 | documents.put( renderer.getOutputName(), renderer ); |
417 | |
} |
418 | |
else |
419 | |
{ |
420 | 0 | getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" ); |
421 | |
} |
422 | |
} |
423 | 0 | return documents; |
424 | |
} |
425 | |
} |