1
22
23 package com.liferay.util;
24
25 import com.liferay.portal.kernel.util.CharPool;
26
27 import com.sun.syndication.feed.synd.SyndContent;
28 import com.sun.syndication.feed.synd.SyndEntry;
29 import com.sun.syndication.feed.synd.SyndFeed;
30 import com.sun.syndication.io.FeedException;
31 import com.sun.syndication.io.SyndFeedOutput;
32
33 import java.io.IOException;
34 import java.io.StringWriter;
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 String export(SyndFeed feed)
63 throws FeedException, IOException {
64
65 feed.setEncoding("UTF-8");
66
67 StringWriter writer = new StringWriter();
68
69 SyndFeedOutput output = new SyndFeedOutput();
70
71 try {
72 return output.outputString(feed);
73 }
74 catch (IllegalDataException ide) {
75
76
78 _regexpStrip(feed);
79
80 return output.outputString(feed);
81 }
82 }
83
84 private static void _regexpStrip(SyndFeed feed) {
85 feed.setTitle(_regexpStrip(feed.getTitle()));
86 feed.setDescription(_regexpStrip(feed.getDescription()));
87
88 List entries = feed.getEntries();
89
90 for (int i = 0; i < entries.size(); i++) {
91 SyndEntry entry = (SyndEntry)entries.get(i);
92
93 entry.setTitle(_regexpStrip(entry.getTitle()));
94
95 SyndContent content = entry.getDescription();
96
97 content.setValue(_regexpStrip(content.getValue()));
98 }
99 }
100
101 private static String _regexpStrip(String text) {
102 text = Normalizer.normalizeToAscii(text);
103
104 char[] array = text.toCharArray();
105
106 for (int i = 0; i < array.length; i++) {
107 String s = String.valueOf(array[i]);
108
109 if (!s.matches(_REGEXP_STRIP)) {
110 array[i] = CharPool.SPACE;
111 }
112 }
113
114 return new String(array);
115 }
116
117 private static final String _REGEXP_STRIP = "[\\d\\w]";
118
119 }