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.util.List;
35
36 import org.jdom.IllegalDataException;
37
38
43 public class RSSUtil {
44
45 public static final String RSS = "rss";
46
47 public static final double[] RSS_VERSIONS = new double[] {
48 0.9, 0.91, 0.93, 0.94, 1.0, 2.0
49 };
50
51 public static final String ATOM = "atom";
52
53 public static final double[] ATOM_VERSIONS = new double[] {0.3, 1.0};
54
55 public static final String DEFAULT_TYPE = ATOM;
56
57 public static final double DEFAULT_VERSION = 1.0;
58
59 public static final String DEFAULT_ENTRY_TYPE = "html";
60
61 public static final String DEFAULT_FEED_TYPE = getFeedType(
62 DEFAULT_TYPE, DEFAULT_VERSION);
63
64 public static final String DISPLAY_STYLE_ABSTRACT = "abstract";
65
66 public static final String DISPLAY_STYLE_FULL_CONTENT = "full-content";
67
68 public static final String DISPLAY_STYLE_TITLE = "title";
69
70 public static String export(SyndFeed feed) throws FeedException {
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
82 _regexpStrip(feed);
83
84 return output.outputString(feed);
85 }
86 }
87
88 public static String getFeedType(String type, double version) {
89 return type + StringPool.UNDERLINE + version;
90 }
91
92 private static void _regexpStrip(SyndFeed feed) {
93 feed.setTitle(_regexpStrip(feed.getTitle()));
94 feed.setDescription(_regexpStrip(feed.getDescription()));
95
96 List<SyndEntry> entries = feed.getEntries();
97
98 for (SyndEntry entry : entries) {
99 entry.setTitle(_regexpStrip(entry.getTitle()));
100
101 SyndContent content = entry.getDescription();
102
103 content.setValue(_regexpStrip(content.getValue()));
104 }
105 }
106
107 private static String _regexpStrip(String text) {
108 text = Normalizer.normalizeToAscii(text);
109
110 char[] array = text.toCharArray();
111
112 for (int i = 0; i < array.length; i++) {
113 String s = String.valueOf(array[i]);
114
115 if (!s.matches(_REGEXP_STRIP)) {
116 array[i] = CharPool.SPACE;
117 }
118 }
119
120 return new String(array);
121 }
122
123 private static final String _REGEXP_STRIP = "[\\d\\w]";
124
125 }