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 org.apache.oro.text.regex.Perl5Substitution;
  19. import org.apache.oro.text.regex.Substitution;
  20. import org.apache.oro.text.regex.Util;
  21. import org.apache.tools.ant.BuildException;
  22. /***
  23. * Regular expression implementation using the Jakarta Oro package
  24. */
  25. public class JakartaOroRegexp extends JakartaOroMatcher implements Regexp {
  26. public JakartaOroRegexp() {
  27. super();
  28. }
  29. public String substitute(String input, String argument, int options)
  30. throws BuildException {
  31. // translate \1 to $1 so that the Perl5Substitution will work
  32. StringBuffer subst = new StringBuffer();
  33. for (int i = 0; i < argument.length(); i++) {
  34. char c = argument.charAt(i);
  35. if (c == '$') {
  36. subst.append('\\');
  37. subst.append('$');
  38. } else if (c == '\\') {
  39. if (++i < argument.length()) {
  40. c = argument.charAt(i);
  41. int value = Character.digit(c, 10);
  42. if (value > -1) {
  43. subst.append("$").append(value);
  44. } else {
  45. subst.append(c);
  46. }
  47. } else {
  48. // XXX - should throw an exception instead?
  49. subst.append('\\');
  50. }
  51. } else {
  52. subst.append(c);
  53. }
  54. }
  55. // Do the substitution
  56. Substitution s =
  57. new Perl5Substitution(subst.toString(),
  58. Perl5Substitution.INTERPOLATE_ALL);
  59. return Util.substitute(matcher,
  60. getCompiledPattern(options),
  61. s,
  62. input,
  63. getSubsOptions(options));
  64. }
  65. protected int getSubsOptions(int options) {
  66. boolean replaceAll = RegexpUtil.hasFlag(options, REPLACE_ALL);
  67. int subsOptions = 1;
  68. if (replaceAll) {
  69. subsOptions = Util.SUBSTITUTE_ALL;
  70. }
  71. return subsOptions;
  72. }
  73. }