001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: /*
019: * Task to rename files based on extension. This task has the following
020: * properties which can be set:
021: * <ul>
022: * <li>fromExtension: </li>
023: * <li>toExtension: </li>
024: * <li>srcDir: </li>
025: * <li>replace: </li>
026: * </ul>
027: */
028:
029: package org.apache.tools.ant.taskdefs.optional;
030:
031: import java.io.File;
032: import org.apache.tools.ant.BuildException;
033: import org.apache.tools.ant.Project;
034: import org.apache.tools.ant.taskdefs.MatchingTask;
035: import org.apache.tools.ant.taskdefs.Move;
036: import org.apache.tools.ant.types.Mapper;
037:
038: /**
039: *
040: * @version 1.2
041: *
042: * @deprecated since 1.5.x.
043: * Use <move> instead
044: */
045: public class RenameExtensions extends MatchingTask {
046:
047: private String fromExtension = "";
048: private String toExtension = "";
049: private boolean replace = false;
050: private File srcDir;
051:
052: private Mapper.MapperType globType;
053:
054: /** Creates new RenameExtensions */
055: public RenameExtensions() {
056: super ();
057: globType = new Mapper.MapperType();
058: globType.setValue("glob");
059: }
060:
061: /**
062: * The string that files must end in to be renamed
063: *
064: * @param from the extension of files being renamed.
065: */
066: public void setFromExtension(String from) {
067: fromExtension = from;
068: }
069:
070: /**
071: * The string that renamed files will end with on
072: * completion
073: *
074: * @param to the extension of the renamed files.
075: */
076: public void setToExtension(String to) {
077: toExtension = to;
078: }
079:
080: /**
081: * store replace attribute - this determines whether the target file
082: * should be overwritten if present
083: *
084: * @param replace if true overwrite any target files that exist.
085: */
086: public void setReplace(boolean replace) {
087: this .replace = replace;
088: }
089:
090: /**
091: * Set the source dir to find the files to be renamed.
092: *
093: * @param srcDir the source directory.
094: */
095: public void setSrcDir(File srcDir) {
096: this .srcDir = srcDir;
097: }
098:
099: /**
100: * Executes the task.
101: *
102: * @throws BuildException is there is a problem in the task execution.
103: */
104: public void execute() throws BuildException {
105:
106: // first off, make sure that we've got a from and to extension
107: if (fromExtension == null || toExtension == null
108: || srcDir == null) {
109: throw new BuildException(
110: "srcDir, fromExtension and toExtension "
111: + "attributes must be set!");
112: }
113:
114: log(
115: "DEPRECATED - The renameext task is deprecated. Use move instead.",
116: Project.MSG_WARN);
117: log("Replace this with:", Project.MSG_INFO);
118: log("<move todir=\"" + srcDir + "\" overwrite=\"" + replace
119: + "\">", Project.MSG_INFO);
120: log(" <fileset dir=\"" + srcDir + "\" />", Project.MSG_INFO);
121: log(" <mapper type=\"glob\"", Project.MSG_INFO);
122: log(" from=\"*" + fromExtension + "\"",
123: Project.MSG_INFO);
124: log(" to=\"*" + toExtension + "\" />",
125: Project.MSG_INFO);
126: log("</move>", Project.MSG_INFO);
127: log(
128: "using the same patterns on <fileset> as you\'ve used here",
129: Project.MSG_INFO);
130:
131: Move move = new Move();
132: move.bindToOwner(this );
133: move.setOwningTarget(getOwningTarget());
134: move.setTaskName(getTaskName());
135: move.setLocation(getLocation());
136: move.setTodir(srcDir);
137: move.setOverwrite(replace);
138:
139: fileset.setDir(srcDir);
140: move.addFileset(fileset);
141:
142: Mapper me = move.createMapper();
143: me.setType(globType);
144: me.setFrom("*" + fromExtension);
145: me.setTo("*" + toExtension);
146:
147: move.execute();
148: }
149:
150: }
|