View Javadoc

1   /*
2    * $Id: BaseTemplateEngine.java 471756 2006-11-06 15:01:43Z husted $
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  package org.apache.struts2.components.template;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileNotFoundException;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.util.HashMap;
29  import java.util.Map;
30  import java.util.Properties;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import com.opensymphony.xwork2.util.ClassLoaderUtil;
36  
37  /***
38   * Base class for template engines.
39   */
40  public abstract class BaseTemplateEngine implements TemplateEngine {
41  
42      private static final Log LOG = LogFactory.getLog(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                      }
81                  }
82  
83                  themeProps.put(template.getTheme(), props);
84              }
85  
86              return props;
87          }
88      }
89  
90      protected String getFinalTemplateName(Template template) {
91          String t = template.toString();
92          if (t.indexOf(".") <= 0) {
93              return t + "." + getSuffix();
94          }
95  
96          return t;
97      }
98  
99      protected String getThemePropertiesFileName() {
100         return DEFAULT_THEME_PROPERTIES_FILE_NAME;
101     }
102 
103     protected abstract String getSuffix();
104 }