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.oro.text.regex.MatchResult;
  20. import org.apache.oro.text.regex.Pattern;
  21. import org.apache.oro.text.regex.Perl5Compiler;
  22. import org.apache.oro.text.regex.Perl5Matcher;
  23. import org.apache.tools.ant.BuildException;
  24. /**
  25. * Implementation of RegexpMatcher for Jakarta-ORO.
  26. *
  27. */
  28. public class JakartaOroMatcher implements RegexpMatcher {
  29. private String pattern;
  30. protected final Perl5Compiler compiler = new Perl5Compiler();
  31. protected final Perl5Matcher matcher = new Perl5Matcher();
  32. public JakartaOroMatcher() {
  33. }
  34. /**
  35. * Set the regexp pattern from the String description.
  36. */
  37. public void setPattern(String pattern) {
  38. this.pattern = pattern;
  39. }
  40. /**
  41. * Get a String representation of the regexp pattern
  42. */
  43. public String getPattern() {
  44. return this.pattern;
  45. }
  46. /**
  47. * Get a compiled representation of the regexp pattern
  48. */
  49. protected Pattern getCompiledPattern(int options)
  50. throws BuildException {
  51. try {
  52. // compute the compiler options based on the input options first
  53. Pattern p = compiler.compile(pattern, getCompilerOptions(options));
  54. return p;
  55. } catch (Exception e) {
  56. throw new BuildException(e);
  57. }
  58. }
  59. /**
  60. * Does the given argument match the pattern?
  61. */
  62. public boolean matches(String argument) throws BuildException {
  63. return matches(argument, MATCH_DEFAULT);
  64. }
  65. /**
  66. * Does the given argument match the pattern?
  67. */
  68. public boolean matches(String input, int options)
  69. throws BuildException {
  70. Pattern p = getCompiledPattern(options);
  71. return matcher.contains(input, p);
  72. }
  73. /**
  74. * Returns a Vector of matched groups found in the argument.
  75. *
  76. * <p>Group 0 will be the full match, the rest are the
  77. * parenthesized subexpressions</p>.
  78. */
  79. public Vector getGroups(String argument) throws BuildException {
  80. return getGroups(argument, MATCH_DEFAULT);
  81. }
  82. /**
  83. * Returns a Vector of matched groups found in the argument.
  84. *
  85. * <p>Group 0 will be the full match, the rest are the
  86. * parenthesized subexpressions</p>.
  87. */
  88. public Vector getGroups(String input, int options)
  89. throws BuildException {
  90. if (!matches(input, options)) {
  91. return null;
  92. }
  93. Vector v = new Vector();
  94. MatchResult mr = matcher.getMatch();
  95. int cnt = mr.groups();
  96. for (int i = 0; i < cnt; i++) {
  97. String match = mr.group(i);
  98. // treat non-matching groups as empty matches
  99. if (match == null) {
  100. match = "";
  101. }
  102. v.addElement(match);
  103. }
  104. return v;
  105. }
  106. protected int getCompilerOptions(int options) {
  107. int cOptions = Perl5Compiler.DEFAULT_MASK;
  108. if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
  109. cOptions |= Perl5Compiler.CASE_INSENSITIVE_MASK;
  110. }
  111. if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
  112. cOptions |= Perl5Compiler.MULTILINE_MASK;
  113. }
  114. if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
  115. cOptions |= Perl5Compiler.SINGLELINE_MASK;
  116. }
  117. return cOptions;
  118. }
  119. }