View Javadoc

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  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-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
31   * @todo Allow selector to be specified.
32   */
33  public class DeleteTask
34      extends VfsTask
35  {
36      private String file;
37      private String srcDirUrl;
38      private String filesList;
39  
40      /***
41       * Sets the file/folder to delete.
42       *
43       * @param file
44       */
45      public void setFile(final String file)
46      {
47          this.file = file;
48      }
49  
50      /***
51       * Sets the source directory
52       */
53      public void setSrcDir(final String srcDir)
54      {
55          this.srcDirUrl = srcDir;
56      }
57  
58      /***
59       * Sets the files to includes
60       */
61      public void setIncludes(final String filesList)
62      {
63          this.filesList = filesList;
64      }
65  
66      /***
67       * Executes this task.
68       */
69      public void execute() throws BuildException
70      {
71          if ((file == null && srcDirUrl == null) || (srcDirUrl != null && filesList == null))
72          {
73              final String message = Messages.getString("vfs.tasks/delete.no-source-files.error");
74              throw new BuildException(message);
75          }
76  
77          try
78          {
79              if (srcDirUrl != null && filesList != null)
80              {
81                  log("Deleting " + filesList + " in the directory " + srcDirUrl);
82                  if (!srcDirUrl.endsWith("/"))
83                  {
84                      srcDirUrl += "/";
85                  }
86                  StringTokenizer tok = new StringTokenizer(filesList, ", \t\n\r\f", false);
87                  while (tok.hasMoreTokens())
88                  {
89                      String nextFile = tok.nextToken();
90                      final FileObject srcFile = resolveFile(srcDirUrl + nextFile);
91                      srcFile.delete(Selectors.SELECT_ALL);
92                  }
93              }
94              else
95              {
96                  final FileObject srcFile = resolveFile(file);
97                  log("Deleting " + srcFile);
98                  srcFile.delete(Selectors.SELECT_ALL);
99              }
100         }
101         catch (final Exception e)
102         {
103             throw new BuildException(e);
104         }
105     }
106 }