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