1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util;
24  
25  import com.liferay.portal.kernel.util.CharPool;
26  import com.liferay.portal.kernel.util.StringPool;
27  
28  import com.sun.syndication.feed.synd.SyndContent;
29  import com.sun.syndication.feed.synd.SyndEntry;
30  import com.sun.syndication.feed.synd.SyndFeed;
31  import com.sun.syndication.io.FeedException;
32  import com.sun.syndication.io.SyndFeedOutput;
33  
34  import java.io.IOException;
35  
36  import java.util.List;
37  
38  import org.jdom.IllegalDataException;
39  
40  /**
41   * <a href="RSSUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class RSSUtil {
47  
48      public static final String RSS = "rss";
49  
50      public static final double[] RSS_VERSIONS = new double[] {
51          0.9, 0.91, 0.93, 0.94, 1.0, 2.0
52      };
53  
54      public static final String ATOM = "atom";
55  
56      public static final double[] ATOM_VERSIONS = new double[] {0.3, 1.0};
57  
58      public static final String DEFAULT_TYPE = ATOM;
59  
60      public static final double DEFAULT_VERSION = 1.0;
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)
69          throws FeedException, IOException {
70  
71          feed.setEncoding(StringPool.UTF8);
72  
73          SyndFeedOutput output = new SyndFeedOutput();
74  
75          try {
76              return output.outputString(feed);
77          }
78          catch (IllegalDataException ide) {
79  
80              // LEP-4450
81  
82              _regexpStrip(feed);
83  
84              return output.outputString(feed);
85          }
86      }
87  
88      private static void _regexpStrip(SyndFeed feed) {
89          feed.setTitle(_regexpStrip(feed.getTitle()));
90          feed.setDescription(_regexpStrip(feed.getDescription()));
91  
92          List<SyndEntry> entries = feed.getEntries();
93  
94          for (SyndEntry entry : entries) {
95              entry.setTitle(_regexpStrip(entry.getTitle()));
96  
97              SyndContent content = entry.getDescription();
98  
99              content.setValue(_regexpStrip(content.getValue()));
100         }
101     }
102 
103     private static String _regexpStrip(String text) {
104         text = Normalizer.normalizeToAscii(text);
105 
106         char[] array = text.toCharArray();
107 
108         for (int i = 0; i < array.length; i++) {
109             String s = String.valueOf(array[i]);
110 
111             if (!s.matches(_REGEXP_STRIP)) {
112                 array[i] = CharPool.SPACE;
113             }
114         }
115 
116         return new String(array);
117     }
118 
119     private static final String _REGEXP_STRIP = "[\\d\\w]";
120 
121 }