From 2f884cec25c7511017c76e5d9c275520deca50e3 Mon Sep 17 00:00:00 2001 From: ryounes Date: Thu, 21 Apr 2011 17:17:28 +0000 Subject: [PATCH] NIHVIVO-2479 Dump templates --- .../ext/dump/BaseDumpDirective.java | 24 +++-- .../freemarker/ext/dump/DumpAllDirective.java | 5 +- .../freemarker/ext/dump/DumpDirective.java | 4 +- .../freemarker/ext/dump/HelpDirective.java | 10 +- .../freemarker/body/partials/dump/dump1.ftl | 99 ++++++++++++++----- 5 files changed, 103 insertions(+), 39 deletions(-) diff --git a/webapp/src/freemarker/ext/dump/BaseDumpDirective.java b/webapp/src/freemarker/ext/dump/BaseDumpDirective.java index 5c33cf75b..31ad9fc0b 100644 --- a/webapp/src/freemarker/ext/dump/BaseDumpDirective.java +++ b/webapp/src/freemarker/ext/dump/BaseDumpDirective.java @@ -50,9 +50,10 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel { private static final Log log = LogFactory.getLog(BaseDumpDirective.class); - protected static final String TEMPLATE_DEFAULT = "dump1.ftl"; // change to dump.ftl when old dump is removed + private static final String TEMPLATE_DEFAULT = "dump1.ftl"; // change to dump.ftl when old dump is removed + private static final Pattern PROPERTY_NAME_PATTERN = Pattern.compile("^(get|is)\\w"); + protected static final String VALUE_UNDEFINED = "Undefined"; - protected static final Pattern PROPERTY_NAME_PATTERN = Pattern.compile("^(get|is)\\w"); enum Key { DATE_TYPE("dateType"), @@ -449,7 +450,7 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel { private Map getTemplateModelDump(TemplateModel model) throws TemplateModelException { // One of the more specific cases should have applied. Track whether this actually occurs. - log.debug("Found model with no known type"); + log.debug("Found template model of type " + model.getClass().getName()); Map map = new HashMap(); Object unwrappedModel = DeepUnwrap.permissiveUnwrap(model); map.put(Key.TYPE.toString(), unwrappedModel.getClass().getName()); @@ -457,18 +458,29 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel { return map; } - protected void dump(String templateName, Map dump, Environment env) + protected void dump(Map dump, Environment env, String title) + throws TemplateException, IOException { + dump(dump, env, title, TEMPLATE_DEFAULT); + } + + protected void dump(Map dump, Environment env, String title, String templateName) throws TemplateException, IOException { // Wrap the dump in another map so the template has a handle to iterate through // the values: <#list dump?keys as key>... - Map> map = new HashMap>(); + Map map = new HashMap(); map.put("dump", dump); + map.put("title", title); + writeDump(map, env, templateName); + } + + protected void writeDump(Map map, Environment env, String templateName) + throws TemplateException, IOException { Template template = env.getConfiguration().getTemplate(templateName); StringWriter sw = new StringWriter(); template.process(map, sw); Writer out = env.getOut(); - out.write(sw.toString()); + out.write(sw.toString()); } protected Map help(String name) { diff --git a/webapp/src/freemarker/ext/dump/DumpAllDirective.java b/webapp/src/freemarker/ext/dump/DumpAllDirective.java index 79044e444..d16a2f1a7 100644 --- a/webapp/src/freemarker/ext/dump/DumpAllDirective.java +++ b/webapp/src/freemarker/ext/dump/DumpAllDirective.java @@ -46,8 +46,9 @@ public class DumpAllDirective extends BaseDumpDirective { "The dump directive doesn't allow nested content."); } - SortedMap dataModelDump = getDataModelDump(env); - dump(TEMPLATE_DEFAULT, dataModelDump, env); + SortedMap dump = getDataModelDump(env); + String title = "Template data model dump for " + env.getTemplate().getName(); + dump(dump, env, title); } SortedMap getDataModelDump(Environment env) throws TemplateModelException { diff --git a/webapp/src/freemarker/ext/dump/DumpDirective.java b/webapp/src/freemarker/ext/dump/DumpDirective.java index c4cf36517..820cfa9b7 100644 --- a/webapp/src/freemarker/ext/dump/DumpDirective.java +++ b/webapp/src/freemarker/ext/dump/DumpDirective.java @@ -51,8 +51,8 @@ public class DumpDirective extends BaseDumpDirective { String varName = o.toString(); //((SimpleScalar)o).getAsString(); Map map = getTemplateVariableDump(varName, env); - - dump(TEMPLATE_DEFAULT, map, env); + String title = "Template variable dump"; + dump(map, env, title); } @Override diff --git a/webapp/src/freemarker/ext/dump/HelpDirective.java b/webapp/src/freemarker/ext/dump/HelpDirective.java index 1fb22d633..6d1e5f262 100644 --- a/webapp/src/freemarker/ext/dump/HelpDirective.java +++ b/webapp/src/freemarker/ext/dump/HelpDirective.java @@ -50,18 +50,16 @@ public class HelpDirective extends BaseDumpDirective { TemplateHashModel dataModel = env.getDataModel(); Object templateModel = dataModel.get(varName); - if (templateModel == null) { - throw new TemplateModelException( - "Value of parameter '" + varName + "' must be the name of a directive or method"); - } - if (! (templateModel instanceof TemplateMethodModel || templateModel instanceof TemplateDirectiveModel)) { throw new TemplateModelException( "Value of parameter '" + varName + "' must be the name of a directive or method"); } Map map = getTemplateVariableDump(varName, env); - dump(TEMPLATE_DEFAULT, map, env); + + String type = templateModel instanceof TemplateMethodModel ? "method" : "directive"; + String title = "Help for " + type; + dump(map, env, title); } @Override diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump1.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump1.ftl index d1bbf3d88..f877c3e02 100644 --- a/webapp/web/templates/freemarker/body/partials/dump/dump1.ftl +++ b/webapp/web/templates/freemarker/body/partials/dump/dump1.ftl @@ -2,38 +2,91 @@ <#-- Template for dump directives --> -<#-- Styles here are temporary; move to css file once stylesheets.add() works --> +<#-- Styles here are temporary; use stylesheets.add() once that's working (see below) --> -<#--
<@dumper.dump dump />
--> +<#-- +<#import "lib-dump.ftl" as dumper> +
<@dumper.dump dump />
+--> -<#if dump?keys?has_content> -
    - <#list dump?keys as key> -
  • - <#assign value = dump[key] /> -

    Variable name: ${key}

    - <#if value.type??>

    Type: ${value.type}

    - <#--

    Value: ${value.value}

    --> - <#-- What to do here depends on time. Test either ${var.type} or ${var.value} --> - <#--

    Value: ${var.value}

    --> -
  • - -
- +
+

${title}

+ + <@doDump dump /> +
+ +<#macro doDump dump> + <#if dump?keys?has_content> +
    + <#list dump?keys as key> +
  • + <#local dumpVal = dump[key] /> +

    Variable name: ${key}

    + + <@doMap dumpVal /> +
  • + +
+ + + +<#macro doMap map> + <#if map.type?has_content> +

Type: ${map.type}

+ + <#if map.dateType?has_content> +

Date type: ${map.dateType}

+ + + + <#if map.value?has_content> +

+ Value: + <#local value = map.value> + <#if value?is_string || value?is_number>${value} + <#elseif value?is_boolean || value?is_number>${value?string} + <#elseif value?is_date>${value?string("EEEE, MMMM dd, yyyy hh:mm:ss a zzz")} + <#elseif value?is_sequence><@doSequence value /> + +

+ + + +<#macro doSequence seq> + <#if seq?has_content> +
    + <#list seq as item> +
  • Item ${item_index}: <@doMap item />
  • + +
+ + <#-- This will work after we move stylesheets to Configuration sharedVariables ${stylesheets.add('')} ---> \ No newline at end of file +-->