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 @Override
039 public Source resolve(String href, String base) {
040 try {
041 String content = null;
042
043 int templatePathIndex = href.indexOf(_GET_TEMPLATE_PATH);
044
045 if (templatePathIndex >= 0) {
046 int templateIdIndex =
047 templatePathIndex + _GET_TEMPLATE_PATH.length();
048
049 long groupId = GetterUtil.getLong(_tokens.get("group_id"));
050 String templateId = href.substring(templateIdIndex);
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 String _languageId;
074 private Map<String, String> _tokens;
075
076 }