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 | 1 | 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 | |
|
93 | |
|
94 | |
|
95 | |
public void execute() |
96 | |
throws MojoExecutionException, MojoFailureException |
97 | |
{ |
98 | 0 | checkMavenVersion(); |
99 | |
|
100 | |
List<MavenReport> filteredReports; |
101 | 0 | if ( generateReports ) |
102 | |
{ |
103 | 0 | filteredReports = filterReports( reports ); |
104 | |
} |
105 | |
else |
106 | |
{ |
107 | 0 | filteredReports = Collections.emptyList(); |
108 | |
} |
109 | |
|
110 | |
try |
111 | |
{ |
112 | 0 | List<Locale> localesList = siteTool.getAvailableLocales( locales ); |
113 | |
|
114 | |
|
115 | 0 | Locale defaultLocale = localesList.get( 0 ); |
116 | 0 | Locale.setDefault( defaultLocale ); |
117 | |
|
118 | 0 | for ( Locale locale : localesList ) |
119 | |
{ |
120 | 0 | renderLocale( locale, filteredReports ); |
121 | |
} |
122 | |
} |
123 | 0 | catch ( RendererException e ) |
124 | |
{ |
125 | 0 | throw new MojoExecutionException( "Error during page generation", e ); |
126 | |
} |
127 | 0 | catch ( IOException e ) |
128 | |
{ |
129 | 0 | throw new MojoExecutionException( "Error during site generation", e ); |
130 | 0 | } |
131 | 0 | } |
132 | |
|
133 | |
private void renderLocale( Locale locale, List<MavenReport> reports ) |
134 | |
throws IOException, RendererException, MojoFailureException, MojoExecutionException |
135 | |
{ |
136 | 0 | SiteRenderingContext context = createSiteRenderingContext( locale ); |
137 | |
|
138 | 0 | context.setInputEncoding( getInputEncoding() ); |
139 | 0 | context.setOutputEncoding( getOutputEncoding() ); |
140 | 0 | context.setValidate( validate ); |
141 | 0 | if ( validate ) |
142 | |
{ |
143 | 0 | getLog().info( "Validation is switched on, xml input documents will be validated!" ); |
144 | |
} |
145 | |
|
146 | 0 | Map<String, DocumentRenderer> documents = locateDocuments( context, reports, locale ); |
147 | |
|
148 | 0 | File outputDir = getOutputDirectory( locale ); |
149 | |
|
150 | |
|
151 | 0 | for ( MavenReport report : reports ) |
152 | |
{ |
153 | 0 | report.setReportOutputDirectory( outputDir ); |
154 | |
} |
155 | |
|
156 | 0 | siteRenderer.render( documents.values(), context, outputDir ); |
157 | |
|
158 | 0 | if ( generateSitemap ) |
159 | |
{ |
160 | 0 | getLog().info( "Generating Sitemap." ); |
161 | |
|
162 | 0 | new SiteMap( getOutputEncoding(), i18n ) |
163 | |
.generate( context.getDecoration(), generatedSiteDirectory, locale ); |
164 | |
} |
165 | |
|
166 | |
|
167 | 0 | context.getSiteDirectories().clear(); |
168 | 0 | context.addSiteDirectory( generatedSiteDirectory ); |
169 | |
|
170 | 0 | documents = siteRenderer.locateDocumentFiles( context ); |
171 | |
|
172 | 0 | siteRenderer.render( documents.values(), context, outputDir ); |
173 | 0 | } |
174 | |
|
175 | |
private File getOutputDirectory( Locale locale ) |
176 | |
{ |
177 | |
File file; |
178 | 0 | if ( locale.getLanguage().equals( Locale.getDefault().getLanguage() ) ) |
179 | |
{ |
180 | 0 | file = outputDirectory; |
181 | |
} |
182 | |
else |
183 | |
{ |
184 | 0 | file = new File( outputDirectory, locale.getLanguage() ); |
185 | |
} |
186 | |
|
187 | |
|
188 | 0 | if ( !file.exists() ) |
189 | |
{ |
190 | 0 | file.mkdirs(); |
191 | |
} |
192 | |
|
193 | 0 | return file; |
194 | |
} |
195 | |
} |