Fixes to variable dump.

This commit is contained in:
rjy7 2010-11-18 23:30:35 +00:00
parent 9bd79b636d
commit ca76cfa4bd
5 changed files with 40 additions and 27 deletions

View file

@ -14,9 +14,7 @@ import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.Portal; import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet.ResponseValues; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditConfiguration;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet.TemplateResponseValues;
import freemarker.template.Configuration;
/** /**
* Freemarker controller and template samples. * Freemarker controller and template samples.
@ -82,6 +80,8 @@ public class SamplesController extends FreemarkerHttpServlet {
body.put("trueStatement", true); body.put("trueStatement", true);
body.put("falseStatement", false); body.put("falseStatement", false);
body.put("pojo", new EditConfiguration());
getBerries(body); getBerries(body);
body.put("bookTitle", "Pride and Prejudice"); body.put("bookTitle", "Pride and Prejudice");

View file

@ -20,7 +20,6 @@ import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModel; import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException; import freemarker.template.TemplateModelException;
import freemarker.template.TemplateNumberModel; import freemarker.template.TemplateNumberModel;
import freemarker.template.TemplateScalarModel;
import freemarker.template.TemplateSequenceModel; import freemarker.template.TemplateSequenceModel;
import freemarker.template.utility.DeepUnwrap; import freemarker.template.utility.DeepUnwrap;
@ -54,9 +53,7 @@ public class DumpHelper {
map.put("var", varName); map.put("var", varName);
if (tm != null) { if (tm != null) {
String type = null;
Object unwrappedModel = null; Object unwrappedModel = null;
try { try {
unwrappedModel = DeepUnwrap.permissiveUnwrap(tm); unwrappedModel = DeepUnwrap.permissiveUnwrap(tm);
} catch (TemplateModelException e) { } catch (TemplateModelException e) {
@ -67,38 +64,51 @@ public class DumpHelper {
// tm.toString() gives wrong results in the case of, e.g., a boolean value in a hash. tm.toString() may // tm.toString() gives wrong results in the case of, e.g., a boolean value in a hash. tm.toString() may
// return a TemplateBooleanModel object, while unwrappedModel.toString() returns "true" or "false." // return a TemplateBooleanModel object, while unwrappedModel.toString() returns "true" or "false."
String value = unwrappedModel.toString(); // tm.toString(); String value = unwrappedModel.toString(); // tm.toString();
String className = unwrappedModel.getClass().getName();
String type = null;
// This case must precede the TemplateScalarModel case, because // For basic Java types such as string, date, boolean, it's most helpful for the dump to
// tm is an instance of StringModel and thus a TemplateScalarModel. // show the shorthand type assigned below, rather than the Java class name. But for our
// BaseTemplateModel objects, show the actual class, since that provides additional
// information about the object (available methods, for example) that it is helpful to
// view in the dump. Not sure if we should handle our application-specific, non-template
// model objects in the same way. For now, these get assigned a shorthand type below.
if (unwrappedModel instanceof BaseTemplateModel) { if (unwrappedModel instanceof BaseTemplateModel) {
type = unwrappedModel.getClass().getName();
value = ((BaseTemplateModel)unwrappedModel).dump(); value = ((BaseTemplateModel)unwrappedModel).dump();
} else if (tm instanceof TemplateScalarModel) { type = className;
type = "string"; }
// Can't use this, because tm of (at least some) POJOs are
// StringModels, which are both TemplateScalarModels and TemplateHashModels
// if (tm instanceof TemplateScalarModel)
else if (unwrappedModel instanceof String) {
type = "String";
} else if (tm instanceof TemplateDateModel) { } else if (tm instanceof TemplateDateModel) {
type = "date"; type = "Date";
} else if (tm instanceof TemplateNumberModel) { } else if (tm instanceof TemplateNumberModel) {
type = "number"; type = "Number";
} else if (tm instanceof TemplateBooleanModel) { } else if (tm instanceof TemplateBooleanModel) {
type = "boolean"; type = "Boolean";
try { try {
value = ((TemplateBooleanModel) tm).getAsBoolean() ? "true" : "false"; value = ((TemplateBooleanModel) tm).getAsBoolean() ? "true" : "false";
} catch (TemplateModelException e) { } catch (TemplateModelException e) {
log.error("Error getting boolean value for " + varName + "."); log.error("Error getting boolean value for " + varName + ".");
} }
} else if (tm instanceof TemplateSequenceModel){ } else if (tm instanceof TemplateSequenceModel){
type = "sequence"; type = "Sequence";
} else if (tm instanceof TemplateHashModel) { } else if (tm instanceof TemplateHashModel) {
type = "hash"; type = "Hash";
// In recursive dump, we've gotten down to a raw string. Just output it. // In recursive dump, we've gotten down to a raw string. Just output it.
// } else if (val == null) { // } else if (val == null) {
// out.write(var); // out.write(var);
// return; // return;
} else { } else {
type = "object"; // One of the above cases should have applied. Just in case not, show the Java class name.
type = className;
} }
map.put("value", value); map.put("value", value);
map.put("type", type); map.put("type", type);
} }
return map; return map;

View file

@ -22,3 +22,7 @@
padding: 1em 0; padding: 1em 0;
margin: 1em 0; margin: 1em 0;
} }
.dump .var p {
margin-bottom: .3em;
}

View file

@ -3,14 +3,12 @@
<#-- Template for dumping a template variable --> <#-- Template for dumping a template variable -->
<div class="var"> <div class="var">
<p><strong>Variable name: <em>${var}</em></strong></p>
<h6>Variable name: <em>${var}</em></h6>
<#if value??> <#if value??>
<strong>Type:</strong> ${type}<br /> <p><strong>Value:</strong> ${value}</p>
<strong>Value:</strong> ${value}<br /> <p><strong>Type:</strong> ${type}</p>
<#else> <#else>
Variable is undefined in the data model <p>Variable is undefined in the data model</p>
</#if> </#if>
</div> </div>

View file

@ -51,6 +51,7 @@
<@dump var="fruit" /> <@dump var="fruit" />
<@dump var="trueStatement" /> <@dump var="trueStatement" />
<@dump var="zoo1" /> <@dump var="zoo1" />
<@dump var="pojo" />
${stylesheets.addFromTheme("/css/sstest.css", "/css/sstest2.css")} ${stylesheets.addFromTheme("/css/sstest.css", "/css/sstest2.css")}
${scripts.addFromTheme("/js/jstest.js")} ${scripts.addFromTheme("/js/jstest.js")}