NIHVIVO-2479 Dump templates
This commit is contained in:
parent
20e65852d5
commit
2f884cec25
5 changed files with 103 additions and 39 deletions
|
@ -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<String, Object> 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<String, Object> map = new HashMap<String, Object>();
|
||||
Object unwrappedModel = DeepUnwrap.permissiveUnwrap(model);
|
||||
map.put(Key.TYPE.toString(), unwrappedModel.getClass().getName());
|
||||
|
@ -457,13 +458,24 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
|
|||
return map;
|
||||
}
|
||||
|
||||
protected void dump(String templateName, Map<String, Object> dump, Environment env)
|
||||
protected void dump(Map<String, Object> dump, Environment env, String title)
|
||||
throws TemplateException, IOException {
|
||||
dump(dump, env, title, TEMPLATE_DEFAULT);
|
||||
}
|
||||
|
||||
protected void dump(Map<String, Object> 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>...</#list>
|
||||
Map<String, Map<String, Object>> map = new HashMap<String, Map<String, Object>>();
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("dump", dump);
|
||||
map.put("title", title);
|
||||
writeDump(map, env, templateName);
|
||||
}
|
||||
|
||||
protected void writeDump(Map<String, Object> map, Environment env, String templateName)
|
||||
throws TemplateException, IOException {
|
||||
Template template = env.getConfiguration().getTemplate(templateName);
|
||||
StringWriter sw = new StringWriter();
|
||||
template.process(map, sw);
|
||||
|
|
|
@ -46,8 +46,9 @@ public class DumpAllDirective extends BaseDumpDirective {
|
|||
"The dump directive doesn't allow nested content.");
|
||||
}
|
||||
|
||||
SortedMap<String, Object> dataModelDump = getDataModelDump(env);
|
||||
dump(TEMPLATE_DEFAULT, dataModelDump, env);
|
||||
SortedMap<String, Object> dump = getDataModelDump(env);
|
||||
String title = "Template data model dump for " + env.getTemplate().getName();
|
||||
dump(dump, env, title);
|
||||
}
|
||||
|
||||
SortedMap<String, Object> getDataModelDump(Environment env) throws TemplateModelException {
|
||||
|
|
|
@ -51,8 +51,8 @@ public class DumpDirective extends BaseDumpDirective {
|
|||
|
||||
String varName = o.toString(); //((SimpleScalar)o).getAsString();
|
||||
Map<String, Object> map = getTemplateVariableDump(varName, env);
|
||||
|
||||
dump(TEMPLATE_DEFAULT, map, env);
|
||||
String title = "Template variable dump";
|
||||
dump(map, env, title);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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<String, Object> 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
|
||||
|
|
|
@ -2,37 +2,90 @@
|
|||
|
||||
<#-- 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) -->
|
||||
<style>
|
||||
ul.dump {
|
||||
padding-top: .75em;
|
||||
padding-bottom: .75em;
|
||||
div.dump {
|
||||
margin-bottom: .5em;
|
||||
border-top: 1px solid #ccc;
|
||||
border-bottom: 1px solid #ccc;
|
||||
margin-bottom: .5em;
|
||||
padding-top: .75em;
|
||||
padding-bottom: .75em;
|
||||
}
|
||||
|
||||
ul.dump li p {
|
||||
margin-bottom: .5em;
|
||||
.dump ul ul {
|
||||
margin-left: 1.5em;
|
||||
}
|
||||
|
||||
.dump ul li {
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.dump ul li p {
|
||||
margin-bottom: .25em;
|
||||
}
|
||||
|
||||
.dump ul.sequence li.list_item {
|
||||
margin-bottom: 1.25em;
|
||||
}
|
||||
</style>
|
||||
|
||||
<#-- <pre><@dumper.dump dump /></pre> -->
|
||||
<#--
|
||||
<#import "lib-dump.ftl" as dumper>
|
||||
<pre><@dumper.dump dump /></pre>
|
||||
-->
|
||||
|
||||
<div class="dump">
|
||||
<h3>${title}</h3>
|
||||
|
||||
<@doDump dump />
|
||||
</div>
|
||||
|
||||
<#macro doDump dump>
|
||||
<#if dump?keys?has_content>
|
||||
<ul class="dump">
|
||||
<ul>
|
||||
<#list dump?keys as key>
|
||||
<li>
|
||||
<#assign value = dump[key] />
|
||||
<#local dumpVal = dump[key] />
|
||||
<p><strong>Variable name:</strong> ${key}</p>
|
||||
<#if value.type??><p><strong>Type:</strong> ${value.type}</p></#if>
|
||||
<#-- <p><strong>Value:</strong> ${value.value}</p> -->
|
||||
<#-- What to do here depends on time. Test either ${var.type} or ${var.value} -->
|
||||
<#-- <p><strong>Value:</strong> ${var.value}</p> -->
|
||||
|
||||
<@doMap dumpVal />
|
||||
</li>
|
||||
</#list>
|
||||
</ul>
|
||||
</#if>
|
||||
</#macro>
|
||||
|
||||
<#macro doMap map>
|
||||
<#if map.type?has_content>
|
||||
<p><strong>Type:</strong> ${map.type}</p>
|
||||
|
||||
<#if map.dateType?has_content>
|
||||
<p><strong>Date type:</strong> ${map.dateType}</p>
|
||||
</#if>
|
||||
</#if>
|
||||
|
||||
<#if map.value?has_content>
|
||||
<p>
|
||||
<strong>Value:</strong>
|
||||
<#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 />
|
||||
</#if>
|
||||
</p>
|
||||
</#if>
|
||||
</#macro>
|
||||
|
||||
<#macro doSequence seq>
|
||||
<#if seq?has_content>
|
||||
<ul class="sequence">
|
||||
<#list seq as item>
|
||||
<li class="list_item">Item ${item_index}: <@doMap item /></li>
|
||||
</#list>
|
||||
</ul>
|
||||
</#if>
|
||||
</#macro>
|
||||
|
||||
<#-- This will work after we move stylesheets to Configuration sharedVariables
|
||||
${stylesheets.add('<link rel="stylesheet" href="/css/fmdump.css">')}
|
||||
|
|
Loading…
Add table
Reference in a new issue