View Javadoc

1   /*
2    * MMBase Lucene module
3    *
4    * The contents of this file are subject to the Mozilla Public License
5    * Version 1.0 (the "License"); you may not use this file except in
6    * compliance with the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS"
10   * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11   * License for the specific language governing rights and limitations
12   * under the License.
13   */
14  package net.sf.mmapps.modules.lucenesearch.util;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  /***
20   * @author Wouter Heijke
21   * @version $Revision: 1.1 $
22   */
23  public class SearchValidator {
24      private static Log log = LogFactory.getLog(SearchValidator.class);
25  
26      private static final char DQUOTE = '\"';
27  
28      private static final char SPACE = ' ';
29  
30      private static final char BACKSLASH = '//';
31  
32      private static final String[] SPECIAL_CHARS = new String[] { "+", "-", "&&", "||", "!", "(", ")", "{", "}", "[",
33              "]", "^", "\"", "~", "*", "?", ":", "//" };
34  
35      private static final char[] ILLEGAL_START_CHARS = { '*', '?' };
36  
37      private static final char[] PAIRED_O_CHARS = { '(', '{', '[' };
38  
39      private static final char[] PAIRED_C_CHARS = { ')', '}', ']' };
40  
41      /***
42       * Checks if a String for some things Lucene expects as a good query string.
43       * 
44       * @param str the String to check, may be null
45       * @return true if the String is not empty and not null and not whitespace
46       */
47      public static boolean check(String str) {
48          int strLen;
49          // no space
50          str = str.trim();
51          // check if we have something at all
52          if (str == null || (strLen = str.length()) == 0) {
53              return false;
54          }
55  
56          // Checks if a String is not empty (""), not null and not whitespace only.
57          for (int i = 0; i < strLen; i++) {
58              if ((!Character.isWhitespace(str.charAt(i)))) {
59                  return true;
60              }
61          }
62          return false;
63      }
64  
65      /***
66       * Validate a string for use as a query
67       * 
68       * @param str The string to check
69       * @return Validated string
70       */
71      public static String validate(String str) {
72          str = str.trim();
73          str = fixStartChars(str);
74          str = fixLooseEndChars(str);
75          log.debug("validated str='" + str + "'");
76          return str;
77      }
78  
79      /***
80       * Check for illegal charackers at the beginning of the user's search string and remove them if found
81       * 
82       * @param str The string to check
83       * @return Validated string
84       */
85      private static String fixStartChars(String str) {
86          log.debug("fixStart str='" + str + "'");
87          StringBuffer buf = new StringBuffer(str);
88          for (int i = 0; i < ILLEGAL_START_CHARS.length; i++) {
89              try {
90                  if (buf.charAt(0) == ILLEGAL_START_CHARS[i]) {
91                      buf.deleteCharAt(0);
92                      log.debug("delete char='" + ILLEGAL_START_CHARS[i] + "'");
93                  }
94              } catch (StringIndexOutOfBoundsException e) {
95                  buf.append("");
96              }
97          }
98          log.debug("fixStart str='" + buf.toString() + "'");
99          return buf.toString();
100     }
101 
102     /***
103      * Check for special Lucene characters floating around at the end of the string and remove them if found
104      * 
105      * @param str The string to check
106      * @return Validated string
107      */
108     private static String fixLooseEndChars(String str) {
109         log.debug("fixEnd str='" + str + "'");
110         for (int i = 0; i < SPECIAL_CHARS.length; i++) {
111             if (str.endsWith(SPACE + SPECIAL_CHARS[i])) {
112                 str = str.substring(0, (str.length() - SPECIAL_CHARS[i].length())).trim();
113                 log.debug("deleted str='" + SPECIAL_CHARS[i] + "'");
114                 i = 0;
115             }
116         }
117         log.debug("fixEnd str='" + str + "'");
118         return str;
119     }
120 
121     /***
122      * Check and fix paired characters
123      * 
124      * @param str The string to check
125      * @return Validated string
126      */
127     private static String fixPairs(String str) {
128         // TODO add code here
129         return str;
130     }
131 
132     /***
133      * Count the occurance of characters
134      * 
135      * @param str The string to check
136      * @param c Character to check
137      * @return Amount of occurances of the character in the string
138      */
139     private static int occurance(String str, char c) {
140         int found = 0;
141         for (int i = 0; i < str.length(); i++) {
142             if (str.charAt(i) == c) {
143                 found++;
144             }
145         }
146         return found;
147     }
148 }