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