001    /**
002     * Copyright (c) 2000-2010 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.util.lucene;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     * @author Mirco Tamburini
023     * @author Josiah Goh
024     */
025    public class KeywordsUtil {
026    
027            public static final String[] SPECIAL = new String[] {
028                    "+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~",
029                    "*", "?", ":", "\\"
030            };
031    
032            public static String escape(String text) {
033                    for (int i = SPECIAL.length - 1; i >= 0; i--) {
034                            text = StringUtil.replace(
035                                    text, SPECIAL[i], StringPool.BACK_SLASH + SPECIAL[i]);
036                    }
037    
038                    return text;
039            }
040    
041            public static String toFuzzy(String keywords) {
042                    if (keywords == null) {
043                            return null;
044                    }
045    
046                    if (!keywords.endsWith(StringPool.TILDE)) {
047                            keywords = keywords + StringPool.TILDE;
048                    }
049    
050                    return keywords;
051            }
052    
053            public static String toWildcard(String keywords) {
054                    if (keywords == null) {
055                            return null;
056                    }
057    
058                    if (!keywords.endsWith(StringPool.STAR)) {
059                            keywords = keywords + StringPool.STAR;
060                    }
061    
062                    return keywords;
063            }
064    
065    }