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   */
46  public class JournalTemplateVelocityResourceListener
47      extends VelocityResourceListener {
48  
49      public InputStream getResourceStream(String source)
50          throws ResourceNotFoundException {
51  
52          InputStream is = null;
53  
54          try {
55              int pos = source.indexOf(JOURNAL_SEPARATOR + StringPool.SLASH);
56  
57              if (pos != -1) {
58                  int x = source.indexOf(StringPool.SLASH, pos);
59                  int y = source.indexOf(StringPool.SLASH, x + 1);
60                  int z = source.indexOf(StringPool.SLASH, y + 1);
61  
62                  long companyId = GetterUtil.getLong(source.substring(x + 1, y));
63                  long groupId = GetterUtil.getLong(source.substring(y + 1, z));
64                  String templateId = source.substring(z + 1);
65  
66                  if (_log.isDebugEnabled()) {
67                      _log.debug(
68                          "Loading {companyId=" + companyId + ",groupId=" +
69                              groupId + ",templateId=" + templateId + "}");
70                  }
71  
72                  JournalTemplate template =
73                      JournalTemplateLocalServiceUtil.getTemplate(
74                          groupId, templateId);
75  
76                  String buffer = template.getXsl();
77  
78                  is = new ByteArrayInputStream(buffer.getBytes());
79              }
80          }
81          catch (PortalException pe) {
82              throw new ResourceNotFoundException(source);
83          }
84          catch (SystemException se) {
85              throw new ResourceNotFoundException(source);
86          }
87  
88          return is;
89      }
90  
91      private static Log _log =
92          LogFactoryUtil.getLog(JournalTemplateVelocityResourceListener.class);
93  
94  }