KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > cache > URLTemplateLoader


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53
54 package freemarker.cache;
55
56 import java.io.IOException JavaDoc;
57 import java.io.InputStreamReader JavaDoc;
58 import java.io.Reader JavaDoc;
59 import java.net.URL JavaDoc;
60
61 /**
62  * This is an abstract template loader that can load templates whose
63  * location can be described by an URL. Subclasses only need to override
64  * the {@link #getURL(String)} method. Both {@link ClassTemplateLoader} and
65  * {@link WebappTemplateLoader} are (quite trivial) subclasses of this class.
66  * @version $Id: URLTemplateLoader.java,v 1.14 2003/01/29 08:01:17 szegedia Exp $
67  * @author Attila Szegedi
68  */

69 public abstract class URLTemplateLoader implements TemplateLoader
70 {
71     public Object JavaDoc findTemplateSource(String JavaDoc name)
72     throws
73         IOException JavaDoc
74     {
75         URL JavaDoc url = getURL(name);
76         return url == null ? null : new URLTemplateSource(url);
77     }
78     
79     /**
80      * Given a template name (plus potential locale decorations) retrieves
81      * an URL that points the template source.
82      * @param name the name of the sought template, including the locale
83      * decorations.
84      * @return an URL that points to the template source, or null if it can
85      * determine that the template source does not exist.
86      */

87     protected abstract URL JavaDoc getURL(String JavaDoc name);
88     
89     public long getLastModified(Object JavaDoc templateSource)
90     {
91         return ((URLTemplateSource) templateSource).lastModified();
92     }
93     
94     public Reader JavaDoc getReader(Object JavaDoc templateSource, String JavaDoc encoding)
95     throws
96         IOException JavaDoc
97     {
98         return new InputStreamReader JavaDoc(
99                 ((URLTemplateSource) templateSource).getInputStream(),
100                 encoding);
101     }
102     
103     public void closeTemplateSource(Object JavaDoc templateSource)
104     throws
105         IOException JavaDoc
106     {
107         ((URLTemplateSource) templateSource).close();
108     }
109
110     /**
111      * Can be used by subclasses to canonicalize URL path prefixes.
112      * @param prefix the path prefix to canonicalize
113      * @return the canonicalized prefix. All backslashes are replaced with
114      * forward slashes, and a trailing slash is appended if the original
115      * prefix wasn't empty and didn't already end with a slash.
116      */

117     protected static String JavaDoc canonicalizePrefix(String JavaDoc prefix)
118     {
119         // make it foolproof
120
prefix = prefix.replace('\\', '/');
121         // ensure there's a trailing slash
122
if (prefix.length() > 0 && !prefix.endsWith("/"))
123         {
124             prefix += "/";
125         }
126         return prefix;
127     }
128 }
129
Popular Tags