From 3dbbf13cd7641772e5a9ccc46030ca1150421983 Mon Sep 17 00:00:00 2001 From: rjy7 Date: Thu, 26 Aug 2010 20:51:55 +0000 Subject: [PATCH] NIHVIVO-564 Dump directive displays a message rather than throwing an error if the variable is undefined in the data model. --- .../web/directives/dump/DumpHelper.java | 88 ++++++++++--------- .../body/partials/dump/dump-hash.ftl | 15 ---- .../body/partials/dump/dump-sequence.ftl | 14 --- .../body/partials/dump/dump-string.ftl | 6 -- .../body/partials/dump/dump-var.ftl | 8 +- 5 files changed, 52 insertions(+), 79 deletions(-) delete mode 100644 webapp/web/templates/freemarker/body/partials/dump/dump-hash.ftl delete mode 100644 webapp/web/templates/freemarker/body/partials/dump/dump-sequence.ftl delete mode 100644 webapp/web/templates/freemarker/body/partials/dump/dump-string.ftl diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpHelper.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpHelper.java index 86de3d4fd..7ec1ec62a 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpHelper.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpHelper.java @@ -48,50 +48,54 @@ public class DumpHelper { log.error("Error getting value of template model " + varName + " from data model."); } - // Just use toString() method for now. Handles nested collections. Could make more sophisticated later. - String value = tm.toString(); - String type = null; - Object unwrappedModel = null; - try { - unwrappedModel = DeepUnwrap.permissiveUnwrap(tm); - } catch (TemplateModelException e) { - log.error("Cannot unwrap template model " + varName + "."); - } - - // This case must precede the TemplateScalarModel case, because - // tm is an instance of StringModel and thus a TemplateScalarModel. - if (unwrappedModel instanceof BaseTemplateModel) { - type = unwrappedModel.getClass().getName(); - value = ((BaseTemplateModel)unwrappedModel).dump(); - } else if (tm instanceof TemplateScalarModel) { - type = "string"; - } else if (tm instanceof TemplateDateModel) { - type = "date"; - } else if (tm instanceof TemplateNumberModel) { - type = "number"; - } else if (tm instanceof TemplateBooleanModel) { - type = "boolean"; - try { - value = ((TemplateBooleanModel) tm).getAsBoolean() ? "true" : "false"; - } catch (TemplateModelException e) { - log.error("Error getting boolean value for " + varName + "."); - } - } else if (tm instanceof TemplateSequenceModel){ - type = "sequence"; - } else if (tm instanceof TemplateHashModel) { - type = "hash"; - // In recursive dump, we've gotten down to a raw string. Just output it. -// } else if (val == null) { -// out.write(var); -// return; - } else { - type = "object"; - } - Map map = new HashMap(); map.put("var", varName); - map.put("value", value); - map.put("type", type); + + if (tm != null) { + + // Just use toString() method for now. Handles nested collections. Could make more sophisticated later. + String type = null; + String value = tm.toString(); + Object unwrappedModel = null; + + try { + unwrappedModel = DeepUnwrap.permissiveUnwrap(tm); + } catch (TemplateModelException e) { + log.error("Cannot unwrap template model " + varName + "."); + } + + // This case must precede the TemplateScalarModel case, because + // tm is an instance of StringModel and thus a TemplateScalarModel. + if (unwrappedModel instanceof BaseTemplateModel) { + type = unwrappedModel.getClass().getName(); + value = ((BaseTemplateModel)unwrappedModel).dump(); + } else if (tm instanceof TemplateScalarModel) { + type = "string"; + } else if (tm instanceof TemplateDateModel) { + type = "date"; + } else if (tm instanceof TemplateNumberModel) { + type = "number"; + } else if (tm instanceof TemplateBooleanModel) { + type = "boolean"; + try { + value = ((TemplateBooleanModel) tm).getAsBoolean() ? "true" : "false"; + } catch (TemplateModelException e) { + log.error("Error getting boolean value for " + varName + "."); + } + } else if (tm instanceof TemplateSequenceModel){ + type = "sequence"; + } else if (tm instanceof TemplateHashModel) { + type = "hash"; + // In recursive dump, we've gotten down to a raw string. Just output it. + // } else if (val == null) { + // out.write(var); + // return; + } else { + type = "object"; + } + map.put("value", value); + map.put("type", type); + } return map; } diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump-hash.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump-hash.ftl deleted file mode 100644 index 5dec2896b..000000000 --- a/webapp/web/templates/freemarker/body/partials/dump/dump-hash.ftl +++ /dev/null @@ -1,15 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Template for dumping hash values --> - -Type: hash
-Values:
- -
    - <#list value?keys as key> - <#-- Shallow (non-recursive) dump --> -
  • ${key} = ${value[key]}
  • - <#-- Deep (recursive) dump -
  • ${key} = <@dump var="${value[key]}" />
  • --> - -
\ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump-sequence.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump-sequence.ftl deleted file mode 100644 index 1f1d079ee..000000000 --- a/webapp/web/templates/freemarker/body/partials/dump/dump-sequence.ftl +++ /dev/null @@ -1,14 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Template for dumping sequence (list and array) values --> - -Type: sequence (array/list)
-Values:
-
    -<#list value as item> - <#-- Shallow (non-recursive) dump --> -
  • ${item_index}: ${item}
  • - <#-- Deep (recursive) dump -
  • ${item_index}: <@dump var="${item}" />
  • --> - -
\ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump-string.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump-string.ftl deleted file mode 100644 index 13c0dd728..000000000 --- a/webapp/web/templates/freemarker/body/partials/dump/dump-string.ftl +++ /dev/null @@ -1,6 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Template for dumping string values --> - -Type: ${type}
-Value: ${value}
diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump-var.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump-var.ftl index 70f36036a..2306fbd0f 100644 --- a/webapp/web/templates/freemarker/body/partials/dump/dump-var.ftl +++ b/webapp/web/templates/freemarker/body/partials/dump/dump-var.ftl @@ -5,8 +5,12 @@
Variable name: ${var}
- Type: ${type}
- Value: ${value}
+ <#if value??> + Type: ${type}
+ Value: ${value}
+ <#else> + Variable is undefined in the data model +