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.util.DocumentUtil;
30 import com.liferay.portlet.journal.model.JournalArticle;
31 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
32
33 import java.util.List;
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 = replace(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 replace(String xml) {
84 try {
85 Document doc = DocumentUtil.readDocumentFromXML(xml);
86
87 Element root = doc.getRootElement();
88
89 replace(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 replace(Element root) throws Exception {
101 Map<String, String> tokens = getTokens();
102
103 long groupId = GetterUtil.getLong(tokens.get("group_id"));
104
105 for (Element el : (List<Element>)root.elements()) {
106 Element dynamicContent = el.element("dynamic-content");
107
108 if (dynamicContent != null) {
109 String text = dynamicContent.getText();
110
111 text = HtmlUtil.stripComments(text);
112 text = HtmlUtil.stripHtml(text);
113 text = text.trim();
114
115
117 if (Validator.isNotNull(text) && text.length() >= 7 &&
118 text.startsWith("[@") && text.endsWith("@]")) {
119
120 text = text.substring(2, text.length() - 2);
121
122 int pos = text.indexOf(";");
123
124 if (pos != -1) {
125 String articleId = text.substring(0, pos);
126 String elementName =
127 text.substring(pos + 1, text.length());
128
129 JournalArticle article =
130 JournalArticleLocalServiceUtil.getArticle(
131 groupId, articleId);
132
133 dynamicContent.clearContent();
134 dynamicContent.addCDATA(
135 _getDynamicContent(
136 article.getContent(), elementName));
137 }
138 }
139
140
142 else if ((text != null) &&
143 (text.startsWith("/image/journal/article?img_id"))) {
144
145 dynamicContent.setText("@cdn_host@@root_path@" + text);
146 }
147 }
148
149 replace(el);
150 }
151 }
152
153 private String _getDynamicContent(String xml, String elementName) {
154 String content = null;
155
156 try {
157 Document doc = DocumentUtil.readDocumentFromXML(xml);
158
159 Element root = doc.getRootElement();
160
161 for (Element el : (List<Element>)root.elements()) {
162 String elName = el.attributeValue("name", StringPool.BLANK);
163
164 if (elName.equals(elementName)) {
165 content = el.elementText("dynamic-content");
166
167 break;
168 }
169 }
170 }
171 catch (Exception e) {
172 _log.error(e, e);
173 }
174
175 return GetterUtil.getString(content);
176 }
177
178 private static Log _log =
179 LogFactory.getLog(ContentTransformerListener.class);
180
181 }