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.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.FileInputStream;
023    import java.io.FileNotFoundException;
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            public InputStream getResourceStream(String source)
035                    throws ResourceNotFoundException {
036    
037                    InputStream is = null;
038    
039                    try {
040                            int pos = source.indexOf(THEME_LOADER_SEPARATOR);
041    
042                            if (pos != -1) {
043                                    String ctxName = source.substring(0, pos);
044    
045                                    ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(
046                                            ctxName);
047    
048                                    if (themeLoader != null) {
049                                            String name =
050                                                    source.substring(pos + THEME_LOADER_SEPARATOR.length());
051    
052                                            String themesPath = themeLoader.getThemesPath();
053    
054                                            if (name.startsWith(themesPath)) {
055                                                    name = name.substring(
056                                                            themesPath.length(), name.length());
057                                            }
058    
059                                            if (_log.isDebugEnabled()) {
060                                                    _log.debug(
061                                                            name + " is associated with the theme loader " +
062                                                                    ctxName + " " + themeLoader);
063                                            }
064    
065                                            is = new FileInputStream(
066                                                    themeLoader.getFileStorage().getPath() + name);
067                                    }
068                                    else {
069                                            _log.error(
070                                                    source + " is not valid because " + ctxName +
071                                                            " does not map to a theme loader");
072                                    }
073                            }
074                    }
075                    catch (FileNotFoundException fnfe) {
076                            throw new ResourceNotFoundException(source);
077                    }
078    
079                    return is;
080            }
081    
082            private static Log _log = LogFactoryUtil.getLog(
083                    ThemeLoaderVelocityResourceListener.class);
084    
085    }