001    /**
002     * Copyright (c) 2000-2011 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                    RSSThreadLocal.setExportRSS(true);
063    
064                    feed.setEncoding(StringPool.UTF8);
065    
066                    SyndFeedOutput output = new SyndFeedOutput();
067    
068                    try {
069                            return output.outputString(feed);
070                    }
071                    catch (IllegalDataException ide) {
072    
073                            // LEP-4450
074    
075                            _regexpStrip(feed);
076    
077                            return output.outputString(feed);
078                    }
079            }
080    
081            public static String getFeedType(String type, double version) {
082                    return type + StringPool.UNDERLINE + version;
083            }
084    
085            public static String getFormatType(String format) {
086                    String formatType = DEFAULT_TYPE;
087    
088                    if (StringUtil.contains(format, ATOM)) {
089                            formatType = RSSUtil.ATOM;
090                    }
091                    else if (StringUtil.contains(format, RSS)) {
092                            formatType = RSSUtil.RSS;
093                    }
094    
095                    return formatType;
096            }
097    
098            public static double getFormatVersion(String format) {
099                    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    }