1/*2 * $Id: WebappClassTemplateLoader.java 1306435 2012-03-28 15:39:11Z nlebas $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122package org.apache.tiles.request.freemarker.servlet;
2324import java.io.IOException;
25import java.io.Reader;
2627import javax.servlet.ServletContext;
2829import freemarker.cache.ClassTemplateLoader;
30import freemarker.cache.TemplateLoader;
31import freemarker.cache.WebappTemplateLoader;
3233/**34 * Delegates loading templates using a {@link WebappTemplateLoader} and, if not35 * found, a {@link ClassTemplateLoader}. The resources are loaded from the36 * webapp root and from the classpath root.37 *38 * @version $Rev: 1306435 $ $Date: 2012-03-29 02:39:11 +1100 (Thu, 29 Mar 2012) $39 */40publicclassWebappClassTemplateLoaderimplements TemplateLoader {
4142/**43 * The webapp template loader.44 */45private WebappTemplateLoader webappTemplateLoader;
4647/**48 * The webapp template loader.49 */50private ClassTemplateLoader classTemplateLoader;
5152/**53 * Constructor.54 *55 * @param servletContext The servlet context.56 */57publicWebappClassTemplateLoader(ServletContext servletContext) {
58 webappTemplateLoader = new WebappTemplateLoader(servletContext);
59 classTemplateLoader = new ClassTemplateLoader(getClass(), "/");
60 }
6162/** {@inheritDoc} */63public Object findTemplateSource(String name) throws IOException {
64 Object retValue = webappTemplateLoader.findTemplateSource(name);
65if (retValue == null) {
66 retValue = classTemplateLoader.findTemplateSource(name);
67 }
68return retValue;
69 }
7071/** {@inheritDoc} */72publicvoid closeTemplateSource(Object templateSource) throws IOException {
73 webappTemplateLoader.closeTemplateSource(templateSource);
74 }
7576/** {@inheritDoc} */77publiclong getLastModified(Object templateSource) {
78return webappTemplateLoader.getLastModified(templateSource);
79 }
8081/** {@inheritDoc} */82public Reader getReader(Object templateSource, String encoding)
83throws IOException {
84return webappTemplateLoader.getReader(templateSource, encoding);
85 }
86 }