diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java new file mode 100644 index 000000000..05f48c8f6 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java @@ -0,0 +1,89 @@ +package edu.cornell.mannlib.vitro.webapp.web.directives; + +import java.io.IOException; +import java.io.Writer; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import freemarker.core.Environment; +import freemarker.template.Configuration; +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateDirectiveBody; +import freemarker.template.TemplateDirectiveModel; +import freemarker.template.TemplateException; +import freemarker.template.TemplateHashModel; +import freemarker.template.TemplateModel; +import freemarker.template.TemplateModelException; +import freemarker.template.utility.DeepUnwrap; + +public class DumpDirective implements TemplateDirectiveModel { + + @SuppressWarnings("unchecked") + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, + TemplateDirectiveBody body) throws TemplateException, IOException { + + if (loopVars.length != 0) { + throw new TemplateModelException( + "The dump directive doesn't allow loop variables."); + } + if (body != null) { + throw new TemplateModelException( + "The dump directive doesn't allow nested content."); + } + + Object o = params.get("var"); + if ( !(o instanceof SimpleScalar)) { + throw new TemplateModelException( + "Value of 'var' must be a string."); + } + String var = ((SimpleScalar)o).getAsString(); + + TemplateHashModel dataModel = env.getDataModel(); + String output; + + if (var.equals("data_model")) { + output = "Data model: "; + Map dm = (Map) DeepUnwrap.permissiveUnwrap(dataModel); + Set varNames = dm.keySet(); + for (Object varName : varNames) { + output += (String) varName + ", "; + } + + // Add shared variables + Configuration config = env.getConfiguration(); + Set sharedVars = config.getSharedVariableNames(); + Iterator i = sharedVars.iterator(); + while (i.hasNext()) { + String sv = (String) i.next(); + TemplateModel tm = config.getSharedVariable(sv); + if (tm instanceof TemplateDirectiveModel || + // Legacy built-ins that are added to all configurations + tm instanceof freemarker.template.utility.CaptureOutput || + tm instanceof freemarker.template.utility.StandardCompress || + tm instanceof freemarker.template.utility.HtmlEscape || + tm instanceof freemarker.template.utility.NormalizeNewlines || + tm instanceof freemarker.template.utility.XmlEscape) { + continue; + } + output += sv + ", "; + } + + output = output.replaceAll(", $", "."); + + } else { + TemplateModel val = dataModel.get(var); + output = var + ": " + val.toString(); + } + + // RY Improve by making presentation of various types more nuanced + // Also merge to a template for formatting + // get config from environment; get a template from config + // merge as in FreeMarkerHttpServlet.mergeToTemplate() + Writer out = env.getOut(); + out.write(output + "
"); + + } + +} diff --git a/webapp/web/templates/freemarker/body/primitiveRdfEdit.fmt b/webapp/web/templates/freemarker/body/primitiveRdfEdit.fmt deleted file mode 100644 index 7e8acc32b..000000000 --- a/webapp/web/templates/freemarker/body/primitiveRdfEdit.fmt +++ /dev/null @@ -1,19 +0,0 @@ -
-
- -additions: - - -retractions: - -
-N3 -TURTLE -RDF/XML -
- - - - -
-
\ 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 5abd6fb1c..6c8f78b72 100644 --- a/webapp/web/templates/freemarker/body/test.ftl +++ b/webapp/web/templates/freemarker/body/test.ftl @@ -23,4 +23,8 @@ -

Animal: ${animal}

\ No newline at end of file +

Animal: ${animal}

+ +<@dump var="fruit" /> +<@dump var="urls" /> +<@dump var="data_model" />