You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ColValidatorEvalContext.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright (c) 2018 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.io.IOException;
  15. import com.healthmarketscience.jackcess.Column;
  16. import com.healthmarketscience.jackcess.InvalidValueException;
  17. import com.healthmarketscience.jackcess.expr.EvalException;
  18. import com.healthmarketscience.jackcess.expr.Identifier;
  19. import com.healthmarketscience.jackcess.expr.Value;
  20. import com.healthmarketscience.jackcess.impl.expr.Expressionator;
  21. import com.healthmarketscience.jackcess.util.ColumnValidator;
  22. /**
  23. *
  24. * @author James Ahlborn
  25. */
  26. public class ColValidatorEvalContext extends ColEvalContext
  27. {
  28. private String _helpStr;
  29. private Object _val;
  30. public ColValidatorEvalContext(ColumnImpl col) {
  31. super(col);
  32. }
  33. ColValidatorEvalContext setExpr(String exprStr, String helpStr) {
  34. setExpr(Expressionator.Type.FIELD_VALIDATOR, exprStr);
  35. _helpStr = helpStr;
  36. return this;
  37. }
  38. ColumnValidator toColumnValidator(ColumnValidator delegate) {
  39. return new InternalColumnValidator(delegate) {
  40. @Override
  41. protected Object internalValidate(Column col, Object val)
  42. throws IOException {
  43. return ColValidatorEvalContext.this.validate(col, val);
  44. }
  45. @Override
  46. protected void appendToString(StringBuilder sb) {
  47. sb.append("expression=").append(ColValidatorEvalContext.this);
  48. }
  49. };
  50. }
  51. private void reset() {
  52. _val = null;
  53. }
  54. @Override
  55. public Value getThisColumnValue() {
  56. return toValue(_val, getCol().getType());
  57. }
  58. @Override
  59. public Value getIdentifierValue(Identifier identifier) {
  60. // col validators can only get "this" column, but they can refer to it by
  61. // name
  62. if(!getCol().isThisColumn(identifier)) {
  63. throw new EvalException("Cannot access other fields for " + identifier);
  64. }
  65. return getThisColumnValue();
  66. }
  67. private Object validate(Column col, Object val) throws IOException {
  68. try {
  69. _val = val;
  70. Boolean result = (Boolean)eval();
  71. if(!result) {
  72. String msg = ((_helpStr != null) ? _helpStr :
  73. "Invalid column value '" + val + "'");
  74. throw new InvalidValueException(withErrorContext(msg));
  75. }
  76. return val;
  77. } finally {
  78. reset();
  79. }
  80. }
  81. }