NIHVIVO-2470 Initial work on new dump template. Handle undefined variables.

This commit is contained in:
ryounes 2011-04-20 21:23:38 +00:00
parent 4bff64ca12
commit 84f779238c
9 changed files with 96 additions and 56 deletions

View file

@ -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<String, Object> value = new HashMap<String, Object>();
// 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<String, Object> dump = new HashMap<String, Object>();
dump.put(varName, value);
return dump;
@ -455,9 +459,13 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
return map;
}
protected void dump(String templateName, Map<String, Object> map, Environment env)
protected void dump(String templateName, Map<String, Object> 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>...</#list>
Map<String, Map<String, Object>> map = new HashMap<String, Map<String, Object>>();
map.put("dump", dump);
Template template = env.getConfiguration().getTemplate(templateName);
StringWriter sw = new StringWriter();
template.process(map, sw);

View file

@ -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<String, Object> dataModelDump = getDataModelDump(env);
dump("dump-all.ftl", dataModelDump, env);
dump(TEMPLATE_DEFAULT, dataModelDump, env);
}
SortedMap<String, Object> getDataModelDump(Environment env) throws TemplateModelException {

View file

@ -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<String, Object> map = getTemplateVariableDump(varName, env);
dump("dumpvar.ftl", map, env);
dump(TEMPLATE_DEFAULT, map, env);
}
@Override

View file

@ -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<String, Object> map = getTemplateVariableDump(varName, env);
dump("dumpvar.ftl", map, env);
dump(TEMPLATE_DEFAULT, map, env);
}
@Override