001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.template;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.template.TemplateConstants;
020    import com.liferay.portal.theme.ThemeLoader;
021    import com.liferay.portal.theme.ThemeLoaderFactory;
022    
023    import java.io.File;
024    import java.io.IOException;
025    
026    import java.net.URL;
027    
028    /**
029     * @author Tina Tian
030     */
031    public class ThemeResourceParser extends URLResourceParser {
032    
033            @Override
034            public URL getURL(String templateId) throws IOException {
035                    int pos = templateId.indexOf(TemplateConstants.THEME_LOADER_SEPARATOR);
036    
037                    if (pos == -1) {
038                            return null;
039                    }
040    
041                    String servletContextName = templateId.substring(0, pos);
042    
043                    ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(
044                            servletContextName);
045    
046                    if (themeLoader == null) {
047                            _log.error(
048                                    templateId + " is not valid because " + servletContextName +
049                                            " does not map to a theme loader");
050    
051                            return null;
052                    }
053    
054                    String templateName = templateId.substring(
055                            pos + TemplateConstants.THEME_LOADER_SEPARATOR.length());
056    
057                    String themesPath = themeLoader.getThemesPath();
058    
059                    if (templateName.startsWith(themesPath)) {
060                            templateId = templateName.substring(themesPath.length());
061                    }
062    
063                    if (_log.isDebugEnabled()) {
064                            _log.debug(
065                                    templateId + " is associated with the theme loader " +
066                                            servletContextName + " " + themeLoader);
067                    }
068    
069                    File fileStorage = themeLoader.getFileStorage();
070    
071                    return new File(fileStorage, templateId).toURI().toURL();
072            }
073    
074            private static Log _log = LogFactoryUtil.getLog(ThemeResourceParser.class);
075    
076    }