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