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