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.spring.osgi.OSGiBeanProperties;
020 import com.liferay.portal.kernel.template.TemplateConstants;
021 import com.liferay.portal.theme.ThemeLoader;
022 import com.liferay.portal.theme.ThemeLoaderFactory;
023
024 import java.io.File;
025 import java.io.IOException;
026
027 import java.net.URL;
028
029
032 @OSGiBeanProperties(
033 property = {
034 "lang.type=" + TemplateConstants.LANG_TYPE_FTL,
035 "lang.type=" + TemplateConstants.LANG_TYPE_VM
036 },
037 service = TemplateResourceParser.class
038 )
039 public class ThemeResourceParser extends URLResourceParser {
040
041 @Override
042 public URL getURL(String templateId) throws IOException {
043 int pos = templateId.indexOf(TemplateConstants.THEME_LOADER_SEPARATOR);
044
045 if (pos == -1) {
046 return null;
047 }
048
049 String servletContextName = templateId.substring(0, pos);
050
051 ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(
052 servletContextName);
053
054 if (themeLoader == null) {
055 _log.error(
056 templateId + " is not valid because " + servletContextName +
057 " does not map to a theme loader");
058
059 return null;
060 }
061
062 String templateName = templateId.substring(
063 pos + TemplateConstants.THEME_LOADER_SEPARATOR.length());
064
065 String themesPath = themeLoader.getThemesPath();
066
067 if (templateName.startsWith(themesPath)) {
068 templateId = templateName.substring(themesPath.length());
069 }
070
071 if (_log.isDebugEnabled()) {
072 _log.debug(
073 templateId + " is associated with the theme loader " +
074 servletContextName + " " + themeLoader);
075 }
076
077 File fileStorage = themeLoader.getFileStorage();
078
079 return new File(fileStorage, templateId).toURI().toURL();
080 }
081
082 private static final Log _log = LogFactoryUtil.getLog(
083 ThemeResourceParser.class);
084
085 }