001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
037     * @author Brian Wing Shun Chan
038     * @author Tina Tian
039     */
040    public class ContentTransformerListener extends BaseTransformerListener {
041    
042            @Override
043            public String onScript(
044                    String script, String xml, String languageId,
045                    Map<String, String> tokens) {
046    
047                    if (_log.isDebugEnabled()) {
048                            _log.debug("onScript");
049                    }
050    
051                    return injectEditInPlace(xml, script);
052            }
053    
054            @Override
055            public String onXml(
056                    String xml, String languageId, Map<String, String> tokens) {
057    
058                    if (_log.isDebugEnabled()) {
059                            _log.debug("onXml");
060                    }
061    
062                    return replace(xml, tokens);
063            }
064    
065            protected String getDynamicContent(String xml, String elementName) {
066                    String content = null;
067    
068                    try {
069                            Document document = SAXReaderUtil.read(xml);
070    
071                            Element rootElement = document.getRootElement();
072    
073                            for (Element element : rootElement.elements()) {
074                                    String curElementName = element.attributeValue(
075                                            "name", StringPool.BLANK);
076    
077                                    if (curElementName.equals(elementName)) {
078                                            content = element.elementText("dynamic-content");
079    
080                                            break;
081                                    }
082                            }
083                    }
084                    catch (Exception e) {
085                            _log.error(e, e);
086                    }
087    
088                    return GetterUtil.getString(content);
089            }
090    
091            protected String injectEditInPlace(String xml, String script) {
092                    try {
093                            Document document = SAXReaderUtil.read(xml);
094    
095                            List<Node> nodes = document.selectNodes("//dynamic-element");
096    
097                            for (Node node : nodes) {
098                                    Element element = (Element)node;
099    
100                                    String name = GetterUtil.getString(
101                                            element.attributeValue("name"));
102                                    String type = GetterUtil.getString(
103                                            element.attributeValue("type"));
104    
105                                    if (!name.startsWith("reserved-") &&
106                                            (type.equals("text") || type.equals("text_area") ||
107                                             type.equals("text_box"))) {
108    
109                                            script = wrapEditInPlaceField(script, name, type, "data");
110                                            script = wrapEditInPlaceField(
111                                                    script, name, type, "getData()");
112                                    }
113                            }
114                    }
115                    catch (Exception e) {
116                            if (_log.isWarnEnabled()) {
117                                    _log.warn(e.getMessage());
118                            }
119                    }
120    
121                    return script;
122            }
123    
124            protected void replace(Element root, Map<String, String> tokens)
125                    throws Exception {
126    
127                    long articleGroupId = GetterUtil.getLong(
128                            tokens.get("article_group_id"));
129    
130                    for (Element el : root.elements()) {
131                            Element dynamicContent = el.element("dynamic-content");
132    
133                            if (dynamicContent != null) {
134                                    String text = dynamicContent.getText();
135    
136                                    text = HtmlUtil.stripComments(text);
137                                    text = HtmlUtil.stripHtml(text);
138                                    text = text.trim();
139    
140                                    // [@articleId;elementName@]
141    
142                                    if (Validator.isNotNull(text) && (text.length() >= 7) &&
143                                            text.startsWith("[@") && text.endsWith("@]")) {
144    
145                                            text = text.substring(2, text.length() - 2);
146    
147                                            int pos = text.indexOf(";");
148    
149                                            if (pos != -1) {
150                                                    String articleId = text.substring(0, pos);
151                                                    String elementName = text.substring(pos + 1);
152    
153                                                    JournalArticle article =
154                                                            JournalArticleLocalServiceUtil.getArticle(
155                                                                    articleGroupId, articleId);
156    
157                                                    dynamicContent.clearContent();
158                                                    dynamicContent.addCDATA(
159                                                            getDynamicContent(
160                                                                    article.getContent(), elementName));
161                                            }
162                                    }
163    
164                                    // Make sure to point images to the full path
165    
166                                    else if ((text != null) &&
167                                                     text.startsWith("/image/journal/article?img_id")) {
168    
169                                            dynamicContent.setText("@cdn_host@@root_path@" + text);
170                                    }
171                            }
172    
173                            replace(el, tokens);
174                    }
175            }
176    
177            /**
178             * Fill one article with content from another approved article. See the
179             * article DOCUMENTATION-INSTALLATION-BORLAND for a sample use case.
180             *
181             * @return the processed string
182             */
183            protected String replace(String xml, Map<String, String> tokens) {
184                    try {
185                            Document document = SAXReaderUtil.read(xml);
186    
187                            Element rootElement = document.getRootElement();
188    
189                            replace(rootElement, tokens);
190    
191                            xml = DDMXMLUtil.formatXML(document);
192                    }
193                    catch (Exception e) {
194                            if (_log.isWarnEnabled()) {
195                                    _log.warn(e.getMessage());
196                            }
197                    }
198    
199                    return xml;
200            }
201    
202            protected String wrapEditInPlaceField(
203                    String script, String name, String type, String call) {
204    
205                    String field = "$" + name + "." + call;
206                    String wrappedField =
207                            "<span class=\"journal-content-eip-" + type + "\" " +
208                                    "id=\"journal-content-field-name-" + name + "\">" + field +
209                                            "</span>";
210    
211                    return StringUtil.replace(
212                            script, "$editInPlace(" + field + ")", wrappedField);
213            }
214    
215            private static Log _log = LogFactoryUtil.getLog(
216                    ContentTransformerListener.class);
217    
218    }