/connect-web/src/main/java/org/osforce/connect/web/tag/freemarker/FreemarkerRenderTag.java

http://focus-sns.googlecode.com/ · Java · 38 lines · 25 code · 6 blank · 7 comment · 0 complexity · 2f0da9312af0369548a4a290ff962dc4 MD5 · raw file

  1. package org.osforce.connect.web.tag.freemarker;
  2. import java.io.IOException;
  3. import java.io.StringReader;
  4. import java.util.Map;
  5. import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
  6. import org.springframework.util.Assert;
  7. import freemarker.core.Environment;
  8. import freemarker.template.Template;
  9. import freemarker.template.TemplateDirectiveBody;
  10. import freemarker.template.TemplateDirectiveModel;
  11. import freemarker.template.TemplateException;
  12. import freemarker.template.TemplateModel;
  13. import freemarker.template.TemplateScalarModel;
  14. /**
  15. *
  16. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  17. * @since 1.1.0
  18. * @create May 31, 2011 - 1:22:56 PM
  19. * <a href="http://www.opensourceforce.org">????</a>
  20. */
  21. public class FreemarkerRenderTag implements TemplateDirectiveModel {
  22. @SuppressWarnings("unchecked")
  23. public void execute(Environment env, Map params, TemplateModel[] loopVars,
  24. TemplateDirectiveBody body) throws TemplateException, IOException {
  25. TemplateScalarModel templateModel = (TemplateScalarModel) params.get("template");
  26. Assert.notNull(templateModel, "Parameter template can not be null!");
  27. Template template = new Template("",
  28. new StringReader(templateModel.getAsString()), env.getConfiguration());
  29. String result = FreeMarkerTemplateUtils.processTemplateIntoString(template, env.getDataModel());
  30. env.getOut().write(result);
  31. }
  32. }