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.portlet.journal.util;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.HttpUtil;
22  
23  import java.util.Map;
24  
25  import javax.xml.transform.Source;
26  import javax.xml.transform.stream.StreamSource;
27  
28  /**
29   * <a href="URIResolver.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class URIResolver implements javax.xml.transform.URIResolver {
34  
35      public URIResolver(Map<String, String> tokens, String languageId) {
36          _tokens = tokens;
37          _languageId = languageId;
38      }
39  
40      public Source resolve(String href, String base) {
41          try {
42              String content = null;
43  
44              int templatePathIndex = href.indexOf(_GET_TEMPLATE_PATH);
45  
46              if (templatePathIndex >= 0) {
47                  int templateIdIndex =
48                      templatePathIndex + _GET_TEMPLATE_PATH.length();
49  
50                  long groupId = GetterUtil.getLong(_tokens.get("group_id"));
51                  String templateId =
52                      href.substring(templateIdIndex, href.length());
53  
54                  content = JournalUtil.getTemplateScript(
55                      groupId, templateId, _tokens, _languageId);
56              }
57              else {
58                  content = HttpUtil.URLtoString(href);
59              }
60  
61              return new StreamSource(new UnsyncStringReader(content));
62          }
63          catch (Exception e) {
64              _log.error(href + " does not reference a valid template");
65  
66              return null;
67          }
68      }
69  
70      private static final String _GET_TEMPLATE_PATH =
71          "/c/journal/get_template?template_id=";
72  
73      private static Log _log = LogFactoryUtil.getLog(URIResolver.class);
74  
75      private Map<String, String> _tokens;
76      private String _languageId;
77  
78  }