001    /**
002     * Copyright (c) 2000-2013 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            @Override
031            public void closeTemplateSource(Object templateSource) {
032                    if (templateSource instanceof URLTemplateSource) {
033                            URLTemplateSource urlTemplateSource =
034                                    (URLTemplateSource)templateSource;
035    
036                            try {
037                                    urlTemplateSource.closeStream();
038                            }
039                            catch (IOException ioe) {
040                            }
041                    }
042            }
043    
044            @Override
045            public Object findTemplateSource(String name) throws IOException {
046                    URL url = getURL(name);
047    
048                    if (url != null) {
049                            return new URLTemplateSource(url);
050                    }
051    
052                    return null;
053            }
054    
055            @Override
056            public long getLastModified(Object templateSource) {
057                    if (templateSource instanceof URLTemplateSource) {
058                            URLTemplateSource urlTemplateSource =
059                                    (URLTemplateSource)templateSource;
060    
061                            return urlTemplateSource.getLastModified();
062                    }
063    
064                    return super.getLastModified(templateSource);
065            }
066    
067            @Override
068            public Reader getReader(Object templateSource, String encoding)
069                    throws IOException {
070    
071                    if (templateSource instanceof URLTemplateSource) {
072                            URLTemplateSource urlTemplateSource =
073                                    (URLTemplateSource)templateSource;
074    
075                            return new UnsyncBufferedReader(
076                                    new InputStreamReader(
077                                            urlTemplateSource.getInputStream(), encoding));
078                    }
079    
080                    return null;
081            }
082    
083            public abstract URL getURL(String name) throws IOException;
084    
085    }