001: package org.apache.turbine.services.template.mapper;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.List;
020: import java.util.ArrayList;
021: import java.util.Arrays;
022:
023: import org.apache.commons.lang.StringUtils;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import org.apache.turbine.services.template.TemplateEngineService;
029: import org.apache.turbine.services.template.TemplateService;
030: import org.apache.turbine.services.template.TurbineTemplate;
031:
032: /**
033: * This mapper is responsible for the lookup of templates for the Layout
034: * It tries to look in various packages for a match:
035: *
036: * 1. about,directions,Driving.vm <- exact match
037: * 2. about,directions,Default.vm <- package match, Default name
038: * 3. about,Default.vm <- stepping up in the hierarchy
039: * 4. Default.vm <- The name configured as default.layout.template
040: * in the corresponding Templating Engine
041:
042: *
043: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
044: * @version $Id: LayoutTemplateMapper.java 264148 2005-08-29 14:21:04Z henning $
045: */
046:
047: public class LayoutTemplateMapper extends BaseTemplateMapper implements
048: Mapper {
049: /** Logging */
050: private static Log log = LogFactory
051: .getLog(LayoutTemplateMapper.class);
052:
053: /**
054: * Default C'tor. If you use this C'tor, you must use
055: * the bean setter to set the various properties needed for
056: * this mapper before first usage.
057: */
058: public LayoutTemplateMapper() {
059: }
060:
061: /**
062: * Look for a given Template, then try the
063: * defaults until we hit the root.
064: *
065: * @param template The template name.
066: * @return The parsed module name.
067: */
068: public String doMapping(String template) {
069: log.debug("doMapping(" + template + ")");
070: // Copy our elements into an array
071: List components = new ArrayList(
072: Arrays
073: .asList(StringUtils
074: .split(
075: template,
076: String
077: .valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
078: int componentSize = components.size() - 1;
079:
080: // This method never gets an empty string passed.
081: // So this is never < 0
082: String templateName = (String) components.get(componentSize);
083: components.remove(componentSize--);
084:
085: log.debug("templateName is " + templateName);
086:
087: // Last element decides, which template Service to use...
088: TemplateEngineService tes = TurbineTemplate
089: .getTemplateEngineService(templateName);
090:
091: if (tes == null) {
092: return null;
093: }
094:
095: String defaultName = TurbineTemplate
096: .getDefaultLayoutTemplateName(templateName); // We're, after all, a Layout Template Mapper...
097:
098: // This is an optimization. If the name we're looking for is
099: // already the default name for the template, don't do a "first run"
100: // which looks for an exact match.
101: boolean firstRun = !templateName.equals(defaultName);
102:
103: for (;;) {
104: String templatePackage = StringUtils.join(components
105: .iterator(), String.valueOf(separator));
106:
107: log.debug("templatePackage is now: " + templatePackage);
108:
109: StringBuffer testName = new StringBuffer();
110:
111: if (!components.isEmpty()) {
112: testName.append(templatePackage);
113: testName.append(separator);
114: }
115:
116: testName.append((firstRun) ? templateName : defaultName);
117:
118: // But the Templating service must look for the name with prefix
119: StringBuffer templatePath = new StringBuffer();
120: if (StringUtils.isNotEmpty(prefix)) {
121: templatePath.append(prefix);
122: templatePath.append(separator);
123: }
124: templatePath.append(testName);
125:
126: log.debug("Looking for " + templatePath);
127:
128: if (tes.templateExists(templatePath.toString())) {
129: log.debug("Found it, returning " + testName);
130: return testName.toString();
131: }
132:
133: if (firstRun) {
134: firstRun = false;
135: } else {
136: // We're no longer on the first Run (so we
137: // already tested the "Default" template)
138: // and the package is empty (we've hit the
139: // root. So we now break the endless loop.
140: if (components.isEmpty()) {
141: break; // for(;;)
142: }
143: // We still have components. Remove the
144: // last one and go through the loop again.
145: components.remove(componentSize--);
146: }
147: }
148:
149: log.debug("Returning default");
150: return getDefaultName(template);
151: }
152: }
|