1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
53
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
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 }