1. /*
  2. * Copyright 2001-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 java.util.regex.Matcher;
  20. import java.util.regex.Pattern;
  21. import java.util.regex.PatternSyntaxException;
  22. import org.apache.tools.ant.BuildException;
  23. /**
  24. * Implementation of RegexpMatcher for the built-in regexp matcher of
  25. * JDK 1.4. UNIX_LINES option is enabled as a default.
  26. *
  27. */
  28. public class Jdk14RegexpMatcher implements RegexpMatcher {
  29. private String pattern;
  30. public Jdk14RegexpMatcher() {
  31. }
  32. /**
  33. * Set the regexp pattern from the String description.
  34. */
  35. public void setPattern(String pattern) {
  36. this.pattern = pattern;
  37. }
  38. /**
  39. * Get a String representation of the regexp pattern
  40. */
  41. public String getPattern() {
  42. return pattern;
  43. }
  44. protected Pattern getCompiledPattern(int options)
  45. throws BuildException {
  46. int cOptions = getCompilerOptions(options);
  47. try {
  48. Pattern p = Pattern.compile(this.pattern, cOptions);
  49. return p;
  50. } catch (PatternSyntaxException e) {
  51. throw new BuildException(e);
  52. }
  53. }
  54. /**
  55. * Does the given argument match the pattern?
  56. */
  57. public boolean matches(String argument) throws BuildException {
  58. return matches(argument, MATCH_DEFAULT);
  59. }
  60. /**
  61. * Does the given argument match the pattern?
  62. */
  63. public boolean matches(String input, int options)
  64. throws BuildException {
  65. try {
  66. Pattern p = getCompiledPattern(options);
  67. return p.matcher(input).find();
  68. } catch (Exception e) {
  69. throw new BuildException(e);
  70. }
  71. }
  72. /**
  73. * Returns a Vector of matched groups found in the argument.
  74. *
  75. * <p>Group 0 will be the full match, the rest are the
  76. * parenthesized subexpressions</p>.
  77. */
  78. public Vector getGroups(String argument) throws BuildException {
  79. return getGroups(argument, MATCH_DEFAULT);
  80. }
  81. /**
  82. * Returns a Vector of matched groups found in the argument.
  83. *
  84. * <p>Group 0 will be the full match, the rest are the
  85. * parenthesized subexpressions</p>.
  86. */
  87. public Vector getGroups(String input, int options)
  88. throws BuildException {
  89. Pattern p = getCompiledPattern(options);
  90. Matcher matcher = p.matcher(input);
  91. if (!matcher.find()) {
  92. return null;
  93. }
  94. Vector v = new Vector();
  95. int cnt = matcher.groupCount();
  96. for (int i = 0; i <= cnt; i++) {
  97. String match = matcher.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. // be strict about line separator
  108. int cOptions = Pattern.UNIX_LINES;
  109. if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
  110. cOptions |= Pattern.CASE_INSENSITIVE;
  111. }
  112. if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
  113. cOptions |= Pattern.MULTILINE;
  114. }
  115. if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
  116. cOptions |= Pattern.DOTALL;
  117. }
  118. return cOptions;
  119. }
  120. }