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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portlet.journal.model.JournalTemplate;
025    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
026    
027    import java.io.InputStream;
028    
029    import org.apache.velocity.exception.ResourceNotFoundException;
030    
031    /**
032     * @author Alexander Chow
033     */
034    public class JournalTemplateVelocityResourceListener
035            extends VelocityResourceListener {
036    
037            public InputStream getResourceStream(String source)
038                    throws ResourceNotFoundException {
039    
040                    InputStream is = null;
041    
042                    try {
043                            int pos = source.indexOf(JOURNAL_SEPARATOR + StringPool.SLASH);
044    
045                            if (pos != -1) {
046                                    int x = source.indexOf(StringPool.SLASH, pos);
047                                    int y = source.indexOf(StringPool.SLASH, x + 1);
048                                    int z = source.indexOf(StringPool.SLASH, y + 1);
049    
050                                    long companyId = GetterUtil.getLong(source.substring(x + 1, y));
051                                    long groupId = GetterUtil.getLong(source.substring(y + 1, z));
052                                    String templateId = source.substring(z + 1);
053    
054                                    if (_log.isDebugEnabled()) {
055                                            _log.debug(
056                                                    "Loading {companyId=" + companyId + ",groupId=" +
057                                                            groupId + ",templateId=" + templateId + "}");
058                                    }
059    
060                                    JournalTemplate template =
061                                            JournalTemplateLocalServiceUtil.getTemplate(
062                                                    groupId, templateId);
063    
064                                    String buffer = template.getXsl();
065    
066                                    is = new UnsyncByteArrayInputStream(buffer.getBytes());
067                            }
068                    }
069                    catch (PortalException pe) {
070                            throw new ResourceNotFoundException(source);
071                    }
072                    catch (SystemException se) {
073                            throw new ResourceNotFoundException(source);
074                    }
075    
076                    return is;
077            }
078    
079            private static Log _log = LogFactoryUtil.getLog(
080                    JournalTemplateVelocityResourceListener.class);
081    
082    }