001    /**
002     * Copyright (c) 2000-present 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     * @author Eduardo Garcia
033     */
034    public class RSSUtil extends com.liferay.portal.kernel.util.RSSUtil {
035    
036            public static String export(SyndFeed feed) throws FeedException {
037                    RSSThreadLocal.setExportRSS(true);
038    
039                    feed.setEncoding(StringPool.UTF8);
040    
041                    SyndFeedOutput output = new SyndFeedOutput();
042    
043                    try {
044                            return output.outputString(feed);
045                    }
046                    catch (IllegalDataException ide) {
047    
048                            // LEP-4450
049    
050                            _regexpStrip(feed);
051    
052                            return output.outputString(feed);
053                    }
054            }
055    
056            private static String _regexpStrip(String text) {
057                    text = Normalizer.normalizeToAscii(text);
058    
059                    char[] array = text.toCharArray();
060    
061                    for (int i = 0; i < array.length; i++) {
062                            String s = String.valueOf(array[i]);
063    
064                            if (!s.matches(_REGEXP_STRIP)) {
065                                    array[i] = CharPool.SPACE;
066                            }
067                    }
068    
069                    return new String(array);
070            }
071    
072            private static void _regexpStrip(SyndFeed syndFeed) {
073                    syndFeed.setTitle(_regexpStrip(syndFeed.getTitle()));
074                    syndFeed.setDescription(_regexpStrip(syndFeed.getDescription()));
075    
076                    List<SyndEntry> syndEntries = syndFeed.getEntries();
077    
078                    for (SyndEntry syndEntry : syndEntries) {
079                            syndEntry.setTitle(_regexpStrip(syndEntry.getTitle()));
080    
081                            SyndContent syndContent = syndEntry.getDescription();
082    
083                            syndContent.setValue(_regexpStrip(syndContent.getValue()));
084                    }
085            }
086    
087            private static final String _REGEXP_STRIP = "[\\d\\w]";
088    
089    }