1
22
23 package com.liferay.portlet.journal.util;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.HtmlUtil;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.kernel.xml.Document;
32 import com.liferay.portal.kernel.xml.Element;
33 import com.liferay.portal.kernel.xml.SAXReaderUtil;
34 import com.liferay.portlet.journal.model.JournalArticle;
35 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
36
37 import java.util.Map;
38
39
45 public class ContentTransformerListener extends TransformerListener {
46
47 public String onXml(String s) {
48 if (_log.isDebugEnabled()) {
49 _log.debug("onXml");
50 }
51
52 s = replace(s);
53
54 return s;
55 }
56
57 public String onScript(String s) {
58 if (_log.isDebugEnabled()) {
59 _log.debug("onScript");
60 }
61
62 return s;
63 }
64
65 public String onOutput(String s) {
66 if (_log.isDebugEnabled()) {
67 _log.debug("onOutput");
68 }
69
70 return s;
71 }
72
73
80 protected String replace(String xml) {
81 try {
82 Document doc = SAXReaderUtil.read(xml);
83
84 Element root = doc.getRootElement();
85
86 replace(root);
87
88 xml = JournalUtil.formatXML(doc);
89 }
90 catch (Exception e) {
91 _log.warn(e.getMessage());
92 }
93
94 return xml;
95 }
96
97 protected void replace(Element root) throws Exception {
98 Map<String, String> tokens = getTokens();
99
100 long groupId = GetterUtil.getLong(tokens.get("group_id"));
101
102 for (Element el : root.elements()) {
103 Element dynamicContent = el.element("dynamic-content");
104
105 if (dynamicContent != null) {
106 String text = dynamicContent.getText();
107
108 text = HtmlUtil.stripComments(text);
109 text = HtmlUtil.stripHtml(text);
110 text = text.trim();
111
112
114 if (Validator.isNotNull(text) && text.length() >= 7 &&
115 text.startsWith("[@") && text.endsWith("@]")) {
116
117 text = text.substring(2, text.length() - 2);
118
119 int pos = text.indexOf(";");
120
121 if (pos != -1) {
122 String articleId = text.substring(0, pos);
123 String elementName =
124 text.substring(pos + 1, text.length());
125
126 JournalArticle article =
127 JournalArticleLocalServiceUtil.getArticle(
128 groupId, articleId);
129
130 dynamicContent.clearContent();
131 dynamicContent.addCDATA(
132 _getDynamicContent(
133 article.getContent(), elementName));
134 }
135 }
136
137
139 else if ((text != null) &&
140 (text.startsWith("/image/journal/article?img_id"))) {
141
142 dynamicContent.setText("@cdn_host@@root_path@" + text);
143 }
144 }
145
146 replace(el);
147 }
148 }
149
150 private String _getDynamicContent(String xml, String elementName) {
151 String content = null;
152
153 try {
154 Document doc = SAXReaderUtil.read(xml);
155
156 Element root = doc.getRootElement();
157
158 for (Element el : root.elements()) {
159 String elName = el.attributeValue("name", StringPool.BLANK);
160
161 if (elName.equals(elementName)) {
162 content = el.elementText("dynamic-content");
163
164 break;
165 }
166 }
167 }
168 catch (Exception e) {
169 _log.error(e, e);
170 }
171
172 return GetterUtil.getString(content);
173 }
174
175 private static Log _log =
176 LogFactoryUtil.getLog(ContentTransformerListener.class);
177
178 }