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