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 org.apache.commons.lang.StringUtils;
020:
021: import org.apache.turbine.services.template.TemplateService;
022: import org.apache.turbine.services.template.TurbineTemplate;
023:
024: /**
025: * This is a mapper like the BaseMapper but it returns its
026: * results with the extension of the template names passed or (if no
027: * extension is passed), the default extension.
028: *
029: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
030: * @version $Id: BaseTemplateMapper.java 264148 2005-08-29 14:21:04Z henning $
031: */
032:
033: public abstract class BaseTemplateMapper extends BaseMapper {
034: /** A prefix which is used to separate the various template types (screen, layouts, navigation) */
035: protected String prefix = "";
036:
037: /**
038: * Default C'tor. If you use this C'tor, you must use
039: * the bean setter to set the various properties needed for
040: * this mapper before first usage.
041: */
042: public BaseTemplateMapper() {
043: super ();
044: }
045:
046: /**
047: * Get the Prefix value.
048: * @return the Prefix value.
049: */
050: public String getPrefix() {
051: return prefix;
052: }
053:
054: /**
055: * Set the Prefix value.
056: * @param prefix The new Prefix value.
057: */
058: public void setPrefix(String prefix) {
059: this .prefix = prefix;
060: }
061:
062: /**
063: * Returns the default name for the passed Template.
064: * If the template has no extension, the default extension
065: * is added.
066: * If the template is empty, the default template is
067: * returned.
068: *
069: * @param template The template name.
070: *
071: * @return the mapped default name for the template.
072: */
073: public String getDefaultName(String template) {
074: String res = super .getDefaultName(template);
075:
076: // Does the Template Name component have an extension?
077: String[] components = StringUtils.split(res, String
078: .valueOf(separator));
079:
080: if (components[components.length - 1]
081: .indexOf(TemplateService.EXTENSION_SEPARATOR) < 0) {
082: StringBuffer resBuf = new StringBuffer();
083: resBuf.append(res);
084: String[] templateComponents = StringUtils
085: .split(
086: template,
087: String
088: .valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR));
089:
090: // Only the extension of the Template name component is interesting...
091: int dotIndex = templateComponents[templateComponents.length - 1]
092: .lastIndexOf(TemplateService.EXTENSION_SEPARATOR);
093: if (dotIndex < 0) {
094: if (StringUtils.isNotEmpty(TurbineTemplate
095: .getDefaultExtension())) {
096: resBuf.append(TemplateService.EXTENSION_SEPARATOR);
097: resBuf
098: .append(TurbineTemplate
099: .getDefaultExtension());
100: }
101: } else {
102: resBuf
103: .append(templateComponents[templateComponents.length - 1]
104: .substring(dotIndex));
105: }
106: res = resBuf.toString();
107: }
108: return res;
109: }
110: }
|