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