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