001: /*
002: * $Id: BaseTemplateEngine.java 570513 2007-08-28 18:14:00Z jholmes $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components.template;
022:
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.FileNotFoundException;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.util.HashMap;
029: import java.util.Map;
030: import java.util.Properties;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import com.opensymphony.xwork2.util.ClassLoaderUtil;
036:
037: /**
038: * Base class for template engines.
039: */
040: public abstract class BaseTemplateEngine implements TemplateEngine {
041:
042: private static final Log LOG = LogFactory
043: .getLog(BaseTemplateEngine.class);
044:
045: /** The default theme properties file name. Default is 'theme.properties' */
046: public static final String DEFAULT_THEME_PROPERTIES_FILE_NAME = "theme.properties";
047:
048: final Map<String, Properties> themeProps = new HashMap<String, Properties>();
049:
050: public Map getThemeProps(Template template) {
051: synchronized (themeProps) {
052: Properties props = (Properties) themeProps.get(template
053: .getTheme());
054: if (props == null) {
055: String propName = template.getDir() + "/"
056: + template.getTheme() + "/"
057: + getThemePropertiesFileName();
058:
059: // WW-1292
060: // let's try getting it from the filesystem
061: File propFile = new File(propName);
062: InputStream is = null;
063: try {
064: if (propFile.exists()) {
065: is = new FileInputStream(propFile);
066: }
067: } catch (FileNotFoundException e) {
068: LOG.warn("Unable to find file in filesystem ["
069: + propFile.getAbsolutePath() + "]");
070: }
071:
072: if (is == null) {
073: // if its not in filesystem. let's try the classpath
074: is = ClassLoaderUtil.getResourceAsStream(propName,
075: getClass());
076: }
077:
078: props = new Properties();
079:
080: if (is != null) {
081: try {
082: props.load(is);
083: } catch (IOException e) {
084: LOG.error("Could not load " + propName, e);
085: } finally {
086: try {
087: is.close();
088: } catch (IOException io) {
089: LOG
090: .warn(
091: "Unable to close input stream",
092: io);
093: }
094: }
095: }
096:
097: themeProps.put(template.getTheme(), props);
098: }
099:
100: return props;
101: }
102: }
103:
104: protected String getFinalTemplateName(Template template) {
105: String t = template.toString();
106: if (t.indexOf(".") <= 0) {
107: return t + "." + getSuffix();
108: }
109:
110: return t;
111: }
112:
113: protected String getThemePropertiesFileName() {
114: return DEFAULT_THEME_PROPERTIES_FILE_NAME;
115: }
116:
117: protected abstract String getSuffix();
118: }
|