    001package org.apache.turbine.services.template.mapper;
    002
    003
    004/*
    005 * Licensed to the Apache Software Foundation (ASF) under one
    006 * or more contributor license agreements.  See the NOTICE file
    007 * distributed with this work for additional information
    008 * regarding copyright ownership.  The ASF licenses this file
    009 * to you under the Apache License, Version 2.0 (the
    010 * "License"); you may not use this file except in compliance
    011 * with the License.  You may obtain a copy of the License at
    012 *
    013 *   http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing,
    016 * software distributed under the License is distributed on an
    017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    018 * KIND, either express or implied.  See the License for the
    019 * specific language governing permissions and limitations
    020 * under the License.
    021 */
    022
    023
    024import org.apache.commons.lang.StringUtils;
    025
    026import org.apache.turbine.services.template.TemplateService;
    027import org.apache.turbine.services.template.TurbineTemplate;
    028
    029/**
    030 * This is a mapper like the BaseMapper but it returns its
    031 * results with the extension of the template names passed or (if no
    032 * extension is passed), the default extension.
    033 *
    034 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    035 * @version $Id: BaseTemplateMapper.java 1709648 2015-10-20 17:08:10Z tv $
    036 */
    037
    038public abstract class BaseTemplateMapper
    039    extends BaseMapper
    040{
    041    /** A prefix which is used to separate the various template types (screen, layouts, navigation) */
    042    protected String prefix = "";
    043
    044    /**
    045     * Default C'tor. If you use this C'tor, you must use
    046     * the bean setter to set the various properties needed for
    047     * this mapper before first usage.
    048     */
    049    public BaseTemplateMapper()
    050    {
    051        super();
    052    }
    053
    054    /**
    055     * Get the Prefix value.
    056     * @return the Prefix value.
    057     */
    058    public String getPrefix()
    059    {
    060        return prefix;
    061    }
    062
    063    /**
    064     * Set the Prefix value.
    065     * @param prefix The new Prefix value.
    066     */
    067    public void setPrefix(String prefix)
    068    {
    069        this.prefix = prefix;
    070    }
    071
    072    /**
    073     * Returns the default name for the passed Template.
    074     * If the template has no extension, the default extension
    075     * is added.
    076     * If the template is empty, the default template is
    077     * returned.
    078     *
    079     * @param template The template name.
    080     *
    081     * @return the mapped default name for the template.
    082     */
    083    public String getDefaultName(String template)
    084    {
    085        String res = super.getDefaultName(template);
    086
    087        // Does the Template Name component have an extension?
    088        String [] components
    089            = StringUtils.split(res, String.valueOf(separator));
    090
    091        if (components[components.length -1 ].indexOf(TemplateService.EXTENSION_SEPARATOR) < 0)
    092        {
    093            StringBuilder resBuf = new StringBuilder();
    094            resBuf.append(res);
    095            String [] templateComponents = StringUtils.split(template, String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR));
    096
    097            // Only the extension of the Template name component is interesting...
    098            int dotIndex = templateComponents[templateComponents.length -1].lastIndexOf(TemplateService.EXTENSION_SEPARATOR);
    099            if (dotIndex < 0)
    100            {
    101                if (StringUtils.isNotEmpty(TurbineTemplate.getDefaultExtension()))
    102                {
    103                    resBuf.append(TemplateService.EXTENSION_SEPARATOR);
    104                    resBuf.append(TurbineTemplate.getDefaultExtension());
    105                }
    106            }
    107            else
    108            {
    109                resBuf.append(templateComponents[templateComponents.length -1].substring(dotIndex));
    110            }
    111            res = resBuf.toString();
    112        }
    113        return res;
    114    }
    115}