001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.model.PortletConstants;
018 import com.liferay.portal.kernel.model.Theme;
019 import com.liferay.portal.kernel.template.TemplateConstants;
020 import com.liferay.portal.kernel.template.TemplateResourceLoaderUtil;
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_JSP = "jsp";
034
035 public static final String TEMPLATE_EXTENSION_VM = "vm";
036
037 public static String getResourcePath(
038 ServletContext servletContext, Theme theme, String portletId,
039 String path) {
040
041 StringBundler sb = new StringBundler(11);
042
043 String themeContextName = GetterUtil.getString(
044 theme.getServletContextName());
045
046 sb.append(themeContextName);
047
048 String servletContextName = StringPool.BLANK;
049
050 String contextPath = servletContext.getContextPath();
051
052 if (!Validator.equals(
053 PortalUtil.getPathContext(contextPath),
054 PortalUtil.getPathContext())) {
055
056 servletContextName = GetterUtil.getString(
057 servletContext.getServletContextName());
058 }
059
060 int start = 0;
061
062 if (path.startsWith(StringPool.SLASH)) {
063 start = 1;
064 }
065
066 int end = path.lastIndexOf(CharPool.PERIOD);
067
068 String extension = theme.getTemplateExtension();
069
070 if (extension.equals(TEMPLATE_EXTENSION_FTL)) {
071 sb.append(theme.getFreeMarkerTemplateLoader());
072 sb.append(theme.getTemplatesPath());
073
074 if (Validator.isNotNull(servletContextName) &&
075 !path.startsWith(StringPool.SLASH.concat(servletContextName))) {
076
077 sb.append(StringPool.SLASH);
078 sb.append(servletContextName);
079 }
080
081 sb.append(StringPool.SLASH);
082 sb.append(path.substring(start, end));
083 sb.append(StringPool.PERIOD);
084
085 if (Validator.isNotNull(portletId)) {
086 sb.append(portletId);
087 sb.append(StringPool.PERIOD);
088 }
089
090 sb.append(TEMPLATE_EXTENSION_FTL);
091
092 return sb.toString();
093 }
094 else if (extension.equals(TEMPLATE_EXTENSION_VM)) {
095 sb.append(theme.getVelocityResourceListener());
096 sb.append(theme.getTemplatesPath());
097
098 if (Validator.isNotNull(servletContextName) &&
099 !path.startsWith(StringPool.SLASH.concat(servletContextName))) {
100
101 sb.append(StringPool.SLASH);
102 sb.append(servletContextName);
103 }
104
105 sb.append(StringPool.SLASH);
106 sb.append(path.substring(start, end));
107 sb.append(StringPool.PERIOD);
108
109 if (Validator.isNotNull(portletId)) {
110 sb.append(portletId);
111 sb.append(StringPool.PERIOD);
112 }
113
114 sb.append(TEMPLATE_EXTENSION_VM);
115
116 return sb.toString();
117 }
118 else {
119 return path;
120 }
121 }
122
123 public static boolean resourceExists(
124 ServletContext servletContext, Theme theme, String portletId,
125 String path)
126 throws Exception {
127
128 Boolean exists = null;
129
130 if (Validator.isNotNull(portletId)) {
131 exists = _resourceExists(servletContext, theme, portletId, path);
132
133 if (!exists && PortletConstants.hasInstanceId(portletId)) {
134 String rootPortletId = PortletConstants.getRootPortletId(
135 portletId);
136
137 exists = _resourceExists(
138 servletContext, theme, rootPortletId, path);
139 }
140
141 if (!exists) {
142 exists = _resourceExists(servletContext, theme, null, path);
143 }
144 }
145
146 if (exists == null) {
147 exists = _resourceExists(servletContext, theme, portletId, path);
148 }
149
150 return exists;
151 }
152
153 private static boolean _resourceExists(
154 ServletContext servletContext, Theme theme, String portletId,
155 String path)
156 throws Exception {
157
158 if (Validator.isNull(path)) {
159 return false;
160 }
161
162 String resourcePath = getResourcePath(
163 servletContext, theme, portletId, path);
164
165 String extension = theme.getTemplateExtension();
166
167 if (extension.equals(TEMPLATE_EXTENSION_FTL)) {
168 return TemplateResourceLoaderUtil.hasTemplateResource(
169 TemplateConstants.LANG_TYPE_FTL, resourcePath);
170 }
171 else if (extension.equals(TEMPLATE_EXTENSION_VM)) {
172 return TemplateResourceLoaderUtil.hasTemplateResource(
173 TemplateConstants.LANG_TYPE_VM, resourcePath);
174 }
175 else {
176 URL url = null;
177
178 if (theme.isWARFile()) {
179 ServletContext themeServletContext = servletContext.getContext(
180 theme.getContextPath());
181
182 url = themeServletContext.getResource(resourcePath);
183 }
184 else {
185 url = servletContext.getResource(resourcePath);
186 }
187
188 if (url == null) {
189 return false;
190 }
191 else {
192 return true;
193 }
194 }
195 }
196
197 }