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