1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.velocity;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portlet.journal.model.JournalTemplate;
29  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
30  
31  import java.io.ByteArrayInputStream;
32  import java.io.InputStream;
33  
34  import org.apache.velocity.exception.ResourceNotFoundException;
35  
36  /**
37   * <a href="JournalTemplateVelocityResourceListener.java.html"><b><i>View Source
38   * </i></b></a>
39   *
40   * @author Alexander Chow
41   *
42   */
43  public class JournalTemplateVelocityResourceListener
44      extends VelocityResourceListener {
45  
46      public InputStream getResourceStream(String source)
47          throws ResourceNotFoundException {
48  
49          InputStream is = null;
50  
51          try {
52              int pos = source.indexOf(JOURNAL_SEPARATOR + StringPool.SLASH);
53  
54              if (pos != -1) {
55                  int x = source.indexOf(StringPool.SLASH, pos);
56                  int y = source.indexOf(StringPool.SLASH, x + 1);
57                  int z = source.indexOf(StringPool.SLASH, y + 1);
58  
59                  long companyId = GetterUtil.getLong(source.substring(x + 1, y));
60                  long groupId = GetterUtil.getLong(source.substring(y + 1, z));
61                  String templateId = source.substring(z + 1);
62  
63                  if (_log.isDebugEnabled()) {
64                      _log.debug(
65                          "Loading {companyId=" + companyId + ",groupId=" +
66                              groupId + ",templateId=" + templateId + "}");
67                  }
68  
69                  JournalTemplate template =
70                      JournalTemplateLocalServiceUtil.getTemplate(
71                          groupId, templateId);
72  
73                  String buffer = template.getXsl();
74  
75                  is = new ByteArrayInputStream(buffer.getBytes());
76              }
77          }
78          catch (PortalException pe) {
79              throw new ResourceNotFoundException(source);
80          }
81          catch (SystemException se) {
82              throw new ResourceNotFoundException(source);
83          }
84  
85          return is;
86      }
87  
88      private static Log _log =
89          LogFactoryUtil.getLog(JournalTemplateVelocityResourceListener.class);
90  
91  }