1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.velocity;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.CharPool;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portlet.journal.model.JournalTemplate;
26  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
27  
28  import java.io.InputStream;
29  
30  import org.apache.velocity.exception.ResourceNotFoundException;
31  
32  /**
33   * <a href="JournalTemplateVelocityResourceListener.java.html"><b><i>View Source
34   * </i></b></a>
35   *
36   * @author Alexander Chow
37   */
38  public class JournalTemplateVelocityResourceListener
39      extends VelocityResourceListener {
40  
41      public InputStream getResourceStream(String source)
42          throws ResourceNotFoundException {
43  
44          InputStream is = null;
45  
46          try {
47              int pos = source.indexOf(JOURNAL_SEPARATOR + StringPool.SLASH);
48  
49              if (pos != -1) {
50                  int x = source.indexOf(CharPool.SLASH, pos);
51                  int y = source.indexOf(CharPool.SLASH, x + 1);
52                  int z = source.indexOf(CharPool.SLASH, y + 1);
53  
54                  long companyId = GetterUtil.getLong(source.substring(x + 1, y));
55                  long groupId = GetterUtil.getLong(source.substring(y + 1, z));
56                  String templateId = source.substring(z + 1);
57  
58                  if (_log.isDebugEnabled()) {
59                      _log.debug(
60                          "Loading {companyId=" + companyId + ",groupId=" +
61                              groupId + ",templateId=" + templateId + "}");
62                  }
63  
64                  JournalTemplate template =
65                      JournalTemplateLocalServiceUtil.getTemplate(
66                          groupId, templateId);
67  
68                  String buffer = template.getXsl();
69  
70                  is = new UnsyncByteArrayInputStream(buffer.getBytes());
71              }
72          }
73          catch (PortalException pe) {
74              throw new ResourceNotFoundException(source);
75          }
76          catch (SystemException se) {
77              throw new ResourceNotFoundException(source);
78          }
79  
80          return is;
81      }
82  
83      private static Log _log = LogFactoryUtil.getLog(
84          JournalTemplateVelocityResourceListener.class);
85  
86  }