Skip to content

Commit

Permalink
[MSITE-1006] MSITE-723 causes duplicate document rendering and log ou…
Browse files Browse the repository at this point in the history
…tput

This closes #183
  • Loading branch information
michael-o committed May 1, 2024
1 parent 86c6d37 commit b3a063b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 94 deletions.
Expand Up @@ -40,6 +40,7 @@
import org.apache.maven.doxia.siterenderer.RendererException;
import org.apache.maven.doxia.siterenderer.SiteRenderer;
import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
import org.apache.maven.doxia.siterenderer.SiteRenderingContext.SiteDirectory;
import org.apache.maven.doxia.tools.SiteTool;
import org.apache.maven.doxia.tools.SiteToolException;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -312,9 +313,11 @@ protected SiteRenderingContext createSiteRenderingContext(Locale locale)
// Generate static site
context.setRootDirectory(project.getBasedir());
if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
context.addSiteDirectory(new File(siteDirectory, locale.toString()));
context.addSiteDirectory(new SiteDirectory(new File(siteDirectory, locale.toString()), true));
context.addSiteDirectory(new SiteDirectory(new File(generatedSiteDirectory, locale.toString()), false));
} else {
context.addSiteDirectory(siteDirectory);
context.addSiteDirectory(new SiteDirectory(siteDirectory, true));
context.addSiteDirectory(new SiteDirectory(generatedSiteDirectory, false));
}

if (moduleExcludes != null) {
Expand Down Expand Up @@ -425,7 +428,7 @@ protected Map<String, List<MavenReport>> categoriseReports(Collection<MavenRepor
protected Map<String, DocumentRenderer> locateDocuments(
SiteRenderingContext context, List<MavenReportExecution> reports, Locale locale)
throws IOException, RendererException {
Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles(context, true);
Map<String, DocumentRenderer> documents = siteRenderer.locateDocumentFiles(context);

Map<String, MavenReport> reportsByOutputName = locateReports(reports, documents, locale);

Expand Down
93 changes: 51 additions & 42 deletions src/main/java/org/apache/maven/plugins/site/render/SiteMojo.java
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -114,7 +115,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// issue caused by report, not really by Doxia Site Renderer
throw new MojoExecutionException(e.getMessage(), e.getCause());
}
throw new MojoExecutionException("Failed to render reports", e);
throw new MojoExecutionException("Failed to render site", e);
} catch (IOException e) {
throw new MojoExecutionException("Error during site generation", e);
}
Expand All @@ -125,45 +126,24 @@ private void renderLocale(
throws IOException, RendererException, MojoFailureException, MojoExecutionException {
SiteRenderingContext context = createSiteRenderingContext(locale);
context.addSiteLocales(supportedLocales);
if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
context.addSiteDirectory(new File(generatedSiteDirectory, locale.toString()));
} else {
context.addSiteDirectory(generatedSiteDirectory);
}

context.setInputEncoding(getInputEncoding());
context.setOutputEncoding(getOutputEncoding());
context.setValidate(validate);
if (validate) {
getLog().info("Validation is switched on, xml input documents will be validated!");
}

// locate all Doxia documents first
Map<String, DocumentRenderer> documents = locateDocuments(context, reports, locale);

// copy resources
siteRenderer.copyResources(context, outputDirectory);

// 1. render Doxia documents first
List<DocumentRenderer> nonDoxiaDocuments = renderDoxiaDocuments(documents, context, outputDirectory, false);
// and finally render Doxia documents
List<DocumentRenderer> nonDoxiaDocuments = renderDoxiaDocuments(documents.values(), context, outputDirectory);

// 2. then non-Doxia documents (e.g., reports)
// then non-Doxia documents (e.g. reports)
renderNonDoxiaDocuments(nonDoxiaDocuments, context, outputDirectory);

// 3. Generated docs must be (re-)done afterwards as they are often generated by reports
context.getSiteDirectories().clear();
if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
context.addSiteDirectory(new File(generatedSiteDirectory, locale.toString()));
} else {
context.addSiteDirectory(generatedSiteDirectory);
}

Map<String, DocumentRenderer> generatedDocuments =
siteRenderer.locateDocumentFiles(context, false /* not editable */);

renderDoxiaDocuments(generatedDocuments, context, outputDirectory, true);

// copy generated resources also
siteRenderer.copyResources(context, outputDirectory);
}

/**
Expand All @@ -173,33 +153,41 @@ private void renderLocale(
* @return the sublist of documents that are not Doxia source files
*/
private List<DocumentRenderer> renderDoxiaDocuments(
Map<String, DocumentRenderer> documents,
SiteRenderingContext context,
File outputDirectory,
boolean generated)
Collection<DocumentRenderer> documents, SiteRenderingContext context, File outputDirectory)
throws RendererException, IOException {
Map<String, DocumentRenderer> doxiaDocuments = new TreeMap<>();
List<DocumentRenderer> doxiaDocuments = new ArrayList<>();
List<DocumentRenderer> generatedDoxiaDocuments = new ArrayList<>();
List<DocumentRenderer> nonDoxiaDocuments = new ArrayList<>();

Map<String, Integer> counts = new TreeMap<>();
Map<String, Integer> generatedCounts = new TreeMap<>();

for (Map.Entry<String, DocumentRenderer> entry : documents.entrySet()) {
DocumentRenderer doc = entry.getValue();

for (DocumentRenderer doc : documents) {
if (doc instanceof DoxiaDocumentRenderer) {
doxiaDocuments.put(entry.getKey(), doc);

DoxiaDocumentRenderer doxia = (DoxiaDocumentRenderer) doc;
boolean editable = doxia.getRenderingContext().isEditable();

if (editable) {
doxiaDocuments.add(doc);
} else {
generatedDoxiaDocuments.add(doc);
}

// count documents per parserId
String parserId = doxia.getRenderingContext().getParserId();
Integer count = counts.get(parserId);
Map<String, Integer> actualCounts;
if (editable) {
actualCounts = counts;
} else {
actualCounts = generatedCounts;
}
Integer count = actualCounts.get(parserId);
if (count == null) {
count = 1;
} else {
count++;
}
counts.put(parserId, count);
actualCounts.put(parserId, count);
} else {
nonDoxiaDocuments.add(doc);
}
Expand All @@ -208,8 +196,7 @@ private List<DocumentRenderer> renderDoxiaDocuments(
if (doxiaDocuments.size() > 0) {
MessageBuilder mb = buffer();
mb.a("Rendering ");
mb.strong(doxiaDocuments.size() + (generated ? " generated" : "") + " Doxia document"
+ (doxiaDocuments.size() > 1 ? "s" : ""));
mb.strong(doxiaDocuments.size() + " Doxia document" + (doxiaDocuments.size() > 1 ? "s" : ""));
mb.a(": ");

boolean first = true;
Expand All @@ -224,7 +211,29 @@ private List<DocumentRenderer> renderDoxiaDocuments(

getLog().info(mb.toString());

siteRenderer.render(doxiaDocuments.values(), context, outputDirectory);
siteRenderer.render(doxiaDocuments, context, outputDirectory);
}

if (generatedDoxiaDocuments.size() > 0) {
MessageBuilder mb = buffer();
mb.a("Rendering ");
mb.strong(generatedDoxiaDocuments.size() + " generated Doxia document"
+ (generatedDoxiaDocuments.size() > 1 ? "s" : ""));
mb.a(": ");

boolean first = true;
for (Map.Entry<String, Integer> entry : generatedCounts.entrySet()) {
if (first) {
first = false;
} else {
mb.a(", ");
}
mb.strong(entry.getValue() + " " + entry.getKey());
}

getLog().info(mb.toString());

siteRenderer.render(generatedDoxiaDocuments, context, outputDirectory);
}

return nonDoxiaDocuments;
Expand All @@ -236,7 +245,7 @@ private List<DocumentRenderer> renderDoxiaDocuments(
* @param documents a collection of documents containing non-Doxia source files
*/
private void renderNonDoxiaDocuments(
List<DocumentRenderer> documents, SiteRenderingContext context, File outputDirectory)
Collection<DocumentRenderer> documents, SiteRenderingContext context, File outputDirectory)
throws RendererException, IOException {
Map<String, Integer> counts = new TreeMap<>();

Expand Down
17 changes: 1 addition & 16 deletions src/main/java/org/apache/maven/plugins/site/run/DoxiaBean.java
Expand Up @@ -34,20 +34,13 @@ public class DoxiaBean {

private Map<String, DocumentRenderer> documents;

private SiteRenderingContext generatedSiteContext;

/**
* @param context context
* @param documents documents
* @param generatedSiteContext context of generated content
*/
public DoxiaBean(
SiteRenderingContext context,
Map<String, DocumentRenderer> documents,
SiteRenderingContext generatedSiteContext) {
public DoxiaBean(SiteRenderingContext context, Map<String, DocumentRenderer> documents) {
this.context = context;
this.documents = documents;
this.generatedSiteContext = generatedSiteContext;
}

public SiteRenderingContext getContext() {
Expand All @@ -65,12 +58,4 @@ public Map<String, DocumentRenderer> getDocuments() {
public void setDocuments(Map<String, DocumentRenderer> documents) {
this.documents = documents;
}

public SiteRenderingContext getGeneratedSiteContext() {
return generatedSiteContext;
}

public void setGeneratedSiteContext(SiteRenderingContext generatedSiteContext) {
this.generatedSiteContext = generatedSiteContext;
}
}
22 changes: 0 additions & 22 deletions src/main/java/org/apache/maven/plugins/site/run/DoxiaFilter.java
Expand Up @@ -108,7 +108,6 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
// Handle locale request
SiteRenderingContext context;
Map<String, DocumentRenderer> documents;
SiteRenderingContext generatedSiteContext;

String localeWanted = "";
for (Locale locale : localesList) {
Expand All @@ -132,7 +131,6 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
}
context = doxiaBean.getContext();
documents = doxiaBean.getDocuments();
generatedSiteContext = doxiaBean.getGeneratedSiteContext();

// ----------------------------------------------------------------------
// Handle report and documents
Expand Down Expand Up @@ -164,26 +162,6 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
} catch (RendererException e) {
throw new ServletException(e);
}
} else if (generatedSiteContext != null) {
try {
Map<String, DocumentRenderer> locateDocuments =
siteRenderer.locateDocumentFiles(generatedSiteContext, false);

if (locateDocuments.containsKey(path)) {
DocumentRenderer docRenderer = locateDocuments.get(path);
logDocumentRenderer(path, localeWanted, docRenderer);
String outputName = docRenderer.getOutputName();
String contentType = MimeTypes.getDefaultMimeByExtension(outputName);
if (contentType != null) {
servletResponse.setContentType(contentType);
}
docRenderer.renderDocument(servletResponse.getWriter(), siteRenderer, generatedSiteContext);

return;
}
} catch (RendererException e) {
throw new ServletException(e);
}
}

filterChain.doFilter(servletRequest, servletResponse);
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/org/apache/maven/plugins/site/run/SiteRunMojo.java
Expand Up @@ -133,21 +133,11 @@ private WebAppContext createWebApplication() throws MojoExecutionException {
i18nContext.setInputEncoding(getInputEncoding());
i18nContext.setOutputEncoding(getOutputEncoding());

SiteRenderingContext i18nGeneratedSiteContext = createSiteRenderingContext(locale);
i18nGeneratedSiteContext.setInputEncoding(getInputEncoding());
i18nGeneratedSiteContext.setOutputEncoding(getOutputEncoding());
i18nGeneratedSiteContext.getSiteDirectories().clear();

File outputDirectory = getOutputDirectory(locale);
List<MavenReportExecution> reports = getReports(outputDirectory);

Map<String, DocumentRenderer> i18nDocuments = locateDocuments(i18nContext, reports, locale);
if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
i18nGeneratedSiteContext.addSiteDirectory(new File(generatedSiteDirectory, locale.toString()));
} else {
i18nGeneratedSiteContext.addSiteDirectory(generatedSiteDirectory);
}
DoxiaBean doxiaBean = new DoxiaBean(i18nContext, i18nDocuments, i18nGeneratedSiteContext);
DoxiaBean doxiaBean = new DoxiaBean(i18nContext, i18nDocuments);

if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
i18nDoxiaContexts.put(locale.toString(), doxiaBean);
Expand Down

0 comments on commit b3a063b

Please sign in to comment.