View Javadoc
1   /*
2    * #%L
3    * Nuiton Validator
4    * %%
5    * Copyright (C) 2013 - 2014 Code Lutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  package org.nuiton.validator.xwork2.field;
23  
24  import com.opensymphony.xwork2.validator.ValidationException;
25  
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  /**
30   * Validator for French cities names :
31   * - No number accepted
32   * - Accept spaces, - and '
33   *
34   * @author Jean Couteau - couteau@codelutin.com
35   * @since 2.3
36   */
37  public class FrenchCityNameFieldValidator extends NuitonFieldValidatorSupport {
38  
39      protected static String CITY_NAME_REGEXP = "[^\\d]+";
40  
41      protected static Pattern p = Pattern.compile(CITY_NAME_REGEXP);
42  
43      //TODO JC18082011 - Deal with cedex
44  
45      @Override
46      public void validateWhenNotSkip(Object object) throws ValidationException {
47  
48          String fieldName = getFieldName();
49          Object value = getFieldValue(fieldName, object);
50  
51          if (value == null) {
52              // no value defined
53              return;
54          }
55          if (value instanceof String) {
56              if ("".equals(value)) {
57                  // no value defined
58                  return;
59              }
60              Matcher m = p.matcher((String) value);
61              if (!m.matches()) {
62                  addFieldError(fieldName, object);
63              }
64          } else {
65              addFieldError(fieldName, object);
66          }
67      }
68  
69      @Override
70      public String getValidatorType() {
71          return "frenchCityName";
72      }
73  }