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