| ThemeLoaderVelocityResourceListener.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * The contents of this file are subject to the terms of the Liferay Enterprise
5 * Subscription License ("License"). You may not use this file except in
6 * compliance with the License. You can obtain a copy of the License by
7 * contacting Liferay, Inc. See the License for the specific language governing
8 * permissions and limitations under the License, including but not limited to
9 * distribution rights of the Software.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 package com.liferay.portal.velocity;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.theme.ThemeLoader;
25 import com.liferay.portal.theme.ThemeLoaderFactory;
26
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.InputStream;
30
31 import org.apache.velocity.exception.ResourceNotFoundException;
32
33 /**
34 * <a href="ThemeLoaderVelocityResourceListener.java.html"><b><i>View Source</i>
35 * </b></a>
36 *
37 * @author Brian Wing Shun Chan
38 *
39 */
40 public class ThemeLoaderVelocityResourceListener
41 extends VelocityResourceListener {
42
43 public InputStream getResourceStream(String source)
44 throws ResourceNotFoundException {
45
46 InputStream is = null;
47
48 try {
49 int pos = source.indexOf(THEME_LOADER_SEPARATOR);
50
51 if (pos != -1) {
52 String ctxName = source.substring(0, pos);
53
54 ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(
55 ctxName);
56
57 if (themeLoader != null) {
58 String name =
59 source.substring(pos + THEME_LOADER_SEPARATOR.length());
60
61 String themesPath = themeLoader.getThemesPath();
62
63 if (name.startsWith(themesPath)) {
64 name = name.substring(
65 themesPath.length(), name.length());
66 }
67
68 if (_log.isDebugEnabled()) {
69 _log.debug(
70 name + " is associated with the theme loader " +
71 ctxName + " " + themeLoader);
72 }
73
74 is = new FileInputStream(
75 themeLoader.getFileStorage().getPath() + name);
76 }
77 else {
78 _log.error(
79 source + " is not valid because " + ctxName +
80 " does not map to a theme loader");
81 }
82 }
83 }
84 catch (FileNotFoundException fnfe) {
85 throw new ResourceNotFoundException(source);
86 }
87
88 return is;
89 }
90
91 private static Log _log =
92 LogFactoryUtil.getLog(ThemeLoaderVelocityResourceListener.class);
93
94 }