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