1   /**
2    * Copyright (c) 2000-2007 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.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  /**
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 = 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                 // [@articleId;elementName@]
120 
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                 // Make sure to point images to the full path
145 
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 }