001
014
015 package com.liferay.portlet.journal.util;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.templateparser.BaseTransformerListener;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.HtmlUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.kernel.xml.Document;
026 import com.liferay.portal.kernel.xml.Element;
027 import com.liferay.portal.kernel.xml.Node;
028 import com.liferay.portal.kernel.xml.SAXReaderUtil;
029 import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
030 import com.liferay.portlet.journal.model.JournalArticle;
031 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
032
033 import java.util.List;
034 import java.util.Map;
035
036
039 public class ContentTransformerListener extends BaseTransformerListener {
040
041 @Override
042 public String onOutput(String s) {
043 if (_log.isDebugEnabled()) {
044 _log.debug("onOutput");
045 }
046
047 return s;
048 }
049
050 @Override
051 public String onScript(String s) {
052 if (_log.isDebugEnabled()) {
053 _log.debug("onScript");
054 }
055
056 s = injectEditInPlace(_xml, s);
057
058 return s;
059 }
060
061 @Override
062 public String onXml(String s) {
063 if (_log.isDebugEnabled()) {
064 _log.debug("onXml");
065 }
066
067 _xml = replace(s);
068
069 return _xml;
070 }
071
072 protected String getDynamicContent(String xml, String elementName) {
073 String content = null;
074
075 try {
076 Document document = SAXReaderUtil.read(xml);
077
078 Element rootElement = document.getRootElement();
079
080 for (Element element : rootElement.elements()) {
081 String curElementName = element.attributeValue(
082 "name", StringPool.BLANK);
083
084 if (curElementName.equals(elementName)) {
085 content = element.elementText("dynamic-content");
086
087 break;
088 }
089 }
090 }
091 catch (Exception e) {
092 _log.error(e, e);
093 }
094
095 return GetterUtil.getString(content);
096 }
097
098 protected String injectEditInPlace(String xml, String script) {
099 try {
100 Document document = SAXReaderUtil.read(xml);
101
102 List<Node> nodes = document.selectNodes("
103
104 for (Node node : nodes) {
105 Element element = (Element)node;
106
107 String name = GetterUtil.getString(
108 element.attributeValue("name"));
109 String type = GetterUtil.getString(
110 element.attributeValue("type"));
111
112 if ((!name.startsWith("reserved-")) &&
113 (type.equals("text") || type.equals("text_area") ||
114 type.equals("text_box"))) {
115
116 script = wrapEditInPlaceField(script, name, type, "data");
117 script = wrapEditInPlaceField(
118 script, name, type, "getData()");
119 }
120 }
121 }
122 catch (Exception e) {
123 _log.warn(e.getMessage());
124 }
125
126 return script;
127 }
128
129 protected void replace(Element root) throws Exception {
130 Map<String, String> tokens = getTokens();
131
132 long groupId = GetterUtil.getLong(tokens.get("group_id"));
133
134 for (Element el : root.elements()) {
135 Element dynamicContent = el.element("dynamic-content");
136
137 if (dynamicContent != null) {
138 String text = dynamicContent.getText();
139
140 text = HtmlUtil.stripComments(text);
141 text = HtmlUtil.stripHtml(text);
142 text = text.trim();
143
144
145
146 if (Validator.isNotNull(text) && text.length() >= 7 &&
147 text.startsWith("[@") && text.endsWith("@]")) {
148
149 text = text.substring(2, text.length() - 2);
150
151 int pos = text.indexOf(";");
152
153 if (pos != -1) {
154 String articleId = text.substring(0, pos);
155 String elementName = text.substring(
156 pos + 1, text.length());
157
158 JournalArticle article =
159 JournalArticleLocalServiceUtil.getArticle(
160 groupId, articleId);
161
162 dynamicContent.clearContent();
163 dynamicContent.addCDATA(
164 getDynamicContent(
165 article.getContent(), elementName));
166 }
167 }
168
169
170
171 else if ((text != null) &&
172 (text.startsWith("/image/journal/article?img_id"))) {
173
174 dynamicContent.setText("@cdn_host@@root_path@" + text);
175 }
176 }
177
178 replace(el);
179 }
180 }
181
182
188 protected String replace(String xml) {
189 try {
190 Document document = SAXReaderUtil.read(xml);
191
192 Element rootElement = document.getRootElement();
193
194 replace(rootElement);
195
196 xml = DDMXMLUtil.formatXML(document);
197 }
198 catch (Exception e) {
199 _log.warn(e.getMessage());
200 }
201
202 return xml;
203 }
204
205 protected String wrapEditInPlaceField(
206 String script, String name, String type, String call) {
207
208 String field = "$" + name + "." + call;
209 String wrappedField =
210 "<span class=\"journal-content-eip-" + type + "\" " +
211 "id=\"journal-content-field-name-" + name + "\">" + field +
212 "</span>";
213
214 return StringUtil.replace(
215 script, "$editInPlace(" + field + ")", wrappedField);
216 }
217
218 private static Log _log = LogFactoryUtil.getLog(
219 ContentTransformerListener.class);
220
221 private String _xml;
222
223 }