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