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.portlet.journal.model.JournalArticle;
29  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
30  import com.liferay.util.Html;
31  
32  import java.io.StringReader;
33  
34  import java.util.Iterator;
35  import java.util.Map;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  import org.dom4j.Document;
41  import org.dom4j.Element;
42  import org.dom4j.io.SAXReader;
43  
44  /**
45   * <a href="ContentTransformerListener.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class ContentTransformerListener extends TransformerListener {
51  
52      public String onXml(String s) {
53          if (_log.isDebugEnabled()) {
54              _log.debug("onXml");
55          }
56  
57          s = replaceContent(s);
58  
59          return s;
60      }
61  
62      public String onScript(String s) {
63          if (_log.isDebugEnabled()) {
64              _log.debug("onScript");
65          }
66  
67          return s;
68      }
69  
70      public String onOutput(String s) {
71          if (_log.isDebugEnabled()) {
72              _log.debug("onOutput");
73          }
74  
75          return s;
76      }
77  
78      /**
79       * Fill one article with content from another approved article. See the
80       * article DOCUMENTATION-INSTALLATION-BORLAND for a sample use case.
81       *
82       * @param       xml the given string
83       * @return      the processed string
84       */
85      protected String replaceContent(String xml) {
86          try {
87              SAXReader reader = new SAXReader();
88  
89              Document doc = reader.read(new StringReader(xml));
90  
91              Element root = doc.getRootElement();
92  
93              replaceContent(root);
94  
95              xml = JournalUtil.formatXML(doc);
96          }
97          catch (Exception e) {
98              _log.warn(e.getMessage());
99          }
100 
101         return xml;
102     }
103 
104     protected void replaceContent(Element root) throws Exception {
105         Map tokens = getTokens();
106 
107         long groupId = GetterUtil.getLong((String)tokens.get("group_id"));
108 
109         Iterator itr = root.elements().iterator();
110 
111         while (itr.hasNext()) {
112             Element el = (Element)itr.next();
113 
114             Element dynamicContent = el.element("dynamic-content");
115 
116             if (dynamicContent != null) {
117                 String text = dynamicContent.getText();
118 
119                 text = Html.stripComments(text);
120                 text = Html.stripHtml(text);
121                 text = text.trim();
122 
123                 // [@articleId;elementName@]
124 
125                 if (Validator.isNotNull(text) && text.length() >= 7 &&
126                     text.startsWith("[@") && text.endsWith("@]")) {
127 
128                     text = text.substring(2, text.length() - 2);
129 
130                     int pos = text.indexOf(";");
131 
132                     if (pos != -1) {
133                         String articleId = text.substring(0, pos);
134                         String elementName =
135                             text.substring(pos + 1, text.length());
136 
137                         JournalArticle article =
138                             JournalArticleLocalServiceUtil.getArticle(
139                                 groupId, articleId);
140 
141                         dynamicContent.setText(StringPool.BLANK);
142                         dynamicContent.addCDATA(
143                             _getDynamicContent(
144                                 article.getContent(), elementName));
145                     }
146                 }
147 
148                 // Make sure to point images to the full path
149 
150                 else if ((text != null) &&
151                          (text.startsWith("/image/journal/article?img_id"))) {
152 
153                     dynamicContent.setText("@cdn_host@@root_path@" + text);
154                 }
155             }
156 
157             replaceContent(el);
158         }
159     }
160 
161     private String _getDynamicContent(String xml, String elementName) {
162         String content = null;
163 
164         try {
165             SAXReader reader = new SAXReader();
166 
167             Document doc = reader.read(new StringReader(xml));
168 
169             Element root = doc.getRootElement();
170 
171             Iterator itr = root.elements().iterator();
172 
173             while (itr.hasNext()) {
174                 Element el = (Element)itr.next();
175 
176                 String elName = el.attributeValue("name", StringPool.BLANK);
177 
178                 if (elName.equals(elementName)) {
179                     content = el.elementText("dynamic-content");
180 
181                     break;
182                 }
183             }
184         }
185         catch (Exception e) {
186             _log.error(e, e);
187         }
188 
189         return GetterUtil.getString(content);
190     }
191 
192     private static Log _log =
193         LogFactory.getLog(ContentTransformerListener.class);
194 
195 }