1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
43   * <a href="ContentTransformerListener.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
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      /**
77       * Fill one article with content from another approved article. See the
78       * article DOCUMENTATION-INSTALLATION-BORLAND for a sample use case.
79       *
80       * @param       xml the given string
81       * @return      the processed string
82       */
83      protected String replaceContent(String xml) {
84          try {
85              Document doc = DocumentUtil.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<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                 // [@articleId;elementName@]
116 
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                 // Make sure to point images to the full path
141 
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             replaceContent(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 }