001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.freemarker;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018    
019    import java.io.IOException;
020    import java.io.InputStreamReader;
021    import java.io.Reader;
022    
023    import java.net.URL;
024    
025    /**
026     * @author Mika Koivisto
027     */
028    public abstract class URLTemplateLoader extends FreeMarkerTemplateLoader {
029    
030            public void closeTemplateSource(Object templateSource) {
031                    if (templateSource instanceof URLTemplateSource) {
032                            URLTemplateSource urlTemplateSource =
033                                    (URLTemplateSource)templateSource;
034    
035                            try {
036                                    urlTemplateSource.closeStream();
037                            }
038                            catch (IOException ioe) {
039                            }
040                    }
041            }
042    
043            public Object findTemplateSource(String name) throws IOException {
044                    URL url = getURL(name);
045    
046                    if (url != null) {
047                            return new URLTemplateSource(url);
048                    }
049    
050                    return null;
051            }
052    
053            public long getLastModified(Object templateSource) {
054                    if (templateSource instanceof URLTemplateSource) {
055                            URLTemplateSource urlTemplateSource =
056                                    (URLTemplateSource)templateSource;
057    
058                            return urlTemplateSource.getLastModified();
059                    }
060    
061                    return super.getLastModified(templateSource);
062            }
063    
064            public Reader getReader(Object templateSource, String encoding)
065                    throws IOException {
066    
067                    if (templateSource instanceof URLTemplateSource) {
068                            URLTemplateSource urlTemplateSource =
069                                    (URLTemplateSource)templateSource;
070    
071                            return new UnsyncBufferedReader(
072                                    new InputStreamReader(
073                                            urlTemplateSource.getInputStream(), encoding));
074                    }
075    
076                    return null;
077            }
078    
079            public abstract URL getURL(String name) throws IOException;
080    
081    }