01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.commons.vfs.tasks;
18:
19: import org.apache.commons.vfs.FileObject;
20: import org.apache.commons.vfs.Selectors;
21: import org.apache.commons.vfs.util.Messages;
22: import org.apache.tools.ant.BuildException;
23:
24: import java.util.StringTokenizer;
25:
26: /**
27: * An Ant task that deletes matching files.
28: *
29: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
30: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
31: * @todo Allow selector to be specified.
32: */
33: public class DeleteTask extends VfsTask {
34: private String file;
35: private String srcDirUrl;
36: private String filesList;
37:
38: /**
39: * Sets the file/folder to delete.
40: *
41: * @param file
42: */
43: public void setFile(final String file) {
44: this .file = file;
45: }
46:
47: /**
48: * Sets the source directory
49: */
50: public void setSrcDir(final String srcDir) {
51: this .srcDirUrl = srcDir;
52: }
53:
54: /**
55: * Sets the files to includes
56: */
57: public void setIncludes(final String filesList) {
58: this .filesList = filesList;
59: }
60:
61: /**
62: * Executes this task.
63: */
64: public void execute() throws BuildException {
65: if ((file == null && srcDirUrl == null)
66: || (srcDirUrl != null && filesList == null)) {
67: final String message = Messages
68: .getString("vfs.tasks/delete.no-source-files.error");
69: throw new BuildException(message);
70: }
71:
72: try {
73: if (srcDirUrl != null && filesList != null) {
74: log("Deleting " + filesList + " in the directory "
75: + srcDirUrl);
76: if (!srcDirUrl.endsWith("/")) {
77: srcDirUrl += "/";
78: }
79: StringTokenizer tok = new StringTokenizer(filesList,
80: ", \t\n\r\f", false);
81: while (tok.hasMoreTokens()) {
82: String nextFile = tok.nextToken();
83: final FileObject srcFile = resolveFile(srcDirUrl
84: + nextFile);
85: srcFile.delete(Selectors.SELECT_ALL);
86: }
87: } else {
88: final FileObject srcFile = resolveFile(file);
89: log("Deleting " + srcFile);
90: srcFile.delete(Selectors.SELECT_ALL);
91: }
92: } catch (final Exception e) {
93: throw new BuildException(e);
94: }
95: }
96: }
|