001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    /**
018     * @author Samuel Kong
019     */
020    public class CSVUtil {
021    
022            public static String encode(Object obj) {
023                    return encode(String.valueOf(obj));
024            }
025    
026            public static String encode(String s) {
027                    if (s == null) {
028                            return null;
029                    }
030    
031                    if ((s.indexOf(CharPool.COMMA) < 0) &&
032                            (s.indexOf(CharPool.QUOTE) < 0) &&
033                            (s.indexOf(CharPool.NEW_LINE) < 0) &&
034                            (s.indexOf(CharPool.RETURN) < 0)) {
035    
036                            return s;
037                    }
038    
039                    s = StringUtil.replace(s, StringPool.QUOTE, StringPool.DOUBLE_QUOTE);
040    
041                    return StringPool.QUOTE.concat(s.concat(StringPool.QUOTE));
042            }
043    
044    }