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.portal.kernel.util;
016    
017    /**
018     * @author Brian Wing Shun Chan
019     * @author Eduardo Garcia
020     */
021    public class RSSUtil {
022    
023            public static final String ATOM = "atom";
024    
025            public static final String DISPLAY_STYLE_ABSTRACT = "abstract";
026    
027            public static final String DISPLAY_STYLE_DEFAULT =
028                    _getDisplayStyleDefault();
029    
030            public static final String DISPLAY_STYLE_FULL_CONTENT = "full-content";
031    
032            public static final String DISPLAY_STYLE_TITLE = "title";
033    
034            public static final String[] DISPLAY_STYLES = new String[] {
035                    DISPLAY_STYLE_ABSTRACT, DISPLAY_STYLE_FULL_CONTENT, DISPLAY_STYLE_TITLE
036            };
037    
038            public static final String ENTRY_TYPE_DEFAULT = "html";
039    
040            public static final String FEED_TYPE_DEFAULT = _getFeedTypeDefault();
041    
042            public static final String[] FEED_TYPES = _getFeedTypes();
043    
044            public static final String FORMAT_DEFAULT = getFeedTypeFormat(
045                    FEED_TYPE_DEFAULT);
046    
047            public static final String RSS = "rss";
048    
049            public static final double VERSION_DEFAULT = getFeedTypeVersion(
050                    FEED_TYPE_DEFAULT);
051    
052            public static String getFeedType(String type, double version) {
053                    return type + StringPool.UNDERLINE + version;
054            }
055    
056            public static String getFeedTypeFormat(String feedType) {
057                    if (Validator.isNotNull(feedType)) {
058                            String[] parts = StringUtil.split(feedType, StringPool.UNDERLINE);
059    
060                            if (parts.length == 2) {
061                                    return GetterUtil.getString(parts[0], FORMAT_DEFAULT);
062                            }
063                    }
064    
065                    return FORMAT_DEFAULT;
066            }
067    
068            public static String getFeedTypeName(String feedType) {
069                    String type = getFeedTypeFormat(feedType);
070    
071                    if (type.equals(ATOM)) {
072                            type = "Atom";
073                    }
074                    else if (type.equals(RSS)) {
075                            type = "RSS";
076                    }
077    
078                    double version = getFeedTypeVersion(feedType);
079    
080                    return type + StringPool.SPACE + version;
081            }
082    
083            public static double getFeedTypeVersion(String feedType) {
084                    if (Validator.isNotNull(feedType)) {
085                            String[] parts = StringUtil.split(feedType, StringPool.UNDERLINE);
086    
087                            if (parts.length == 2) {
088                                    return GetterUtil.getDouble(parts[1], VERSION_DEFAULT);
089                            }
090                    }
091    
092                    return VERSION_DEFAULT;
093            }
094    
095            public static String getFormatType(String format) {
096                    if (format == null) {
097                            return FORMAT_DEFAULT;
098                    }
099    
100                    int x = format.indexOf(ATOM);
101    
102                    if (x >= 0) {
103                            return ATOM;
104                    }
105    
106                    int y = format.indexOf(RSS);
107    
108                    if (y >= 0) {
109                            return RSS;
110                    }
111    
112                    return FORMAT_DEFAULT;
113            }
114    
115            public static double getFormatVersion(String format) {
116                    if (format == null) {
117                            return VERSION_DEFAULT;
118                    }
119    
120                    int x = format.indexOf("10");
121    
122                    if (x >= 0) {
123                            return 1.0;
124                    }
125    
126                    int y = format.indexOf("20");
127    
128                    if (y >= 0) {
129                            return 2.0;
130                    }
131    
132                    return VERSION_DEFAULT;
133            }
134    
135            private static String _getDisplayStyleDefault() {
136                    return GetterUtil.getString(
137                            PropsUtil.get(PropsKeys.RSS_FEED_DISPLAY_STYLE_DEFAULT),
138                            DISPLAY_STYLE_FULL_CONTENT);
139            }
140    
141            private static String _getFeedTypeDefault() {
142                    return GetterUtil.getString(
143                            PropsUtil.get(PropsKeys.RSS_FEED_TYPE_DEFAULT),
144                            getFeedType(ATOM, 1.0));
145            }
146    
147            private static String[] _getFeedTypes() {
148                    return GetterUtil.getStringValues(
149                            PropsUtil.getArray(PropsKeys.RSS_FEED_TYPES),
150                            new String[] {FEED_TYPE_DEFAULT});
151            }
152    
153    }