1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }