001
014
015 package com.liferay.portal.velocity;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.CharPool;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portlet.journal.model.JournalTemplate;
024 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
025
026 import java.io.InputStream;
027
028 import org.apache.velocity.exception.ResourceNotFoundException;
029
030
033 public class JournalTemplateVelocityResourceListener
034 extends VelocityResourceListener {
035
036 @Override
037 public InputStream getResourceStream(String source)
038 throws ResourceNotFoundException {
039
040 try {
041 return doGetResourceStream(source);
042 }
043 catch (Exception e) {
044 throw new ResourceNotFoundException(source);
045 }
046 }
047
048 protected InputStream doGetResourceStream(String source) throws Exception {
049 int pos = source.indexOf(_SOURCE_PREFIX);
050
051 if (pos == -1) {
052 return null;
053 }
054
055 int x = source.indexOf(CharPool.SLASH, pos);
056 int y = source.indexOf(CharPool.SLASH, x + 1);
057 int z = source.indexOf(CharPool.SLASH, y + 1);
058
059 long companyId = GetterUtil.getLong(source.substring(x + 1, y));
060 long groupId = GetterUtil.getLong(source.substring(y + 1, z));
061 String templateId = source.substring(z + 1);
062
063 if (_log.isDebugEnabled()) {
064 _log.debug(
065 "Loading {companyId=" + companyId + ", groupId=" + groupId +
066 ", templateId=" + templateId + "}");
067 }
068
069 JournalTemplate journalTemplate =
070 JournalTemplateLocalServiceUtil.getTemplate(groupId, templateId);
071
072 String xsl = journalTemplate.getXsl();
073
074 return new UnsyncByteArrayInputStream(xsl.getBytes());
075 }
076
077 private static final String _SOURCE_PREFIX = JOURNAL_SEPARATOR.concat(
078 StringPool.SLASH);
079
080 private static Log _log = LogFactoryUtil.getLog(
081 JournalTemplateVelocityResourceListener.class);
082
083 }