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.velocity;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.theme.ThemeLoader;
20  import com.liferay.portal.theme.ThemeLoaderFactory;
21  
22  import java.io.FileInputStream;
23  import java.io.FileNotFoundException;
24  import java.io.InputStream;
25  
26  import org.apache.velocity.exception.ResourceNotFoundException;
27  
28  /**
29   * <a href="ThemeLoaderVelocityResourceListener.java.html"><b><i>View Source</i>
30   * </b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class ThemeLoaderVelocityResourceListener
35      extends VelocityResourceListener {
36  
37      public InputStream getResourceStream(String source)
38          throws ResourceNotFoundException {
39  
40          InputStream is = null;
41  
42          try {
43              int pos = source.indexOf(THEME_LOADER_SEPARATOR);
44  
45              if (pos != -1) {
46                  String ctxName = source.substring(0, pos);
47  
48                  ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(
49                      ctxName);
50  
51                  if (themeLoader != null) {
52                      String name =
53                          source.substring(pos + THEME_LOADER_SEPARATOR.length());
54  
55                      String themesPath = themeLoader.getThemesPath();
56  
57                      if (name.startsWith(themesPath)) {
58                          name = name.substring(
59                              themesPath.length(), name.length());
60                      }
61  
62                      if (_log.isDebugEnabled()) {
63                          _log.debug(
64                              name + " is associated with the theme loader " +
65                                  ctxName + " " + themeLoader);
66                      }
67  
68                      is = new FileInputStream(
69                          themeLoader.getFileStorage().getPath() + name);
70                  }
71                  else {
72                      _log.error(
73                          source + " is not valid because " + ctxName +
74                              " does not map to a theme loader");
75                  }
76              }
77          }
78          catch (FileNotFoundException fnfe) {
79              throw new ResourceNotFoundException(source);
80          }
81  
82          return is;
83      }
84  
85      private static Log _log = LogFactoryUtil.getLog(
86          ThemeLoaderVelocityResourceListener.class);
87  
88  }