001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import com.sun.syndication.feed.synd.SyndContent;
022    import com.sun.syndication.feed.synd.SyndEntry;
023    import com.sun.syndication.feed.synd.SyndFeed;
024    import com.sun.syndication.io.FeedException;
025    import com.sun.syndication.io.SyndFeedOutput;
026    
027    import java.util.List;
028    
029    import org.jdom.IllegalDataException;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class RSSUtil {
035    
036            public static final String RSS = "rss";
037    
038            public static final double[] RSS_VERSIONS = new double[] {
039                    0.9, 0.91, 0.93, 0.94, 1.0, 2.0
040            };
041    
042            public static final String ATOM = "atom";
043    
044            public static final double[] ATOM_VERSIONS = new double[] {0.3, 1.0};
045    
046            public static final String DEFAULT_TYPE = ATOM;
047    
048            public static final double DEFAULT_VERSION = 1.0;
049    
050            public static final String DEFAULT_ENTRY_TYPE = "html";
051    
052            public static final String DEFAULT_FEED_TYPE = getFeedType(
053                    DEFAULT_TYPE, DEFAULT_VERSION);
054    
055            public static final String DISPLAY_STYLE_ABSTRACT = "abstract";
056    
057            public static final String DISPLAY_STYLE_FULL_CONTENT = "full-content";
058    
059            public static final String DISPLAY_STYLE_TITLE = "title";
060    
061            public static String export(SyndFeed feed) throws FeedException {
062                    feed.setEncoding(StringPool.UTF8);
063    
064                    SyndFeedOutput output = new SyndFeedOutput();
065    
066                    try {
067                            return output.outputString(feed);
068                    }
069                    catch (IllegalDataException ide) {
070    
071                            // LEP-4450
072    
073                            _regexpStrip(feed);
074    
075                            return output.outputString(feed);
076                    }
077            }
078    
079            public static String getFeedType(String type, double version) {
080                    return type + StringPool.UNDERLINE + version;
081            }
082    
083            public static String getFormatType(String format) {
084                    String formatType = DEFAULT_TYPE;
085    
086                    if (StringUtil.contains(format, ATOM)) {
087                            formatType = RSSUtil.ATOM;
088                    }
089                    else if (StringUtil.contains(format, RSS)) {
090                            formatType = RSSUtil.RSS;
091                    }
092    
093                    return formatType;
094            }
095    
096            public static double getFormatVersion(String format) {
097                    double formatVersion = DEFAULT_VERSION;
098    
099                    if (StringUtil.contains(format, "10")) {
100                            formatVersion = 1.0;
101                    }
102                    else if (StringUtil.contains(format, "20")) {
103                            formatVersion = 2.0;
104                    }
105    
106                    return formatVersion;
107            }
108    
109            private static void _regexpStrip(SyndFeed feed) {
110                    feed.setTitle(_regexpStrip(feed.getTitle()));
111                    feed.setDescription(_regexpStrip(feed.getDescription()));
112    
113                    List<SyndEntry> entries = feed.getEntries();
114    
115                    for (SyndEntry entry : entries) {
116                            entry.setTitle(_regexpStrip(entry.getTitle()));
117    
118                            SyndContent content = entry.getDescription();
119    
120                            content.setValue(_regexpStrip(content.getValue()));
121                    }
122            }
123    
124            private static String _regexpStrip(String text) {
125                    text = Normalizer.normalizeToAscii(text);
126    
127                    char[] array = text.toCharArray();
128    
129                    for (int i = 0; i < array.length; i++) {
130                            String s = String.valueOf(array[i]);
131    
132                            if (!s.matches(_REGEXP_STRIP)) {
133                                    array[i] = CharPool.SPACE;
134                            }
135                    }
136    
137                    return new String(array);
138            }
139    
140            private static final String _REGEXP_STRIP = "[\\d\\w]";
141    
142    }