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.

RegexpValidator.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.validator;
  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;
  19. import com.vaadin.data.ValidationResult;
  20. import com.vaadin.data.ValueContext;
  21. /**
  22. * A string validator comparing the string against a Java regular expression.
  23. * Both complete matches and substring matches are supported.
  24. * <p>
  25. * For the Java regular expression syntax, see {@link java.util.regex.Pattern}.
  26. *
  27. * @author Vaadin Ltd.
  28. * @since 8.0
  29. */
  30. @SuppressWarnings("serial")
  31. public class RegexpValidator extends AbstractValidator<String> {
  32. private Pattern pattern;
  33. private boolean complete;
  34. private transient Matcher matcher = null;
  35. /**
  36. * Creates a validator for checking that the regular expression matches the
  37. * complete string to validate.
  38. *
  39. * @param errorMessage
  40. * the message to display in case the value does not validate.
  41. * @param regexp
  42. * a Java regular expression
  43. */
  44. public RegexpValidator(String errorMessage, String regexp) {
  45. this(errorMessage, regexp, true);
  46. }
  47. /**
  48. * Creates a validator for checking that the regular expression matches the
  49. * string to validate.
  50. *
  51. * @param errorMessage
  52. * the message to display in case the value does not validate.
  53. * @param regexp
  54. * a Java regular expression
  55. * @param complete
  56. * true to use check for a complete match, false to look for a
  57. * matching substring
  58. *
  59. */
  60. public RegexpValidator(String errorMessage, String regexp,
  61. boolean complete) {
  62. super(errorMessage);
  63. pattern = Pattern.compile(regexp);
  64. this.complete = complete;
  65. }
  66. @Override
  67. public ValidationResult apply(String value, ValueContext context) {
  68. return toResult(value, isValid(value));
  69. }
  70. @Override
  71. public String toString() {
  72. return "RegexpValidator[" + pattern + "]";
  73. }
  74. /**
  75. * Returns whether the given string matches the regular expression.
  76. *
  77. * @param value
  78. * the string to match
  79. * @return true if the string matched, false otherwise
  80. */
  81. protected boolean isValid(String value) {
  82. if (value == null) {
  83. return true;
  84. }
  85. if (complete) {
  86. return getMatcher(value).matches();
  87. } else {
  88. return getMatcher(value).find();
  89. }
  90. }
  91. /**
  92. * Returns a new or reused matcher for the pattern.
  93. *
  94. * @param value
  95. * the string to find matches in
  96. * @return a matcher for the string
  97. */
  98. private Matcher getMatcher(String value) {
  99. if (matcher == null) {
  100. matcher = pattern.matcher(value);
  101. } else {
  102. matcher.reset(value);
  103. }
  104. return matcher;
  105. }
  106. }