1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util;
16  
17  import com.liferay.portal.kernel.util.CharPool;
18  import com.liferay.portal.kernel.util.StringPool;
19  
20  import com.sun.syndication.feed.synd.SyndContent;
21  import com.sun.syndication.feed.synd.SyndEntry;
22  import com.sun.syndication.feed.synd.SyndFeed;
23  import com.sun.syndication.io.FeedException;
24  import com.sun.syndication.io.SyndFeedOutput;
25  
26  import java.util.List;
27  
28  import org.jdom.IllegalDataException;
29  
30  /**
31   * <a href="RSSUtil.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class RSSUtil {
36  
37      public static final String RSS = "rss";
38  
39      public static final double[] RSS_VERSIONS = new double[] {
40          0.9, 0.91, 0.93, 0.94, 1.0, 2.0
41      };
42  
43      public static final String ATOM = "atom";
44  
45      public static final double[] ATOM_VERSIONS = new double[] {0.3, 1.0};
46  
47      public static final String DEFAULT_TYPE = ATOM;
48  
49      public static final double DEFAULT_VERSION = 1.0;
50  
51      public static final String DEFAULT_ENTRY_TYPE = "html";
52  
53      public static final String DEFAULT_FEED_TYPE = getFeedType(
54          DEFAULT_TYPE, DEFAULT_VERSION);
55  
56      public static final String DISPLAY_STYLE_ABSTRACT = "abstract";
57  
58      public static final String DISPLAY_STYLE_FULL_CONTENT = "full-content";
59  
60      public static final String DISPLAY_STYLE_TITLE = "title";
61  
62      public static String export(SyndFeed feed) throws FeedException {
63          feed.setEncoding(StringPool.UTF8);
64  
65          SyndFeedOutput output = new SyndFeedOutput();
66  
67          try {
68              return output.outputString(feed);
69          }
70          catch (IllegalDataException ide) {
71  
72              // LEP-4450
73  
74              _regexpStrip(feed);
75  
76              return output.outputString(feed);
77          }
78      }
79  
80      public static String getFeedType(String type, double version) {
81          return type + StringPool.UNDERLINE + version;
82      }
83  
84      private static void _regexpStrip(SyndFeed feed) {
85          feed.setTitle(_regexpStrip(feed.getTitle()));
86          feed.setDescription(_regexpStrip(feed.getDescription()));
87  
88          List<SyndEntry> entries = feed.getEntries();
89  
90          for (SyndEntry entry : entries) {
91              entry.setTitle(_regexpStrip(entry.getTitle()));
92  
93              SyndContent content = entry.getDescription();
94  
95              content.setValue(_regexpStrip(content.getValue()));
96          }
97      }
98  
99      private static String _regexpStrip(String text) {
100         text = Normalizer.normalizeToAscii(text);
101 
102         char[] array = text.toCharArray();
103 
104         for (int i = 0; i < array.length; i++) {
105             String s = String.valueOf(array[i]);
106 
107             if (!s.matches(_REGEXP_STRIP)) {
108                 array[i] = CharPool.SPACE;
109             }
110         }
111 
112         return new String(array);
113     }
114 
115     private static final String _REGEXP_STRIP = "[\\d\\w]";
116 
117 }