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 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 List<MavenReport> filteredReports = new ArrayList<MavenReport>( reports.size() );
184 for ( MavenReport report : reports )
185 {
186 if ( report.canGenerateReport() )
187 {
188 filteredReports.add( report );
189 }
190 }
191 return filteredReports;
192 }
193
194 protected SiteRenderingContext createSiteRenderingContext( Locale locale )
195 throws MojoExecutionException, IOException, MojoFailureException
196 {
197 if ( attributes == null )
198 {
199 attributes = new HashMap<String, Object>();
200 }
201
202 if ( attributes.get( "project" ) == null )
203 {
204 attributes.put( "project", project );
205 }
206
207 if ( attributes.get( "inputEncoding" ) == null )
208 {
209 attributes.put( "inputEncoding", getInputEncoding() );
210 }
211
212 if ( attributes.get( "outputEncoding" ) == null )
213 {
214 attributes.put( "outputEncoding", getOutputEncoding() );
215 }
216
217
218 for ( Map.Entry<Object, Object> entry : project.getProperties().entrySet() )
219 {
220 attributes.put( (String) entry.getKey(), entry.getValue() );
221 }
222
223 DecorationModel decorationModel;
224 try
225 {
226 decorationModel = siteTool.getDecorationModel( project, reactorProjects, localRepository, repositories,
227 siteTool.getRelativePath( siteDirectory.getAbsolutePath(),
228 project.getBasedir().getAbsolutePath() ),
229 locale, getInputEncoding(), getOutputEncoding() );
230 }
231 catch ( SiteToolException e )
232 {
233 throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
234 }
235
236 if ( relativizeDecorationLinks )
237 {
238 final String url = project.getUrl();
239
240 if ( url == null )
241 {
242 getLog().warn( "No project URL defined - decoration links will not be relativized!" );
243 }
244 else
245 {
246 getLog().info( "Relativizing decoration links with respect to project URL: " + url );
247 assembler.resolvePaths( decorationModel, url );
248 }
249 }
250
251 if ( template != null )
252 {
253 if ( templateFile != null )
254 {
255 getLog().warn( "'template' configuration is ignored when 'templateFile' is set" );
256 }
257 else
258 {
259 templateFile = new File( templateDirectory, template );
260 }
261 }
262
263 File skinFile;
264 try
265 {
266 Artifact skinArtifact =
267 siteTool.getSkinArtifactFromRepository( localRepository, repositories, decorationModel );
268 getLog().info( "Rendering site with " + skinArtifact.getId() + " skin." );
269
270 skinFile = skinArtifact.getFile();
271 }
272 catch ( SiteToolException e )
273 {
274 throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
275 }
276 SiteRenderingContext context;
277 if ( templateFile != null )
278 {
279 if ( !templateFile.exists() )
280 {
281 throw new MojoFailureException( "Template file '" + templateFile + "' does not exist" );
282 }
283 context = siteRenderer.createContextForTemplate( templateFile, skinFile, attributes, decorationModel,
284 project.getName(), locale );
285 }
286 else
287 {
288 context = siteRenderer.createContextForSkin( skinFile, attributes, decorationModel, project.getName(),
289 locale );
290 }
291
292
293 if ( !locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
294 {
295 context.addSiteDirectory( new File( siteDirectory, locale.getLanguage() ) );
296 context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "xdoc" );
297 context.addModuleDirectory( new File( xdocDirectory, locale.getLanguage() ), "fml" );
298 }
299 else
300 {
301 context.addSiteDirectory( siteDirectory );
302 context.addModuleDirectory( xdocDirectory, "xdoc" );
303 context.addModuleDirectory( xdocDirectory, "fml" );
304 }
305
306 if ( moduleExcludes != null )
307 {
308 context.setModuleExcludes( moduleExcludes );
309 }
310
311 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 Map<String, MavenReport> reportsByOutputName = new HashMap<String, MavenReport>();
331 for ( Iterator<MavenReport> i = reports.iterator(); i.hasNext(); )
332 {
333 MavenReport report = i.next();
334
335 String outputName = report.getOutputName() + ".html";
336
337
338 reportsByOutputName.put( report.getOutputName(), report );
339
340 if ( documents.containsKey( outputName ) )
341 {
342 String displayLanguage = locale.getDisplayLanguage( Locale.ENGLISH );
343
344 getLog().info( "Skipped \"" + report.getName( locale ) + "\" report, file \"" + outputName
345 + "\" already exists for the " + displayLanguage + " version." );
346 i.remove();
347 }
348 else
349 {
350 RenderingContext renderingContext = new RenderingContext( siteDirectory, outputName );
351 ReportDocumentRenderer renderer = new ReportDocumentRenderer( report, renderingContext, getLog() );
352 documents.put( outputName, renderer );
353 }
354 }
355 return reportsByOutputName;
356 }
357
358
359
360
361
362
363
364
365 protected Map<String, List<MavenReport>> categoriseReports( Collection<MavenReport> reports )
366 {
367 Map<String, List<MavenReport>> categories = new HashMap<String, List<MavenReport>>();
368 for ( MavenReport report : reports )
369 {
370 List<MavenReport> categoryReports = categories.get( report.getCategoryName() );
371 if ( categoryReports == null )
372 {
373 categoryReports = new ArrayList<MavenReport>();
374 categories.put( report.getCategoryName(), categoryReports );
375 }
376 categoryReports.add( report );
377 }
378 return categories;
379 }
380
381 protected Map<String, DocumentRenderer> locateDocuments( SiteRenderingContext context, List<MavenReport> reports,
382 Locale locale )
383 throws IOException, RendererException
384 {
385 Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles( context );
386
387 Map<String, MavenReport> reportsByOutputName = locateReports( reports, documents, locale );
388
389
390 Map<String, List<MavenReport>> categories = categoriseReports( reportsByOutputName.values() );
391
392 siteTool.populateReportsMenu( context.getDecoration(), locale, categories );
393 populateReportItems( context.getDecoration(), locale, reportsByOutputName );
394
395 if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_INFORMATION ) && generateProjectInfo )
396 {
397 List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_INFORMATION );
398
399 RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-info.html" );
400 String title = i18n.getString( "site-plugin", locale, "report.information.title" );
401 String desc1 = i18n.getString( "site-plugin", locale, "report.information.description1" );
402 String desc2 = i18n.getString( "site-plugin", locale, "report.information.description2" );
403 DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
404 i18n, categoryReports, getLog() );
405
406 if ( !documents.containsKey( renderer.getOutputName() ) )
407 {
408 documents.put( renderer.getOutputName(), renderer );
409 }
410 else
411 {
412 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
413 }
414 }
415
416 if ( categories.containsKey( MavenReport.CATEGORY_PROJECT_REPORTS ) )
417 {
418 List<MavenReport> categoryReports = categories.get( MavenReport.CATEGORY_PROJECT_REPORTS );
419 RenderingContext renderingContext = new RenderingContext( siteDirectory, "project-reports.html" );
420 String title = i18n.getString( "site-plugin", locale, "report.project.title" );
421 String desc1 = i18n.getString( "site-plugin", locale, "report.project.description1" );
422 String desc2 = i18n.getString( "site-plugin", locale, "report.project.description2" );
423 DocumentRenderer renderer = new CategorySummaryDocumentRenderer( renderingContext, title, desc1, desc2,
424 i18n, categoryReports, getLog() );
425
426 if ( !documents.containsKey( renderer.getOutputName() ) )
427 {
428 documents.put( renderer.getOutputName(), renderer );
429 }
430 else
431 {
432 getLog().info( "Category summary '" + renderer.getOutputName() + "' skipped; already exists" );
433 }
434 }
435 return documents;
436 }
437
438 protected void populateReportItems( DecorationModel decorationModel, Locale locale,
439 Map<String, MavenReport> reportsByOutputName )
440 {
441 for ( Menu menu : decorationModel.getMenus() )
442 {
443 populateItemRefs( menu.getItems(), locale, reportsByOutputName );
444 }
445 }
446
447 private void populateItemRefs( List<MenuItem> items, Locale locale, Map<String, MavenReport> reportsByOutputName )
448 {
449 for ( Iterator<MenuItem> i = items.iterator(); i.hasNext(); )
450 {
451 MenuItem item = i.next();
452
453 if ( item.getRef() != null )
454 {
455 MavenReport report = reportsByOutputName.get( item.getRef() );
456
457 if ( report != null )
458 {
459
460 if ( item.getName() == null )
461 {
462 item.setName( report.getName( locale ) );
463 }
464
465 if ( item.getHref() == null || item.getHref().length() == 0 )
466 {
467 item.setHref( report.getOutputName() + ".html" );
468 }
469 }
470 else
471 {
472 getLog().warn( "Unrecognised reference: '" + item.getRef() + "'" );
473 i.remove();
474 }
475 }
476
477 populateItemRefs( item.getItems(), locale, reportsByOutputName );
478 }
479 }
480 }