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