From bda278e59bfeef6741a1e88ca9251319bf5e28dc Mon Sep 17 00:00:00 2001 From: rjy7 Date: Fri, 21 May 2010 15:55:26 +0000 Subject: [PATCH] Implement StylesheetList and ScriptList view objects and new strategy for templates to direct stylesheets and script files to be loaded onto a page. --- .../freemarker/BrowseController.java | 1 - .../freemarker/FreeMarkerHttpServlet.java | 58 +++++++------------ .../mannlib/vitro/webapp/view/VClassView.java | 2 +- .../mannlib/vitro/webapp/view/ViewObject.java | 2 +- .../vitro/webapp/view/fileList/FileList.java | 51 ++++++++++++++++ .../webapp/view/fileList/ScriptList.java | 22 +++++++ .../webapp/view/fileList/StylesheetList.java | 23 ++++++++ .../web/templates/freemarker/body/about.ftl | 1 - .../templates/freemarker/body/classGroups.ftl | 3 +- .../freemarker/body/commentForm/form.ftl | 11 ++-- .../freemarker/components/scripts.ftl | 3 +- .../freemarker/components/stylesheets.ftl | 10 +--- .../web/templates/freemarker/page/default.ftl | 7 +++ 13 files changed, 138 insertions(+), 56 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/FileList.java create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/ScriptList.java create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/StylesheetList.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/BrowseController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/BrowseController.java index 25e5ff016..5ccca87f1 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/BrowseController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/BrowseController.java @@ -82,7 +82,6 @@ public class BrowseController extends FreeMarkerHttpServlet { String message = ""; List groups = getGroups(vreq.getWebappDaoFactory().getVClassGroupDao(), vreq.getPortal().getPortalId()); - if (groups == null || groups.isEmpty()) { message = "There are not yet any items in the system."; body.put("message", message); 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 cd8561427..67d9a4cb1 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 @@ -34,9 +34,12 @@ import edu.cornell.mannlib.vitro.webapp.controller.ContactMailServlet; import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.utils.StringUtils; +import edu.cornell.mannlib.vitro.webapp.view.fileList.ScriptList; +import edu.cornell.mannlib.vitro.webapp.view.fileList.StylesheetList; import edu.cornell.mannlib.vitro.webapp.view.menu.TabMenu; import edu.cornell.mannlib.vitro.webapp.web.BreadCrumbsUtil; import edu.cornell.mannlib.vitro.webapp.web.PortalWebUtil; + import freemarker.cache.ClassTemplateLoader; import freemarker.cache.FileTemplateLoader; import freemarker.cache.MultiTemplateLoader; @@ -67,7 +70,7 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet { // a getBody() and getTitle() method and use the parent doGet() method. public void doGet( HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException { - + try { callSuperGet(request, response); // ?? doSetup(request, response); @@ -137,33 +140,8 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet { protected String mergeBodyToTemplate(String templateName, Map map) { templateName = "body/" + templateName; String body = mergeToTemplate(templateName, map).toString(); - extractLinkTagsFromBody(body); return body; } - - // This is the only way to do this in FreeMarker. We cannot: (1) put a sequence of stylesheets in the template - // context which the template can add to, because the template cannot call methods on the container. The template - // can create a container but not add to one. - // (2) create a sequence of stylesheets or a scalar to hold the name of a stylesheet in the template, because - // it does not get passed back to the controller. The template can create only local variables. - - // *** RY But we can create a view object with an add method, that the templates could use to add to the - // list. *** - private String extractLinkTagsFromBody(String body) { - List links = new ArrayList(); - - String re = "]*>"; - Pattern pattern = Pattern.compile(re); - Matcher matcher = pattern.matcher(body); - while (matcher.find()) { - links.add(matcher.group()); - } - - root.put("stylesheets", links); // SIDE-EFFECT - - body = matcher.replaceAll(""); - return body; - } protected void write(HttpServletResponse response) { @@ -218,17 +196,20 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet { root.put("tagline", portal.getShortHand()); root.put("breadcrumbs", BreadCrumbsUtil.getBreadCrumbsDiv(vreq)); - String themeDir = portal.getThemeDir(); + String themeDir = getThemeDir(); setUrls(portalId, themeDir); setLoginInfo(); setCopyrightInfo(); setThemeInfo(themeDir); - // *** TEMPORARY. The templates shouldn't need to know this. Doing temporarily for script files - // till we put the script/css loading strategy in place. (Templates make a call to add files - // to a view object. These get iterated through in scripts.ftl and stylesheets.ftl.) - setSharedVariable("contextPath", contextPath); + // Here themeDir SHOULD NOT have the context path already added to it. + setSharedVariable("stylesheets", new StylesheetList(themeDir)); + setSharedVariable("scripts", new ScriptList()); + } + + public String getThemeDir() { + return portal.getThemeDir().replaceAll("/$", ""); } // Define the URLs that are accessible to the templates. Note that we do not create menus here, @@ -265,7 +246,7 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet { urls.put("logout", getUrl(Routes.LOGOUT, logoutParams)); urls.put("siteAdmin", getUrl(Routes.SITE_ADMIN)); - System.out.println("LOGOUT: " + urls.get("logout")); + setSharedVariable("urls", urls); } @@ -310,14 +291,15 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet { private final void setThemeInfo(String themeDir) { - setSharedVariable("themeDir", getUrl(themeDir)); + themeDir = getUrl(themeDir); + setSharedVariable("themeDir", themeDir); // We'll need to separate theme-general and theme-specific stylesheet // dirs, so we need either two attributes or a list. - setSharedVariable("stylesheetDir", getUrl(themeDir + "css/")); - - setSharedVariable("siteIconDir", getUrl(themeDir + "site_icons/")); + setSharedVariable("stylesheetDir", themeDir + "/css"); + setSharedVariable("siteIconDir", themeDir + "/site_icons"); + } // Define template locations. Template loader will look first in the theme-specific @@ -325,10 +307,10 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet { // RY We cannot do this in FreeMarkerSetup because (a) the theme depends on the portal, // and we have multi-portal installations, and (b) we need to support theme-switching on the fly. // To make more efficient, we could do this once, and then have a listener that does it again - // when theme is switched.BUT this doesn't support (a), only (b), so we have to do it on every request. + // when theme is switched. BUT this doesn't support (a), only (b), so we have to do it on every request. protected final void setTemplateLoader() { - String themeTemplateDir = context.getRealPath(portal.getThemeDir()) + "/ftl"; + String themeTemplateDir = context.getRealPath(getThemeDir()) + "/ftl"; String vitroTemplateDir = context.getRealPath("/templates/freemarker"); try { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/VClassView.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/VClassView.java index 29872e389..6b42add97 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/VClassView.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/VClassView.java @@ -32,7 +32,7 @@ public class VClassView extends ViewObject { return getUrl(URL, params); } - public int getEntityCount() { + public int getIndividualCount() { return vclass.getEntityCount(); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/ViewObject.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/ViewObject.java index 2617a5198..1a12e798d 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/ViewObject.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/ViewObject.java @@ -10,7 +10,7 @@ import org.apache.commons.logging.LogFactory; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreeMarkerHttpServlet; // RY We may want an interface that the superclass would implement. -// RY Consider using FreeMarker's DisplayObjectWrapper instead, or extending it. +// RY Consider using FreeMarker's object wrappers instead. public abstract class ViewObject { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/FileList.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/FileList.java new file mode 100644 index 000000000..06987927e --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/FileList.java @@ -0,0 +1,51 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.view.fileList; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import edu.cornell.mannlib.vitro.webapp.view.ViewObject; + +public abstract class FileList extends ViewObject { + + protected List list = null; + private String themeDir = null; + + public FileList() { + this.list = new ArrayList(); + } + + public FileList(String themeDir) { + this(); + this.themeDir = themeDir; + } + + public FileList(List list) { + this.list = list; + } + + public void add(String path) { + list.add(getUrl(path)); + } + + public void addFromTheme(String path) { + path = themeDir + getThemeSubDir() + path; + add(path); + } + + public String getTags() { + String tags = ""; + + Iterator i = list.iterator(); + while (i.hasNext()) { + tags += getTag(i.next()); + } + return tags; + } + + protected abstract String getThemeSubDir(); + protected abstract String getTag(String url); + +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/ScriptList.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/ScriptList.java new file mode 100644 index 000000000..b233654bc --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/ScriptList.java @@ -0,0 +1,22 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.view.fileList; + +public class ScriptList extends FileList { + + protected static final String THEME_SUBDIR = "/js"; + + public ScriptList() { } + + public ScriptList(String themeDir) { + super(themeDir); + } + + protected String getTag(String url) { + return ""; + } + + protected String getThemeSubDir() { + return THEME_SUBDIR; + } +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/StylesheetList.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/StylesheetList.java new file mode 100644 index 000000000..865817af5 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/fileList/StylesheetList.java @@ -0,0 +1,23 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.view.fileList; + +public class StylesheetList extends FileList { + + protected static final String THEME_SUBDIR = "/css"; + + public StylesheetList() { } + + public StylesheetList(String themeDir) { + super(themeDir); + } + + protected String getTag(String url) { + return ""; + } + + protected String getThemeSubDir() { + return THEME_SUBDIR; + } + +} diff --git a/webapp/web/templates/freemarker/body/about.ftl b/webapp/web/templates/freemarker/body/about.ftl index 262812102..e256860a0 100644 --- a/webapp/web/templates/freemarker/body/about.ftl +++ b/webapp/web/templates/freemarker/body/about.ftl @@ -11,4 +11,3 @@ <#if acknowledgeText??>
${acknowledgeText}
- \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/classGroups.ftl b/webapp/web/templates/freemarker/body/classGroups.ftl index cf15052d8..017c3c434 100644 --- a/webapp/web/templates/freemarker/body/classGroups.ftl +++ b/webapp/web/templates/freemarker/body/classGroups.ftl @@ -10,7 +10,8 @@

${classGroup.publicName}

diff --git a/webapp/web/templates/freemarker/body/commentForm/form.ftl b/webapp/web/templates/freemarker/body/commentForm/form.ftl index 1d603eb96..32db78fa5 100644 --- a/webapp/web/templates/freemarker/body/commentForm/form.ftl +++ b/webapp/web/templates/freemarker/body/commentForm/form.ftl @@ -27,7 +27,7 @@

-
+ @@ -41,11 +41,11 @@

- + -
+
-<#-- RY This is temporary. --> - \ No newline at end of file +<#-- RY This is temporary. +--> +${scripts.add("/js/commentForm.js")} \ No newline at end of file diff --git a/webapp/web/templates/freemarker/components/scripts.ftl b/webapp/web/templates/freemarker/components/scripts.ftl index a38c12498..da0e5ac59 100644 --- a/webapp/web/templates/freemarker/components/scripts.ftl +++ b/webapp/web/templates/freemarker/components/scripts.ftl @@ -1,5 +1,6 @@ <#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> +${scripts.tags} + <#include "googleAnalytics.ftl"> -<#-- OTHER SCRIPT TAGS HERE --> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/components/stylesheets.ftl b/webapp/web/templates/freemarker/components/stylesheets.ftl index d44a8c9c1..087ac9ee1 100644 --- a/webapp/web/templates/freemarker/components/stylesheets.ftl +++ b/webapp/web/templates/freemarker/components/stylesheets.ftl @@ -1,10 +1,6 @@ <#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - - + + -<#if stylesheeets??> - <#list stylesheets as stylesheet> - ${stylesheet} - - \ No newline at end of file +${stylesheets.tags} \ No newline at end of file diff --git a/webapp/web/templates/freemarker/page/default.ftl b/webapp/web/templates/freemarker/page/default.ftl index 70da460f2..79e4e25fa 100644 --- a/webapp/web/templates/freemarker/page/default.ftl +++ b/webapp/web/templates/freemarker/page/default.ftl @@ -36,3 +36,10 @@ <#include "/components/scripts.ftl"> + +<#-- How to add a stylesheet: +${stylesheets.addFromTheme("/sample.css"); +${stylesheets.add("/themes/vivo-basic/css/sample.css"} +NOT (gets contextPath added twice): +${stylesheets.add(stylesheetDir + "/sample.css")} +--> \ No newline at end of file