View Javadoc

1   /*
2    * $Id: BaseTemplateEngine.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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                  // WW-1292
56                  // let's try getting it from the filesystem
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                      // if its not in filesystem. let's try the classpath
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 }