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