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. /*
  18. * Task to rename files based on extension. This task has the following
  19. * properties which can be set:
  20. * <ul>
  21. * <li>fromExtension: </li>
  22. * <li>toExtension: </li>
  23. * <li>srcDir: </li>
  24. * <li>replace: </li>
  25. * </ul>
  26. */
  27. package org.apache.tools.ant.taskdefs.optional;
  28. import java.io.File;
  29. import org.apache.tools.ant.BuildException;
  30. import org.apache.tools.ant.Project;
  31. import org.apache.tools.ant.taskdefs.MatchingTask;
  32. import org.apache.tools.ant.taskdefs.Move;
  33. import org.apache.tools.ant.types.Mapper;
  34. /**
  35. *
  36. * @version 1.2
  37. *
  38. * @deprecated Use <move> instead
  39. */
  40. public class RenameExtensions extends MatchingTask {
  41. private String fromExtension = "";
  42. private String toExtension = "";
  43. private boolean replace = false;
  44. private File srcDir;
  45. private Mapper.MapperType globType;
  46. /** Creates new RenameExtensions */
  47. public RenameExtensions() {
  48. super();
  49. globType = new Mapper.MapperType();
  50. globType.setValue("glob");
  51. }
  52. /**
  53. * The string that files must end in to be renamed
  54. *
  55. * @param from the extension of files being renamed.
  56. */
  57. public void setFromExtension(String from) {
  58. fromExtension = from;
  59. }
  60. /**
  61. * The string that renamed files will end with on
  62. * completion
  63. *
  64. * @param to the extension of the renamed files.
  65. */
  66. public void setToExtension(String to) {
  67. toExtension = to;
  68. }
  69. /**
  70. * store replace attribute - this determines whether the target file
  71. * should be overwritten if present
  72. *
  73. * @param replace if true overwrite any target files that exist.
  74. */
  75. public void setReplace(boolean replace) {
  76. this.replace = replace;
  77. }
  78. /**
  79. * Set the source dir to find the files to be renamed.
  80. *
  81. * @param srcDir the source directory.
  82. */
  83. public void setSrcDir(File srcDir) {
  84. this.srcDir = srcDir;
  85. }
  86. /**
  87. * Executes the task.
  88. *
  89. * @throws BuildException is there is a problem in the task execution.
  90. */
  91. public void execute() throws BuildException {
  92. // first off, make sure that we've got a from and to extension
  93. if (fromExtension == null || toExtension == null || srcDir == null) {
  94. throw new BuildException("srcDir, fromExtension and toExtension "
  95. + "attributes must be set!");
  96. }
  97. log("DEPRECATED - The renameext task is deprecated. Use move instead.",
  98. Project.MSG_WARN);
  99. log("Replace this with:", Project.MSG_INFO);
  100. log("<move todir=\"" + srcDir + "\" overwrite=\"" + replace + "\">",
  101. Project.MSG_INFO);
  102. log(" <fileset dir=\"" + srcDir + "\" />", Project.MSG_INFO);
  103. log(" <mapper type=\"glob\"", Project.MSG_INFO);
  104. log(" from=\"*" + fromExtension + "\"", Project.MSG_INFO);
  105. log(" to=\"*" + toExtension + "\" />", Project.MSG_INFO);
  106. log("</move>", Project.MSG_INFO);
  107. log("using the same patterns on <fileset> as you\'ve used here",
  108. Project.MSG_INFO);
  109. Move move = (Move) getProject().createTask("move");
  110. move.setOwningTarget(getOwningTarget());
  111. move.setTaskName(getTaskName());
  112. move.setLocation(getLocation());
  113. move.setTodir(srcDir);
  114. move.setOverwrite(replace);
  115. fileset.setDir(srcDir);
  116. move.addFileset(fileset);
  117. Mapper me = move.createMapper();
  118. me.setType(globType);
  119. me.setFrom("*" + fromExtension);
  120. me.setTo("*" + toExtension);
  121. move.execute();
  122. }
  123. }