001: package org.apache.turbine.services.template.mapper;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.commons.lang.StringUtils;
023:
024: import org.apache.turbine.services.template.TurbineTemplate;
025: import org.apache.turbine.services.template.TemplateEngineService;
026:
027: /**
028: * A base class for the various mappers which contains common
029: * code.
030: *
031: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
032: * @version $Id: BaseMapper.java 264148 2005-08-29 14:21:04Z henning $
033: */
034:
035: public abstract class BaseMapper {
036: /** True if this mapper should cache template -> name mappings */
037: private boolean useCache = false;
038:
039: /** Default cache size. Just a number out of thin air. Will be set at init time */
040: private int cacheSize = 5;
041:
042: /** The internal template -> name mapping cache */
043: private Map templateCache = null;
044:
045: /** The name of the default property to pull from the Template Engine Service if the default is requested */
046: protected String defaultProperty;
047:
048: /** The separator used to concatenate the result parts for this mapper. */
049: protected char separator;
050:
051: // Note: You might _not_ use TurbineTemplate.<xxx> in the C'tor and the init method.
052: // The service isn't configured yet and if you do, the Broker will try to reinit the
053: // Service which leads to an endless loop and a deadlock.
054:
055: /**
056: * Default C'tor. If you use this C'tor, you must use
057: * the bean setter to set the various properties needed for
058: * this mapper before first usage.
059: */
060: public BaseMapper() {
061: }
062:
063: /**
064: * Get the CacheSize value.
065: * @return the CacheSize value.
066: */
067: public int getCacheSize() {
068: return cacheSize;
069: }
070:
071: /**
072: * Set the CacheSize value.
073: * @param cacheSize The new CacheSize value.
074: */
075: public void setCacheSize(int cacheSize) {
076: this .cacheSize = cacheSize;
077: }
078:
079: /**
080: * Get the UseCache value.
081: * @return the UseCache value.
082: */
083: public boolean isUseCache() {
084: return useCache;
085: }
086:
087: /**
088: * Set the UseCache value.
089: * @param newUseCache The new UseCache value.
090: */
091: public void setUseCache(boolean useCache) {
092: this .useCache = useCache;
093: }
094:
095: /**
096: * Get the DefaultProperty value.
097: * @return the DefaultProperty value.
098: */
099: public String getDefaultProperty() {
100: return defaultProperty;
101: }
102:
103: /**
104: * Set the DefaultProperty value.
105: * @param defaultProperty The new DefaultProperty value.
106: */
107: public void setDefaultProperty(String defaultProperty) {
108: this .defaultProperty = defaultProperty;
109: }
110:
111: /**
112: * Get the Separator value.
113: * @return the Separator value.
114: */
115: public char getSeparator() {
116: return separator;
117: }
118:
119: /**
120: * Set the Separator value.
121: * @param separator The new Separator value.
122: */
123: public void setSeparator(char separator) {
124: this .separator = separator;
125: }
126:
127: /**
128: * Initializes the Mapper. Must be called before the mapper might be used.
129: */
130: public void init() {
131: if (useCache) {
132: templateCache = new HashMap(cacheSize);
133: }
134: }
135:
136: /**
137: * Returns the default name for the passed Template.
138: * If the passed template has no extension,
139: * the default extension is assumed.
140: * If the template is empty, the default template is
141: * returned.
142: *
143: * @param template The template name.
144: *
145: * @return the mapped default name for the template.
146: */
147:
148: public String getDefaultName(String template) {
149: // We might get a Name without an extension passed. If yes, then we use
150: // the Default extension
151:
152: TemplateEngineService tes = TurbineTemplate
153: .getTemplateEngineService(template);
154:
155: if (StringUtils.isEmpty(template) || (tes == null)) {
156: return TurbineTemplate.getDefaultTemplate();
157: }
158:
159: String defaultName = (String) tes
160: .getTemplateEngineServiceConfiguration().get(
161: defaultProperty);
162:
163: return StringUtils.isEmpty(defaultName) ? TurbineTemplate
164: .getDefaultTemplate() : defaultName;
165: }
166:
167: /**
168: * Return the first match name for the given template name.
169: *
170: * @param template The template name.
171: *
172: * @return The first matching class or template name.
173: */
174: public String getMappedName(String template) {
175: if (StringUtils.isEmpty(template)) {
176: return null;
177: }
178:
179: if (useCache && templateCache.containsKey(template)) {
180: return (String) templateCache.get(template);
181: }
182:
183: String res = doMapping(template);
184:
185: // Never cache "null" return values and empty Strings.
186: if (useCache && StringUtils.isNotEmpty(res)) {
187: templateCache.put(template, res);
188: }
189:
190: return res;
191: }
192:
193: /**
194: * The actual mapping implementation class. It
195: * is guaranteed that never an empty or null
196: * template name is passed to it. This might
197: * return null.
198: *
199: * @param template The template name.
200: * @return The mapped class or template name.
201: */
202: public abstract String doMapping(String template);
203: }
|