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