View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.form.validator;
18  
19  import org.exoplatform.web.application.ApplicationMessage;
20  import org.exoplatform.webui.exception.MessageException;
21  import org.exoplatform.webui.form.UIFormInput;
22  import org.exoplatform.webui.form.validator.Validator;
23  
24  /**
25   * Created by The eXo Platform SAS
26   * Author : Ly Dinh Quang
27   *          quang.ly@exoplatform.com
28   *          xxx5669@yahoo.com
29   * Jul 9, 2008
30   */
31  public class SearchValidator implements Validator {
32    public void validate(UIFormInput uiInput) throws Exception {
33      String inputValue = ((String)uiInput.getValue());
34      if (inputValue == null || inputValue.trim().length() == 0) {
35        throwException("SearchValidator.msg.empty-input", uiInput);
36      }
37      inputValue = inputValue.trim();
38      switch (inputValue.length()) {
39        case 1:
40          checkOneChar(inputValue, uiInput);
41          break;
42        case 2:
43          checkTwoChars(inputValue, uiInput);
44          break;
45        default:
46          checkMoreChars(inputValue, uiInput);
47          break;
48      }
49    }
50  
51    private void checkOneChar(String s, UIFormInput uiInput) throws MessageException {
52      String[] arrFilterChars = {"+", "-", "&", "|", "!", "(", ")", "{", "}", "[", "]", "^", "\"",
53          "~", "*", "?", ":", "\\"};
54      if (checkArr(s, arrFilterChars)) {
55        throwException("SearchValidator.msg.Invalid-char", uiInput);
56      }
57    }
58  
59    private void checkTwoChars(String s, UIFormInput uiInput) throws MessageException {
60      String s2 = "";
61      if (s.startsWith("+") || s.startsWith("-") || s.startsWith("!")) {
62        s2 = s.substring(1, 2);
63        checkOneChar(s2, uiInput);
64      } else if (s.endsWith("~") || s.endsWith("?") || s.endsWith("*")) {
65        s2 = s.substring(0, 1);
66        String[] arrFilterChars1 = {"+", "-", "&", "|", "!", "(", ")", "{", "}", "[", "]", "^", "\"",
67            ":", "\\"};
68        if (checkArr(s2, arrFilterChars1)) {
69          throwException("SearchValidator.msg.Invalid-char", uiInput);
70        }
71      } else {
72        String s3 = s.substring(0, 1);
73        String s4 = s.substring(1, 2);
74  
75        String[] arrFilterChars2 = {"+", "-", "&", "|", "!", "(", ")", "{", "}", "[", "]", "^", "\"",
76            "~", "*", "?", ":", "\\"};
77        if (checkArr(s3, arrFilterChars2)) {
78          throwException("SearchValidator.msg.Invalid-char", uiInput);
79        }
80        if (checkArr(s4, arrFilterChars2)) {
81          throwException("SearchValidator.msg.Invalid-char", uiInput);
82        }
83      }
84    }
85  
86    private void checkMoreChars(String s, UIFormInput uiInput) throws MessageException {
87      String[] arrFilterChars = {"-", "&&", "||", "!", "(", ")", "}", "]", "^", ":", "&", "|"};
88      for (String filter : arrFilterChars) {
89        if (s.startsWith(filter)) { throwException("SearchValidator.msg.Invalid-char", uiInput); }
90      }
91      String[] arrFilterChars2 = {"+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"",
92          "~", "*", "?", ":", "\\", "&", "|"};
93      for (String filter : arrFilterChars2) {
94        int index = s.indexOf(filter);
95        if (index > -1 && !checkBackSlash(s, index)) {
96          //Check FuzzySearch
97          if (filter.equals("~")) {
98            if (index == 0) {
99              String regex = "~\\w+";
100             if (!s.matches(regex)) { throwException("SearchValidator.msg.Invalid-char", uiInput); }
101           } else {
102             if (checkChar(s, index, -1, " ") || checkChar(s, index, +1, " ")) {
103               throwException("SearchValidator.msg.Invalid-char4", uiInput);
104             } else if (checkChar(s, index, -1, "\"")) {
105               int x = s.indexOf("\"");
106               if (x > -1 && x != index - 1) {
107                 try {
108                   String subString = concatSpace(s.substring(index + 1, s.length()));
109                   Double.parseDouble(subString);
110                 } catch (Exception e) {
111                   throwException("SearchValidator.msg.Invalid-char2", uiInput);
112                 }
113               } else {
114                 throwException("SearchValidator.msg.Invalid-char", uiInput);
115               }
116             } else {
117                 String subString = concatSpace(s.substring(index + 1, s.length()));
118                 double numberAt = 0;
119                 try {
120                   numberAt = Double.parseDouble(subString);
121                 } catch (NumberFormatException e) {
122                   throwException("SearchValidator.msg.Invalid-char2", uiInput);
123                 }
124                 if (numberAt >= 1 || numberAt < 0) {
125                   throwException("SearchValidator.msg.Invalid-char1", uiInput);
126                 }
127             }
128           }
129         } else if (filter.equals("^")) {
130           if (checkChar(s, index, -1, " ") || checkChar(s, index, +1, " ")) {
131             throwException("SearchValidator.msg.Invalid-char5", uiInput);
132           } else {
133             String subString = concatSpace(s.substring(index + 1, s.length()));
134             try {
135               Double.parseDouble(subString);
136             } catch (NumberFormatException e) {
137               throwException("SearchValidator.msg.Invalid-char3", uiInput);
138             }
139           }
140         } else {
141           if (filter.equals("*") || filter.equals("?")) { return; }
142           throwException("SearchValidator.msg.Invalid-char", uiInput);
143 //        } else if (filter.equals("[") || filter.equals("]")) {
144 //          String regex = "\\w*\\[\\w+ [Tt][Oo] \\w+\\]\\w*";
145 //          if (!s.matches(regex)) {
146 //            throwException("SearchValidator.msg.Invalid-char", uiInput);
147 //          }
148         }
149       }
150     }
151   }
152 
153   private boolean checkChar(String s, int index, int forward, String c) {
154     if (index == 0 || (index + forward == s.length())) { return false; }
155     String charToString = String.valueOf(s.charAt(index + forward));
156     if (charToString.equals(c)) { return true; }
157     return false;
158   }
159 
160   private boolean checkBackSlash(String s, int index) {
161     if (index == 0) { return false; }
162     String charToString = String.valueOf(s.charAt(index - 1));
163     if (charToString.equalsIgnoreCase("\\")) { return true; }
164     return false;
165   }
166 
167   private boolean checkArr(String s, String[] arrFilterChars) {
168     for (String filter : arrFilterChars) {
169       if (s.equals(filter)) {
170         return true;
171       }
172     }
173     return false;
174   }
175 
176   private String concatSpace(String s) {
177     char[] arrayChar = s.toCharArray();
178     int index = 0;
179     for (int i = 0; i < arrayChar.length; i++) {
180       if (String.valueOf(arrayChar[i]).equals(" ")) {
181         index = i;
182         break;
183       }
184     }
185     if (index != 0) { return s.substring(0, index); }
186     return s;
187   }
188 
189   private void throwException(String s, UIFormInput uiInput) throws MessageException {
190     Object[] args = { uiInput.getName() };
191     throw new MessageException(new ApplicationMessage(s, args, ApplicationMessage.WARNING));
192   }
193 }