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