001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }