View Javadoc

1   package br.com.ibnetwork.guara.view.impl.freemarker;
2   
3   import java.io.IOException;
4   import java.io.Writer;
5   import java.util.Iterator;
6   import java.util.Map;
7   
8   import org.apache.avalon.framework.configuration.Configurable;
9   import org.apache.avalon.framework.configuration.Configuration;
10  import org.apache.avalon.framework.configuration.ConfigurationException;
11  import org.apache.avalon.framework.service.ServiceException;
12  import org.apache.avalon.framework.service.ServiceManager;
13  import org.apache.avalon.framework.service.Serviceable;
14  
15  import freemarker.template.Template;
16  import freemarker.template.TemplateMethodModel;
17  
18  import br.com.ibnetwork.xingu.factory.Factory;
19  import br.com.ibnetwork.guara.view.Context;
20  import br.com.ibnetwork.guara.view.TemplateEngineException;
21  import br.com.ibnetwork.guara.view.impl.TemplateEngineSupport;
22  
23  /***
24   * @author <a href="mailto:leandro@ibnetwork.com.br">leandro</a>
25   */
26  public class FreemarkerTemplateEngine
27  	extends TemplateEngineSupport
28  	implements Configurable, Serviceable
29  {
30      private freemarker.template.Configuration cfg;
31      
32      private Factory factory;
33  
34      private FreemarkerConfiguration wrapper;
35      
36      public void service(ServiceManager manager) 
37          throws ServiceException
38      {
39          factory = (Factory) manager.lookup(Factory.ROLE);
40      }
41      
42      public void configure(Configuration conf)
43      	throws ConfigurationException
44      {
45          super.configure(conf);
46          wrapper = (FreemarkerConfiguration) factory.create(FreemarkerConfiguration.class);
47          cfg = new freemarker.template.Configuration();
48          try
49          {
50              wrapper.configureTemplateEngine(configurationFile,cfg);
51          }
52          catch (Exception e)
53          {
54              throw new ConfigurationException("Error on freemarker configuration",e);
55          }
56      }
57  
58      public void merge(String templateName, Context context, Writer w)
59      	throws TemplateEngineException
60      {
61          String realName = toFileName(templateName);
62          if(log.isDebugEnabled()) log.debug("rendering [" + realName + "]");
63          try
64          {
65              Template template = cfg.getTemplate(realName);
66              addMethods(context, wrapper.getMethods());
67              template.process(context.getMap(),w);
68          }
69          catch (Exception e)
70          {
71              throw new TemplateEngineException("Error merging template ["+realName+"]",e);
72          }
73      }
74  
75      private void addMethods(Context context, Map methods)
76      {
77          for(Iterator it = methods.keySet().iterator(); it.hasNext();)
78          {
79              String methodName = (String) it.next();
80              TemplateMethodModel method = (TemplateMethodModel) methods.get(methodName);
81              context.put(methodName,method);
82          }
83      }
84  
85      public boolean templateExists(String templateName)
86      	throws TemplateEngineException
87      {
88          String realName = toFileName(templateName);
89          try
90          {
91              Template template = cfg.getTemplate(realName);
92              return template != null;
93          }
94          catch (IOException e)
95          {
96              /*
97               * Ignored. 
98               * Freemarker doesn't provide any API call to determine if a template
99               * exists or not without trying to parse it
100              */
101             return false;
102         }
103     }
104 }