001
014
015 package com.liferay.portlet.journal.util;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.GetterUtil;
020 import com.liferay.portal.kernel.util.HtmlUtil;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.kernel.xml.Document;
024 import com.liferay.portal.kernel.xml.Element;
025 import com.liferay.portal.kernel.xml.SAXReaderUtil;
026 import com.liferay.portlet.journal.model.JournalArticle;
027 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
028
029 import java.util.Map;
030
031
034 public class ContentTransformerListener extends TransformerListener {
035
036 public String onXml(String s) {
037 if (_log.isDebugEnabled()) {
038 _log.debug("onXml");
039 }
040
041 s = replace(s);
042
043 return s;
044 }
045
046 public String onScript(String s) {
047 if (_log.isDebugEnabled()) {
048 _log.debug("onScript");
049 }
050
051 return s;
052 }
053
054 public String onOutput(String s) {
055 if (_log.isDebugEnabled()) {
056 _log.debug("onOutput");
057 }
058
059 return s;
060 }
061
062
068 protected String replace(String xml) {
069 try {
070 Document doc = SAXReaderUtil.read(xml);
071
072 Element root = doc.getRootElement();
073
074 replace(root);
075
076 xml = JournalUtil.formatXML(doc);
077 }
078 catch (Exception e) {
079 _log.warn(e.getMessage());
080 }
081
082 return xml;
083 }
084
085 protected void replace(Element root) throws Exception {
086 Map<String, String> tokens = getTokens();
087
088 long groupId = GetterUtil.getLong(tokens.get("group_id"));
089
090 for (Element el : root.elements()) {
091 Element dynamicContent = el.element("dynamic-content");
092
093 if (dynamicContent != null) {
094 String text = dynamicContent.getText();
095
096 text = HtmlUtil.stripComments(text);
097 text = HtmlUtil.stripHtml(text);
098 text = text.trim();
099
100
101
102 if (Validator.isNotNull(text) && text.length() >= 7 &&
103 text.startsWith("[@") && text.endsWith("@]")) {
104
105 text = text.substring(2, text.length() - 2);
106
107 int pos = text.indexOf(";");
108
109 if (pos != -1) {
110 String articleId = text.substring(0, pos);
111 String elementName =
112 text.substring(pos + 1, text.length());
113
114 JournalArticle article =
115 JournalArticleLocalServiceUtil.getArticle(
116 groupId, articleId);
117
118 dynamicContent.clearContent();
119 dynamicContent.addCDATA(
120 _getDynamicContent(
121 article.getContent(), elementName));
122 }
123 }
124
125
126
127 else if ((text != null) &&
128 (text.startsWith("/image/journal/article?img_id"))) {
129
130 dynamicContent.setText("@cdn_host@@root_path@" + text);
131 }
132 }
133
134 replace(el);
135 }
136 }
137
138 private String _getDynamicContent(String xml, String elementName) {
139 String content = null;
140
141 try {
142 Document doc = SAXReaderUtil.read(xml);
143
144 Element root = doc.getRootElement();
145
146 for (Element el : root.elements()) {
147 String elName = el.attributeValue("name", StringPool.BLANK);
148
149 if (elName.equals(elementName)) {
150 content = el.elementText("dynamic-content");
151
152 break;
153 }
154 }
155 }
156 catch (Exception e) {
157 _log.error(e, e);
158 }
159
160 return GetterUtil.getString(content);
161 }
162
163 private static Log _log = LogFactoryUtil.getLog(
164 ContentTransformerListener.class);
165
166 }