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