1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.site.render;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Map;
29 import java.util.TreeMap;
30
31 import org.apache.maven.doxia.siterenderer.DocumentRenderer;
32 import org.apache.maven.doxia.siterenderer.DoxiaDocumentRenderer;
33 import org.apache.maven.doxia.siterenderer.RendererException;
34 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
35 import org.apache.maven.doxia.tools.SiteTool;
36 import org.apache.maven.execution.MavenSession;
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.apache.maven.plugin.MojoFailureException;
39 import org.apache.maven.plugins.annotations.Mojo;
40 import org.apache.maven.plugins.annotations.Parameter;
41 import org.apache.maven.plugins.annotations.ResolutionScope;
42 import org.apache.maven.project.MavenProject;
43 import org.apache.maven.reporting.MavenReportException;
44 import org.apache.maven.reporting.exec.MavenReportExecution;
45 import org.apache.maven.shared.utils.logging.MessageBuilder;
46
47 import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
48
49
50
51
52
53
54
55
56
57
58
59
60 @Mojo(name = "site", requiresDependencyResolution = ResolutionScope.TEST, requiresReports = true, threadSafe = true)
61 public class SiteMojo extends AbstractSiteRenderingMojo {
62
63
64
65 @Parameter(property = "siteOutputDirectory", defaultValue = "${project.reporting.outputDirectory}")
66 protected File outputDirectory;
67
68
69
70
71 @Parameter(property = "generateReports", defaultValue = "true")
72 private boolean generateReports;
73
74
75
76
77
78
79
80 @Parameter(property = "validate", defaultValue = "false")
81 private boolean validate;
82
83
84
85
86 public void execute() throws MojoExecutionException, MojoFailureException {
87 if (skip) {
88 getLog().info("maven.site.skip = true: Skipping site generation");
89 return;
90 }
91
92 if (getLog().isDebugEnabled()) {
93 getLog().debug("executing Site Mojo");
94 }
95
96 checkInputEncoding();
97
98 try {
99 List<Locale> localesList = getLocales();
100
101 for (Locale locale : localesList) {
102 getLog().info("Rendering site for "
103 + buffer().strong(
104 (!locale.equals(SiteTool.DEFAULT_LOCALE)
105 ? "locale '" + locale + "'"
106 : "default locale"))
107 .toString());
108 File outputDirectory = getOutputDirectory(locale);
109 List<MavenReportExecution> reports =
110 generateReports ? getReports(outputDirectory) : Collections.emptyList();
111 renderLocale(locale, reports, localesList, outputDirectory);
112 }
113 } catch (RendererException e) {
114 if (e.getCause() instanceof MavenReportException) {
115
116 throw new MojoExecutionException(e.getMessage(), e.getCause());
117 }
118 throw new MojoExecutionException("Failed to render site", e);
119 } catch (IOException e) {
120 throw new MojoExecutionException("Error during site generation", e);
121 }
122 }
123
124 private void renderLocale(
125 Locale locale, List<MavenReportExecution> reports, List<Locale> supportedLocales, File outputDirectory)
126 throws IOException, RendererException, MojoFailureException, MojoExecutionException {
127 SiteRenderingContext context = createSiteRenderingContext(locale);
128 context.addSiteLocales(supportedLocales);
129 context.setInputEncoding(getInputEncoding());
130 context.setOutputEncoding(getOutputEncoding());
131 context.setValidate(validate);
132 if (validate) {
133 getLog().info("Validation is switched on, xml input documents will be validated!");
134 }
135
136
137 Map<String, DocumentRenderer> documents = locateDocuments(context, reports, locale);
138
139
140 siteRenderer.copyResources(context, outputDirectory);
141
142
143 List<DocumentRenderer> nonDoxiaDocuments = renderDoxiaDocuments(documents.values(), context, outputDirectory);
144
145
146 renderNonDoxiaDocuments(nonDoxiaDocuments, context, outputDirectory);
147 }
148
149
150
151
152
153
154
155 private List<DocumentRenderer> renderDoxiaDocuments(
156 Collection<DocumentRenderer> documents, SiteRenderingContext context, File outputDirectory)
157 throws RendererException, IOException {
158 List<DocumentRenderer> doxiaDocuments = new ArrayList<>();
159 List<DocumentRenderer> generatedDoxiaDocuments = new ArrayList<>();
160 List<DocumentRenderer> nonDoxiaDocuments = new ArrayList<>();
161
162 Map<String, Integer> counts = new TreeMap<>();
163 Map<String, Integer> generatedCounts = new TreeMap<>();
164
165 for (DocumentRenderer doc : documents) {
166 if (doc instanceof DoxiaDocumentRenderer) {
167 DoxiaDocumentRenderer doxia = (DoxiaDocumentRenderer) doc;
168 boolean editable = doxia.getRenderingContext().isEditable();
169
170 if (editable) {
171 doxiaDocuments.add(doc);
172 } else {
173 generatedDoxiaDocuments.add(doc);
174 }
175
176
177 String parserId = doxia.getRenderingContext().getParserId();
178 Map<String, Integer> actualCounts;
179 if (editable) {
180 actualCounts = counts;
181 } else {
182 actualCounts = generatedCounts;
183 }
184 Integer count = actualCounts.get(parserId);
185 if (count == null) {
186 count = 1;
187 } else {
188 count++;
189 }
190 actualCounts.put(parserId, count);
191 } else {
192 nonDoxiaDocuments.add(doc);
193 }
194 }
195
196 if (doxiaDocuments.size() > 0) {
197 MessageBuilder mb = buffer();
198 mb.a("Rendering ");
199 mb.strong(doxiaDocuments.size() + " Doxia document" + (doxiaDocuments.size() > 1 ? "s" : ""));
200 mb.a(": ");
201
202 boolean first = true;
203 for (Map.Entry<String, Integer> entry : counts.entrySet()) {
204 if (first) {
205 first = false;
206 } else {
207 mb.a(", ");
208 }
209 mb.strong(entry.getValue() + " " + entry.getKey());
210 }
211
212 getLog().info(mb.toString());
213
214 siteRenderer.render(doxiaDocuments, context, outputDirectory);
215 }
216
217 if (generatedDoxiaDocuments.size() > 0) {
218 MessageBuilder mb = buffer();
219 mb.a("Rendering ");
220 mb.strong(generatedDoxiaDocuments.size() + " generated Doxia document"
221 + (generatedDoxiaDocuments.size() > 1 ? "s" : ""));
222 mb.a(": ");
223
224 boolean first = true;
225 for (Map.Entry<String, Integer> entry : generatedCounts.entrySet()) {
226 if (first) {
227 first = false;
228 } else {
229 mb.a(", ");
230 }
231 mb.strong(entry.getValue() + " " + entry.getKey());
232 }
233
234 getLog().info(mb.toString());
235
236 siteRenderer.render(generatedDoxiaDocuments, context, outputDirectory);
237 }
238
239 return nonDoxiaDocuments;
240 }
241
242
243
244
245
246
247 private void renderNonDoxiaDocuments(
248 Collection<DocumentRenderer> documents, SiteRenderingContext context, File outputDirectory)
249 throws RendererException, IOException {
250 Map<String, Integer> counts = new TreeMap<>();
251
252 for (DocumentRenderer doc : documents) {
253 String type;
254 if (doc instanceof ReportDocumentRenderer || doc instanceof SitePluginReportDocumentRenderer) {
255 type = "report";
256 } else {
257 type = "other";
258 }
259
260 Integer count = counts.get(type);
261 if (count == null) {
262 count = 1;
263 } else {
264 count++;
265 }
266 counts.put(type, count);
267 }
268
269 if (documents.size() > 0) {
270 for (Map.Entry<String, Integer> entry : counts.entrySet()) {
271 String type = entry.getKey();
272 Integer count = entry.getValue();
273
274 MessageBuilder mb = buffer();
275 mb.a("Rendering ");
276 mb.strong(count + " " + type + " document" + (count > 1 ? "s" : ""));
277
278 getLog().info(mb.toString());
279 }
280
281 siteRenderer.render(documents, context, outputDirectory);
282 }
283 }
284
285 private File getOutputDirectory(Locale locale) {
286 File file;
287 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
288 file = new File(outputDirectory, locale.toString());
289 } else {
290 file = outputDirectory;
291 }
292
293
294 if (!file.exists()) {
295 file.mkdirs();
296 }
297
298 return file;
299 }
300
301 public MavenProject getProject() {
302 return project;
303 }
304
305 public MavenSession getSession() {
306 return mavenSession;
307 }
308 }