001
014
015 package com.liferay.portlet.journal.util;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.HttpUtil;
022
023 import java.util.Map;
024
025 import javax.xml.transform.Source;
026 import javax.xml.transform.stream.StreamSource;
027
028
031 public class URIResolver implements javax.xml.transform.URIResolver {
032
033 public URIResolver(Map<String, String> tokens, String languageId) {
034 _tokens = tokens;
035 _languageId = languageId;
036 }
037
038 public Source resolve(String href, String base) {
039 try {
040 String content = null;
041
042 int templatePathIndex = href.indexOf(_GET_TEMPLATE_PATH);
043
044 if (templatePathIndex >= 0) {
045 int templateIdIndex =
046 templatePathIndex + _GET_TEMPLATE_PATH.length();
047
048 long groupId = GetterUtil.getLong(_tokens.get("group_id"));
049 String templateId = href.substring(templateIdIndex);
050
051 content = JournalUtil.getTemplateScript(
052 groupId, templateId, _tokens, _languageId);
053 }
054 else {
055 content = HttpUtil.URLtoString(href);
056 }
057
058 return new StreamSource(new UnsyncStringReader(content));
059 }
060 catch (Exception e) {
061 _log.error(href + " does not reference a valid template");
062
063 return null;
064 }
065 }
066
067 private static final String _GET_TEMPLATE_PATH =
068 "/c/journal/get_template?template_id=";
069
070 private static Log _log = LogFactoryUtil.getLog(URIResolver.class);
071
072 private String _languageId;
073 private Map<String, String> _tokens;
074
075 }