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.ecm.webui.utils.Utils;
20  import org.exoplatform.web.application.ApplicationMessage;
21  import org.exoplatform.webui.exception.MessageException;
22  import org.exoplatform.webui.form.UIFormInput;
23  import org.exoplatform.webui.form.validator.Validator;
24  
25  /**
26   * Created by The eXo Platform SAS
27   * Author : Ly Dinh Quang
28   *          quang.ly@exoplatform.com
29   *      xxx5669@yahoo.com
30   * Mar 20, 2008
31   */
32  public class StandardNameValidator implements Validator {
33  
34    public void validate(UIFormInput uiInput) throws Exception {
35      String inputValue = ((String)uiInput.getValue()).trim();
36      if (inputValue == null || inputValue.length() == 0) {
37        throwException("ECMStandardPropertyNameValidator.msg.empty-input", uiInput);
38      }
39      switch (inputValue.length()) {
40      case 1:
41        checkOneChar(inputValue, uiInput);
42        break;
43      case 2:
44        checkTwoChars(inputValue, uiInput);
45      default:
46        checkMoreChars(inputValue, uiInput);
47        break;
48      }
49  
50    }
51  
52    /**
53     *
54     * @param s
55     * @param arrFilterChars
56     * @return
57     */
58    private boolean checkArr(String s, String[] arrFilterChars) {
59      for (String filter : arrFilterChars) {
60        if (s.equals(filter)) {
61          return true;
62        }
63      }
64      return false;
65    }
66  
67    /**
68     * Check String Input s if s.length() = 1
69     * @param s
70     * @param uiInput
71     * @throws MessageException
72     */
73    private void checkOneChar(String s, UIFormInput uiInput) throws MessageException {
74      if (checkArr(s, Utils.SPECIALCHARACTER)) {
75        throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
76      }
77    }
78  
79    /**
80     * Check String Input s if s.length() = 2
81     * @param s
82     * @param uiInput
83     */
84    private void checkTwoChars(String s, UIFormInput uiInput) throws MessageException {
85      String s2 = "";
86      if (s.startsWith(".")) {
87        s2 = s.substring(1, 2);
88        checkOneChar(s2, uiInput);
89      } else if (s.endsWith(".")) {
90        s2 = s.substring(0, 1);
91        checkOneChar(s2, uiInput);
92      } else {
93        String s3 = s.substring(0, 1);
94        String s4 = s.substring(1, 2);
95  
96        String[] arrFilterChars = {".", "/", ":", "[", "]", "*", "'", "|", "\""} ;
97        if (checkArr(s3, arrFilterChars)) {
98          throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
99        } else {
100         if (checkArr(s4, arrFilterChars)) {
101           throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
102         }
103       }
104     }
105   }
106 
107   /**
108    * Check String Input s if s.length() > 2
109    * @param s
110    * @param uiInput
111    */
112   private void checkMoreChars(String s, UIFormInput uiInput) throws MessageException {
113     //check nonspace start and end char
114     String[] arrFilterChars = {"/", ":", "[", "]", "*", "'", "|", "\""} ;
115     //get start and end char
116     String s1 = s.substring(0, 1);
117     String s2 = s.substring(s.length() - 1, s.length());
118     if (checkArr(s1, arrFilterChars)) {
119       throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
120     } else if (checkArr(s2, arrFilterChars)){
121       throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
122     } else {
123       String s3 = s.substring(1, s.length() - 1);
124       for(String filterChar : arrFilterChars) {
125         if(s3.indexOf(filterChar) > -1) {
126           throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
127         }
128           }
129     }
130   }
131 
132   private void throwException(String s, UIFormInput uiInput) throws MessageException {
133     Object[] args = { uiInput.getName() };
134     throw new MessageException(new ApplicationMessage(s, args, ApplicationMessage.WARNING));
135   }
136 
137 }