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