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