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 mapper is responsible for the lookup of templates for the Layout
037: * It tries to look in various packages for a match:
038: *
039: * 1. about,directions,Driving.vm <- exact match
040: * 2. about,directions,Default.vm <- package match, Default name
041: * 3. about,Default.vm <- stepping up in the hierarchy
042: * 4. Default.vm <- The name configured as default.layout.template
043: * in the corresponding Templating Engine
044:
045: *
046: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
047: * @version $Id: LayoutTemplateMapper.java 534527 2007-05-02 16:10:59Z tv $
048: */
049:
050: public class LayoutTemplateMapper extends BaseTemplateMapper implements
051: Mapper {
052: /** Logging */
053: private static Log log = LogFactory
054: .getLog(LayoutTemplateMapper.class);
055:
056: /**
057: * Default C'tor. If you use this C'tor, you must use
058: * the bean setter to set the various properties needed for
059: * this mapper before first usage.
060: */
061: public LayoutTemplateMapper() {
062: }
063:
064: /**
065: * Look for a given Template, then try the
066: * defaults until we hit the root.
067: *
068: * @param template The template name.
069: * @return The parsed module name.
070: */
071: public String doMapping(String template) {
072: log.debug("doMapping(" + template + ")");
073: // Copy our elements into an array
074: List components = new ArrayList(
075: Arrays
076: .asList(StringUtils
077: .split(
078: template,
079: String
080: .valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
081: int componentSize = components.size() - 1;
082:
083: // This method never gets an empty string passed.
084: // So this is never < 0
085: String templateName = (String) components.get(componentSize);
086: components.remove(componentSize--);
087:
088: log.debug("templateName is " + templateName);
089:
090: // Last element decides, which template Service to use...
091: TemplateEngineService tes = TurbineTemplate
092: .getTemplateEngineService(templateName);
093:
094: if (tes == null) {
095: return null;
096: }
097:
098: String defaultName = TurbineTemplate
099: .getDefaultLayoutTemplateName(templateName); // We're, after all, a Layout Template Mapper...
100:
101: // This is an optimization. If the name we're looking for is
102: // already the default name for the template, don't do a "first run"
103: // which looks for an exact match.
104: boolean firstRun = !templateName.equals(defaultName);
105:
106: for (;;) {
107: String templatePackage = StringUtils.join(components
108: .iterator(), String.valueOf(separator));
109:
110: log.debug("templatePackage is now: " + templatePackage);
111:
112: StringBuffer testName = new StringBuffer();
113:
114: if (!components.isEmpty()) {
115: testName.append(templatePackage);
116: testName.append(separator);
117: }
118:
119: testName.append((firstRun) ? templateName : defaultName);
120:
121: // But the Templating service must look for the name with prefix
122: StringBuffer templatePath = new StringBuffer();
123: if (StringUtils.isNotEmpty(prefix)) {
124: templatePath.append(prefix);
125: templatePath.append(separator);
126: }
127: templatePath.append(testName);
128:
129: log.debug("Looking for " + templatePath);
130:
131: if (tes.templateExists(templatePath.toString())) {
132: log.debug("Found it, returning " + testName);
133: return testName.toString();
134: }
135:
136: if (firstRun) {
137: firstRun = false;
138: } else {
139: // We're no longer on the first Run (so we
140: // already tested the "Default" template)
141: // and the package is empty (we've hit the
142: // root. So we now break the endless loop.
143: if (components.isEmpty()) {
144: break; // for(;;)
145: }
146: // We still have components. Remove the
147: // last one and go through the loop again.
148: components.remove(componentSize--);
149: }
150: }
151:
152: log.debug("Returning default");
153: return getDefaultName(template);
154: }
155: }
|