001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.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    /**
028     * @author Raymond Aug??
029     */
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 (!contextPath.equals(PortalUtil.getPathContext())) {
054                            servletContextName = GetterUtil.getString(
055                                    servletContext.getServletContextName());
056                    }
057    
058                    int start = 0;
059    
060                    if (path.startsWith(StringPool.SLASH)) {
061                            start = 1;
062                    }
063    
064                    int end = path.lastIndexOf(CharPool.PERIOD);
065    
066                    String extension = theme.getTemplateExtension();
067    
068                    if (extension.equals(TEMPLATE_EXTENSION_FTL)) {
069                            sb.append(theme.getFreeMarkerTemplateLoader());
070                            sb.append(theme.getTemplatesPath());
071    
072                            if (Validator.isNotNull(servletContextName) &&
073                                    !path.startsWith(StringPool.SLASH.concat(servletContextName))) {
074    
075                                    sb.append(StringPool.SLASH);
076                                    sb.append(servletContextName);
077                            }
078    
079                            sb.append(StringPool.SLASH);
080                            sb.append(path.substring(start, end));
081                            sb.append(StringPool.PERIOD);
082    
083                            if (Validator.isNotNull(portletId)) {
084                                    sb.append(portletId);
085                                    sb.append(StringPool.PERIOD);
086                            }
087    
088                            sb.append(TEMPLATE_EXTENSION_FTL);
089    
090                            return sb.toString();
091                    }
092                    else if (extension.equals(TEMPLATE_EXTENSION_VM)) {
093                            sb.append(theme.getVelocityResourceListener());
094                            sb.append(theme.getTemplatesPath());
095    
096                            if (Validator.isNotNull(servletContextName) &&
097                                    !path.startsWith(StringPool.SLASH.concat(servletContextName))) {
098    
099                                    sb.append(StringPool.SLASH);
100                                    sb.append(servletContextName);
101                            }
102    
103                            sb.append(StringPool.SLASH);
104                            sb.append(path.substring(start, end));
105                            sb.append(StringPool.PERIOD);
106    
107                            if (Validator.isNotNull(portletId)) {
108                                    sb.append(portletId);
109                                    sb.append(StringPool.PERIOD);
110                            }
111    
112                            sb.append(TEMPLATE_EXTENSION_VM);
113    
114                            return sb.toString();
115                    }
116                    else {
117                            return path;
118                    }
119            }
120    
121            public static boolean resourceExists(
122                            ServletContext servletContext, Theme theme, String portletId,
123                            String path)
124                    throws Exception {
125    
126                    Boolean exists = null;
127    
128                    if (Validator.isNotNull(portletId)) {
129                            exists = _resourceExists(servletContext, theme, portletId, path);
130    
131                            if (!exists &&
132                                    portletId.contains(PortletConstants.INSTANCE_SEPARATOR)) {
133    
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 FreeMarkerEngineUtil.resourceExists(resourcePath);
169                    }
170                    else if (extension.equals(TEMPLATE_EXTENSION_VM)) {
171                            return VelocityEngineUtil.resourceExists(resourcePath);
172                    }
173                    else {
174                            URL url = null;
175    
176                            if (theme.isWARFile()) {
177                                    ServletContext themeServletContext = servletContext.getContext(
178                                            theme.getContextPath());
179    
180                                    url = themeServletContext.getResource(resourcePath);
181                            }
182                            else {
183                                    url = servletContext.getResource(resourcePath);
184                            }
185    
186                            if (url == null) {
187                                    return false;
188                            }
189                            else {
190                                    return true;
191                            }
192                    }
193            }
194    
195    }