Improved implementation of script/stylesheet object wrapping to make them writable.

This commit is contained in:
rjy7 2010-06-09 16:13:24 +00:00
parent 4c3259163b
commit 1aa4bb00d6
3 changed files with 16 additions and 20 deletions

View file

@ -6,10 +6,8 @@ import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
@ -38,11 +36,12 @@ import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
public class FreeMarkerHttpServlet extends VitroHttpServlet {
@ -137,18 +136,20 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
private void setScriptAndStylesheetObjects(String themeDir) {
// Temporarily switch to an object wrapper that exposes write methods.
// The templates can add files to the script and stylesheet lists
// by calling the add() method.
ObjectWrapper defaultWrapper = config.getObjectWrapper();
config.setObjectWrapper(new DefaultObjectWrapper());
// Use an object wrapper that exposes write methods, instead of the
// configuration's object wrapper, which doesn't, so the templates can
// add stylesheets and scripts to the lists by calling their add() methods.
BeansWrapper wrapper = new DefaultObjectWrapper();
try {
// Here themeDir SHOULD NOT have the context path already added to it.
setSharedVariable("stylesheets", new StylesheetList(themeDir));
setSharedVariable("scripts", new ScriptList());
config.setObjectWrapper(defaultWrapper);
TemplateModel stylesheets = wrapper.wrap(new StylesheetList(themeDir));
setSharedVariable("stylesheets", stylesheets);
TemplateModel scripts = wrapper.wrap(new ScriptList());
setSharedVariable("scripts", scripts);
} catch (TemplateModelException e) {
log.error("Error creating stylesheet and script TemplateModels");
}
}
// Define template locations. Template loader will look first in the theme-specific

View file

@ -70,9 +70,6 @@ public class IndividualView extends ViewObject {
return primaryLink;
}
// RY Here we really want to return List<LinkView> objects. Instead of writing the LinkView class by hand,
// it would be better to use an alternate FreeMarker BeanWrapper that creates a read-only template data object.
// That would also be used to return the lists of object properties and data properties of the individual.
public List<Link> getLinks() {
List<Link> additionalLinks = individual.getLinksList();
List<Link> links = new ArrayList<Link>(additionalLinks.size()+1);

View file

@ -44,8 +44,6 @@ public class VClassGroupView extends ViewObject {
}
public List<VClassView> getClasses() {
// Do we need to store the classes as an instance member? Would we ever access this method more than once per template?
// Don't do this in the constructor, since we might not need it.
if (classes == null) {
List<VClass> classList = vClassGroup.getVitroClassList();
classes = new ArrayList<VClassView>();