1. /*
  2. * Copyright 2000,2002-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;
  18. import java.util.Vector;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.util.regexp.RegexpMatcher;
  21. import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
  22. /**
  23. * Implementation of FileNameMapper that does regular expression
  24. * replacements.
  25. *
  26. */
  27. public class RegexpPatternMapper implements FileNameMapper {
  28. protected RegexpMatcher reg = null;
  29. protected char[] to = null;
  30. protected StringBuffer result = new StringBuffer();
  31. public RegexpPatternMapper() throws BuildException {
  32. reg = (new RegexpMatcherFactory()).newRegexpMatcher();
  33. }
  34. /**
  35. * Sets the "from" pattern. Required.
  36. */
  37. public void setFrom(String from) throws BuildException {
  38. try {
  39. reg.setPattern(from);
  40. } catch (NoClassDefFoundError e) {
  41. // depending on the implementation the actual RE won't
  42. // get instantiated in the constructor.
  43. throw new BuildException("Cannot load regular expression matcher",
  44. e);
  45. }
  46. }
  47. /**
  48. * Sets the "to" pattern. Required.
  49. */
  50. public void setTo(String to) {
  51. this.to = to.toCharArray();
  52. }
  53. /**
  54. * Returns null if the source file name doesn't match the
  55. * "from" pattern, an one-element array containing the
  56. * translated file otherwise.
  57. */
  58. public String[] mapFileName(String sourceFileName) {
  59. if (reg == null || to == null
  60. || !reg.matches(sourceFileName)) {
  61. return null;
  62. }
  63. return new String[] {replaceReferences(sourceFileName)};
  64. }
  65. /**
  66. * Replace all backreferences in the to pattern with the matched
  67. * groups of the source.
  68. */
  69. protected String replaceReferences(String source) {
  70. Vector v = reg.getGroups(source);
  71. result.setLength(0);
  72. for (int i = 0; i < to.length; i++) {
  73. if (to[i] == '\\') {
  74. if (++i < to.length) {
  75. int value = Character.digit(to[i], 10);
  76. if (value > -1) {
  77. result.append((String) v.elementAt(value));
  78. } else {
  79. result.append(to[i]);
  80. }
  81. } else {
  82. // XXX - should throw an exception instead?
  83. result.append('\\');
  84. }
  85. } else {
  86. result.append(to[i]);
  87. }
  88. }
  89. return result.substring(0);
  90. }
  91. }