001    /**
002     * Copyright (c) 2000-2010 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.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    }