01: /*
02: * Copyright 2004-2006 Fouad HAMDI with the idea
03: * of SameLAN, S.L. Soluciones Tecnológicas.
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.csvbeans.samples.validators;
18:
19: import org.csvbeans.Property;
20: import org.csvbeans.exceptions.ValidationException;
21: import org.csvbeans.validators.FieldValidator;
22:
23: /**
24: * Dummy country validator. Work with a property named continent.
25: *
26: * @author Fouad Hamdi
27: * @since 0.7
28: */
29: public class CountryValidator extends FieldValidator {
30:
31: public void validate(String value) throws ValidationException {
32: Property continent = getProperty("continent");
33: if (continent != null) {
34: String continentValue = (String) continent.getValue();
35: if (ZipCodeValidator.EUROPE.equals(continentValue)
36: && !isEuropeanCountry(value)) {
37: throw new ValidationException("The country [" + value
38: + "] is not in Europe", null);
39: } else if (ZipCodeValidator.AMERICA.equals(continentValue)
40: && !isUSCountry(value)) {
41: throw new ValidationException("The country [" + value
42: + "] is not in America", null);
43: }
44: } else {
45: throw new ValidationException(
46: "The continent property must be defined for the CountryValidator",
47: null);
48: }
49: }
50:
51: private boolean isEuropeanCountry(String country) {
52: return "France".equalsIgnoreCase(country)
53: || "Spain".equalsIgnoreCase(country); // and so on
54: }
55:
56: private boolean isUSCountry(String country) {
57: return "United States".equalsIgnoreCase(country)
58: || "Bolivia".equalsIgnoreCase(country); // and so on
59: }
60:
61: }
|