KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > RenameExtensions


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 /*
19  * Task to rename files based on extension. This task has the following
20  * properties which can be set:
21  * <ul>
22  * <li>fromExtension: </li>
23  * <li>toExtension: </li>
24  * <li>srcDir: </li>
25  * <li>replace: </li>
26  * </ul>
27  */

28
29 package org.apache.tools.ant.taskdefs.optional;
30
31 import java.io.File JavaDoc;
32 import org.apache.tools.ant.BuildException;
33 import org.apache.tools.ant.Project;
34 import org.apache.tools.ant.taskdefs.MatchingTask;
35 import org.apache.tools.ant.taskdefs.Move;
36 import org.apache.tools.ant.types.Mapper;
37
38 /**
39  *
40  * @version 1.2
41  *
42  * @deprecated since 1.5.x.
43  * Use &lt;move&gt; instead
44  */

45 public class RenameExtensions extends MatchingTask {
46
47     private String JavaDoc fromExtension = "";
48     private String JavaDoc toExtension = "";
49     private boolean replace = false;
50     private File JavaDoc srcDir;
51
52     private Mapper.MapperType globType;
53
54
55     /** Creates new RenameExtensions */
56     public RenameExtensions() {
57         super();
58         globType = new Mapper.MapperType();
59         globType.setValue("glob");
60     }
61
62     /**
63      * The string that files must end in to be renamed
64      *
65      * @param from the extension of files being renamed.
66      */

67     public void setFromExtension(String JavaDoc from) {
68         fromExtension = from;
69     }
70
71     /**
72      * The string that renamed files will end with on
73      * completion
74      *
75      * @param to the extension of the renamed files.
76      */

77     public void setToExtension(String JavaDoc to) {
78         toExtension = to;
79     }
80
81     /**
82      * store replace attribute - this determines whether the target file
83      * should be overwritten if present
84      *
85      * @param replace if true overwrite any target files that exist.
86      */

87     public void setReplace(boolean replace) {
88         this.replace = replace;
89     }
90
91     /**
92      * Set the source dir to find the files to be renamed.
93      *
94      * @param srcDir the source directory.
95      */

96     public void setSrcDir(File JavaDoc srcDir) {
97         this.srcDir = srcDir;
98     }
99
100     /**
101      * Executes the task.
102      *
103      * @throws BuildException is there is a problem in the task execution.
104      */

105     public void execute() throws BuildException {
106
107         // first off, make sure that we've got a from and to extension
108
if (fromExtension == null || toExtension == null || srcDir == null) {
109             throw new BuildException("srcDir, fromExtension and toExtension "
110                 + "attributes must be set!");
111         }
112
113         log("DEPRECATED - The renameext task is deprecated. Use move instead.",
114             Project.MSG_WARN);
115         log("Replace this with:", Project.MSG_INFO);
116         log("<move todir=\"" + srcDir + "\" overwrite=\"" + replace + "\">",
117             Project.MSG_INFO);
118         log(" <fileset dir=\"" + srcDir + "\" />", Project.MSG_INFO);
119         log(" <mapper type=\"glob\"", Project.MSG_INFO);
120         log(" from=\"*" + fromExtension + "\"", Project.MSG_INFO);
121         log(" to=\"*" + toExtension + "\" />", Project.MSG_INFO);
122         log("</move>", Project.MSG_INFO);
123         log("using the same patterns on <fileset> as you\'ve used here",
124             Project.MSG_INFO);
125
126         Move move = new Move();
127         move.bindToOwner(this);
128         move.setOwningTarget(getOwningTarget());
129         move.setTaskName(getTaskName());
130         move.setLocation(getLocation());
131         move.setTodir(srcDir);
132         move.setOverwrite(replace);
133
134         fileset.setDir(srcDir);
135         move.addFileset(fileset);
136
137         Mapper me = move.createMapper();
138         me.setType(globType);
139         me.setFrom("*" + fromExtension);
140         me.setTo("*" + toExtension);
141
142         move.execute();
143     }
144
145 }
146
Popular Tags