1 /***
2 * Copyright (C) 2008 rweber <quietgenie@users.sourceforge.net>
3 *
4 * This file is part of CsvObjectMapper.
5 *
6 * CsvObjectMapper is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CsvObjectMapper is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with CsvObjectMapper. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /***
21 *
22 */
23 package com.projectnine.csvmapper;
24
25 import org.apache.commons.chain.Chain;
26 import org.apache.commons.chain.Command;
27 import org.apache.commons.chain.Context;
28
29 /***
30 * A {@link CsvFieldValidator} may be used to validate that the value of a CSV
31 * field falls within certain parameters.
32 *
33 * @author robweber
34 *
35 */
36 public abstract class CsvFieldValidator implements Command {
37 private boolean required = false;
38
39
40
41
42
43
44
45
46 /***
47 * Child classes should invoke super{@link #execute(Context)} in order to
48 * access simple progress tracking of the {@link Context} through a
49 * {@link Chain} of {@link CsvFieldValidator}s.
50 *
51 * Return false to run the next command in the chain (if there is a next
52 * command); return true to stop execution with this command; throw an
53 * Exception when validation fails.
54 */
55 public final boolean execute(Context context) throws Exception {
56 boolean execute = false;
57 CsvFieldValidationContext csvFieldValidationContext = (CsvFieldValidationContext) context;
58
59
60
61 csvFieldValidationContext.setCurrentValidationCommandName(getClass()
62 .getName());
63
64
65 if (required && csvFieldValidationContext.getValueToValidate() != null
66 && !csvFieldValidationContext.getValueToValidate().equals("")) {
67 execute = doValidate(csvFieldValidationContext);
68 }
69 return execute;
70 }
71
72 protected abstract boolean doValidate(
73 CsvFieldValidationContext csvFieldValidationContext)
74 throws Exception;
75
76 /***
77 * @param required
78 * the required to set
79 */
80 public void setRequired(boolean required) {
81 this.required = required;
82 }
83
84 }