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 java.util.List;
023: import java.util.ArrayList;
024: import java.util.Arrays;
025:
026: import org.apache.commons.lang.StringUtils;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import org.apache.turbine.services.template.TemplateEngineService;
032: import org.apache.turbine.services.template.TemplateService;
033: import org.apache.turbine.services.template.TurbineTemplate;
034:
035: /**
036: * This is a pretty simple mapper which returns template pathes for
037: * a supplied template name. If the path does not exist, it looks for
038: * a templated called "Default" in the same package.
039: * This path can be used by the TemplateEngine to access
040: * a certain resource to actually render the template.
041: *
042: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
043: * @version $Id: ScreenDefaultTemplateMapper.java 534527 2007-05-02 16:10:59Z tv $
044: */
045:
046: public class ScreenDefaultTemplateMapper extends BaseTemplateMapper
047: implements Mapper {
048: /** Logging */
049: private static Log log = LogFactory
050: .getLog(ScreenDefaultTemplateMapper.class);
051:
052: /**
053: * Default C'tor. If you use this C'tor, you must use
054: * the bean setter to set the various properties needed for
055: * this mapper before first usage.
056: */
057: public ScreenDefaultTemplateMapper() {
058: }
059:
060: /**
061: * Look for a given Template, then try the
062: * default.
063: *
064: * @param template The template name.
065: * @return The parsed module name.
066: */
067: public String doMapping(String template) {
068: log.debug("doMapping(" + template + ")");
069: // Copy our elements into an array
070: List components = new ArrayList(
071: Arrays
072: .asList(StringUtils
073: .split(
074: template,
075: String
076: .valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
077: int componentSize = components.size() - 1;
078:
079: // This method never gets an empty string passed.
080: // So this is never < 0
081: String templateName = (String) components.get(componentSize);
082: components.remove(componentSize--);
083:
084: log.debug("templateName is " + templateName);
085:
086: // Last element decides, which template Service to use...
087: TemplateEngineService tes = TurbineTemplate
088: .getTemplateEngineService(templateName);
089:
090: if (tes == null) {
091: return null;
092: }
093:
094: String defaultName = "Default.vm";
095:
096: // This is an optimization. If the name we're looking for is
097: // already the default name for the template, don't do a "first run"
098: // which looks for an exact match.
099: boolean firstRun = !templateName.equals(defaultName);
100:
101: for (;;) {
102: String templatePackage = StringUtils.join(components
103: .iterator(), String.valueOf(separator));
104:
105: log.debug("templatePackage is now: " + templatePackage);
106:
107: StringBuffer testName = new StringBuffer();
108:
109: if (!components.isEmpty()) {
110: testName.append(templatePackage);
111: testName.append(separator);
112: }
113:
114: testName.append((firstRun) ? templateName : defaultName);
115:
116: // But the Templating service must look for the name with prefix
117: StringBuffer templatePath = new StringBuffer();
118: if (StringUtils.isNotEmpty(prefix)) {
119: templatePath.append(prefix);
120: templatePath.append(separator);
121: }
122: templatePath.append(testName);
123:
124: log.debug("Looking for " + templatePath);
125:
126: if (tes.templateExists(templatePath.toString())) {
127: log.debug("Found it, returning " + testName);
128: return testName.toString();
129: }
130:
131: if (firstRun) {
132: firstRun = false;
133: } else {
134: // We run this loop only two times. The
135: // first time with the 'real' name and the
136: // second time with "Default". The second time
137: // we will end up here and break the for(;;) loop.
138: break;
139: }
140: }
141:
142: log.debug("Returning default");
143: return getDefaultName(template);
144: }
145: }
|