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 a97721bd9..fd64e4104 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 @@ -316,6 +316,9 @@ public class FreemarkerHttpServlet extends VitroHttpServlet { 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("help", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.HelpDirective()); + map.put("dump1", new freemarker.ext.dump.DumpDirective()); + map.put("dumpAll1", new freemarker.ext.dump.DumpAllDirective()); + map.put("help1", new freemarker.ext.dump.HelpDirective()); map.put("url", new edu.cornell.mannlib.vitro.webapp.web.directives.UrlDirective()); map.put("widget", new edu.cornell.mannlib.vitro.webapp.web.directives.WidgetDirective()); return map; diff --git a/webapp/src/freemarker/ext/dump/BaseDumpDirective.java b/webapp/src/freemarker/ext/dump/BaseDumpDirective.java index 8c45a559f..b2849bc08 100644 --- a/webapp/src/freemarker/ext/dump/BaseDumpDirective.java +++ b/webapp/src/freemarker/ext/dump/BaseDumpDirective.java @@ -50,15 +50,17 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel { private static final Log log = LogFactory.getLog(BaseDumpDirective.class); - Pattern PROPERTY_NAME_PATTERN = Pattern.compile("^(get|is)\\w"); + protected static final String TEMPLATE_DEFAULT = "dump1.ftl"; // change to dump.ftl when old dump is removed + protected static final String VALUE_UNDEFINED = "Undefined"; + protected static final Pattern PROPERTY_NAME_PATTERN = Pattern.compile("^(get|is)\\w"); enum Key { + DATE_TYPE("dateType"), METHODS("methods"), PROPERTIES("properties"), TYPE("type"), - VALUE("value"), - DATE_TYPE("dateType"); - + VALUE("value"); + private final String key; Key(String key) { @@ -71,17 +73,18 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel { } enum Type { - STRING("String"), - NUMBER("Number"), BOOLEAN("Boolean"), + COLLECTION("Collection"), DATE("Date"), - SEQUENCE("Sequence"), + DIRECTIVE("Directive"), HASH("Hash"), // Technically it's a HashEx, but for the templates call it a Hash HASH_EX("Hash"), // ("HashEx") - COLLECTION("Collection"), - METHOD("Method"), - DIRECTIVE("Directive"); + METHOD("Method"), + NUMBER("Number"), + SEQUENCE("Sequence"), + STRING("String"), + UNDEFINED("Undefined"); private final String type; @@ -124,21 +127,22 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel { Map value = new HashMap(); - // Don't return null if model == null. We still want to send the map to the template. - if (model != null) { - // TemplateMethodModel and TemplateDirectiveModel objects can only be - // included in the data model at the top level. - if (model instanceof TemplateMethodModel) { - value.putAll( getTemplateModelDump( ( TemplateMethodModel)model, varName ) ); - - } else if (model instanceof TemplateDirectiveModel) { - value.putAll( getTemplateModelDump( ( TemplateDirectiveModel)model, varName ) ); - - } else { - value.putAll(getDump(model)); - } + if (model == null) { + value.put(Key.TYPE.toString(), Type.UNDEFINED); + value.put(Key.VALUE.toString(), VALUE_UNDEFINED); + + // TemplateMethodModel and TemplateDirectiveModel objects can only be + // included in the data model at the top level. + } else if (model instanceof TemplateMethodModel) { + value.putAll( getTemplateModelDump( ( TemplateMethodModel)model, varName ) ); + + } else if (model instanceof TemplateDirectiveModel) { + value.putAll( getTemplateModelDump( ( TemplateDirectiveModel)model, varName ) ); + + } else { + value.putAll(getDump(model)); } - + Map dump = new HashMap(); dump.put(varName, value); return dump; @@ -455,9 +459,13 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel { return map; } - protected void dump(String templateName, Map map, Environment env) + protected void dump(String templateName, Map dump, Environment env) 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.put("dump", dump); Template template = env.getConfiguration().getTemplate(templateName); StringWriter sw = new StringWriter(); template.process(map, sw); diff --git a/webapp/src/freemarker/ext/dump/DumpAllDirective.java b/webapp/src/freemarker/ext/dump/DumpAllDirective.java index cad3b2e9e..79044e444 100644 --- a/webapp/src/freemarker/ext/dump/DumpAllDirective.java +++ b/webapp/src/freemarker/ext/dump/DumpAllDirective.java @@ -4,7 +4,6 @@ package freemarker.ext.dump; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -48,7 +47,7 @@ public class DumpAllDirective extends BaseDumpDirective { } SortedMap dataModelDump = getDataModelDump(env); - dump("dump-all.ftl", dataModelDump, env); + dump(TEMPLATE_DEFAULT, dataModelDump, env); } 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 f30fd67b3..cb0e51704 100644 --- a/webapp/src/freemarker/ext/dump/DumpDirective.java +++ b/webapp/src/freemarker/ext/dump/DumpDirective.java @@ -37,16 +37,10 @@ public class DumpDirective extends BaseDumpDirective { "The dump directive doesn't allow nested content."); } - Object o = params.get("var"); - if ( !(o instanceof SimpleScalar)) { - throw new TemplateModelException( - "Value of parameter 'var' must be a string."); - } - - String varName = ((SimpleScalar)o).getAsString(); + String varName = params.get("var").toString(); Map map = getTemplateVariableDump(varName, env); - dump("dumpvar.ftl", map, env); + dump(TEMPLATE_DEFAULT, map, env); } @Override diff --git a/webapp/src/freemarker/ext/dump/HelpDirective.java b/webapp/src/freemarker/ext/dump/HelpDirective.java index 2433edd75..581d22a8d 100644 --- a/webapp/src/freemarker/ext/dump/HelpDirective.java +++ b/webapp/src/freemarker/ext/dump/HelpDirective.java @@ -41,12 +41,7 @@ public class HelpDirective extends BaseDumpDirective { "Must specify 'for' argument."); } - if ( !(o instanceof SimpleScalar)) { - throw new TemplateModelException( - "Value of parameter 'for' must be a string."); - } - - String varName = ((SimpleScalar)o).getAsString(); + String varName = params.get("var").toString(); TemplateHashModel dataModel = env.getDataModel(); Object templateModel = dataModel.get(varName); @@ -61,7 +56,7 @@ public class HelpDirective extends BaseDumpDirective { } Map map = getTemplateVariableDump(varName, env); - dump("dumpvar.ftl", map, env); + dump(TEMPLATE_DEFAULT, map, env); } @Override diff --git a/webapp/test/freemarker/ext/dump/DumpDirectiveTest.java b/webapp/test/freemarker/ext/dump/DumpDirectiveTest.java index 77276e9be..2d6fd9149 100644 --- a/webapp/test/freemarker/ext/dump/DumpDirectiveTest.java +++ b/webapp/test/freemarker/ext/dump/DumpDirectiveTest.java @@ -71,6 +71,22 @@ public class DumpDirectiveTest { Logger.getLogger(BaseDumpDirective.class).setLevel(Level.OFF); } + @Test + public void dumpUndefinedValue() { + + String varName = "dog"; + Map dataModel = new HashMap(); + + Map expectedDumpValue = new HashMap(); + expectedDumpValue.put(Key.TYPE.toString(), Type.UNDEFINED); + expectedDumpValue.put(Key.VALUE.toString(), BaseDumpDirective.VALUE_UNDEFINED); + + Map expectedDump = new HashMap(); + expectedDump.put(varName, expectedDumpValue); + + test(varName, dataModel, expectedDump); + } + @Test public void dumpString() { diff --git a/webapp/web/templates/freemarker/body/partials/dump/describe.ftl b/webapp/web/templates/freemarker/body/partials/dump/describe.ftl deleted file mode 100644 index 79fc5e0a5..000000000 --- a/webapp/web/templates/freemarker/body/partials/dump/describe.ftl +++ /dev/null @@ -1,13 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Template for displaying directive describe --> - -
-

Methods available to variable ${var}

- - <#list methods as method> - ${method}
- -
- -${stylesheets.add('')} \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump-all.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump-all.ftl deleted file mode 100644 index 841b7965f..000000000 --- a/webapp/web/templates/freemarker/body/partials/dump/dump-all.ftl +++ /dev/null @@ -1 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump1.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump1.ftl new file mode 100644 index 000000000..d1bbf3d88 --- /dev/null +++ b/webapp/web/templates/freemarker/body/partials/dump/dump1.ftl @@ -0,0 +1,39 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for dump directives --> + +<#-- Styles here are temporary; move to css file once stylesheets.add() works --> + + +<#--
<@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}

    --> +
  • + +
+ + +<#-- This will work after we move stylesheets to Configuration sharedVariables +${stylesheets.add('')} +--> \ No newline at end of file