001    /**
002     * Copyright (c) 2000-2013 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    
020    import com.sun.syndication.feed.synd.SyndContent;
021    import com.sun.syndication.feed.synd.SyndEntry;
022    import com.sun.syndication.feed.synd.SyndFeed;
023    import com.sun.syndication.io.FeedException;
024    import com.sun.syndication.io.SyndFeedOutput;
025    
026    import java.util.List;
027    
028    import org.jdom.IllegalDataException;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class RSSUtil {
034    
035            public static final String ATOM = "atom";
036    
037            public static final double[] ATOM_VERSIONS = new double[] {0.3, 1.0};
038    
039            public static final String DISPLAY_STYLE_ABSTRACT = "abstract";
040    
041            public static final String DISPLAY_STYLE_FULL_CONTENT = "full-content";
042    
043            public static final String DISPLAY_STYLE_TITLE = "title";
044    
045            public static final String ENTRY_TYPE_DEFAULT = "html";
046    
047            public static final String FEED_TYPE_DEFAULT = getFeedType(
048                    RSSUtil.TYPE_DEFAULT, RSSUtil.VERSION_DEFAULT);
049    
050            public static final String RSS = "rss";
051    
052            public static final double[] RSS_VERSIONS = new double[] {
053                    0.9, 0.91, 0.93, 0.94, 1.0, 2.0
054            };
055    
056            public static final String TYPE_DEFAULT = ATOM;
057    
058            public static final double VERSION_DEFAULT = 1.0;
059    
060            public static String export(SyndFeed feed) throws FeedException {
061                    RSSThreadLocal.setExportRSS(true);
062    
063                    feed.setEncoding(StringPool.UTF8);
064    
065                    SyndFeedOutput output = new SyndFeedOutput();
066    
067                    try {
068                            return output.outputString(feed);
069                    }
070                    catch (IllegalDataException ide) {
071    
072                            // LEP-4450
073    
074                            _regexpStrip(feed);
075    
076                            return output.outputString(feed);
077                    }
078            }
079    
080            public static String getFeedType(String type, double version) {
081                    return type + StringPool.UNDERLINE + version;
082            }
083    
084            public static String getFormatType(String format) {
085                    if (format == null) {
086                            return TYPE_DEFAULT;
087                    }
088    
089                    int x = format.indexOf(ATOM);
090    
091                    if (x >= 0) {
092                            return ATOM;
093                    }
094    
095                    int y = format.indexOf(RSS);
096    
097                    if (y >= 0) {
098                            return RSS;
099                    }
100    
101                    return TYPE_DEFAULT;
102            }
103    
104            public static double getFormatVersion(String format) {
105                    if (format == null) {
106                            return VERSION_DEFAULT;
107                    }
108    
109                    int x = format.indexOf("10");
110    
111                    if (x >= 0) {
112                            return 1.0;
113                    }
114    
115                    int y = format.indexOf("20");
116    
117                    if (y >= 0) {
118                            return 2.0;
119                    }
120    
121                    return VERSION_DEFAULT;
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 void _regexpStrip(SyndFeed syndFeed) {
141                    syndFeed.setTitle(_regexpStrip(syndFeed.getTitle()));
142                    syndFeed.setDescription(_regexpStrip(syndFeed.getDescription()));
143    
144                    List<SyndEntry> syndEntries = syndFeed.getEntries();
145    
146                    for (SyndEntry syndEntry : syndEntries) {
147                            syndEntry.setTitle(_regexpStrip(syndEntry.getTitle()));
148    
149                            SyndContent syndContent = syndEntry.getDescription();
150    
151                            syndContent.setValue(_regexpStrip(syndContent.getValue()));
152                    }
153            }
154    
155            private static final String _REGEXP_STRIP = "[\\d\\w]";
156    
157    }