Skip to content

Commit

Permalink
Magic
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Apr 18, 2024
1 parent 4c160ac commit bc03519
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 87 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -198,8 +198,8 @@ under the License.
<javaVersion>8</javaVersion>
<!-- for dependencies -->
<jettyVersion>9.4.53.v20231009</jettyVersion>
<doxiaVersion>2.0.0-M8</doxiaVersion>
<doxiaSitetoolsVersion>2.0.0-M16</doxiaSitetoolsVersion>
<doxiaVersion>2.0.0-M10</doxiaVersion>
<doxiaSitetoolsVersion>2.0.0-M18-SNAPSHOT</doxiaSitetoolsVersion>
<wagonVersion>3.5.3</wagonVersion>
<slf4jVersion>1.7.36</slf4jVersion>
<!-- for ITs -->
Expand Down
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
87 changes: 52 additions & 35 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 @@ -132,32 +133,16 @@ private void renderLocale(
getLog().info("Validation is switched on, xml input documents will be validated!");
}

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

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

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

context.getSiteDirectories().clear();
if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
context.addSiteDirectory(new File(generatedSiteDirectory, locale.toString()));
} else {
context.addSiteDirectory(generatedSiteDirectory);
}

// 2.1 locate generated (non-editable) Doxia documents
Map<String, DocumentRenderer> generatedDocuments =
siteRenderer.locateDocumentFiles(context, false /* not editable */);

// 2.2 copy generated resources also
siteRenderer.copyResources(context, outputDirectory);
// 2.3 render generated (non-editable) Doxia documents
renderDoxiaDocuments(generatedDocuments, context, outputDirectory, true);

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

Expand All @@ -168,33 +153,43 @@ private void renderLocale(
* @return the sublist of documents that are not Doxia source files
*/
private List<DocumentRenderer> renderDoxiaDocuments(
Map<String, DocumentRenderer> documents,
Collection<DocumentRenderer> documents,
SiteRenderingContext context,
File outputDirectory,
boolean generated)
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 @@ -203,7 +198,7 @@ private List<DocumentRenderer> renderDoxiaDocuments(
if (doxiaDocuments.size() > 0) {
MessageBuilder mb = buffer();
mb.a("Rendering ");
mb.strong(doxiaDocuments.size() + (generated ? " generated" : "") + " Doxia document"
mb.strong(doxiaDocuments.size() + " Doxia document"
+ (doxiaDocuments.size() > 1 ? "s" : ""));
mb.a(": ");

Expand All @@ -219,7 +214,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 @@ -231,7 +248,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
15 changes: 1 addition & 14 deletions src/main/java/org/apache/maven/plugins/site/run/DoxiaBean.java
Expand Up @@ -34,20 +34,15 @@ 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) {
Map<String, DocumentRenderer> documents) {
this.context = context;
this.documents = documents;
this.generatedSiteContext = generatedSiteContext;
}

public SiteRenderingContext getContext() {
Expand All @@ -65,12 +60,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 bc03519

Please sign in to comment.