001
014
015 package com.liferay.portal.template;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.template.TemplateConstants;
020 import com.liferay.portal.theme.ThemeLoader;
021 import com.liferay.portal.theme.ThemeLoaderFactory;
022
023 import java.io.File;
024 import java.io.IOException;
025
026 import java.net.URL;
027
028
031 public class ThemeResourceParser extends URLResourceParser {
032
033 @Override
034 public URL getURL(String templateId) throws IOException {
035 int pos = templateId.indexOf(TemplateConstants.THEME_LOADER_SEPARATOR);
036
037 if (pos == -1) {
038 return null;
039 }
040
041 String servletContextName = templateId.substring(0, pos);
042
043 ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(
044 servletContextName);
045
046 if (themeLoader == null) {
047 _log.error(
048 templateId + " is not valid because " + servletContextName +
049 " does not map to a theme loader");
050
051 return null;
052 }
053
054 String templateName = templateId.substring(
055 pos + TemplateConstants.THEME_LOADER_SEPARATOR.length());
056
057 String themesPath = themeLoader.getThemesPath();
058
059 if (templateName.startsWith(themesPath)) {
060 templateId = templateName.substring(themesPath.length());
061 }
062
063 if (_log.isDebugEnabled()) {
064 _log.debug(
065 templateId + " is associated with the theme loader " +
066 servletContextName + " " + themeLoader);
067 }
068
069 File fileStorage = themeLoader.getFileStorage();
070
071 return new File(fileStorage, templateId).toURI().toURL();
072 }
073
074 private static Log _log = LogFactoryUtil.getLog(ThemeResourceParser.class);
075
076 }