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.Collections;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Map;
29
30 import org.apache.maven.doxia.siterenderer.DocumentRenderer;
31 import org.apache.maven.doxia.siterenderer.RendererException;
32 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.MojoFailureException;
35 import org.apache.maven.reporting.MavenReport;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class SiteMojo
51 extends AbstractSiteRenderingMojo
52 {
53
54
55
56
57
58 protected File outputDirectory;
59
60
61
62
63
64
65 private boolean generateReports;
66
67
68
69
70
71
72
73 private boolean generateSitemap;
74
75
76
77
78
79
80
81
82
83
84 private boolean validate;
85
86
87
88
89
90
91
92 private boolean skip;
93
94
95
96
97
98
99
100
101
102
103 public void execute()
104 throws MojoExecutionException, MojoFailureException
105 {
106 if ( skip )
107 {
108 getLog().info( "maven.site.skip = true: Skipping site generation" );
109 return;
110 }
111
112 checkMavenVersion();
113
114 List<MavenReport> filteredReports;
115 if ( generateReports )
116 {
117 filteredReports = filterReports( reports );
118 }
119 else
120 {
121 filteredReports = Collections.emptyList();
122 }
123
124 try
125 {
126 List<Locale> localesList = siteTool.getAvailableLocales( locales );
127
128
129 Locale defaultLocale = localesList.get( 0 );
130 Locale.setDefault( defaultLocale );
131
132 for ( Locale locale : localesList )
133 {
134 renderLocale( locale, filteredReports );
135 }
136 }
137 catch ( RendererException e )
138 {
139 throw new MojoExecutionException( "Error during page generation", e );
140 }
141 catch ( IOException e )
142 {
143 throw new MojoExecutionException( "Error during site generation", e );
144 }
145 }
146
147 private void renderLocale( Locale locale, List<MavenReport> reports )
148 throws IOException, RendererException, MojoFailureException, MojoExecutionException
149 {
150 SiteRenderingContext context = createSiteRenderingContext( locale );
151
152 context.setInputEncoding( getInputEncoding() );
153 context.setOutputEncoding( getOutputEncoding() );
154 context.setValidate( validate );
155 if ( validate )
156 {
157 getLog().info( "Validation is switched on, xml input documents will be validated!" );
158 }
159
160 Map<String, DocumentRenderer> documents = locateDocuments( context, reports, locale );
161
162 File outputDir = getOutputDirectory( locale );
163
164
165 for ( MavenReport report : reports )
166 {
167 report.setReportOutputDirectory( outputDir );
168 }
169
170 siteRenderer.render( documents.values(), context, outputDir );
171
172 if ( generateSitemap )
173 {
174 getLog().info( "Generating Sitemap." );
175
176 new SiteMap( getOutputEncoding(), i18n )
177 .generate( context.getDecoration(), generatedSiteDirectory, locale );
178 }
179
180
181 context.getSiteDirectories().clear();
182 context.addSiteDirectory( generatedSiteDirectory );
183
184 documents = siteRenderer.locateDocumentFiles( context );
185
186 siteRenderer.render( documents.values(), context, outputDir );
187 }
188
189 private File getOutputDirectory( Locale locale )
190 {
191 File file;
192 if ( locale.getLanguage().equals( Locale.getDefault().getLanguage() ) )
193 {
194 file = outputDirectory;
195 }
196 else
197 {
198 file = new File( outputDirectory, locale.getLanguage() );
199 }
200
201
202 if ( !file.exists() )
203 {
204 file.mkdirs();
205 }
206
207 return file;
208 }
209 }