1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.freemarker;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
18  
19  import java.io.IOException;
20  import java.io.InputStreamReader;
21  import java.io.Reader;
22  
23  import java.net.URL;
24  
25  /**
26   * <a href="URLTemplateLoader.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Mika Koivisto
29   */
30  public abstract class URLTemplateLoader extends FreeMarkerTemplateLoader {
31  
32      public void closeTemplateSource(Object templateSource) {
33          if (templateSource instanceof URLTemplateSource) {
34              URLTemplateSource urlTemplateSource =
35                  (URLTemplateSource)templateSource;
36  
37              try {
38                  urlTemplateSource.closeStream();
39              }
40              catch (IOException ioe) {
41              }
42          }
43      }
44  
45      public Object findTemplateSource(String name) throws IOException {
46          URL url = getURL(name);
47  
48          if (url != null) {
49              return new URLTemplateSource(url);
50          }
51  
52          return null;
53      }
54  
55      public long getLastModified(Object templateSource) {
56          if (templateSource instanceof URLTemplateSource) {
57              URLTemplateSource urlTemplateSource =
58                  (URLTemplateSource)templateSource;
59  
60              return urlTemplateSource.getLastModified();
61          }
62  
63          return super.getLastModified(templateSource);
64      }
65  
66      public Reader getReader(Object templateSource, String encoding)
67          throws IOException {
68  
69          if (templateSource instanceof URLTemplateSource) {
70              URLTemplateSource urlTemplateSource =
71                  (URLTemplateSource)templateSource;
72  
73              return new UnsyncBufferedReader(
74                  new InputStreamReader(
75                      urlTemplateSource.getInputStream(), encoding));
76          }
77  
78          return null;
79      }
80  
81      public abstract URL getURL(String name) throws IOException;
82  
83  }