1
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
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 }