001    /**
002     * Copyright (c) 2000-2010 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.freemarker;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.theme.ThemeLoader;
020    import com.liferay.portal.theme.ThemeLoaderFactory;
021    
022    import java.io.File;
023    import java.io.IOException;
024    
025    import java.net.URL;
026    
027    /**
028     * @author Mika Koivisto
029     */
030    public class ThemeLoaderTemplateLoader extends URLTemplateLoader {
031    
032            public URL getURL(String name) throws IOException {
033                    int pos = name.indexOf(THEME_LOADER_SEPARATOR);
034    
035                    if (pos != -1) {
036                            String ctxName = name.substring(0, pos);
037    
038                            ThemeLoader themeLoader =
039                                    ThemeLoaderFactory.getThemeLoader(ctxName);
040    
041                            if (themeLoader != null) {
042                                    String templateName =
043                                            name.substring(pos + THEME_LOADER_SEPARATOR.length());
044    
045                                    String themesPath = themeLoader.getThemesPath();
046    
047                                    if (templateName.startsWith(themesPath)) {
048                                            name =
049                                                    templateName.substring(
050                                                            themesPath.length(), templateName.length());
051                                    }
052    
053                                    if (_log.isDebugEnabled()) {
054                                            _log.debug(
055                                                    name + " is associated with the theme loader " +
056                                                            ctxName + " " + themeLoader);
057                                    }
058    
059                                    File fileStorage = themeLoader.getFileStorage();
060    
061                                    return new File(fileStorage.getPath() + name).toURI().toURL();
062    
063                            }
064                            else {
065                                    _log.error(
066                                            name + " is not valid because " + ctxName +
067                                                    " does not map to a theme loader");
068                            }
069                    }
070    
071                    return null;
072            }
073    
074            private static Log _log = LogFactoryUtil.getLog(
075                    ThemeLoaderTemplateLoader.class);
076    
077    }