001    /**
002     * Copyright (c) 2000-2012 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_VM = "vm";
035    
036            public static String getResourcePath(
037                    ServletContext servletContext, Theme theme, String portletId,
038                    String path) {
039    
040                    StringBundler sb = new StringBundler(9);
041    
042                    String themeContextName = GetterUtil.getString(
043                            theme.getServletContextName());
044    
045                    sb.append(themeContextName);
046    
047                    String servletContextName = StringPool.BLANK;
048    
049                    String contextPath = ContextPathUtil.getContextPath(servletContext);
050    
051                    if (!contextPath.equals(PortalUtil.getPathContext())) {
052                            servletContextName = GetterUtil.getString(
053                                    servletContext.getServletContextName());
054                    }
055    
056                    int start = 0;
057    
058                    if (path.startsWith(StringPool.SLASH)) {
059                            start = 1;
060                    }
061    
062                    int end = path.lastIndexOf(CharPool.PERIOD);
063    
064                    String extension = theme.getTemplateExtension();
065    
066                    if (extension.equals(TEMPLATE_EXTENSION_FTL)) {
067                            sb.append(theme.getFreeMarkerTemplateLoader());
068                            sb.append(theme.getTemplatesPath());
069    
070                            if (Validator.isNotNull(servletContextName) &&
071                                    !path.startsWith(StringPool.SLASH.concat(servletContextName))) {
072    
073                                    sb.append(StringPool.SLASH);
074                                    sb.append(servletContextName);
075                            }
076    
077                            sb.append(StringPool.SLASH);
078                            sb.append(path.substring(start, end));
079                            sb.append(StringPool.PERIOD);
080    
081                            if (Validator.isNotNull(portletId)) {
082                                    sb.append(portletId);
083                                    sb.append(StringPool.PERIOD);
084                            }
085    
086                            sb.append(TEMPLATE_EXTENSION_FTL);
087    
088                            return sb.toString();
089                    }
090                    else if (extension.equals(TEMPLATE_EXTENSION_VM)) {
091                            sb.append(theme.getVelocityResourceListener());
092                            sb.append(theme.getTemplatesPath());
093    
094                            if (Validator.isNotNull(servletContextName) &&
095                                    !path.startsWith(StringPool.SLASH.concat(servletContextName))) {
096    
097                                    sb.append(StringPool.SLASH);
098                                    sb.append(servletContextName);
099                            }
100    
101                            sb.append(StringPool.SLASH);
102                            sb.append(path.substring(start, end));
103                            sb.append(StringPool.PERIOD);
104    
105                            if (Validator.isNotNull(portletId)) {
106                                    sb.append(portletId);
107                                    sb.append(StringPool.PERIOD);
108                            }
109    
110                            sb.append(TEMPLATE_EXTENSION_VM);
111    
112                            return sb.toString();
113                    }
114                    else {
115                            return path;
116                    }
117            }
118    
119            public static boolean resourceExists(
120                            ServletContext servletContext, Theme theme, String portletId,
121                            String path)
122                    throws Exception {
123    
124                    Boolean exists = null;
125    
126                    if (Validator.isNotNull(portletId)) {
127                            exists = _resourceExists(servletContext, theme, portletId, path);
128    
129                            if (!exists &&
130                                    portletId.contains(PortletConstants.INSTANCE_SEPARATOR)) {
131    
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 FreeMarkerEngineUtil.resourceExists(resourcePath);
167                    }
168                    else if (extension.equals(TEMPLATE_EXTENSION_VM)) {
169                            return VelocityEngineUtil.resourceExists(resourcePath);
170                    }
171                    else {
172                            URL url = null;
173    
174                            if (theme.isWARFile()) {
175                                    ServletContext themeServletContext = servletContext.getContext(
176                                            theme.getContextPath());
177    
178                                    url = themeServletContext.getResource(resourcePath);
179                            }
180                            else {
181                                    url = servletContext.getResource(resourcePath);
182                            }
183    
184                            if (url == null) {
185                                    return false;
186                            }
187                            else {
188                                    return true;
189                            }
190                    }
191            }
192    
193    }