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.ArrayList;
023: import java.util.Arrays;
024: import java.util.List;
025:
026: import org.apache.commons.lang.StringUtils;
027:
028: import org.apache.turbine.modules.Loader;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: import org.apache.turbine.services.template.TemplateService;
034:
035: /**
036: * This mapper tries to map Template names to class names. If no direct match
037: * is found, it tries matches "upwards" in the package hierarchy until either
038: * a match is found or the root is hit. Then it returns the name of the
039: * default class from the TemplateEngineService.
040: *
041: * 1. about.directions.Driving <- direct matching the template to the class name
042: * 2. about.directions.Default <- matching the package, class name is Default
043: * 3. about.Default <- stepping up in the package hierarchy, looking for Default
044: * 4. Default <- Class called "Default" without package
045: * 5. VelocityScreen <- The class configured by the Service (VelocityService) to
046: *
047: * Please note, that no actual packages are searched. This is the scope of the
048: * TemplateEngine Loader which is passed at construction time.
049: *
050: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
051: * @version $Id: ClassMapper.java 534527 2007-05-02 16:10:59Z tv $
052: */
053:
054: public class ClassMapper extends BaseMapper implements Mapper {
055: /** The loader for actually trying out the package names */
056: private Loader loader = null;
057:
058: /** Logging */
059: private static Log log = LogFactory.getLog(ClassMapper.class);
060:
061: /**
062: * Default C'tor. If you use this C'tor, you must use
063: * the bean setter to set the various properties needed for
064: * this mapper before first usage.
065: */
066: public ClassMapper() {
067: }
068:
069: /**
070: * Get the Loader value.
071: * @return the Loader value.
072: */
073: public Loader getLoader() {
074: return loader;
075: }
076:
077: /**
078: * Set the Loader value.
079: * @param loader The new Loader value.
080: */
081: public void setLoader(Loader loader) {
082: this .loader = loader;
083: log.debug("Loader is " + this .loader);
084: }
085:
086: /**
087: * Strip off a possible extension, replace all "," with "."
088: * Look through the given package path until a match is found.
089: *
090: * @param template The template name.
091: * @return A class name for the given template.
092: */
093: public String doMapping(String template) {
094: log.debug("doMapping(" + template + ")");
095:
096: // Copy our elements into an array
097: List components = new ArrayList(
098: Arrays
099: .asList(StringUtils
100: .split(
101: template,
102: String
103: .valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
104: int componentSize = components.size() - 1;
105:
106: // This method never gets an empty string passed.
107: // So this is never < 0
108: String className = (String) components.get(componentSize);
109: components.remove(componentSize--);
110:
111: log.debug("className is " + className);
112:
113: // Strip off a possible Extension
114: int dotIndex = className
115: .lastIndexOf(TemplateService.EXTENSION_SEPARATOR);
116: className = (dotIndex < 0) ? className : className.substring(0,
117: dotIndex);
118:
119: // This is an optimization. If the name we're looking for is
120: // already the default name for the template, don't do a "first run"
121: // which looks for an exact match.
122: boolean firstRun = !className
123: .equals(TemplateService.DEFAULT_NAME);
124:
125: for (;;) {
126: String pkg = StringUtils.join(components.iterator(), String
127: .valueOf(separator));
128: StringBuffer testName = new StringBuffer();
129:
130: log.debug("classPackage is now: " + pkg);
131:
132: if (!components.isEmpty()) {
133: testName.append(pkg);
134: testName.append(separator);
135: }
136:
137: testName.append((firstRun) ? className
138: : TemplateService.DEFAULT_NAME);
139:
140: log.debug("Looking for " + testName);
141: try {
142: loader.getAssembler(testName.toString());
143: log.debug("Found it, returning " + testName);
144: return testName.toString();
145: } catch (Exception e) {
146: // Not found. Go on.
147: }
148:
149: if (firstRun) {
150: firstRun = false;
151: } else {
152: if (components.isEmpty()) {
153: break; // for(;;)
154: }
155: components.remove(componentSize--);
156: }
157: }
158:
159: log.debug("Returning default");
160: return getDefaultName(template);
161: }
162: }
|