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 =
050 href.substring(templateIdIndex, href.length());
051
052 content = JournalUtil.getTemplateScript(
053 groupId, templateId, _tokens, _languageId);
054 }
055 else {
056 content = HttpUtil.URLtoString(href);
057 }
058
059 return new StreamSource(new UnsyncStringReader(content));
060 }
061 catch (Exception e) {
062 _log.error(href + " does not reference a valid template");
063
064 return null;
065 }
066 }
067
068 private static final String _GET_TEMPLATE_PATH =
069 "/c/journal/get_template?template_id=";
070
071 private static Log _log = LogFactoryUtil.getLog(URIResolver.class);
072
073 private Map<String, String> _tokens;
074 private String _languageId;
075
076 }