1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
40   * <a href="JournalTemplateVelocityResourceListener.java.html"><b><i>View Source
41   * </i></b></a>
42   *
43   * @author Alexander Chow
44   */
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  }