KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > CopyPath


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 package org.apache.tools.ant.taskdefs;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.Task;
24 import org.apache.tools.ant.util.FileUtils;
25 import org.apache.tools.ant.util.FileNameMapper;
26 import org.apache.tools.ant.types.Path;
27 import org.apache.tools.ant.types.Reference;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 /**
33  * Copy the contents of a path to a destination, using the mapper of choice
34  *
35  * @since Ant 1.7
36  *
37  * @ant.task category="filesystem"
38  */

39
40 public class CopyPath extends Task {
41
42     // Error messages
43
/** No destdir attribute */
44     public static final String JavaDoc ERROR_NO_DESTDIR = "No destDir specified";
45
46     /** No path */
47     public static final String JavaDoc ERROR_NO_PATH = "No path specified";
48
49     /** No mapper */
50     public static final String JavaDoc ERROR_NO_MAPPER = "No mapper specified";
51
52     // fileutils
53
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
54
55     // --- Fields --
56
private FileNameMapper mapper;
57
58     private Path path;
59
60     private File JavaDoc destDir;
61
62     // TODO not read, yet in a public setter
63
private long granularity = FILE_UTILS.getFileTimestampGranularity();
64
65     private boolean preserveLastModified = false;
66
67     /**
68      * The dest dir attribute.
69      * @param destDir the value of the destdir attribute.
70      */

71     public void setDestDir(File JavaDoc destDir) {
72         this.destDir = destDir;
73     }
74
75     /**
76      * add a mapper
77      *
78      * @param newmapper the mapper to add.
79      */

80     public void add(FileNameMapper newmapper) {
81         if (mapper != null) {
82             throw new BuildException("Only one mapper allowed");
83         }
84         mapper = newmapper;
85     }
86
87     /**
88      * Set the path to be used when running the Java class.
89      *
90      * @param s
91      * an Ant Path object containing the path.
92      */

93     public void setPath(Path s) {
94         createPath().append(s);
95     }
96
97     /**
98      * Set the path to use by reference.
99      *
100      * @param r
101      * a reference to an existing path.
102      */

103     public void setPathRef(Reference r) {
104         createPath().setRefid(r);
105     }
106
107     /**
108      * Create a path.
109      *
110      * @return a path to be configured.
111      */

112     public Path createPath() {
113         if (path == null) {
114             path = new Path(getProject());
115         }
116         return path;
117     }
118
119     /**
120      * Set the number of milliseconds leeway to give before deciding a
121      * target is out of date.
122      * TODO: This is not yet used.
123      * @param granularity the granularity used to decide if a target is out of
124      * date.
125      */

126     public void setGranularity(long granularity) {
127         this.granularity = granularity;
128     }
129
130     /**
131      * Give the copied files the same last modified time as the original files.
132      * @param preserveLastModified if true preserve the modified time;
133      * default is false.
134      */

135     public void setPreserveLastModified(boolean preserveLastModified) {
136         this.preserveLastModified = preserveLastModified;
137     }
138
139     /**
140      * Ensure we have a consistent and legal set of attributes, and set any
141      * internal flags necessary based on different combinations of attributes.
142      *
143      * @throws BuildException
144      * if an error occurs.
145      */

146     protected void validateAttributes() throws BuildException {
147         if (destDir == null) {
148             throw new BuildException(ERROR_NO_DESTDIR);
149         }
150         if (mapper == null) {
151             throw new BuildException(ERROR_NO_MAPPER);
152         }
153         if (path == null) {
154             throw new BuildException(ERROR_NO_PATH);
155         }
156     }
157
158     /**
159      * This is a very minimal derivative of the nomal copy logic.
160      *
161      * @throws BuildException
162      * if something goes wrong with the build.
163      */

164     public void execute() throws BuildException {
165         validateAttributes();
166         String JavaDoc[] sourceFiles = path.list();
167         if (sourceFiles.length == 0) {
168             log("Path is empty", Project.MSG_VERBOSE);
169             return;
170         }
171
172         for (int sources = 0; sources < sourceFiles.length; sources++) {
173
174             String JavaDoc sourceFileName = sourceFiles[sources];
175             File JavaDoc sourceFile = new File JavaDoc(sourceFileName);
176             String JavaDoc[] toFiles = (String JavaDoc[]) mapper.mapFileName(sourceFileName);
177
178             for (int i = 0; i < toFiles.length; i++) {
179                 String JavaDoc destFileName = toFiles[i];
180                 File JavaDoc destFile = new File JavaDoc(destDir, destFileName);
181
182                 if (sourceFile.equals(destFile)) {
183                     log("Skipping self-copy of " + sourceFileName, Project.MSG_VERBOSE);
184                     continue;
185                 }
186                 if (sourceFile.isDirectory()) {
187                     log("Skipping directory " + sourceFileName);
188                     continue;
189                 }
190                 try {
191                     log("Copying " + sourceFile + " to " + destFile, Project.MSG_VERBOSE);
192
193                     FILE_UTILS.copyFile(sourceFile, destFile, null, null, false,
194                             preserveLastModified, null, null, getProject());
195                 } catch (IOException JavaDoc ioe) {
196                     String JavaDoc msg = "Failed to copy " + sourceFile + " to " + destFile + " due to "
197                             + ioe.getMessage();
198                     if (destFile.exists() && !destFile.delete()) {
199                         msg += " and I couldn't delete the corrupt " + destFile;
200                     }
201                     throw new BuildException(msg, ioe, getLocation());
202                 }
203             }
204         }
205     }
206 }
207
Popular Tags