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. /**
  19. * Implementation of FileNameMapper that does simple wildcard pattern
  20. * replacements.
  21. *
  22. * <p>This does simple translations like *.foo -> *.bar where the
  23. * prefix to .foo will be left unchanged. It only handles a single *
  24. * character, use regular expressions for more complicated
  25. * situations.</p>
  26. *
  27. * <p>This is one of the more useful Mappers, it is used by javac for
  28. * example.</p>
  29. *
  30. */
  31. public class GlobPatternMapper implements FileNameMapper {
  32. /**
  33. * Part of "from" pattern before the *.
  34. */
  35. protected String fromPrefix = null;
  36. /**
  37. * Part of "from" pattern after the *.
  38. */
  39. protected String fromPostfix = null;
  40. /**
  41. * Length of the prefix ("from" pattern).
  42. */
  43. protected int prefixLength;
  44. /**
  45. * Length of the postfix ("from" pattern).
  46. */
  47. protected int postfixLength;
  48. /**
  49. * Part of "to" pattern before the *.
  50. */
  51. protected String toPrefix = null;
  52. /**
  53. * Part of "to" pattern after the *.
  54. */
  55. protected String toPostfix = null;
  56. /**
  57. * Sets the "from" pattern. Required.
  58. */
  59. public void setFrom(String from) {
  60. int index = from.lastIndexOf("*");
  61. if (index == -1) {
  62. fromPrefix = from;
  63. fromPostfix = "";
  64. } else {
  65. fromPrefix = from.substring(0, index);
  66. fromPostfix = from.substring(index + 1);
  67. }
  68. prefixLength = fromPrefix.length();
  69. postfixLength = fromPostfix.length();
  70. }
  71. /**
  72. * Sets the "to" pattern. Required.
  73. */
  74. public void setTo(String to) {
  75. int index = to.lastIndexOf("*");
  76. if (index == -1) {
  77. toPrefix = to;
  78. toPostfix = "";
  79. } else {
  80. toPrefix = to.substring(0, index);
  81. toPostfix = to.substring(index + 1);
  82. }
  83. }
  84. /**
  85. * Returns null if the source file name doesn't match the
  86. * "from" pattern, an one-element array containing the
  87. * translated file otherwise.
  88. */
  89. public String[] mapFileName(String sourceFileName) {
  90. if (fromPrefix == null
  91. || !sourceFileName.startsWith(fromPrefix)
  92. || !sourceFileName.endsWith(fromPostfix)) {
  93. return null;
  94. }
  95. return new String[] {toPrefix
  96. + extractVariablePart(sourceFileName)
  97. + toPostfix};
  98. }
  99. /**
  100. * Returns the part of the given string that matches the * in the
  101. * "from" pattern.
  102. */
  103. protected String extractVariablePart(String name) {
  104. return name.substring(prefixLength,
  105. name.length() - postfixLength);
  106. }
  107. }