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.velocity;
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.FileInputStream;
024    import java.io.InputStream;
025    
026    import org.apache.velocity.exception.ResourceNotFoundException;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class ThemeLoaderVelocityResourceListener
032            extends VelocityResourceListener {
033    
034            @Override
035            public InputStream getResourceStream(String source)
036                    throws ResourceNotFoundException {
037    
038                    try {
039                            return doGetResourceStream(source);
040                    }
041                    catch (Exception e) {
042                            throw new ResourceNotFoundException(source);
043                    }
044            }
045    
046            protected InputStream doGetResourceStream(String source) throws Exception {
047                    int pos = source.indexOf(THEME_LOADER_SEPARATOR);
048    
049                    if (pos == -1) {
050                            return null;
051                    }
052    
053                    String servletContextName = source.substring(0, pos);
054    
055                    ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(
056                            servletContextName);
057    
058                    if (themeLoader == null) {
059                            _log.error(
060                                    source + " is not valid because " + servletContextName +
061                                            " does not map to a theme loader");
062    
063                            return null;
064                    }
065    
066                    String name = source.substring(pos + THEME_LOADER_SEPARATOR.length());
067    
068                    String themesPath = themeLoader.getThemesPath();
069    
070                    if (name.startsWith(themesPath)) {
071                            name = name.substring(themesPath.length(), name.length());
072                    }
073    
074                    if (_log.isDebugEnabled()) {
075                            _log.debug(
076                                    name + " is associated with the theme loader " +
077                                            servletContextName + " " + themeLoader);
078                    }
079    
080                    File fileStorage = themeLoader.getFileStorage();
081    
082                    return new FileInputStream(fileStorage.getPath() + name);
083            }
084    
085            private static Log _log = LogFactoryUtil.getLog(
086                    ThemeLoaderVelocityResourceListener.class);
087    
088    }