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