001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.CharPool;
024:
025: import com.sun.syndication.feed.synd.SyndContent;
026: import com.sun.syndication.feed.synd.SyndEntry;
027: import com.sun.syndication.feed.synd.SyndFeed;
028: import com.sun.syndication.io.FeedException;
029: import com.sun.syndication.io.SyndFeedOutput;
030:
031: import java.io.IOException;
032:
033: import java.util.List;
034:
035: import org.jdom.IllegalDataException;
036:
037: /**
038: * <a href="RSSUtil.java.html"><b><i>View Source</i></b></a>
039: *
040: * @author Brian Wing Shun Chan
041: *
042: */
043: public class RSSUtil {
044:
045: public static final String RSS = "rss";
046:
047: public static final double[] RSS_VERSIONS = new double[] { 0.9,
048: 0.91, 0.93, 0.94, 1.0, 2.0 };
049:
050: public static final String ATOM = "atom";
051:
052: public static final double[] ATOM_VERSIONS = new double[] { 0.3,
053: 1.0 };
054:
055: public static final String DEFAULT_TYPE = ATOM;
056:
057: public static final double DEFAULT_VERSION = 1.0;
058:
059: public static final String DISPLAY_STYLE_ABSTRACT = "abstract";
060:
061: public static final String DISPLAY_STYLE_FULL_CONTENT = "full-content";
062:
063: public static final String DISPLAY_STYLE_TITLE = "title";
064:
065: public static String export(SyndFeed feed) throws FeedException,
066: IOException {
067:
068: feed.setEncoding("UTF-8");
069:
070: SyndFeedOutput output = new SyndFeedOutput();
071:
072: try {
073: return output.outputString(feed);
074: } catch (IllegalDataException ide) {
075:
076: // LEP-4450
077:
078: _regexpStrip(feed);
079:
080: return output.outputString(feed);
081: }
082: }
083:
084: private static void _regexpStrip(SyndFeed feed) {
085: feed.setTitle(_regexpStrip(feed.getTitle()));
086: feed.setDescription(_regexpStrip(feed.getDescription()));
087:
088: List entries = feed.getEntries();
089:
090: for (int i = 0; i < entries.size(); i++) {
091: SyndEntry entry = (SyndEntry) entries.get(i);
092:
093: entry.setTitle(_regexpStrip(entry.getTitle()));
094:
095: SyndContent content = entry.getDescription();
096:
097: content.setValue(_regexpStrip(content.getValue()));
098: }
099: }
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: }
|