From 5e8eb807320beb5f8bdd97dbe3b271477ab2e686 Mon Sep 17 00:00:00 2001 From: rjy7 Date: Thu, 29 Jul 2010 17:08:27 +0000 Subject: [PATCH] NIHVIVO-564 Modifications to directive help output. Included changing template directive iface to an abstract class that provides a default help() method and some utilities. --- .../BaseTemplateDirectiveModel.java | 38 +++++++++++++++++++ .../directives/DumpDataModelDirective.java | 16 +++++--- .../webapp/web/directives/DumpDirective.java | 10 +++-- .../VitroTemplateDirectiveModel.java | 12 ------ .../body/partials/dump/directive-help.ftl | 38 +++++++++++++++++++ .../body/partials/dump/dump-datamodel.ftl | 6 +-- .../partials/dump/dump-directive-help.ftl | 29 -------------- 7 files changed, 95 insertions(+), 54 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/BaseTemplateDirectiveModel.java delete mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/VitroTemplateDirectiveModel.java create mode 100644 webapp/web/templates/freemarker/body/partials/dump/directive-help.ftl delete mode 100644 webapp/web/templates/freemarker/body/partials/dump/dump-directive-help.ftl 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 new file mode 100644 index 000000000..4a98d588d --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/BaseTemplateDirectiveModel.java @@ -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.directives; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHelper; +import freemarker.template.Configuration; +import freemarker.template.TemplateDirectiveModel; + +public abstract class BaseTemplateDirectiveModel implements TemplateDirectiveModel { + + public String help(Configuration config) { + Map map = new HashMap(); + + String name = getDirectiveName(); + map.put("name", name); + + return mergeToTemplate(map, config); + } + + protected String getDirectiveName() { + String className = this.getClass().getName(); + String[] nameParts = className.split("\\."); + String directiveName = nameParts[nameParts.length-1]; + directiveName = directiveName.replaceAll("Directive$", ""); + directiveName = directiveName.substring(0, 1).toLowerCase() + directiveName.substring(1); + return directiveName; + } + + protected String mergeToTemplate(Map map, Configuration config) { + return new FreemarkerHelper().mergeMapToTemplate("directive-help.ftl", map, config); + } + +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java index 3000b98e0..5b97767c1 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java @@ -25,7 +25,7 @@ import freemarker.template.TemplateModel; import freemarker.template.TemplateModelException; import freemarker.template.utility.DeepUnwrap; -public class DumpDataModelDirective implements VitroTemplateDirectiveModel { +public class DumpDataModelDirective extends BaseTemplateDirectiveModel { private static final Log log = LogFactory.getLog(DumpDataModelDirective.class); @@ -61,8 +61,8 @@ public class DumpDataModelDirective implements VitroTemplateDirectiveModel { // Send the two lists of strings (variables and directives) to dump-datamodel.ftl. // That way, the directive dump won't be broken up into two pieces, for example. Object value = dm.get(var); - if (value instanceof VitroTemplateDirectiveModel) { - String help = ((VitroTemplateDirectiveModel) value).help(config); + if (value instanceof BaseTemplateDirectiveModel) { + String help = ((BaseTemplateDirectiveModel) value).help(config); directives.put(var, help); } else { models.put(var, value); @@ -89,18 +89,22 @@ public class DumpDataModelDirective implements VitroTemplateDirectiveModel { } - @Override + @Override public String help(Configuration config) { Map map = new HashMap(); + + String name = getDirectiveName(); + map.put("name", name); + map.put("usage", "Dump the contents of the template data model."); map.put("comments", "Sequences (lists and arrays) are enclosed in square brackets. Hashes are enclosed in curly braces."); List examples = new ArrayList(); - examples.add("<@dumpDataModel />"); + examples.add("<@" + name + " />"); map.put("examples", examples); - return new FreemarkerHelper().mergeMapToTemplate("dump-directive-help.ftl", map, config); + return mergeToTemplate(map, config); } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java index 7bb51b496..6088be00c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java @@ -27,7 +27,7 @@ import freemarker.template.TemplateNumberModel; import freemarker.template.TemplateScalarModel; import freemarker.template.TemplateSequenceModel; -public class DumpDirective implements VitroTemplateDirectiveModel { +public class DumpDirective extends BaseTemplateDirectiveModel { private static final Log log = LogFactory.getLog(DumpDirective.class); @@ -116,6 +116,10 @@ public class DumpDirective implements VitroTemplateDirectiveModel { public String help(Configuration config) { Map map = new HashMap(); + + String name = getDirectiveName(); + map.put("name", name); + map.put("usage", "Dump the contents of a template variable."); map.put("comments", "Sequences (lists and arrays) are enclosed in square brackets. Hashes are enclosed in curly braces."); @@ -125,10 +129,10 @@ public class DumpDirective implements VitroTemplateDirectiveModel { map.put("params", params); List examples = new ArrayList(); - examples.add("<@dump var=\"urls\" />"); + examples.add("<@" + name + " var=\"urls\" />"); map.put("examples", examples); - return new FreemarkerHelper().mergeMapToTemplate("dump-directive-help.ftl", map, config); + return mergeToTemplate(map, config); } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/VitroTemplateDirectiveModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/VitroTemplateDirectiveModel.java deleted file mode 100644 index b02153c7d..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/VitroTemplateDirectiveModel.java +++ /dev/null @@ -1,12 +0,0 @@ -/* $This file is distributed under the terms of the license in /doc/license.txt$ */ - -package edu.cornell.mannlib.vitro.webapp.web.directives; - -import freemarker.template.Configuration; -import freemarker.template.TemplateDirectiveModel; - -public interface VitroTemplateDirectiveModel extends TemplateDirectiveModel { - - public String help(Configuration config); - -} diff --git a/webapp/web/templates/freemarker/body/partials/dump/directive-help.ftl b/webapp/web/templates/freemarker/body/partials/dump/directive-help.ftl new file mode 100644 index 000000000..1599a7798 --- /dev/null +++ b/webapp/web/templates/freemarker/body/partials/dump/directive-help.ftl @@ -0,0 +1,38 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for displaying directive help --> + +
+

Directive name: ${name}

+ + <#if usage??> +

Usage: ${usage}

+ + <#if comments??> +

Comments: ${comments}

+ + +
Parameters:
+ <#if params?? && params?keys?has_content> +
    + <#list params?keys as param> +
  • ${param}: ${params[param]}
  • + +
+ <#else> +

none

+ + +
Examples:
+ <#if examples??> +
    + <#list examples as example> +
  • ${example}
  • + +
+ + + <#else> +

No help available for this directive.

+ +
\ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump-datamodel.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump-datamodel.ftl index b300a68bb..d5b747fad 100644 --- a/webapp/web/templates/freemarker/body/partials/dump/dump-datamodel.ftl +++ b/webapp/web/templates/freemarker/body/partials/dump/dump-datamodel.ftl @@ -15,12 +15,10 @@

DIRECTIVES

+
    <#list directives?keys as directive> -
  • -

    Directive name: ${directive}

    - ${directives[directive]!"no help available for this directive"} -
  • +
  • ${directives[directive]}
diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump-directive-help.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump-directive-help.ftl deleted file mode 100644 index 793075081..000000000 --- a/webapp/web/templates/freemarker/body/partials/dump/dump-directive-help.ftl +++ /dev/null @@ -1,29 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Template for dumping directive help --> - -

Usage: ${usage!}

- -<#if comments??> -

Comments: ${comments}

- - -
Parameters:
-<#if params?? && params?keys?has_content> -
    - <#list params?keys as param> -
  • ${param}: ${params[param]}
  • - -
-<#else> -

none

- - -
Examples:
-<#if examples??> -
    - <#list examples as example> -
  • ${example}
  • - -
- \ No newline at end of file