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 org.apache.maven.doxia.siterenderer.RendererException; |
23 | |
import org.apache.maven.doxia.siterenderer.SiteRenderingContext; |
24 | |
import org.apache.maven.plugin.MojoExecutionException; |
25 | |
import org.apache.maven.plugin.MojoFailureException; |
26 | |
import org.apache.maven.reporting.MavenReport; |
27 | |
|
28 | |
import java.io.File; |
29 | |
import java.io.IOException; |
30 | |
import java.util.Collections; |
31 | |
import java.util.Iterator; |
32 | |
import java.util.List; |
33 | |
import java.util.Locale; |
34 | |
import java.util.Map; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 1 | public class SiteMojo |
50 | |
extends AbstractSiteRenderingMojo |
51 | |
{ |
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 | |
List filteredReports; |
99 | 0 | if ( generateReports ) |
100 | |
{ |
101 | 0 | filteredReports = filterReports( reports ); |
102 | |
} |
103 | |
else |
104 | |
{ |
105 | 0 | filteredReports = Collections.EMPTY_LIST; |
106 | |
} |
107 | |
|
108 | |
try |
109 | |
{ |
110 | 0 | List localesList = siteTool.getAvailableLocales( locales ); |
111 | |
|
112 | |
|
113 | 0 | Locale defaultLocale = (Locale) localesList.get( 0 ); |
114 | 0 | Locale.setDefault( defaultLocale ); |
115 | |
|
116 | 0 | for ( Iterator iterator = localesList.iterator(); iterator.hasNext(); ) |
117 | |
{ |
118 | 0 | Locale locale = (Locale) iterator.next(); |
119 | |
|
120 | 0 | renderLocale( locale, filteredReports ); |
121 | 0 | } |
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, |
134 | |
List reports ) |
135 | |
throws IOException, RendererException, MojoFailureException, MojoExecutionException |
136 | |
{ |
137 | 0 | SiteRenderingContext context = createSiteRenderingContext( locale ); |
138 | |
|
139 | 0 | context.setInputEncoding( getInputEncoding() ); |
140 | 0 | context.setOutputEncoding( getOutputEncoding() ); |
141 | 0 | context.setValidate( validate ); |
142 | 0 | if ( validate ) |
143 | |
{ |
144 | 0 | getLog().info( "Validation is switched on, xml input documents will be validated!" ); |
145 | |
} |
146 | |
|
147 | 0 | Map documents = locateDocuments( context, reports, locale ); |
148 | |
|
149 | 0 | File outputDir = getOutputDirectory( locale ); |
150 | |
|
151 | |
|
152 | 0 | for ( Iterator i = reports.iterator(); i.hasNext(); ) |
153 | |
{ |
154 | 0 | MavenReport report = (MavenReport) i.next(); |
155 | 0 | report.setReportOutputDirectory( outputDir ); |
156 | 0 | } |
157 | |
|
158 | 0 | siteRenderer.render( documents.values(), context, outputDir ); |
159 | |
|
160 | 0 | if ( generateSitemap ) |
161 | |
{ |
162 | 0 | getLog().info( "Generating Sitemap." ); |
163 | |
|
164 | 0 | new SiteMap( getOutputEncoding(), i18n ) |
165 | |
.generate( context.getDecoration(), generatedSiteDirectory, locale ); |
166 | |
} |
167 | |
|
168 | |
|
169 | 0 | context.getSiteDirectories().clear(); |
170 | 0 | context.addSiteDirectory( generatedSiteDirectory ); |
171 | |
|
172 | 0 | documents = siteRenderer.locateDocumentFiles( context ); |
173 | |
|
174 | 0 | siteRenderer.render( documents.values(), context, outputDir ); |
175 | 0 | } |
176 | |
|
177 | |
private File getOutputDirectory( Locale locale ) |
178 | |
{ |
179 | |
File file; |
180 | 0 | if ( locale.getLanguage().equals( Locale.getDefault().getLanguage() ) ) |
181 | |
{ |
182 | 0 | file = outputDirectory; |
183 | |
} |
184 | |
else |
185 | |
{ |
186 | 0 | file = new File( outputDirectory, locale.getLanguage() ); |
187 | |
} |
188 | |
|
189 | |
|
190 | 0 | if ( !file.exists() ) |
191 | |
{ |
192 | 0 | file.mkdirs(); |
193 | |
} |
194 | |
|
195 | 0 | return file; |
196 | |
} |
197 | |
} |