View Javadoc

1   package br.com.ibnetwork.guara.view.impl;
2   
3   import java.net.URL;
4   
5   import org.apache.avalon.framework.configuration.Configurable;
6   import org.apache.avalon.framework.configuration.Configuration;
7   import org.apache.avalon.framework.configuration.ConfigurationException;
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  
11  import br.com.ibnetwork.guara.view.Context;
12  import br.com.ibnetwork.guara.view.TemplateEngine;
13  import br.com.ibnetwork.guara.view.TemplateEngineException;
14  
15  /***
16   * @author <a href="mailto:leandro@ibnetwork.com.br">leandro</a>
17   */
18  public abstract class TemplateEngineSupport
19  	implements TemplateEngine, Configurable
20  {
21      protected Log log = LogFactory.getLog(this.getClass());
22  
23      protected String extension;
24      
25      protected String encoding;
26      
27      protected String onErrorTemplate;
28      
29      protected URL configurationFile;
30      
31      public void configure(Configuration conf)
32  		throws ConfigurationException
33  	{
34          extension = conf.getChild("templates").getAttribute("extension",".txt");
35          encoding = conf.getChild("encoding").getAttribute("name","ISO-8859-1");
36          onErrorTemplate = conf.getChild("templates").getChild("onError").getAttribute("template","screens.Error");
37          String fileName = conf.getChild("file").getAttribute("name",null);
38          if(fileName != null)
39          {
40              configurationFile = Thread.currentThread().getContextClassLoader().getResource(fileName);
41          }
42          log.info("TemplateEngine configuration file ["+configurationFile+"]");
43  	}
44  
45      public Context createContext()
46  		throws TemplateEngineException
47  	{
48          return new ContextImpl(20);
49  	}
50      
51      public String toFileName(String templateName)
52      {
53          if(templateName.endsWith(extension))
54          {
55              int pos = templateName.length() - extension.length();
56              templateName = templateName.substring(0,pos);
57          }
58          String tmp = templateName.replaceAll("//.","/");
59          return tmp + extension;
60      }
61  
62      public String toTemplateName(String templateName)
63      {
64          if(templateName.startsWith("/"))
65          {
66              templateName = templateName.substring(1);
67          }
68          if(templateName.endsWith(extension))
69          {
70              int pos = templateName.length() - extension.length();
71              templateName = templateName.substring(0,pos);
72          }
73          String tmp = templateName.replaceAll("/","//.");
74          return tmp;
75      }
76      
77      public String getTemplateExtension()
78      {
79          return extension;
80      }
81  
82      public String getEncoding()
83      {
84          return encoding;
85      }
86      
87      public String getOnErrorTemplate()
88      {
89      	return onErrorTemplate;
90      }
91  
92  }