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