001
014
015 package com.liferay.portal.freemarker;
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.File;
023 import java.io.IOException;
024
025 import java.net.URL;
026
027
030 public class ThemeLoaderTemplateLoader extends URLTemplateLoader {
031
032 @Override
033 public URL getURL(String name) throws IOException {
034 int pos = name.indexOf(THEME_LOADER_SEPARATOR);
035
036 if (pos != -1) {
037 String ctxName = name.substring(0, pos);
038
039 ThemeLoader themeLoader =
040 ThemeLoaderFactory.getThemeLoader(ctxName);
041
042 if (themeLoader != null) {
043 String templateName =
044 name.substring(pos + THEME_LOADER_SEPARATOR.length());
045
046 String themesPath = themeLoader.getThemesPath();
047
048 if (templateName.startsWith(themesPath)) {
049 name =
050 templateName.substring(
051 themesPath.length(), templateName.length());
052 }
053
054 if (_log.isDebugEnabled()) {
055 _log.debug(
056 name + " is associated with the theme loader " +
057 ctxName + " " + themeLoader);
058 }
059
060 File fileStorage = themeLoader.getFileStorage();
061
062 return new File(fileStorage.getPath() + name).toURI().toURL();
063
064 }
065 else {
066 _log.error(
067 name + " is not valid because " + ctxName +
068 " does not map to a theme loader");
069 }
070 }
071
072 return null;
073 }
074
075 private static Log _log = LogFactoryUtil.getLog(
076 ThemeLoaderTemplateLoader.class);
077
078 }