1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.healthmarketscience.jackcess.impl;
18
19 import java.io.IOException;
20
21 import com.healthmarketscience.jackcess.Column;
22 import com.healthmarketscience.jackcess.InvalidValueException;
23 import com.healthmarketscience.jackcess.expr.EvalException;
24 import com.healthmarketscience.jackcess.expr.Identifier;
25 import com.healthmarketscience.jackcess.expr.Value;
26 import com.healthmarketscience.jackcess.impl.expr.Expressionator;
27 import com.healthmarketscience.jackcess.util.ColumnValidator;
28
29
30
31
32
33 public class ColValidatorEvalContext extends ColEvalContext
34 {
35 private String _helpStr;
36 private Object _val;
37
38 public ColValidatorEvalContext(ColumnImpl col) {
39 super(col);
40 }
41
42 ColValidatorEvalContext setExpr(String exprStr, String helpStr) {
43 setExpr(Expressionator.Type.FIELD_VALIDATOR, exprStr);
44 _helpStr = helpStr;
45 return this;
46 }
47
48 ColumnValidatorealthmarketscience/jackcess/util/ColumnValidator.html#ColumnValidator">ColumnValidator toColumnValidator(ColumnValidator delegate) {
49 return new InternalColumnValidator(delegate) {
50 @Override
51 protected Object internalValidate(Column col, Object val)
52 throws IOException {
53 return ColValidatorEvalContext.this.validate(val);
54 }
55 @Override
56 protected void appendToString(StringBuilder sb) {
57 sb.append("expression=").append(ColValidatorEvalContext.this);
58 }
59 };
60 }
61
62 private void reset() {
63 _val = null;
64 }
65
66 @Override
67 public Value getThisColumnValue() {
68 return toValue(_val);
69 }
70
71 @Override
72 public Value getIdentifierValue(Identifier identifier) {
73
74
75 if(!getCol().isThisColumn(identifier)) {
76 throw new EvalException("Cannot access other fields for " + identifier);
77 }
78 return getThisColumnValue();
79 }
80
81 private Object validate(Object val) throws IOException {
82 try {
83 _val = val;
84 Boolean result = (Boolean)eval();
85 if(!result) {
86 String msg = ((_helpStr != null) ? _helpStr :
87 "Invalid column value '" + val + "'");
88 throw new InvalidValueException(withErrorContext(msg));
89 }
90 return val;
91 } finally {
92 reset();
93 }
94 }
95 }