View Javadoc
1   /*
2   Copyright (c) 2018 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
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   * @author James Ahlborn
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      // col validators can only get "this" column, but they can refer to it by
74      // name
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  }