1
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
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 DEFAULT_ENTRY_TYPE = "html";
63
64 public static final String DEFAULT_FEED_TYPE = getFeedType(
65 DEFAULT_TYPE, DEFAULT_VERSION);
66
67 public static final String DISPLAY_STYLE_ABSTRACT = "abstract";
68
69 public static final String DISPLAY_STYLE_FULL_CONTENT = "full-content";
70
71 public static final String DISPLAY_STYLE_TITLE = "title";
72
73 public static String export(SyndFeed feed)
74 throws FeedException, IOException {
75
76 feed.setEncoding(StringPool.UTF8);
77
78 SyndFeedOutput output = new SyndFeedOutput();
79
80 try {
81 return output.outputString(feed);
82 }
83 catch (IllegalDataException ide) {
84
85
87 _regexpStrip(feed);
88
89 return output.outputString(feed);
90 }
91 }
92
93 public static String getFeedType(String type, double version) {
94 return type + StringPool.UNDERLINE + version;
95 }
96
97 private static void _regexpStrip(SyndFeed feed) {
98 feed.setTitle(_regexpStrip(feed.getTitle()));
99 feed.setDescription(_regexpStrip(feed.getDescription()));
100
101 List<SyndEntry> entries = feed.getEntries();
102
103 for (SyndEntry entry : entries) {
104 entry.setTitle(_regexpStrip(entry.getTitle()));
105
106 SyndContent content = entry.getDescription();
107
108 content.setValue(_regexpStrip(content.getValue()));
109 }
110 }
111
112 private static String _regexpStrip(String text) {
113 text = Normalizer.normalizeToAscii(text);
114
115 char[] array = text.toCharArray();
116
117 for (int i = 0; i < array.length; i++) {
118 String s = String.valueOf(array[i]);
119
120 if (!s.matches(_REGEXP_STRIP)) {
121 array[i] = CharPool.SPACE;
122 }
123 }
124
125 return new String(array);
126 }
127
128 private static final String _REGEXP_STRIP = "[\\d\\w]";
129
130 }