1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  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 org.apache.tools.ant.util.regexp;
  18. import java.util.Vector;
  19. import org.apache.regexp.RE;
  20. import org.apache.regexp.RESyntaxException;
  21. import org.apache.tools.ant.BuildException;
  22. /**
  23. * Implementation of RegexpMatcher for Jakarta-Regexp.
  24. *
  25. */
  26. public class JakartaRegexpMatcher implements RegexpMatcher {
  27. private String pattern;
  28. /**
  29. * Set the regexp pattern from the String description.
  30. */
  31. public void setPattern(String pattern) {
  32. this.pattern = pattern;
  33. }
  34. /**
  35. * Get a String representation of the regexp pattern
  36. */
  37. public String getPattern() {
  38. return pattern;
  39. }
  40. protected RE getCompiledPattern(int options)
  41. throws BuildException {
  42. int cOptions = getCompilerOptions(options);
  43. try {
  44. RE reg = new RE(pattern);
  45. reg.setMatchFlags(cOptions);
  46. return reg;
  47. } catch (RESyntaxException e) {
  48. throw new BuildException(e);
  49. }
  50. }
  51. /**
  52. * Does the given argument match the pattern?
  53. */
  54. public boolean matches(String argument) throws BuildException {
  55. return matches(argument, MATCH_DEFAULT);
  56. }
  57. /**
  58. * Does the given argument match the pattern?
  59. */
  60. public boolean matches(String input, int options)
  61. throws BuildException {
  62. return matches(input, getCompiledPattern(options));
  63. }
  64. private boolean matches(String input, RE reg) {
  65. return reg.match(input);
  66. }
  67. /**
  68. * Returns a Vector of matched groups found in the argument.
  69. *
  70. * <p>Group 0 will be the full match, the rest are the
  71. * parenthesized subexpressions</p>.
  72. */
  73. public Vector getGroups(String argument) throws BuildException {
  74. return getGroups(argument, MATCH_DEFAULT);
  75. }
  76. public Vector getGroups(String input, int options)
  77. throws BuildException {
  78. RE reg = getCompiledPattern(options);
  79. if (!matches(input, reg)) {
  80. return null;
  81. }
  82. Vector v = new Vector();
  83. int cnt = reg.getParenCount();
  84. for (int i = 0; i < cnt; i++) {
  85. String match = reg.getParen(i);
  86. // treat non-matching groups as empty matches
  87. if (match == null) {
  88. match = "";
  89. }
  90. v.addElement(match);
  91. }
  92. return v;
  93. }
  94. protected int getCompilerOptions(int options) {
  95. int cOptions = RE.MATCH_NORMAL;
  96. if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
  97. cOptions |= RE.MATCH_CASEINDEPENDENT;
  98. }
  99. if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
  100. cOptions |= RE.MATCH_MULTILINE;
  101. }
  102. if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
  103. cOptions |= RE.MATCH_SINGLELINE;
  104. }
  105. return cOptions;
  106. }
  107. }