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 * TODO - Allow selector to be specified.
029 * </p>
030 */
031public class DeleteTask extends VfsTask {
032
033    private String file;
034    private String srcDirUrl;
035    private String filesList;
036
037    /**
038     * Constructs a new instance.
039     */
040    public DeleteTask() {
041        // empty
042    }
043
044    /**
045     * Executes this task.
046     *
047     * @throws BuildException if an error occurs.
048     */
049    @Override
050    public void execute() throws BuildException {
051        if (srcDirUrl == null ? file == null : filesList == null) {
052            final String message = Messages.getString("vfs.tasks/delete.no-source-files.error");
053            throw new BuildException(message);
054        }
055
056        try {
057            if (srcDirUrl != null) {
058                log("Deleting " + filesList + " in the directory " + srcDirUrl);
059                if (!srcDirUrl.endsWith("/")) {
060                    srcDirUrl += "/";
061                }
062                final StringTokenizer tok = new StringTokenizer(filesList, ", \t\n\r\f", false);
063                while (tok.hasMoreTokens()) {
064                    final String nextFile = tok.nextToken();
065                    final FileObject srcFile = resolveFile(srcDirUrl + nextFile);
066                    srcFile.deleteAll();
067                }
068            } else {
069                final FileObject srcFile = resolveFile(file);
070                log("Deleting " + srcFile.getPublicURIString());
071                srcFile.deleteAll();
072            }
073        } catch (final Exception e) {
074            throw new BuildException(e);
075        }
076    }
077
078    /**
079     * Sets the file/folder to delete.
080     *
081     * @param file The name of the file.
082     */
083    public void setFile(final String file) {
084        this.file = file;
085    }
086
087    /**
088     * Sets the files to include.
089     *
090     * @param filesList The list of files.
091     */
092    public void setIncludes(final String filesList) {
093        this.filesList = filesList;
094    }
095
096    /**
097     * Sets the source directory.
098     *
099     * @param srcDirUrl The source directory.
100     */
101    public void setSrcDir(final String srcDirUrl) {
102        this.srcDirUrl = srcDirUrl;
103    }
104}