1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.velocity;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.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.GetterUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portlet.journal.model.JournalTemplate;
25  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
26  
27  import java.io.InputStream;
28  
29  import org.apache.velocity.exception.ResourceNotFoundException;
30  
31  /**
32   * <a href="JournalTemplateVelocityResourceListener.java.html"><b><i>View Source
33   * </i></b></a>
34   *
35   * @author Alexander Chow
36   */
37  public class JournalTemplateVelocityResourceListener
38      extends VelocityResourceListener {
39  
40      public InputStream getResourceStream(String source)
41          throws ResourceNotFoundException {
42  
43          InputStream is = null;
44  
45          try {
46              int pos = source.indexOf(JOURNAL_SEPARATOR + StringPool.SLASH);
47  
48              if (pos != -1) {
49                  int x = source.indexOf(StringPool.SLASH, pos);
50                  int y = source.indexOf(StringPool.SLASH, x + 1);
51                  int z = source.indexOf(StringPool.SLASH, y + 1);
52  
53                  long companyId = GetterUtil.getLong(source.substring(x + 1, y));
54                  long groupId = GetterUtil.getLong(source.substring(y + 1, z));
55                  String templateId = source.substring(z + 1);
56  
57                  if (_log.isDebugEnabled()) {
58                      _log.debug(
59                          "Loading {companyId=" + companyId + ",groupId=" +
60                              groupId + ",templateId=" + templateId + "}");
61                  }
62  
63                  JournalTemplate template =
64                      JournalTemplateLocalServiceUtil.getTemplate(
65                          groupId, templateId);
66  
67                  String buffer = template.getXsl();
68  
69                  is = new UnsyncByteArrayInputStream(buffer.getBytes());
70              }
71          }
72          catch (PortalException pe) {
73              throw new ResourceNotFoundException(source);
74          }
75          catch (SystemException se) {
76              throw new ResourceNotFoundException(source);
77          }
78  
79          return is;
80      }
81  
82      private static Log _log = LogFactoryUtil.getLog(
83          JournalTemplateVelocityResourceListener.class);
84  
85  }