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 dea7d875f..1db4a155c 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 @@ -282,8 +282,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.DumpDirective()); - map.put("dumpAll", new edu.cornell.mannlib.vitro.webapp.web.directives.DumpAllDirective()); + 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()); } // 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 9be22093b..353ecb6b1 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 @@ -7,11 +7,17 @@ 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.dump.DumpDirective; import freemarker.template.Configuration; import freemarker.template.TemplateDirectiveModel; public abstract class BaseTemplateDirectiveModel implements TemplateDirectiveModel { + + private static final Log log = LogFactory.getLog(BaseTemplateDirectiveModel.class); public String help(Configuration config) { Map map = new HashMap(); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpAllDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpAllDirective.java similarity index 66% rename from webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpAllDirective.java rename to webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpAllDirective.java index d3d2b8072..a6e60adab 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpAllDirective.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpAllDirective.java @@ -1,6 +1,6 @@ /* $This file is distributed under the terms of the license in /doc/license.txt$ */ -package edu.cornell.mannlib.vitro.webapp.web.directives; +package edu.cornell.mannlib.vitro.webapp.web.directives.dump; import java.io.IOException; import java.io.Writer; @@ -14,6 +14,7 @@ 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 freemarker.core.Environment; import freemarker.template.Configuration; @@ -46,46 +47,39 @@ public class DumpAllDirective extends BaseTemplateDirectiveModel { throw new TemplateModelException( "The dumpAll directive doesn't allow nested content."); } - - Configuration config = env.getConfiguration(); - TemplateHashModel dataModel = env.getDataModel(); - Map models = new HashMap(); - Map directives = new HashMap(); - - Map dm = (Map) DeepUnwrap.permissiveUnwrap(dataModel); + + TemplateHashModel dataModel = env.getDataModel(); + Map dm = (Map) DeepUnwrap.permissiveUnwrap(dataModel); List varNames = new ArrayList(dm.keySet()); Collections.sort(varNames); + + DumpHelper helper = new DumpHelper(env); + Configuration config = env.getConfiguration(); + List models = new ArrayList(); + List directives = new ArrayList(); + for (String var : varNames) { - // RY Instead, push each var/directive through the template and return a string. - // The meat of dumpDirective will go in a helper. - // 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 BaseTemplateDirectiveModel) { String help = ((BaseTemplateDirectiveModel) value).help(config); - directives.put(var, help); + directives.add(help); } else { - models.put(var, value); + models.add(helper.getVariableDump(var)); } } - String templateName = "dump-all.ftl"; - Map map = new HashMap(); map.put("models", models); map.put("directives", directives); - map.put("stylesheets", dataModel.get("stylesheets")); - map.put("dump", dataModel.get("dump")); - // Put the current datamodel into the new datamodel so its values can be dumped with the dump directive - // RY Another way to do this would be to loop through the data model here, merging each variable with - // the dump-var.ftl template and adding it to the output string. - map.put("datamodel", dataModel); map.put("containingTemplate", env.getTemplate().getName()); - FreemarkerHelper helper = new FreemarkerHelper(config); - String output = helper.mergeMapToTemplate(templateName, map); - Writer out = env.getOut(); - out.write(output); + try { + map.put("stylesheets", dataModel.get("stylesheets")); + } catch (TemplateModelException e) { + log.error("Error getting value of stylesheets variable from data model."); + } + + helper.writeDump("dumpAll.ftl", map, "template data model"); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpDirective.java similarity index 51% rename from webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java rename to webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpDirective.java index 83ef0616c..e8da4ccaf 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpDirective.java @@ -1,6 +1,6 @@ /* $This file is distributed under the terms of the license in /doc/license.txt$ */ -package edu.cornell.mannlib.vitro.webapp.web.directives; +package edu.cornell.mannlib.vitro.webapp.web.directives.dump; import java.io.IOException; import java.io.Writer; @@ -13,6 +13,7 @@ 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; @@ -52,76 +53,23 @@ public class DumpDirective extends BaseTemplateDirectiveModel { throw new TemplateModelException( "Value of parameter 'var' must be a string."); } - String var = ((SimpleScalar)o).getAsString(); - Object r = params.get("dataModelDump"); - boolean dataModelDump = false; - if (r != null) { - if ( !(r instanceof TemplateBooleanModel)) { - throw new TemplateModelException( - "Value of parameter 'recursive' must be a boolean: true or false without quotation marks."); - } - dataModelDump = ((TemplateBooleanModel) r).getAsBoolean(); - } - + String var = ((SimpleScalar)o).getAsString(); + DumpHelper helper = new DumpHelper(env); + Map map = helper.getVariableDumpData(var); + map.put("var", helper.getVariableDump(var)); + TemplateHashModel dataModel = env.getDataModel(); - if (dataModelDump) { - dataModel = (TemplateHashModel) dataModel.get("datamodel"); - } - - TemplateModel val = null; try { - val = dataModel.get(var); - } catch (TemplateModelException tme) { - log.error("Error getting value of template model " + var + " from data model."); - } - - // Just use this for now. Handles nested collections. - String value = val.toString(); - String type = null; - Object unwrappedModel = DeepUnwrap.permissiveUnwrap(val); - - // This case must precede the TemplateScalarModel case, because - // val is an instance of StringModel. - if (unwrappedModel instanceof BaseTemplateModel) { - type = unwrappedModel.getClass().getName(); - value = ((BaseTemplateModel)unwrappedModel).dump(); - } else if (val instanceof TemplateScalarModel) { - type = "string"; - } else if (val instanceof TemplateDateModel) { - type = "date"; - } else if (val instanceof TemplateNumberModel) { - type = "number"; - } else if (val instanceof TemplateBooleanModel) { - value = ((TemplateBooleanModel) val).getAsBoolean() ? "true" : "false"; - type = "boolean"; - } else if (val instanceof TemplateSequenceModel){ - type = "sequence"; - } else if (val 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; - // Add a case for BaseTemplateModel - our template model objects will have a dump() method. - } else { - type = "object"; + map.put("stylesheets", dataModel.get("stylesheets")); + } catch (TemplateModelException e) { + log.error("Error getting value of stylesheets variable from data model."); } - Map map = new HashMap(); - map.put("var", var); - map.put("value", value); - map.put("type", type); - - map.put("stylesheets", dataModel.get("stylesheets")); - //map.put("dump", this); // would need for recursive calls - - String output = new FreemarkerHelper(env.getConfiguration()).mergeMapToTemplate("dump-var.ftl", map); - Writer out = env.getOut(); - out.write(output); - + helper.writeDump("dump.ftl", map, var); } + public String help(Configuration config) { Map map = new HashMap(); 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 new file mode 100644 index 000000000..d4e48020b --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpHelper.java @@ -0,0 +1,111 @@ +/* $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.HashMap; +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.templatemodels.BaseTemplateModel; +import freemarker.core.Environment; +import freemarker.template.TemplateBooleanModel; +import freemarker.template.TemplateDateModel; +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 DumpHelper { + + private static final Log log = LogFactory.getLog(DumpHelper.class); + + private Environment environment = null; + + public DumpHelper(Environment env) { + environment = env; + } + + public String getVariableDump(String varName) { + Map map = getVariableDumpData(varName); + return new FreemarkerHelper(environment.getConfiguration()).mergeMapToTemplate("dump-var.ftl", map); + } + + public Map getVariableDumpData(String varName) { + TemplateHashModel dataModel = environment.getDataModel(); + + TemplateModel tm = null; + try { + tm = dataModel.get(varName); + } catch (TemplateModelException tme) { + 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. + 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); + + return map; + } + + public void writeDump(String templateName, Map map, String modelName) { + + FreemarkerHelper helper = new FreemarkerHelper(environment.getConfiguration()); + String output = helper.mergeMapToTemplate(templateName, map); + Writer out = environment.getOut(); + try { + out.write(output); + } catch (IOException e) { + log.error("Error writing dump of " + modelName + "."); + } + } + +} 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 d5b747fad..000000000 --- a/webapp/web/templates/freemarker/body/partials/dump/dump-all.ftl +++ /dev/null @@ -1,25 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Template for dumping the template data model --> - -
- -

Data Model Dump for Template ${containingTemplate}

- -

VARIABLES

- -
    - <#list models?keys as key> -
  • <@dump var="${key}" dataModelDump=true />
  • - -
- -

DIRECTIVES

- -
    - <#list directives?keys as directive> -
  • ${directives[directive]}
  • - -
- -
\ No newline at end of file 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 b53010a78..62c51a767 100644 --- a/webapp/web/templates/freemarker/body/partials/dump/dump-var.ftl +++ b/webapp/web/templates/freemarker/body/partials/dump/dump-var.ftl @@ -1,14 +1,12 @@ <#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> <#-- Template for dumping a template variable --> + +
-
- -
Dump of template variable ${var}:
- +
Variable name: ${var}
Type: ${type}
Value: ${value}
- +
-${stylesheets.add("/css/dump.css")} \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump.ftl new file mode 100644 index 000000000..232607e2d --- /dev/null +++ b/webapp/web/templates/freemarker/body/partials/dump/dump.ftl @@ -0,0 +1,9 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for dump directive --> + +

Template variable dump

+ +${var} + +${stylesheets.add("/css/dump.css")} \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/partials/dump/dumpAll.ftl b/webapp/web/templates/freemarker/body/partials/dump/dumpAll.ftl new file mode 100644 index 000000000..3e8debe2c --- /dev/null +++ b/webapp/web/templates/freemarker/body/partials/dump/dumpAll.ftl @@ -0,0 +1,27 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for dumpAll directive (dumping template data model) --> + +
+ +

Data model dump for template ${containingTemplate}

+ +

VARIABLES

+ +
    + <#list models as model> +
  • ${model}
  • + +
+ +

DIRECTIVES

+ +
    + <#list directives as directive> +
  • ${directive}
  • + +
+ +
+ +${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 0f43eb3f3..d7af70551 100644 --- a/webapp/web/templates/freemarker/body/test.ftl +++ b/webapp/web/templates/freemarker/body/test.ftl @@ -46,17 +46,18 @@

Berries: ${berries}

-<#-- + <@dump var="now" /> <@dump var="urls" /> <@dump var="fruit" /> <@dump var="trueStatement" /> <@dump var="falseStatement" /> <@dump var="zoo1" /> ---> + ${stylesheets.addFromTheme("/sstest.css", "/sstest2.css")} ${scripts.addFromTheme("/jstest.js")} ${scripts.add("/js/script1.js", "/js/script2.js", "/js/script3.js")} + <@dumpAll />