1
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
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
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 }