1
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
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 }