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.context.ContextManager;
20: import org.csvbeans.exceptions.ValidationException;
21: import org.csvbeans.validators.FieldValidator;
22:
23: /**
24: * Dummy Zip Code validator. Set a context property with the
25: * continent of the zip code.
26: *
27: * @author Fouad Hamdi
28: * @since 0.7
29: */
30: public class ZipCodeValidator extends FieldValidator {
31:
32: public static final String AMERICA = "AM";
33: public static final String EUROPE = "EU";
34:
35: public void validate(String value) throws ValidationException {
36: if (value != null) {
37: if (value.startsWith(EUROPE)) {
38: getContextManager().setAttribute("zipCodeContinent",
39: EUROPE, ContextManager.RECORD_CONTEXT);
40: } else if (value.startsWith(AMERICA)) {
41: getContextManager().setAttribute("zipCodeContinent",
42: AMERICA, ContextManager.RECORD_CONTEXT);
43: } else {
44: throw new ValidationException(
45: "Unknown zip code country ! " + value, null);
46: }
47: }
48:
49: }
50:
51: public void init(ContextManager manager) {
52: super .init(manager);
53: System.out
54: .println("Here, you can open some resource and store it in the "
55: + "context manager or in the validator.");
56: }
57:
58: public void onEndingSessionContext() {
59: System.out
60: .println("Here, you can release the opened resources at the end of the parsing or building of the csv file.");
61: }
62:
63: public void onEndingRecordContext() {
64: System.out
65: .println("Here, you can release resources after each record parsing or building.");
66: }
67: }
|