1
14
15 package com.liferay.util;
16
17 import com.liferay.portal.kernel.util.CharPool;
18 import com.liferay.portal.kernel.util.StringPool;
19 import com.liferay.portal.kernel.util.StringUtil;
20
21 import com.sun.syndication.feed.synd.SyndContent;
22 import com.sun.syndication.feed.synd.SyndEntry;
23 import com.sun.syndication.feed.synd.SyndFeed;
24 import com.sun.syndication.io.FeedException;
25 import com.sun.syndication.io.SyndFeedOutput;
26
27 import java.util.List;
28
29 import org.jdom.IllegalDataException;
30
31
36 public class RSSUtil {
37
38 public static final String RSS = "rss";
39
40 public static final double[] RSS_VERSIONS = new double[] {
41 0.9, 0.91, 0.93, 0.94, 1.0, 2.0
42 };
43
44 public static final String ATOM = "atom";
45
46 public static final double[] ATOM_VERSIONS = new double[] {0.3, 1.0};
47
48 public static final String DEFAULT_TYPE = ATOM;
49
50 public static final double DEFAULT_VERSION = 1.0;
51
52 public static final String DEFAULT_ENTRY_TYPE = "html";
53
54 public static final String DEFAULT_FEED_TYPE = getFeedType(
55 DEFAULT_TYPE, DEFAULT_VERSION);
56
57 public static final String DISPLAY_STYLE_ABSTRACT = "abstract";
58
59 public static final String DISPLAY_STYLE_FULL_CONTENT = "full-content";
60
61 public static final String DISPLAY_STYLE_TITLE = "title";
62
63 public static String export(SyndFeed feed) throws FeedException {
64 feed.setEncoding(StringPool.UTF8);
65
66 SyndFeedOutput output = new SyndFeedOutput();
67
68 try {
69 return output.outputString(feed);
70 }
71 catch (IllegalDataException ide) {
72
73
75 _regexpStrip(feed);
76
77 return output.outputString(feed);
78 }
79 }
80
81 public static String getFeedType(String type, double version) {
82 return type + StringPool.UNDERLINE + version;
83 }
84
85 public static String getFormatType(String format) {
86 String formatType = DEFAULT_TYPE;
87
88 if (StringUtil.contains(format, ATOM)) {
89 formatType = RSSUtil.ATOM;
90 }
91 else if (StringUtil.contains(format, RSS)) {
92 formatType = RSSUtil.RSS;
93 }
94
95 return formatType;
96 }
97
98 public static double getFormatVersion(String format) {
99 double formatVersion = DEFAULT_VERSION;
100
101 if (StringUtil.contains(format, "10")) {
102 formatVersion = 1.0;
103 }
104 else if (StringUtil.contains(format, "20")) {
105 formatVersion = 2.0;
106 }
107
108 return formatVersion;
109 }
110
111 private static void _regexpStrip(SyndFeed feed) {
112 feed.setTitle(_regexpStrip(feed.getTitle()));
113 feed.setDescription(_regexpStrip(feed.getDescription()));
114
115 List<SyndEntry> entries = feed.getEntries();
116
117 for (SyndEntry entry : entries) {
118 entry.setTitle(_regexpStrip(entry.getTitle()));
119
120 SyndContent content = entry.getDescription();
121
122 content.setValue(_regexpStrip(content.getValue()));
123 }
124 }
125
126 private static String _regexpStrip(String text) {
127 text = Normalizer.normalizeToAscii(text);
128
129 char[] array = text.toCharArray();
130
131 for (int i = 0; i < array.length; i++) {
132 String s = String.valueOf(array[i]);
133
134 if (!s.matches(_REGEXP_STRIP)) {
135 array[i] = CharPool.SPACE;
136 }
137 }
138
139 return new String(array);
140 }
141
142 private static final String _REGEXP_STRIP = "[\\d\\w]";
143
144 }