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.vfs2.tasks;
18  
19  import java.util.StringTokenizer;
20  
21  import org.apache.commons.vfs2.FileObject;
22  import org.apache.commons.vfs2.util.Messages;
23  import org.apache.tools.ant.BuildException;
24  
25  /**
26   * An Ant task that deletes matching files.
27   * <p>
28   * TOOD - Allow selector to be specified.
29   * </p>
30   */
31  public class DeleteTask extends VfsTask {
32      private String file;
33      private String srcDirUrl;
34      private String filesList;
35  
36      /**
37       * Sets the file/folder to delete.
38       *
39       * @param file The name of the file.
40       */
41      public void setFile(final String file) {
42          this.file = file;
43      }
44  
45      /**
46       * Sets the source directory.
47       *
48       * @param srcDir The source directory.
49       */
50      public void setSrcDir(final String srcDir) {
51          this.srcDirUrl = srcDir;
52      }
53  
54      /**
55       * Sets the files to include.
56       *
57       * @param filesList The list of files.
58       */
59      public void setIncludes(final String filesList) {
60          this.filesList = filesList;
61      }
62  
63      /**
64       * Executes this task.
65       *
66       * @throws BuildException if an error occurs.
67       */
68      @Override
69      public void execute() throws BuildException {
70          if ((file == null && srcDirUrl == null) || (srcDirUrl != null && filesList == null)) {
71              final String message = Messages.getString("vfs.tasks/delete.no-source-files.error");
72              throw new BuildException(message);
73          }
74  
75          try {
76              if (srcDirUrl != null) {
77                  log("Deleting " + filesList + " in the directory " + srcDirUrl);
78                  if (!srcDirUrl.endsWith("/")) {
79                      srcDirUrl += "/";
80                  }
81                  final StringTokenizer tok = new StringTokenizer(filesList, ", \t\n\r\f", false);
82                  while (tok.hasMoreTokens()) {
83                      final String nextFile = tok.nextToken();
84                      final FileObject srcFile = resolveFile(srcDirUrl + nextFile);
85                      srcFile.deleteAll();
86                  }
87              } else {
88                  final FileObject srcFile = resolveFile(file);
89                  log("Deleting " + srcFile.getPublicURIString());
90                  srcFile.deleteAll();
91              }
92          } catch (final Exception e) {
93              throw new BuildException(e);
94          }
95      }
96  }