1
22
23 package com.liferay.portal.velocity;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portlet.journal.model.JournalTemplate;
32 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
33
34 import java.io.ByteArrayInputStream;
35 import java.io.InputStream;
36
37 import org.apache.velocity.exception.ResourceNotFoundException;
38
39
45 public class JournalTemplateVelocityResourceListener
46 extends VelocityResourceListener {
47
48 public InputStream getResourceStream(String source)
49 throws ResourceNotFoundException {
50
51 InputStream is = null;
52
53 try {
54 int pos = source.indexOf(JOURNAL_SEPARATOR + StringPool.SLASH);
55
56 if (pos != -1) {
57 int x = source.indexOf(StringPool.SLASH, pos);
58 int y = source.indexOf(StringPool.SLASH, x + 1);
59 int z = source.indexOf(StringPool.SLASH, y + 1);
60
61 long companyId = GetterUtil.getLong(source.substring(x + 1, y));
62 long groupId = GetterUtil.getLong(source.substring(y + 1, z));
63 String templateId = source.substring(z + 1);
64
65 if (_log.isDebugEnabled()) {
66 _log.debug(
67 "Loading {companyId=" + companyId + ",groupId=" +
68 groupId + ",templateId=" + templateId + "}");
69 }
70
71 JournalTemplate template =
72 JournalTemplateLocalServiceUtil.getTemplate(
73 groupId, templateId);
74
75 String buffer = template.getXsl();
76
77 is = new ByteArrayInputStream(buffer.getBytes());
78 }
79 }
80 catch (PortalException pe) {
81 throw new ResourceNotFoundException(source);
82 }
83 catch (SystemException se) {
84 throw new ResourceNotFoundException(source);
85 }
86
87 return is;
88 }
89
90 private static Log _log =
91 LogFactoryUtil.getLog(JournalTemplateVelocityResourceListener.class);
92
93 }