FreemarkerPresentationManager now loads Freemarker templates for applications from theme
git-svn-id: https://svn.libreccm.org/ccm/trunk@5824 8810af33-2d31-482b-a856-94f89814c4dfmaster
parent
c814f32d5a
commit
4452fb6fa6
|
|
@ -10,14 +10,22 @@ import com.arsdigita.xml.Document;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
|
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
|
||||||
|
import freemarker.ext.dom.NodeModel;
|
||||||
|
import freemarker.template.Configuration;
|
||||||
|
import freemarker.template.Template;
|
||||||
|
import freemarker.template.TemplateException;
|
||||||
import org.libreccm.theming.manifest.ThemeManifest;
|
import org.libreccm.theming.manifest.ThemeManifest;
|
||||||
import org.libreccm.theming.manifest.ThemeManifestUtil;
|
import org.libreccm.theming.manifest.ThemeManifestUtil;
|
||||||
|
import org.w3c.dom.NamedNodeMap;
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
@ -137,61 +145,140 @@ public class FreeMarkerPresentationManager implements PresentationManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToDo
|
// ToDo
|
||||||
// Get Freemarker templates by File API or by HTTP?
|
final NamedNodeMap pageAttrs = root.getAttributes();
|
||||||
// Or via getResourceAsStream?
|
final Node applicationNameAttr = pageAttrs.getNamedItem("application");
|
||||||
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
|
final Node applicationClassAttr = pageAttrs.getNamedItem("class");
|
||||||
response.setContentType("text/plain");
|
final String applicationName = applicationNameAttr.getNodeValue();
|
||||||
try (PrintWriter writer = response.getWriter()) {
|
final String applicationClass = applicationClassAttr.getNodeValue();
|
||||||
writer.append("Data:\n");
|
|
||||||
writer
|
final Optional<ApplicationTemplate> applicationTemplate
|
||||||
.append("Current Site Name: ")
|
= findApplicationTemplate(
|
||||||
.append(currentSiteName).append("\n");
|
templates,
|
||||||
writer
|
applicationName,
|
||||||
.append("isSubSite: ")
|
applicationClass);
|
||||||
.append(Boolean.toString(isSubSite))
|
final String applicationTemplatePath;
|
||||||
.append("\n");
|
if (applicationTemplate.isPresent()) {
|
||||||
writer
|
applicationTemplatePath = applicationTemplate.get().getTemplate();
|
||||||
.append("default theme: ")
|
} else {
|
||||||
.append(defaultTheme)
|
applicationTemplatePath = templates.getDefaultApplicationTemplate();
|
||||||
.append("\n");
|
}
|
||||||
writer
|
|
||||||
.append("selected theme: ")
|
final Configuration configuration = new Configuration(
|
||||||
.append(selectedTheme)
|
Configuration.VERSION_2_3_28);
|
||||||
.append("\n");
|
configuration.setServletContextForTemplateLoading(servletContext,
|
||||||
writer
|
themePath);
|
||||||
.append("preview theme? ")
|
configuration.setDefaultEncoding("UTF-8");
|
||||||
.append(Boolean.toString(preview))
|
|
||||||
.append("\n");
|
final Map<String, Object> data = new HashMap<>();
|
||||||
writer
|
data.put("ccm", NodeModel.wrap(root));
|
||||||
.append("themePath: ")
|
|
||||||
.append(themePath)
|
final Template template;
|
||||||
.append("\n");
|
try {
|
||||||
writer
|
template = configuration.getTemplate(applicationTemplatePath);
|
||||||
.append("themeManifestPath: ")
|
|
||||||
.append(themeManifestPath)
|
|
||||||
.append("\n");
|
|
||||||
writer
|
|
||||||
.append("themeManifest: ")
|
|
||||||
.append(manifest.toString())
|
|
||||||
.append("\n");
|
|
||||||
writer
|
|
||||||
.append("theme name: ")
|
|
||||||
.append(manifest.getName())
|
|
||||||
.append("\n");
|
|
||||||
writer
|
|
||||||
.append("Application templates:\n");
|
|
||||||
for (final ApplicationTemplate template : templates
|
|
||||||
.getApplications()) {
|
|
||||||
writer
|
|
||||||
.append("\t")
|
|
||||||
.append(template.toString())
|
|
||||||
.append("\n");
|
|
||||||
}
|
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw new UncheckedWrapperException(ex);
|
throw new UncheckedWrapperException(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
|
||||||
|
response.setContentType("text/html");
|
||||||
|
|
||||||
|
try (PrintWriter writer = response.getWriter()) {
|
||||||
|
|
||||||
|
template.process(data, writer);
|
||||||
|
|
||||||
|
// writer.append("Data:\n");
|
||||||
|
// writer
|
||||||
|
// .append("Current Site Name: ")
|
||||||
|
// .append(currentSiteName).append("\n");
|
||||||
|
// writer
|
||||||
|
// .append("isSubSite: ")
|
||||||
|
// .append(Boolean.toString(isSubSite))
|
||||||
|
// .append("\n");
|
||||||
|
// writer
|
||||||
|
// .append("default theme: ")
|
||||||
|
// .append(defaultTheme)
|
||||||
|
// .append("\n");
|
||||||
|
// writer
|
||||||
|
// .append("selected theme: ")
|
||||||
|
// .append(selectedTheme)
|
||||||
|
// .append("\n");
|
||||||
|
// writer
|
||||||
|
// .append("preview theme? ")
|
||||||
|
// .append(Boolean.toString(preview))
|
||||||
|
// .append("\n");
|
||||||
|
// writer
|
||||||
|
// .append("themePath: ")
|
||||||
|
// .append(themePath)
|
||||||
|
// .append("\n");
|
||||||
|
// writer
|
||||||
|
// .append("themeManifestPath: ")
|
||||||
|
// .append(themeManifestPath)
|
||||||
|
// .append("\n");
|
||||||
|
// writer
|
||||||
|
// .append("themeManifest: ")
|
||||||
|
// .append(manifest.toString())
|
||||||
|
// .append("\n");
|
||||||
|
// writer
|
||||||
|
// .append("theme name: ")
|
||||||
|
// .append(manifest.getName())
|
||||||
|
// .append("\n");
|
||||||
|
// writer
|
||||||
|
// .append("Application name: ")
|
||||||
|
// .append(applicationName)
|
||||||
|
// .append("\n");
|
||||||
|
// writer
|
||||||
|
// .append("Application class: ")
|
||||||
|
// .append(applicationClass)
|
||||||
|
// .append("\n");
|
||||||
|
// writer
|
||||||
|
// .append("Application templates:\n");
|
||||||
|
// for (final ApplicationTemplate template : templates
|
||||||
|
// .getApplications()) {
|
||||||
|
// writer
|
||||||
|
// .append("\t")
|
||||||
|
// .append(template.toString())
|
||||||
|
// .append("\n");
|
||||||
|
// }
|
||||||
|
} catch (IOException | TemplateException ex) {
|
||||||
|
throw new UncheckedWrapperException(ex);
|
||||||
|
}
|
||||||
|
|
||||||
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Optional<ApplicationTemplate> findApplicationTemplate(
|
||||||
|
final Templates templates,
|
||||||
|
final String applicationName,
|
||||||
|
final String applicationClass) {
|
||||||
|
|
||||||
|
final Optional<ApplicationTemplate> forNameAndClass = templates
|
||||||
|
.getApplications()
|
||||||
|
.stream()
|
||||||
|
.filter(template -> filterApplicationTemplates(template,
|
||||||
|
applicationName,
|
||||||
|
applicationClass))
|
||||||
|
.findAny();
|
||||||
|
if (forNameAndClass.isPresent()) {
|
||||||
|
return forNameAndClass;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
final Optional<ApplicationTemplate> forName = templates
|
||||||
|
.getApplications()
|
||||||
|
.stream()
|
||||||
|
.filter(tpl -> tpl.getApplicationName().equals(applicationName))
|
||||||
|
.findAny();
|
||||||
|
|
||||||
|
return forName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean filterApplicationTemplates(
|
||||||
|
final ApplicationTemplate template,
|
||||||
|
final String applicationName,
|
||||||
|
final String applicationClass) {
|
||||||
|
|
||||||
|
return template.getApplicationName().equals(applicationName)
|
||||||
|
&& template.getApplicationClass().equals(applicationClass);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,10 +30,18 @@ public class Templates {
|
||||||
@XmlElement(name = "applications", namespace = THEMES_XML_NS)
|
@XmlElement(name = "applications", namespace = THEMES_XML_NS)
|
||||||
private List<ApplicationTemplate> applications;
|
private List<ApplicationTemplate> applications;
|
||||||
|
|
||||||
|
@XmlElement(name = "default-application-template",
|
||||||
|
namespace = THEMES_XML_NS)
|
||||||
|
private String defaultApplicationTemplate;
|
||||||
|
|
||||||
@XmlElementWrapper(name = "contentitems", namespace = THEMES_XML_NS)
|
@XmlElementWrapper(name = "contentitems", namespace = THEMES_XML_NS)
|
||||||
@XmlElement(name = "contentitems", namespace = THEMES_XML_NS)
|
@XmlElement(name = "contentitems", namespace = THEMES_XML_NS)
|
||||||
private List<ContentItemTemplate> contentItems;
|
private List<ContentItemTemplate> contentItems;
|
||||||
|
|
||||||
|
@XmlElement(name = "default-contentitem-template",
|
||||||
|
namespace = THEMES_XML_NS)
|
||||||
|
private String defaultContentItemsTemplate;
|
||||||
|
|
||||||
public Templates() {
|
public Templates() {
|
||||||
|
|
||||||
applications = new ArrayList<>();
|
applications = new ArrayList<>();
|
||||||
|
|
@ -60,6 +68,15 @@ public class Templates {
|
||||||
this.applications = new ArrayList<>(applications);
|
this.applications = new ArrayList<>(applications);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getDefaultApplicationTemplate() {
|
||||||
|
return defaultApplicationTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDefaultApplicationTemplate(
|
||||||
|
final String defaultApplicationTemplate) {
|
||||||
|
this.defaultApplicationTemplate = defaultApplicationTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
public List<ContentItemTemplate> getContentItems() {
|
public List<ContentItemTemplate> getContentItems() {
|
||||||
|
|
||||||
return Collections.unmodifiableList(contentItems);
|
return Collections.unmodifiableList(contentItems);
|
||||||
|
|
@ -80,4 +97,12 @@ public class Templates {
|
||||||
this.contentItems = new ArrayList<>(contentItems);
|
this.contentItems = new ArrayList<>(contentItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getDefaultContentItemsTemplate() {
|
||||||
|
return defaultContentItemsTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDefaultContentItemsTemplate(
|
||||||
|
final String defaultContentItemsTemplate) {
|
||||||
|
this.defaultContentItemsTemplate = defaultContentItemsTemplate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue