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