1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.components.template;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Properties;
32
33 import com.opensymphony.xwork2.util.ClassLoaderUtil;
34 import com.opensymphony.xwork2.util.logging.Logger;
35 import com.opensymphony.xwork2.util.logging.LoggerFactory;
36
37 /***
38 * Base class for template engines.
39 */
40 public abstract class BaseTemplateEngine implements TemplateEngine {
41
42 private static final Logger LOG = LoggerFactory.getLogger(BaseTemplateEngine.class);
43
44 /*** The default theme properties file name. Default is 'theme.properties' */
45 public static final String DEFAULT_THEME_PROPERTIES_FILE_NAME = "theme.properties";
46
47 final Map<String, Properties> themeProps = new HashMap<String, Properties>();
48
49 public Map getThemeProps(Template template) {
50 synchronized (themeProps) {
51 Properties props = (Properties) themeProps.get(template.getTheme());
52 if (props == null) {
53 String propName = template.getDir() + "/" + template.getTheme() + "/"+getThemePropertiesFileName();
54
55
56
57 File propFile = new File(propName);
58 InputStream is = null;
59 try {
60 if (propFile.exists()) {
61 is = new FileInputStream(propFile);
62 }
63 }
64 catch(FileNotFoundException e) {
65 LOG.warn("Unable to find file in filesystem ["+propFile.getAbsolutePath()+"]");
66 }
67
68 if (is == null) {
69
70 is = ClassLoaderUtil.getResourceAsStream(propName, getClass());
71 }
72
73 props = new Properties();
74
75 if (is != null) {
76 try {
77 props.load(is);
78 } catch (IOException e) {
79 LOG.error("Could not load " + propName, e);
80 } finally {
81 try {
82 is.close();
83 } catch(IOException io) {
84 LOG.warn("Unable to close input stream", io);
85 }
86 }
87 }
88
89 themeProps.put(template.getTheme(), props);
90 }
91
92 return props;
93 }
94 }
95
96 protected String getFinalTemplateName(Template template) {
97 String t = template.toString();
98 if (t.indexOf(".") <= 0) {
99 return t + "." + getSuffix();
100 }
101
102 return t;
103 }
104
105 protected String getThemePropertiesFileName() {
106 return DEFAULT_THEME_PROPERTIES_FILE_NAME;
107 }
108
109 protected abstract String getSuffix();
110 }