NIHVIVO-1384 Change script and stylesheet template objects to take the entire tag as a parameter, rather than just the src/href attribute.

This commit is contained in:
rjy7 2011-03-08 15:44:05 +00:00
parent b33b6bc21e
commit 8b89cad590
32 changed files with 108 additions and 213 deletions

View file

@ -36,9 +36,8 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.Tem
import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
import edu.cornell.mannlib.vitro.webapp.web.BreadCrumbsUtil;
import edu.cornell.mannlib.vitro.webapp.web.PortalWebUtil;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.Tags;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.User;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.files.Scripts;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.files.Stylesheets;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.MainMenu;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.TabMenu;
import freemarker.ext.beans.BeansWrapper;
@ -290,35 +289,21 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
return wrapper;
}
private TemplateModel getStylesheetList(String themeDir) {
private TemplateModel getTagList() {
// For script and stylesheet lists, use an object wrapper that exposes write methods,
// instead of the configuration's object wrapper, which doesn't. The templates can
// add stylesheets and scripts to the lists by calling their add() methods.
BeansWrapper wrapper = getNonDefaultBeansWrapper(BeansWrapper.EXPOSE_SAFE);
try {
// Here themeDir SHOULD NOT have the context path already added to it.
return wrapper.wrap(new Stylesheets(themeDir));
return wrapper.wrap(new Tags());
} catch (TemplateModelException e) {
log.error("Error creating stylesheet TemplateModel");
log.error("Error creating Tags template model");
return null;
}
}
private TemplateModel getScriptList(String themeDir) {
// For script and stylesheet lists, use an object wrapper that exposes write methods,
// instead of the configuration's object wrapper, which doesn't. The templates can
// add stylesheets and scripts to the lists by calling their add() methods.
BeansWrapper wrapper = getNonDefaultBeansWrapper(BeansWrapper.EXPOSE_SAFE);
try {
return wrapper.wrap(new Scripts(themeDir));
} catch (TemplateModelException e) {
log.error("Error creating script TemplateModel");
return null;
}
}
/**
* Add any Java directives the templates should have access to.
* This is public and static so that these may be used by other classes during
@ -368,9 +353,9 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
map.put("themeDir", themeDir);
map.put("currentTheme", themeDir.substring(themeDir.lastIndexOf('/')+1));
map.put("stylesheets", getStylesheetList(themeDir));
map.put("scripts", getScriptList(themeDir));
map.put("headScripts", getScriptList(themeDir));
map.put("stylesheets", getTagList());
map.put("scripts", getTagList());
map.put("headScripts", getTagList());
map.putAll(getDirectives());
map.putAll(getMethods());

View file

@ -0,0 +1,38 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.templatemodels;
import java.util.LinkedHashSet;
public class Tags extends BaseTemplateModel {
protected LinkedHashSet<String> tags = null;
public Tags() {
this.tags = new LinkedHashSet<String>();
}
public Tags(LinkedHashSet<String> tags) {
this.tags = tags;
}
public void add(String... tags) {
for (String tag : tags) {
add(tag);
}
}
public void add(String tag) {
tags.add(tag);
}
public String getList() {
String tagList = "";
for (String tag : tags) {
tagList += tag;
}
return tagList;
}
}

View file

@ -1,84 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.files;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
public abstract class Files extends BaseTemplateModel {
protected LinkedHashSet<String> list = null;
private String themeDir = null;
@SuppressWarnings("serial")
private static final Set<String> allowedExternalUrlPatterns = new HashSet<String>() {{
add("http://");
add("https://");
}};
public Files() {
this.list = new LinkedHashSet<String>();
}
public Files(String themeDir) {
this();
this.themeDir = themeDir;
}
public Files(LinkedHashSet<String> list) {
this.list = list;
}
public void add(String path) {
// Allow for an external url
for (String currentPattern : allowedExternalUrlPatterns) {
if (path.startsWith(currentPattern)) {
list.add(path);
return;
}
}
// If an external url pattern was not found.
list.add(getUrl(path));
}
public void add(String... paths) {
for (String path : paths) {
add(path);
}
}
public void addFromTheme(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
path = themeDir + path;
add(path);
}
public void addFromTheme(String... paths) {
for (String path : paths) {
addFromTheme(path);
}
}
public String getTags() {
String tags = "";
for (String file : list) {
tags += getTag(file);
}
return tags;
}
public String dump() {
return list.toString();
}
protected abstract String getTag(String url);
}

View file

@ -1,16 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.files;
public class Scripts extends Files {
public Scripts() { }
public Scripts(String themeDir) {
super(themeDir);
}
protected String getTag(String url) {
return "<script type=\"text/javascript\" src=\"" + url + "\"></script>\n";
}
}

View file

@ -1,17 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.files;
public class Stylesheets extends Files {
public Stylesheets() { }
public Stylesheets(String themeDir) {
super(themeDir);
}
protected String getTag(String url) {
return "<link rel=\"stylesheet\" href=\"" + url + "\" />\n";
}
}

View file

@ -41,9 +41,12 @@ public abstract class Widget {
Map<String, Object> map = new HashMap<String, Object>();
try {
// Once we remove portals, we can define these as Configuration shared variables. Then the
// templates will automatically get these and we don't have to add them to the data model.
map.put("stylesheets", dataModel.get("stylesheets"));
map.put("scripts", dataModel.get("scripts"));
map.put("headScripts", dataModel.get("headScripts"));
map.put("urls", dataModel.get("urls"));
} catch (TemplateModelException e) {
log.error("Error getting asset values from data model.");
}