1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.lucene;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  import com.liferay.portal.kernel.util.StringUtil;
19  
20  /**
21   * <a href="KeywordsUtil.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Brian Wing Shun Chan
24   * @author Mirco Tamburini
25   * @author Josiah Goh
26   */
27  public class KeywordsUtil {
28  
29      public static final String[] SPECIAL = new String[] {
30          "+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~",
31          "*", "?", ":", "\\"
32      };
33  
34      public static String escape(String text) {
35          for (int i = SPECIAL.length - 1; i >= 0; i--) {
36              text = StringUtil.replace(
37                  text, SPECIAL[i], StringPool.BACK_SLASH + SPECIAL[i]);
38          }
39  
40          return text;
41      }
42  
43      public static String toFuzzy(String keywords) {
44          if (keywords == null) {
45              return null;
46          }
47  
48          if (!keywords.endsWith(StringPool.TILDE)) {
49              keywords = keywords + StringPool.TILDE;
50          }
51  
52          return keywords;
53      }
54  
55      public static String toWildcard(String keywords) {
56          if (keywords == null) {
57              return null;
58          }
59  
60          if (!keywords.endsWith(StringPool.STAR)) {
61              keywords = keywords + StringPool.STAR;
62          }
63  
64          return keywords;
65      }
66  
67  }