001    package 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    
024    import org.apache.commons.lang.StringUtils;
025    import org.apache.turbine.services.template.TemplateEngineService;
026    import org.apache.turbine.services.template.TemplateService;
027    import org.apache.turbine.services.template.TurbineTemplate;
028    
029    /**
030     * This is a pretty simple mapper which returns template pathes for
031     * a supplied template name. This path can be used by the TemplateEngine
032     * to access a certain resource to actually render the template.
033     *
034     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
035     * @version $Id: ScreenTemplateMapper.java 1078552 2011-03-06 19:58:46Z tv $
036     */
037    
038    public class ScreenTemplateMapper
039        extends BaseTemplateMapper
040        implements Mapper
041    {
042        /**
043         * Default C'tor. If you use this C'tor, you must use
044         * the bean setter to set the various properties needed for
045         * this mapper before first usage.
046         */
047        public ScreenTemplateMapper()
048        {
049            super();
050        }
051    
052        /**
053         * Check, whether the provided name exists. Returns null
054         * if the screen does not exist.
055         *
056         * @param template The template name.
057         * @return The matching screen name.
058         */
059        @Override
060        public String doMapping(String template)
061        {
062            String [] components = StringUtils.split(template, String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR));
063    
064            // Last element decides, which template Service to use...
065            TemplateEngineService tes =
066                TurbineTemplate.getTemplateEngineService(components[components.length - 1]);
067    
068            String templatePackage = StringUtils.join(components, String.valueOf(separator));
069    
070            // But the Templating service must look for the name with prefix
071            StringBuffer testPath = new StringBuffer();
072            if (StringUtils.isNotEmpty(prefix))
073            {
074                testPath.append(prefix);
075                testPath.append(separator);
076            }
077            testPath.append(templatePackage);
078    
079            return (tes != null && tes.templateExists(testPath.toString()))
080                ? templatePackage
081                : null;
082        }
083    }
084    
085    
086    
087