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