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            /**
050             * @deprecated As of 6.2.0, replaced by {@link #FORMAT_DEFAULT}
051             */
052            @Deprecated
053            public static final String TYPE_DEFAULT = FORMAT_DEFAULT;
054    
055            public static final double VERSION_DEFAULT = getFeedTypeVersion(
056                    FEED_TYPE_DEFAULT);
057    
058            public static String getFeedType(String type, double version) {
059                    return type + StringPool.UNDERLINE + version;
060            }
061    
062            public static String getFeedTypeFormat(String feedType) {
063                    if (Validator.isNotNull(feedType)) {
064                            String[] parts = StringUtil.split(feedType, StringPool.UNDERLINE);
065    
066                            if (parts.length == 2) {
067                                    return GetterUtil.getString(parts[0], FORMAT_DEFAULT);
068                            }
069                    }
070    
071                    return FORMAT_DEFAULT;
072            }
073    
074            public static String getFeedTypeName(String feedType) {
075                    String type = getFeedTypeFormat(feedType);
076    
077                    if (type.equals(ATOM)) {
078                            type = "Atom";
079                    }
080                    else if (type.equals(RSS)) {
081                            type = "RSS";
082                    }
083    
084                    double version = getFeedTypeVersion(feedType);
085    
086                    return type + StringPool.SPACE + version;
087            }
088    
089            public static double getFeedTypeVersion(String feedType) {
090                    if (Validator.isNotNull(feedType)) {
091                            String[] parts = StringUtil.split(feedType, StringPool.UNDERLINE);
092    
093                            if (parts.length == 2) {
094                                    return GetterUtil.getDouble(parts[1], VERSION_DEFAULT);
095                            }
096                    }
097    
098                    return VERSION_DEFAULT;
099            }
100    
101            public static String getFormatType(String format) {
102                    if (format == null) {
103                            return FORMAT_DEFAULT;
104                    }
105    
106                    int x = format.indexOf(ATOM);
107    
108                    if (x >= 0) {
109                            return ATOM;
110                    }
111    
112                    int y = format.indexOf(RSS);
113    
114                    if (y >= 0) {
115                            return RSS;
116                    }
117    
118                    return FORMAT_DEFAULT;
119            }
120    
121            public static double getFormatVersion(String format) {
122                    if (format == null) {
123                            return VERSION_DEFAULT;
124                    }
125    
126                    int x = format.indexOf("10");
127    
128                    if (x >= 0) {
129                            return 1.0;
130                    }
131    
132                    int y = format.indexOf("20");
133    
134                    if (y >= 0) {
135                            return 2.0;
136                    }
137    
138                    return VERSION_DEFAULT;
139            }
140    
141            private static String _getDisplayStyleDefault() {
142                    return GetterUtil.getString(
143                            PropsUtil.get(PropsKeys.RSS_FEED_DISPLAY_STYLE_DEFAULT),
144                            DISPLAY_STYLE_FULL_CONTENT);
145            }
146    
147            private static String _getFeedTypeDefault() {
148                    return GetterUtil.getString(
149                            PropsUtil.get(PropsKeys.RSS_FEED_TYPE_DEFAULT),
150                            getFeedType(ATOM, 1.0));
151            }
152    
153            private static String[] _getFeedTypes() {
154                    return GetterUtil.getStringValues(
155                            PropsUtil.getArray(PropsKeys.RSS_FEED_TYPES),
156                            new String[] {FEED_TYPE_DEFAULT});
157            }
158    
159    }