View Javadoc

1   /*
2    * $Id: BaseTemplateEngine.java 441909 2006-09-10 05:28:16Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components.template;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileNotFoundException;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import com.opensymphony.xwork2.util.ClassLoaderUtil;
33  
34  /***
35   * Base class for template engines.
36   */
37  public abstract class BaseTemplateEngine implements TemplateEngine {
38  	
39      private static final Log LOG = LogFactory.getLog(BaseTemplateEngine.class);
40      
41      /*** The default theme properties file name. Default is 'theme.properties' */
42      public static final String DEFAULT_THEME_PROPERTIES_FILE_NAME = "theme.properties";
43  
44      final Map<String, Properties> themeProps = new HashMap<String, Properties>();
45  
46      public Map getThemeProps(Template template) {
47          synchronized (themeProps) {
48              Properties props = (Properties) themeProps.get(template.getTheme());
49              if (props == null) {
50                  String propName = template.getDir() + "/" + template.getTheme() + "/"+getThemePropertiesFileName();
51                  
52  //              WW-1292
53                  // let's try getting it from the filesystem
54                  File propFile = new File(propName);
55                  InputStream is = null;
56                  try {
57                  	if (propFile.exists()) {
58                  		is = new FileInputStream(propFile);
59                  	}
60                  }
61                  catch(FileNotFoundException e) {
62                  	LOG.warn("Unable to find file in filesystem ["+propFile.getAbsolutePath()+"]");
63                  }
64                  
65                  if (is == null) {
66                  	// if its not in filesystem. let's try the classpath
67                  	is = ClassLoaderUtil.getResourceAsStream(propName, getClass());
68                  }
69                  
70                  props = new Properties();
71  
72                  if (is != null) {
73                      try {
74                          props.load(is);
75                      } catch (IOException e) {
76                          LOG.error("Could not load " + propName, e);
77                      }
78                  }
79  
80                  themeProps.put(template.getTheme(), props);
81              }
82  
83              return props;
84          }
85      }
86  
87      protected String getFinalTemplateName(Template template) {
88          String t = template.toString();
89          if (t.indexOf(".") <= 0) {
90              return t + "." + getSuffix();
91          }
92  
93          return t;
94      }
95      
96      protected String getThemePropertiesFileName() {
97      	return DEFAULT_THEME_PROPERTIES_FILE_NAME;
98      }
99  
100     protected abstract String getSuffix();
101 }