From 216d7a7905113e645058e89fa6c4279a4ce25f9d Mon Sep 17 00:00:00 2001 From: rjy7 Date: Thu, 5 Aug 2010 19:54:24 +0000 Subject: [PATCH] NIHVIVO-564 Help directive for directive help output. --- .../freemarker/FreemarkerHttpServlet.java | 3 +- .../BaseTemplateDirectiveModel.java | 2 +- .../web/directives/dump/HelpDirective.java | 104 ++++++++++++++++++ ...{directive-help.ftl => help-directive.ftl} | 0 .../freemarker/body/partials/dump/help.ftl | 10 ++ webapp/web/templates/freemarker/body/test.ftl | 2 + 6 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/HelpDirective.java rename webapp/web/templates/freemarker/body/partials/dump/{directive-help.ftl => help-directive.ftl} (100%) create mode 100644 webapp/web/templates/freemarker/body/partials/dump/help.ftl 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 1db4a155c..8c61fdd5e 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 @@ -283,7 +283,8 @@ public class FreemarkerHttpServlet extends VitroHttpServlet { // Add any Java directives the templates should have access to private void addDirectives(Map map) { map.put("dump", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DumpDirective()); - map.put("dumpAll", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DumpAllDirective()); + map.put("dumpAll", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DumpAllDirective()); + map.put("help", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.HelpDirective()); } // Add variables that should be available only to the page's root map, not to the body. diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/BaseTemplateDirectiveModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/BaseTemplateDirectiveModel.java index 353ecb6b1..31eae996a 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/BaseTemplateDirectiveModel.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/BaseTemplateDirectiveModel.java @@ -38,7 +38,7 @@ public abstract class BaseTemplateDirectiveModel implements TemplateDirectiveMod } protected String mergeToTemplate(Map map, Configuration config) { - return new FreemarkerHelper(config).mergeMapToTemplate("directive-help.ftl", map); + return new FreemarkerHelper(config).mergeMapToTemplate("help-directive.ftl", map); } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/HelpDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/HelpDirective.java new file mode 100644 index 000000000..4055295b8 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/HelpDirective.java @@ -0,0 +1,104 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.web.directives.dump; + +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHelper; +import edu.cornell.mannlib.vitro.webapp.web.directives.BaseTemplateDirectiveModel; +import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel; +import freemarker.core.Environment; +import freemarker.template.Configuration; +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateBooleanModel; +import freemarker.template.TemplateDateModel; +import freemarker.template.TemplateDirectiveBody; +import freemarker.template.TemplateException; +import freemarker.template.TemplateHashModel; +import freemarker.template.TemplateModel; +import freemarker.template.TemplateModelException; +import freemarker.template.TemplateNumberModel; +import freemarker.template.TemplateScalarModel; +import freemarker.template.TemplateSequenceModel; +import freemarker.template.utility.DeepUnwrap; + +public class HelpDirective extends BaseTemplateDirectiveModel { + + private static final Log log = LogFactory.getLog(HelpDirective.class); + + @SuppressWarnings("unchecked") + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, + TemplateDirectiveBody body) throws TemplateException, IOException { + + if (loopVars.length != 0) { + throw new TemplateModelException( + "The help directive doesn't allow loop variables."); + } + if (body != null) { + throw new TemplateModelException( + "The help directive doesn't allow nested content."); + } + + Object o = params.get("directive"); + if ( !(o instanceof SimpleScalar)) { + throw new TemplateModelException( + "Value of parameter 'directive' must be a string."); + } + + String directiveName = ((SimpleScalar)o).getAsString(); + TemplateHashModel dataModel = env.getDataModel(); + Map dm = (Map) DeepUnwrap.permissiveUnwrap(dataModel); + Object value = dm.get(directiveName); + + if (! (value instanceof BaseTemplateDirectiveModel) ) { + throw new TemplateModelException( + directiveName + " must be the name of a directive."); + } + + Configuration config = env.getConfiguration(); + Map map = new HashMap(); + + String help = ((BaseTemplateDirectiveModel) value).help(config); + map.put("help", help); + + try { + map.put("stylesheets", dataModel.get("stylesheets")); + } catch (TemplateModelException e) { + log.error("Error getting value of stylesheets variable from data model."); + } + + DumpHelper helper = new DumpHelper(env); + helper.writeDump("help.ftl", map, directiveName); + + } + + + public String help(Configuration config) { + Map map = new HashMap(); + + String name = getDirectiveName(); + map.put("name", name); + + map.put("effect", "Output directive help."); + + Map params = new HashMap(); + params.put("var", "name of directive"); + map.put("params", params); + + List examples = new ArrayList(); + examples.add("<@" + name + " directive=\"dump\" />"); + map.put("examples", examples); + + return mergeToTemplate(map, config); + } + +} diff --git a/webapp/web/templates/freemarker/body/partials/dump/directive-help.ftl b/webapp/web/templates/freemarker/body/partials/dump/help-directive.ftl similarity index 100% rename from webapp/web/templates/freemarker/body/partials/dump/directive-help.ftl rename to webapp/web/templates/freemarker/body/partials/dump/help-directive.ftl diff --git a/webapp/web/templates/freemarker/body/partials/dump/help.ftl b/webapp/web/templates/freemarker/body/partials/dump/help.ftl new file mode 100644 index 000000000..92e230320 --- /dev/null +++ b/webapp/web/templates/freemarker/body/partials/dump/help.ftl @@ -0,0 +1,10 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for displaying directive help --> + + +

Template directive help

+ +${help} + +${stylesheets.add("/css/dump.css")} \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/test.ftl b/webapp/web/templates/freemarker/body/test.ftl index d7af70551..970fac65f 100644 --- a/webapp/web/templates/freemarker/body/test.ftl +++ b/webapp/web/templates/freemarker/body/test.ftl @@ -61,3 +61,5 @@ ${scripts.add("/js/script1.js", "/js/script2.js", "/js/script3.js")} <@dumpAll /> + +<@help directive="dump" />