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.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    /**
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 && PortletConstants.hasInstanceId(portletId)) {
132                                    String rootPortletId = PortletConstants.getRootPortletId(
133                                            portletId);
134    
135                                    exists = _resourceExists(
136                                            servletContext, theme, rootPortletId, path);
137                            }
138    
139                            if (!exists) {
140                                    exists = _resourceExists(servletContext, theme, null, path);
141                            }
142                    }
143    
144                    if (exists == null) {
145                            exists = _resourceExists(servletContext, theme, portletId, path);
146                    }
147    
148                    return exists;
149            }
150    
151            private static boolean _resourceExists(
152                            ServletContext servletContext, Theme theme, String portletId,
153                            String path)
154                    throws Exception {
155    
156                    if (Validator.isNull(path)) {
157                            return false;
158                    }
159    
160                    String resourcePath = getResourcePath(
161                            servletContext, theme, portletId, path);
162    
163                    String extension = theme.getTemplateExtension();
164    
165                    if (extension.equals(TEMPLATE_EXTENSION_FTL)) {
166                            return TemplateResourceLoaderUtil.hasTemplateResource(
167                                    TemplateConstants.LANG_TYPE_FTL, resourcePath);
168                    }
169                    else if (extension.equals(TEMPLATE_EXTENSION_VM)) {
170                            return TemplateResourceLoaderUtil.hasTemplateResource(
171                                    TemplateConstants.LANG_TYPE_VM, 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    }