001
014
015 package com.liferay.portal.velocity;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.theme.ThemeLoader;
020 import com.liferay.portal.theme.ThemeLoaderFactory;
021
022 import java.io.FileInputStream;
023 import java.io.FileNotFoundException;
024 import java.io.InputStream;
025
026 import org.apache.velocity.exception.ResourceNotFoundException;
027
028
031 public class ThemeLoaderVelocityResourceListener
032 extends VelocityResourceListener {
033
034 public InputStream getResourceStream(String source)
035 throws ResourceNotFoundException {
036
037 InputStream is = null;
038
039 try {
040 int pos = source.indexOf(THEME_LOADER_SEPARATOR);
041
042 if (pos != -1) {
043 String ctxName = source.substring(0, pos);
044
045 ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(
046 ctxName);
047
048 if (themeLoader != null) {
049 String name =
050 source.substring(pos + THEME_LOADER_SEPARATOR.length());
051
052 String themesPath = themeLoader.getThemesPath();
053
054 if (name.startsWith(themesPath)) {
055 name = name.substring(
056 themesPath.length(), name.length());
057 }
058
059 if (_log.isDebugEnabled()) {
060 _log.debug(
061 name + " is associated with the theme loader " +
062 ctxName + " " + themeLoader);
063 }
064
065 is = new FileInputStream(
066 themeLoader.getFileStorage().getPath() + name);
067 }
068 else {
069 _log.error(
070 source + " is not valid because " + ctxName +
071 " does not map to a theme loader");
072 }
073 }
074 }
075 catch (FileNotFoundException fnfe) {
076 throw new ResourceNotFoundException(source);
077 }
078
079 return is;
080 }
081
082 private static Log _log = LogFactoryUtil.getLog(
083 ThemeLoaderVelocityResourceListener.class);
084
085 }