001    /**
002     * Copyright (c) 2000-present 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.template;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
020    import com.liferay.portal.kernel.template.TemplateConstants;
021    import com.liferay.portal.theme.ThemeLoader;
022    import com.liferay.portal.theme.ThemeLoaderFactory;
023    
024    import java.io.File;
025    import java.io.IOException;
026    
027    import java.net.URL;
028    
029    /**
030     * @author Tina Tian
031     */
032    @OSGiBeanProperties(
033            property = {
034                    "lang.type=" + TemplateConstants.LANG_TYPE_FTL,
035                    "lang.type=" + TemplateConstants.LANG_TYPE_VM
036            },
037            service = TemplateResourceParser.class
038    )
039    public class ThemeResourceParser extends URLResourceParser {
040    
041            @Override
042            public URL getURL(String templateId) throws IOException {
043                    int pos = templateId.indexOf(TemplateConstants.THEME_LOADER_SEPARATOR);
044    
045                    if (pos == -1) {
046                            return null;
047                    }
048    
049                    String servletContextName = templateId.substring(0, pos);
050    
051                    ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(
052                            servletContextName);
053    
054                    if (themeLoader == null) {
055                            _log.error(
056                                    templateId + " is not valid because " + servletContextName +
057                                            " does not map to a theme loader");
058    
059                            return null;
060                    }
061    
062                    String templateName = templateId.substring(
063                            pos + TemplateConstants.THEME_LOADER_SEPARATOR.length());
064    
065                    String themesPath = themeLoader.getThemesPath();
066    
067                    if (templateName.startsWith(themesPath)) {
068                            templateId = templateName.substring(themesPath.length());
069                    }
070    
071                    if (_log.isDebugEnabled()) {
072                            _log.debug(
073                                    templateId + " is associated with the theme loader " +
074                                            servletContextName + " " + themeLoader);
075                    }
076    
077                    File fileStorage = themeLoader.getFileStorage();
078    
079                    return new File(fileStorage, templateId).toURI().toURL();
080            }
081    
082            private static final Log _log = LogFactoryUtil.getLog(
083                    ThemeResourceParser.class);
084    
085    }