001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022
023 package org.jboss.dna.common.util;
024
025 import java.io.File;
026 import java.net.MalformedURLException;
027 import java.net.URL;
028
029 public class FileUtil {
030
031 /**
032 * Delete the file or directory at the supplied path. This method works on a directory that is not empty, unlike the
033 * {@link File#delete()} method.
034 *
035 * @param path the path to the file or directory that is to be deleted
036 * @return true if the file or directory at the supplied path existed and was successfully deleted, or false otherwise
037 */
038 public static boolean delete( String path ) {
039 if (path == null || path.trim().length() == 0) return false;
040 return delete(new File(path));
041 }
042
043 /**
044 * Delete the file or directory given by the supplied reference. This method works on a directory that is not empty, unlike
045 * the {@link File#delete()} method.
046 *
047 * @param fileOrDirectory the reference to the Java File object that is to be deleted
048 * @return true if the supplied file or directory existed and was successfully deleted, or false otherwise
049 */
050 public static boolean delete( File fileOrDirectory ) {
051 if (fileOrDirectory == null) return false;
052 if (!fileOrDirectory.exists()) return false;
053
054 // The file/directory exists, so if a directory delete all of the contents ...
055 if (fileOrDirectory.isDirectory()) {
056 for (File childFile : fileOrDirectory.listFiles()) {
057 delete(childFile); // recursive call (good enough for now until we need something better)
058 }
059 // Now an empty directory ...
060 }
061 // Whether this is a file or empty directory, just delete it ...
062 return fileOrDirectory.delete();
063 }
064
065 /**
066 * Utility to convert {@link File} to {@link URL}.
067 *
068 * @param filePath the path of the file
069 * @return the {@link URL} representation of the file.
070 * @throws MalformedURLException
071 * @throws IllegalArgumentException if the file path is null, empty or blank
072 */
073 public static URL convertFileToURL( String filePath ) throws MalformedURLException {
074 CheckArg.isNotEmpty(filePath, "filePath");
075 File file = new File(filePath.trim());
076 return file.toURI().toURL();
077 }
078
079 }