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.lang3.StringUtils;
025import org.apache.turbine.services.TurbineServices;
026import org.apache.turbine.services.template.TemplateService;
027
028/**
029 * This is a mapper like the BaseMapper but it returns its
030 * results with the extension of the template names passed or (if no
031 * extension is passed), the default extension.
032 *
033 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
034 * @version $Id$
035 */
036
037public abstract class BaseTemplateMapper
038    extends BaseMapper
039{
040    /** A prefix which is used to separate the various template types (screen, layouts, navigation) */
041    protected String prefix = "";
042
043    /**
044     * Default C'tor. If you use this C'tor, you must use
045     * the bean setter to set the various properties needed for
046     * this mapper before first usage.
047     */
048    public BaseTemplateMapper()
049    {
050        super();
051    }
052
053    /**
054     * Get the Prefix value.
055     * @return the Prefix value.
056     */
057    public String getPrefix()
058    {
059        return prefix;
060    }
061
062    /**
063     * Set the Prefix value.
064     * @param prefix The new Prefix value.
065     */
066    public void setPrefix(String prefix)
067    {
068        this.prefix = prefix;
069    }
070
071    /**
072     * Returns the default name for the passed Template.
073     * If the template has no extension, the default extension
074     * is added.
075     * If the template is empty, the default template is
076     * returned.
077     *
078     * @param template The template name.
079     *
080     * @return the mapped default name for the template.
081     */
082    @Override
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                TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
102
103                if (StringUtils.isNotEmpty(templateService.getDefaultExtension()))
104                {
105                    resBuf.append(TemplateService.EXTENSION_SEPARATOR);
106                    resBuf.append(templateService.getDefaultExtension());
107                }
108            }
109            else
110            {
111                resBuf.append(templateComponents[templateComponents.length -1].substring(dotIndex));
112            }
113            res = resBuf.toString();
114        }
115        return res;
116    }
117}