1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.journal.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.HtmlUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.kernel.xml.Document;
24  import com.liferay.portal.kernel.xml.Element;
25  import com.liferay.portal.kernel.xml.SAXReaderUtil;
26  import com.liferay.portlet.journal.model.JournalArticle;
27  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
28  
29  import java.util.Map;
30  
31  /**
32   * <a href="ContentTransformerListener.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class ContentTransformerListener extends TransformerListener {
37  
38      public String onXml(String s) {
39          if (_log.isDebugEnabled()) {
40              _log.debug("onXml");
41          }
42  
43          s = replace(s);
44  
45          return s;
46      }
47  
48      public String onScript(String s) {
49          if (_log.isDebugEnabled()) {
50              _log.debug("onScript");
51          }
52  
53          return s;
54      }
55  
56      public String onOutput(String s) {
57          if (_log.isDebugEnabled()) {
58              _log.debug("onOutput");
59          }
60  
61          return s;
62      }
63  
64      /**
65       * Fill one article with content from another approved article. See the
66       * article DOCUMENTATION-INSTALLATION-BORLAND for a sample use case.
67       *
68       * @return the processed string
69       */
70      protected String replace(String xml) {
71          try {
72              Document doc = SAXReaderUtil.read(xml);
73  
74              Element root = doc.getRootElement();
75  
76              replace(root);
77  
78              xml = JournalUtil.formatXML(doc);
79          }
80          catch (Exception e) {
81              _log.warn(e.getMessage());
82          }
83  
84          return xml;
85      }
86  
87      protected void replace(Element root) throws Exception {
88          Map<String, String> tokens = getTokens();
89  
90          long groupId = GetterUtil.getLong(tokens.get("group_id"));
91  
92          for (Element el : root.elements()) {
93              Element dynamicContent = el.element("dynamic-content");
94  
95              if (dynamicContent != null) {
96                  String text = dynamicContent.getText();
97  
98                  text = HtmlUtil.stripComments(text);
99                  text = HtmlUtil.stripHtml(text);
100                 text = text.trim();
101 
102                 // [@articleId;elementName@]
103 
104                 if (Validator.isNotNull(text) && text.length() >= 7 &&
105                     text.startsWith("[@") && text.endsWith("@]")) {
106 
107                     text = text.substring(2, text.length() - 2);
108 
109                     int pos = text.indexOf(";");
110 
111                     if (pos != -1) {
112                         String articleId = text.substring(0, pos);
113                         String elementName =
114                             text.substring(pos + 1, text.length());
115 
116                         JournalArticle article =
117                             JournalArticleLocalServiceUtil.getArticle(
118                                 groupId, articleId);
119 
120                         dynamicContent.clearContent();
121                         dynamicContent.addCDATA(
122                             _getDynamicContent(
123                                 article.getContent(), elementName));
124                     }
125                 }
126 
127                 // Make sure to point images to the full path
128 
129                 else if ((text != null) &&
130                          (text.startsWith("/image/journal/article?img_id"))) {
131 
132                     dynamicContent.setText("@cdn_host@@root_path@" + text);
133                 }
134             }
135 
136             replace(el);
137         }
138     }
139 
140     private String _getDynamicContent(String xml, String elementName) {
141         String content = null;
142 
143         try {
144             Document doc = SAXReaderUtil.read(xml);
145 
146             Element root = doc.getRootElement();
147 
148             for (Element el : root.elements()) {
149                 String elName = el.attributeValue("name", StringPool.BLANK);
150 
151                 if (elName.equals(elementName)) {
152                     content = el.elementText("dynamic-content");
153 
154                     break;
155                 }
156             }
157         }
158         catch (Exception e) {
159             _log.error(e, e);
160         }
161 
162         return GetterUtil.getString(content);
163     }
164 
165     private static Log _log = LogFactoryUtil.getLog(
166         ContentTransformerListener.class);
167 
168 }