001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.tasks; 018 019import java.util.StringTokenizer; 020 021import org.apache.commons.vfs2.FileObject; 022import org.apache.commons.vfs2.util.Messages; 023import org.apache.tools.ant.BuildException; 024 025/** 026 * An Ant task that deletes matching files. 027 * <p> 028 * TOOD - Allow selector to be specified. 029 */ 030public class DeleteTask extends VfsTask { 031 private String file; 032 private String srcDirUrl; 033 private String filesList; 034 035 /** 036 * Sets the file/folder to delete. 037 * 038 * @param file The name of the file. 039 */ 040 public void setFile(final String file) { 041 this.file = file; 042 } 043 044 /** 045 * Sets the source directory. 046 * 047 * @param srcDir The source directory. 048 */ 049 public void setSrcDir(final String srcDir) { 050 this.srcDirUrl = srcDir; 051 } 052 053 /** 054 * Sets the files to include. 055 * 056 * @param filesList The list of files. 057 */ 058 public void setIncludes(final String filesList) { 059 this.filesList = filesList; 060 } 061 062 /** 063 * Executes this task. 064 * 065 * @throws BuildException if an error occurs. 066 */ 067 @Override 068 public void execute() throws BuildException { 069 if ((file == null && srcDirUrl == null) || (srcDirUrl != null && filesList == null)) { 070 final String message = Messages.getString("vfs.tasks/delete.no-source-files.error"); 071 throw new BuildException(message); 072 } 073 074 try { 075 if (srcDirUrl != null && filesList != null) { 076 log("Deleting " + filesList + " in the directory " + srcDirUrl); 077 if (!srcDirUrl.endsWith("/")) { 078 srcDirUrl += "/"; 079 } 080 final StringTokenizer tok = new StringTokenizer(filesList, ", \t\n\r\f", false); 081 while (tok.hasMoreTokens()) { 082 final String nextFile = tok.nextToken(); 083 final FileObject srcFile = resolveFile(srcDirUrl + nextFile); 084 srcFile.deleteAll(); 085 } 086 } else { 087 final FileObject srcFile = resolveFile(file); 088 log("Deleting " + srcFile.getPublicURIString()); 089 srcFile.deleteAll(); 090 } 091 } catch (final Exception e) { 092 throw new BuildException(e); 093 } 094 } 095}