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.util;
21  
22  import com.liferay.portal.kernel.util.CharPool;
23  import com.liferay.portal.kernel.util.StringPool;
24  
25  import com.sun.syndication.feed.synd.SyndContent;
26  import com.sun.syndication.feed.synd.SyndEntry;
27  import com.sun.syndication.feed.synd.SyndFeed;
28  import com.sun.syndication.io.FeedException;
29  import com.sun.syndication.io.SyndFeedOutput;
30  
31  import java.util.List;
32  
33  import org.jdom.IllegalDataException;
34  
35  /**
36   * <a href="RSSUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class RSSUtil {
42  
43      public static final String RSS = "rss";
44  
45      public static final double[] RSS_VERSIONS = new double[] {
46          0.9, 0.91, 0.93, 0.94, 1.0, 2.0
47      };
48  
49      public static final String ATOM = "atom";
50  
51      public static final double[] ATOM_VERSIONS = new double[] {0.3, 1.0};
52  
53      public static final String DEFAULT_TYPE = ATOM;
54  
55      public static final double DEFAULT_VERSION = 1.0;
56  
57      public static final String DEFAULT_ENTRY_TYPE = "html";
58  
59      public static final String DEFAULT_FEED_TYPE = getFeedType(
60          DEFAULT_TYPE, DEFAULT_VERSION);
61  
62      public static final String DISPLAY_STYLE_ABSTRACT = "abstract";
63  
64      public static final String DISPLAY_STYLE_FULL_CONTENT = "full-content";
65  
66      public static final String DISPLAY_STYLE_TITLE = "title";
67  
68      public static String export(SyndFeed feed) throws FeedException {
69          feed.setEncoding(StringPool.UTF8);
70  
71          SyndFeedOutput output = new SyndFeedOutput();
72  
73          try {
74              return output.outputString(feed);
75          }
76          catch (IllegalDataException ide) {
77  
78              // LEP-4450
79  
80              _regexpStrip(feed);
81  
82              return output.outputString(feed);
83          }
84      }
85  
86      public static String getFeedType(String type, double version) {
87          return type + StringPool.UNDERLINE + version;
88      }
89  
90      private static void _regexpStrip(SyndFeed feed) {
91          feed.setTitle(_regexpStrip(feed.getTitle()));
92          feed.setDescription(_regexpStrip(feed.getDescription()));
93  
94          List<SyndEntry> entries = feed.getEntries();
95  
96          for (SyndEntry entry : entries) {
97              entry.setTitle(_regexpStrip(entry.getTitle()));
98  
99              SyndContent content = entry.getDescription();
100 
101             content.setValue(_regexpStrip(content.getValue()));
102         }
103     }
104 
105     private static String _regexpStrip(String text) {
106         text = Normalizer.normalizeToAscii(text);
107 
108         char[] array = text.toCharArray();
109 
110         for (int i = 0; i < array.length; i++) {
111             String s = String.valueOf(array[i]);
112 
113             if (!s.matches(_REGEXP_STRIP)) {
114                 array[i] = CharPool.SPACE;
115             }
116         }
117 
118         return new String(array);
119     }
120 
121     private static final String _REGEXP_STRIP = "[\\d\\w]";
122 
123 }