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.StringPool;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portlet.journal.model.JournalArticle;
29 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
30 import com.liferay.util.Html;
31
32 import java.io.StringReader;
33
34 import java.util.Iterator;
35 import java.util.Map;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 import org.dom4j.Document;
41 import org.dom4j.Element;
42 import org.dom4j.io.SAXReader;
43
44
50 public class ContentTransformerListener extends TransformerListener {
51
52 public String onXml(String s) {
53 if (_log.isDebugEnabled()) {
54 _log.debug("onXml");
55 }
56
57 s = replaceContent(s);
58
59 return s;
60 }
61
62 public String onScript(String s) {
63 if (_log.isDebugEnabled()) {
64 _log.debug("onScript");
65 }
66
67 return s;
68 }
69
70 public String onOutput(String s) {
71 if (_log.isDebugEnabled()) {
72 _log.debug("onOutput");
73 }
74
75 return s;
76 }
77
78
85 protected String replaceContent(String xml) {
86 try {
87 SAXReader reader = new SAXReader();
88
89 Document doc = reader.read(new StringReader(xml));
90
91 Element root = doc.getRootElement();
92
93 replaceContent(root);
94
95 xml = JournalUtil.formatXML(doc);
96 }
97 catch (Exception e) {
98 _log.warn(e.getMessage());
99 }
100
101 return xml;
102 }
103
104 protected void replaceContent(Element root) throws Exception {
105 Map tokens = getTokens();
106
107 long groupId = GetterUtil.getLong((String)tokens.get("group_id"));
108
109 Iterator itr = root.elements().iterator();
110
111 while (itr.hasNext()) {
112 Element el = (Element)itr.next();
113
114 Element dynamicContent = el.element("dynamic-content");
115
116 if (dynamicContent != null) {
117 String text = dynamicContent.getText();
118
119 text = Html.stripComments(text);
120 text = Html.stripHtml(text);
121 text = text.trim();
122
123
125 if (Validator.isNotNull(text) && text.length() >= 7 &&
126 text.startsWith("[@") && text.endsWith("@]")) {
127
128 text = text.substring(2, text.length() - 2);
129
130 int pos = text.indexOf(";");
131
132 if (pos != -1) {
133 String articleId = text.substring(0, pos);
134 String elementName =
135 text.substring(pos + 1, text.length());
136
137 JournalArticle article =
138 JournalArticleLocalServiceUtil.getArticle(
139 groupId, articleId);
140
141 dynamicContent.setText(StringPool.BLANK);
142 dynamicContent.addCDATA(
143 _getDynamicContent(
144 article.getContent(), elementName));
145 }
146 }
147
148
150 else if ((text != null) &&
151 (text.startsWith("/image/journal/article?img_id"))) {
152
153 dynamicContent.setText("@cdn_host@@root_path@" + text);
154 }
155 }
156
157 replaceContent(el);
158 }
159 }
160
161 private String _getDynamicContent(String xml, String elementName) {
162 String content = null;
163
164 try {
165 SAXReader reader = new SAXReader();
166
167 Document doc = reader.read(new StringReader(xml));
168
169 Element root = doc.getRootElement();
170
171 Iterator itr = root.elements().iterator();
172
173 while (itr.hasNext()) {
174 Element el = (Element)itr.next();
175
176 String elName = el.attributeValue("name", StringPool.BLANK);
177
178 if (elName.equals(elementName)) {
179 content = el.elementText("dynamic-content");
180
181 break;
182 }
183 }
184 }
185 catch (Exception e) {
186 _log.error(e, e);
187 }
188
189 return GetterUtil.getString(content);
190 }
191
192 private static Log _log =
193 LogFactory.getLog(ContentTransformerListener.class);
194
195 }