stringtemplate-spring

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

src/main/java/au/id/djc/stringtemplate/webmvc/StringTemplateViewResolver.java (2585B) - raw

      1 package au.id.djc.stringtemplate.webmvc;
      2 
      3 import java.nio.charset.Charset;
      4 import java.util.ArrayList;
      5 import java.util.List;
      6 
      7 import org.antlr.stringtemplate.StringTemplateErrorListener;
      8 import org.springframework.beans.factory.BeanFactoryUtils;
      9 import org.springframework.beans.factory.InitializingBean;
     10 import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
     11 import org.springframework.web.servlet.view.AbstractUrlBasedView;
     12 
     13 import au.id.djc.stringtemplate.AttributeRenderer;
     14 
     15 public class StringTemplateViewResolver extends AbstractTemplateViewResolver implements InitializingBean {
     16     
     17     private String rootTemplateName;
     18     private Charset charset;
     19     private StringTemplateErrorListener errorListener;
     20     private List<AttributeRenderer> attributeRenderers;
     21     
     22     @Override
     23     public void afterPropertiesSet() throws Exception {
     24         if (errorListener == null) {
     25             throw new IllegalArgumentException("Property 'errorListener' is required");
     26         }
     27     }
     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     /**
     42      * If this property is not set, the default behaviour is to use all
     43      * {@link AttributeRenderer} implementations in the current context.
     44      */
     45     public void setAttributeRenderers(List<AttributeRenderer> attributeRenderers) {
     46         this.attributeRenderers = attributeRenderers;
     47     }
     48 
     49     public StringTemplateViewResolver() {
     50         setViewClass(requiredViewClass());
     51     }
     52 
     53     @Override
     54     protected Class<?> requiredViewClass() {
     55         return StringTemplateView.class;
     56     }
     57     
     58     @Override
     59     protected AbstractUrlBasedView buildView(String viewName) throws Exception {
     60         StringTemplateView view = (StringTemplateView) super.buildView(viewName);
     61         if (rootTemplateName != null) view.setRootTemplateName(rootTemplateName);
     62         if (charset != null) view.setCharset(charset);
     63         view.setErrorListener(errorListener);
     64         if (attributeRenderers == null) {
     65             view.setAttributeRenderers(new ArrayList<AttributeRenderer>(
     66                     BeanFactoryUtils.beansOfTypeIncludingAncestors(getApplicationContext(), AttributeRenderer.class).values()));
     67         } else {
     68             view.setAttributeRenderers(attributeRenderers);
     69         }
     70         return view;
     71     }
     72 
     73 }