1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.velocity;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.theme.ThemeLoader;
28  import com.liferay.portal.theme.ThemeLoaderFactory;
29  
30  import java.io.FileInputStream;
31  import java.io.FileNotFoundException;
32  import java.io.InputStream;
33  
34  import org.apache.velocity.exception.ResourceNotFoundException;
35  
36  /**
37   * <a href="ThemeLoaderVelocityResourceListener.java.html"><b><i>View Source</i>
38   * </b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class ThemeLoaderVelocityResourceListener
44      extends VelocityResourceListener {
45  
46      public InputStream getResourceStream(String source)
47          throws ResourceNotFoundException {
48  
49          InputStream is = null;
50  
51          try {
52              int pos = source.indexOf(THEME_LOADER_SEPARATOR);
53  
54              if (pos != -1) {
55                  String ctxName = source.substring(0, pos);
56  
57                  ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(
58                      ctxName);
59  
60                  if (themeLoader != null) {
61                      String name =
62                          source.substring(pos + THEME_LOADER_SEPARATOR.length());
63  
64                      String themesPath = themeLoader.getThemesPath();
65  
66                      if (name.startsWith(themesPath)) {
67                          name = name.substring(
68                              themesPath.length(), name.length());
69                      }
70  
71                      if (_log.isDebugEnabled()) {
72                          _log.debug(
73                              name + " is associated with the theme loader " +
74                                  ctxName + " " + themeLoader);
75                      }
76  
77                      is = new FileInputStream(
78                          themeLoader.getFileStorage().getPath() + name);
79                  }
80                  else {
81                      _log.error(
82                          source + " is not valid because " + ctxName +
83                              " does not map to a theme loader");
84                  }
85              }
86          }
87          catch (FileNotFoundException fnfe) {
88              throw new ResourceNotFoundException(source);
89          }
90  
91          return is;
92      }
93  
94      private static Log _log =
95          LogFactoryUtil.getLog(ThemeLoaderVelocityResourceListener.class);
96  
97  }