/projects/castor-1.3.3/xml/src/main/java/org/exolab/castor/util/SunRegExpEvaluator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 76 lines · 25 code · 9 blank · 42 comment · 4 complexity · 26be045dd8629a281b23242e7d180ca1 MD5 · raw file

  1. /*
  2. * Copyright 2010 Werner Guttmann
  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. package org.exolab.castor.util;
  17. import java.util.regex.Pattern;
  18. import java.util.regex.PatternSyntaxException;
  19. /**
  20. * An implementation of {@link RegExpEvaluator} that uses the Java Regular
  21. * Expression library.
  22. *
  23. * @author <a href="mailto:george76@hotmail.com">George Varghese</a>
  24. * @since 1.3.2
  25. **/
  26. public class SunRegExpEvaluator implements RegExpEvaluator {
  27. /**
  28. * The Regular expression
  29. **/
  30. private Pattern _pattern;
  31. /**
  32. * Sets the regular expression to match against during a call to #matches
  33. *
  34. * @param rexpr
  35. * the regular expression
  36. **/
  37. public void setExpression(String rexpr) {
  38. if (rexpr != null) {
  39. try {
  40. // -- patch and compile expression
  41. _pattern = Pattern.compile(rexpr);
  42. } catch (PatternSyntaxException ex) {
  43. String err = "RegExp Syntax error: ";
  44. err += ex.getMessage();
  45. err += " ; error occured with the following "
  46. + "regular expression: " + rexpr;
  47. throw new IllegalArgumentException(err, ex);
  48. }
  49. }
  50. }
  51. /**
  52. * Returns true if the given String is matched by the regular expression of
  53. * this RegExpEvaluator
  54. *
  55. * @param value
  56. * the String to check the production of
  57. * @return true if the given string matches the regular expression of this
  58. * RegExpEvaluator
  59. * @see #setExpression
  60. **/
  61. public boolean matches(String value) {
  62. if (_pattern != null) {
  63. return _pattern.matcher(value).matches();
  64. }
  65. return true;
  66. }
  67. }