KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > tasks > DeleteTask


1 /*
2  * Copyright 2002-2005 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 package org.apache.commons.vfs.tasks;
17
18 import org.apache.commons.vfs.FileObject;
19 import org.apache.commons.vfs.Selectors;
20 import org.apache.commons.vfs.util.Messages;
21 import org.apache.tools.ant.BuildException;
22
23 import java.util.StringTokenizer JavaDoc;
24
25 /**
26  * An Ant task that deletes matching files.
27  *
28  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
29  * @todo Allow selector to be specified.
30  */

31 public class DeleteTask
32     extends VfsTask
33 {
34     private String JavaDoc file;
35     private String JavaDoc srcDirUrl;
36     private String JavaDoc filesList;
37
38     /**
39      * Sets the file/folder to delete.
40      *
41      * @param file
42      */

43     public void setFile(final String JavaDoc file)
44     {
45         this.file = file;
46     }
47
48     /**
49      * Sets the source directory
50      */

51     public void setSrcDir(final String JavaDoc srcDir)
52     {
53         this.srcDirUrl = srcDir;
54     }
55
56     /**
57      * Sets the files to includes
58      */

59     public void setIncludes(final String JavaDoc filesList)
60     {
61         this.filesList = filesList;
62     }
63
64     /**
65      * Executes this task.
66      */

67     public void execute() throws BuildException
68     {
69         if ((file == null && srcDirUrl == null) || (srcDirUrl != null && filesList == null))
70         {
71             final String JavaDoc message = Messages.getString("vfs.tasks/delete.no-source-files.error");
72             throw new BuildException(message);
73         }
74
75         try
76         {
77             if (srcDirUrl != null && filesList != null)
78             {
79                 log("Deleting " + filesList + " in the directory " + srcDirUrl);
80                 if (!srcDirUrl.endsWith("/"))
81                 {
82                     srcDirUrl += "/";
83                 }
84                 StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(filesList, ", \t\n\r\f", false);
85                 while (tok.hasMoreTokens())
86                 {
87                     String JavaDoc nextFile = tok.nextToken();
88                     final FileObject srcFile = resolveFile(srcDirUrl + nextFile);
89                     srcFile.delete(Selectors.SELECT_ALL);
90                 }
91             }
92             else
93             {
94                 final FileObject srcFile = resolveFile(file);
95                 log("Deleting " + srcFile);
96                 srcFile.delete(Selectors.SELECT_ALL);
97             }
98         }
99         catch (final Exception JavaDoc e)
100         {
101             throw new BuildException(e);
102         }
103     }
104 }
105
Popular Tags