stringtemplate-spring

Spring integration for StringTemplate
git clone https://code.djc.id.au/git/stringtemplate-spring/

src/main/java/au/id/djc/stringtemplate/webmvc/StringTemplateView.java (2417B) - raw

      1 package au.id.djc.stringtemplate.webmvc;
      2 
      3 import java.io.InputStream;
      4 import java.io.InputStreamReader;
      5 import java.io.PrintWriter;
      6 import java.nio.charset.Charset;
      7 import java.util.List;
      8 import java.util.Locale;
      9 import java.util.Map;
     10 
     11 import javax.servlet.http.HttpServletRequest;
     12 import javax.servlet.http.HttpServletResponse;
     13 
     14 import org.antlr.stringtemplate.StringTemplate;
     15 import org.antlr.stringtemplate.StringTemplateErrorListener;
     16 import org.antlr.stringtemplate.StringTemplateGroup;
     17 import org.antlr.stringtemplate.language.DefaultTemplateLexer;
     18 import org.springframework.web.servlet.view.AbstractTemplateView;
     19 
     20 import au.id.djc.stringtemplate.AttributeRenderer;
     21 
     22 public class StringTemplateView extends AbstractTemplateView {
     23 
     24     private String rootTemplateName = "root";
     25     private Charset charset = Charset.defaultCharset();
     26     private StringTemplateErrorListener errorListener;
     27     private List<AttributeRenderer> attributeRenderers;
     28     
     29     public void setRootTemplateName(String rootTemplateName) {
     30         this.rootTemplateName = rootTemplateName;
     31     }
     32     
     33     public void setCharset(Charset charset) {
     34         this.charset = charset;
     35     }
     36     
     37     public void setErrorListener(StringTemplateErrorListener errorListener) {
     38         this.errorListener = errorListener;
     39     }
     40     
     41     public void setAttributeRenderers(List<AttributeRenderer> attributeRenderers) {
     42         this.attributeRenderers = attributeRenderers;
     43     }
     44 
     45     @Override
     46     public boolean checkResource(Locale locale) throws Exception {
     47         return getClass().getResourceAsStream(getUrl()) != null;
     48     }
     49 
     50     @Override
     51     protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
     52             throws Exception {
     53         InputStream groupStream = getClass().getResourceAsStream(getUrl());
     54         StringTemplateGroup templateGroup = new StringTemplateGroup(new InputStreamReader(groupStream, charset),
     55                 DefaultTemplateLexer.class, errorListener);
     56         for (AttributeRenderer attributeRenderer: attributeRenderers) {
     57             templateGroup.registerRenderer(attributeRenderer.getTargetClass(), attributeRenderer);
     58         }
     59         StringTemplate template = templateGroup.getInstanceOf(rootTemplateName);
     60         template.setAttributes(model);
     61         PrintWriter writer = response.getWriter();
     62         writer.print(template);
     63     }
     64 
     65 }