001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.freemarker.FreeMarkerEngineUtil;
018 import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
019 import com.liferay.portal.model.Theme;
020 import com.liferay.portal.util.PortalUtil;
021
022 import java.net.URL;
023
024 import javax.servlet.ServletContext;
025
026
029 public class ThemeHelper {
030
031 public static final String TEMPLATE_EXTENSION_FTL = "ftl";
032
033 public static final String TEMPLATE_EXTENSION_VM = "vm";
034
035 public static String getResourcePath(
036 ServletContext servletContext, Theme theme, String path) {
037
038 StringBundler sb = new StringBundler(9);
039
040 String themeContextName = GetterUtil.getString(
041 theme.getServletContextName());
042
043 sb.append(themeContextName);
044
045 String servletContextName = StringPool.BLANK;
046
047 String contextPath = ContextPathUtil.getContextPath(servletContext);
048
049 if (!contextPath.equals(PortalUtil.getPathContext())) {
050 servletContextName = GetterUtil.getString(
051 servletContext.getServletContextName());
052 }
053
054 int start = 0;
055
056 if (path.startsWith(StringPool.SLASH)) {
057 start = 1;
058 }
059
060 int end = path.lastIndexOf(CharPool.PERIOD);
061
062 String extension = theme.getTemplateExtension();
063
064 if (extension.equals(TEMPLATE_EXTENSION_FTL)) {
065 sb.append(theme.getFreeMarkerTemplateLoader());
066 sb.append(theme.getTemplatesPath());
067
068 if (Validator.isNotNull(servletContextName) &&
069 !path.startsWith(StringPool.SLASH.concat(servletContextName))) {
070
071 sb.append(StringPool.SLASH);
072 sb.append(servletContextName);
073 }
074
075 sb.append(StringPool.SLASH);
076 sb.append(path.substring(start, end));
077 sb.append(StringPool.PERIOD);
078 sb.append(TEMPLATE_EXTENSION_FTL);
079
080 return sb.toString();
081 }
082 else if (extension.equals(TEMPLATE_EXTENSION_VM)) {
083 sb.append(theme.getVelocityResourceListener());
084 sb.append(theme.getTemplatesPath());
085
086 if (Validator.isNotNull(servletContextName) &&
087 !path.startsWith(StringPool.SLASH.concat(servletContextName))) {
088
089 sb.append(StringPool.SLASH);
090 sb.append(servletContextName);
091 }
092
093 sb.append(StringPool.SLASH);
094 sb.append(path.substring(start, end));
095 sb.append(StringPool.PERIOD);
096 sb.append(TEMPLATE_EXTENSION_VM);
097
098 return sb.toString();
099 }
100 else {
101 return path;
102 }
103 }
104
105 public static boolean resourceExists(
106 ServletContext servletContext, Theme theme, String path)
107 throws Exception {
108
109 if (Validator.isNull(path)) {
110 return false;
111 }
112
113 String resourcePath = getResourcePath(servletContext, theme, path);
114
115 String extension = theme.getTemplateExtension();
116
117 if (extension.equals(TEMPLATE_EXTENSION_FTL)) {
118 return FreeMarkerEngineUtil.resourceExists(resourcePath);
119 }
120 else if (extension.equals(TEMPLATE_EXTENSION_VM)) {
121 return VelocityEngineUtil.resourceExists(resourcePath);
122 }
123 else {
124 URL url = null;
125
126 if (theme.isWARFile()) {
127 ServletContext themeServletContext = servletContext.getContext(
128 theme.getContextPath());
129
130 url = themeServletContext.getResource(resourcePath);
131 }
132 else {
133 url = servletContext.getResource(resourcePath);
134 }
135
136 if (url == null) {
137 return false;
138 }
139 else {
140 return true;
141 }
142 }
143 }
144
145 }