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.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    }