From 1aa4bb00d67c2cbb54e32e5542c5bee6c92ca45c Mon Sep 17 00:00:00 2001 From: rjy7 Date: Wed, 9 Jun 2010 16:13:24 +0000 Subject: [PATCH] Improved implementation of script/stylesheet object wrapping to make them writable. --- .../freemarker/FreeMarkerHttpServlet.java | 31 ++++++++++--------- .../vitro/webapp/view/IndividualView.java | 3 -- .../vitro/webapp/view/VClassGroupView.java | 2 -- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java index 669d58e51..206fc3864 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java @@ -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()); - - // Here themeDir SHOULD NOT have the context path already added to it. - setSharedVariable("stylesheets", new StylesheetList(themeDir)); - setSharedVariable("scripts", new ScriptList()); - - config.setObjectWrapper(defaultWrapper); - + // 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. + 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 diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/IndividualView.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/IndividualView.java index b9de51583..0dd1706e1 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/IndividualView.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/IndividualView.java @@ -70,9 +70,6 @@ public class IndividualView extends ViewObject { return primaryLink; } - // RY Here we really want to return List 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 getLinks() { List additionalLinks = individual.getLinksList(); List links = new ArrayList(additionalLinks.size()+1); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/VClassGroupView.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/VClassGroupView.java index 08b30b10a..063e8b32e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/VClassGroupView.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/VClassGroupView.java @@ -44,8 +44,6 @@ public class VClassGroupView extends ViewObject { } public List 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 classList = vClassGroup.getVitroClassList(); classes = new ArrayList();