001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.components.template;
006:
007: import com.opensymphony.util.ClassLoaderUtil;
008: import com.opensymphony.webwork.views.JspSupportServlet;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import java.io.File;
014: import java.io.FileInputStream;
015: import java.io.FileNotFoundException;
016: import java.io.IOException;
017: import java.io.InputStream;
018: import java.util.HashMap;
019: import java.util.Map;
020: import java.util.Properties;
021:
022: /**
023: * Base class for template engines.
024: *
025: * @author jcarreira
026: * @author tm_jee
027: */
028: public abstract class BaseTemplateEngine implements TemplateEngine {
029:
030: private static final Log LOG = LogFactory
031: .getLog(BaseTemplateEngine.class);
032:
033: /** The default theme properties file name. Default is 'theme.properties' */
034: public static final String DEFAULT_THEME_PROPERTIES_FILE_NAME = "theme.properties";
035:
036: final Map themeProps = new HashMap();
037:
038: public Map getThemeProps(Template template) {
039: synchronized (themeProps) {
040: Properties props = (Properties) themeProps.get(template
041: .getTheme());
042: if (props == null) {
043: String propName = template.getDir() + "/"
044: + template.getTheme() + "/"
045: + getThemePropertiesFileName();
046:
047: // WW-1292
048: // let's try getting it from the filesystem
049: File propFile = new File(propName);
050: InputStream is = null;
051: try {
052: if (propFile.exists()) {
053: is = new FileInputStream(propFile);
054: }
055: } catch (FileNotFoundException e) {
056: LOG.warn("Unable to find file in filesystem ["
057: + propFile.getAbsolutePath() + "]");
058: }
059:
060: if (is == null) {
061: // let's try webapp's context
062: if (JspSupportServlet.jspSupportServlet != null) {
063: String _propName = propName.trim();
064: if (!_propName.startsWith("/")) {
065: _propName = "/" + _propName;
066: }
067: is = JspSupportServlet.jspSupportServlet
068: .getServletContext()
069: .getResourceAsStream(_propName);
070: }
071: }
072:
073: if (is == null) {
074: // if its not in filesystem. let's try the classpath
075: is = ClassLoaderUtil.getResourceAsStream(propName,
076: getClass());
077: }
078:
079: props = new Properties();
080:
081: if (is != null) {
082: try {
083: props.load(is);
084: } catch (IOException e) {
085: LOG.error("Could not load " + propName, e);
086: }
087: }
088:
089: themeProps.put(template.getTheme(), props);
090: }
091:
092: return props;
093: }
094: }
095:
096: protected String getFinalTemplateName(Template template) {
097: String t = template.toString();
098: if (t.indexOf(".") <= 0) {
099: 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: }
|