You are viewing a plain text version of this content. The canonical link for it is here.
Posted to muse-commits@ws.apache.org by da...@apache.org on 2006/07/17 05:51:23 UTC

svn commit: r422613 - /webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/FileUtils.java

Author: danj
Date: Sun Jul 16 20:51:20 2006
New Revision: 422613

URL: http://svn.apache.org/viewvc?rev=422613&view=rev
Log:
patch from Andrew Eberbach - the FileUtils.loadFromContext() method is used by the new components in muse-tools (MUSE-24).

Modified:
    webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/FileUtils.java

Modified: webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/FileUtils.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/FileUtils.java?rev=422613&r1=422612&r2=422613&view=diff
==============================================================================
--- webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/FileUtils.java (original)
+++ webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/FileUtils.java Sun Jul 16 20:51:20 2006
@@ -1,563 +1,591 @@
-/*=============================================================================*
- *  Copyright 2006 The Apache Software Foundation
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *=============================================================================*/
-
-package org.apache.muse.util;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.io.Writer;
-
-import org.apache.muse.util.messages.Messages;
-import org.apache.muse.util.messages.MessagesFactory;
-
-/**
- * 
- * FileUtils is a collection of routines for common file system operations.
- *
- * @author Dan Jemiolo (danj)
- *
- */
-
-public final class FileUtils
-{
-    //
-    // Used to lookup all exception messages
-    //
-    private static Messages _MESSAGES = MessagesFactory.get(FileUtils.class);
-    
-    /**
-     * 
-     * The application's current working directory.
-     * 
-     */
-    public static final File CURRENT_DIR = new File(".");
-    
-    /**
-     * 
-     * Merges the two paths to create a valid version of the second path. 
-     * This method should be used when you encounter a relative path in 
-     * a document and must resolve it based on the path of the current 
-     * document. An example would be:
-     * <br><br>
-     * <b>original path</b> - files/customers/Orders.xml
-     * <br><br>
-     * <b>relative path</b> - ../Accounts.xml
-     * <br><br>
-     * <b>result</b> - files/customers/Accounts.xml
-     * <br><br>
-     * The only time this method cannot be used is if the original path 
-     * is for a file that is in the root (has no directory as part of 
-     * its path) and the relative path starts with "..".
-     *
-     * @param originalPath
-     *        The path of the file that references another file.
-     * 
-     * @param relativePath
-     *        The path of the other file, which is relative to the original.
-     * 
-     * @return A proper path for the other file, one that can be used to 
-     *         open and verify the file.
-     *
-     */
-    public static String createRelativePath(String originalPath, 
-                                            String relativePath)
-    {
-        if (originalPath == null)
-            throw new NullPointerException(_MESSAGES.get("NullOriginalPath"));
-
-        if (relativePath == null)
-            throw new NullPointerException(_MESSAGES.get("NullRelativePath"));
-        
-        //
-        // remove any .. reference by taking off the last section/ of 
-        // the original path
-        //
-        if (relativePath.startsWith("../"))
-        {
-            int slash = originalPath.lastIndexOf('/');
-            originalPath = originalPath.substring(0, slash);
-            relativePath = relativePath.substring(3);
-        }
-        
-        int slash = originalPath.lastIndexOf('/');
-        
-        if (slash < 0)
-            return relativePath;
-        
-        String dir = originalPath.substring(0, slash + 1);
-        return dir + relativePath;
-    }
-    
-    /**
-     * 
-     * This is a convenience method that calls find(File, String, boolean) 
-     * with the last parameter set to "false" (does not match directories).
-     * 
-     * @see #find(File, String, boolean)
-     *
-     */
-    public static File find(File contextRoot, String fileName)
-    {
-        return find(contextRoot, fileName, false);
-    }
-    
-    /**
-     * 
-     * Searches through the directory tree under the given context directory 
-     * and finds the first file that matches the file name. If the third 
-     * parameter is true, the method will also try to match directories, not 
-     * just "regular" files.
-     * 
-     * @param contextRoot
-     *        The directory to start the search from.
-     * 
-     * @param fileName
-     *        The name of the file (or directory) to search for.
-     * 
-     * @param matchDirectories
-     *        True if the method should try and match the name against 
-     *        directory names, not just file names.
-     * 
-     * @return The java.io.File representing the <em>first</em> file or 
-     *         directory with the given name, or null if it was not found.
-     *
-     */
-    public static File find(File contextRoot, String fileName, boolean matchDirectories)
-    {
-        if (contextRoot == null)
-            throw new NullPointerException(_MESSAGES.get("NullContextRoot"));
-        
-        if (fileName == null)
-            throw new NullPointerException(_MESSAGES.get("NullFileName"));
-        
-        if (!contextRoot.isDirectory())
-        {
-            Object[] filler = { contextRoot.getAbsolutePath() };
-            String message = _MESSAGES.get("NotDirectory", filler);
-            throw new IllegalArgumentException(message);
-        }
-        
-        File[] files = contextRoot.listFiles();
-        
-        //
-        // for all children of the current directory...
-        //
-        for (int n = 0; n < files.length; ++n)
-        {
-            String nextName = files[n].getName();
-
-            //
-            // if we find a directory, there are two possibilities:
-            //
-            // 1. the names match, AND we are told to match directories.
-            //    in this case we're done
-            //
-            // 2. not told to match directories, so recurse 
-            //
-            if (files[n].isDirectory())
-            {
-                if (nextName.equals(fileName) && matchDirectories)
-                    return files[n];
-                
-                File match = find(files[n], fileName);
-                
-                if (match != null)
-                    return match;
-            }
-            
-            //
-            // in the case of regular files, just check the names
-            //
-            else if (nextName.equals(fileName))
-                return files[n];            
-        }
-        
-        return null;
-    }
-    
-	private static void copyDirectory(File source, File destination)
-		throws IOException
-	{
-		File nextDirectory = new File(destination, source.getName());
-
-		//
-		// create the directory if necessary...
-		//
-		if (!nextDirectory.exists() && !nextDirectory.mkdirs())
-        {
-            Object[] filler = { nextDirectory.getAbsolutePath() };
-            String message = _MESSAGES.get("DirCopyFailed", filler);
-			throw new IOException(message);
-        }
-
-		File[] files = source.listFiles();
-
-		//
-		// and then all the items below the directory...
-		//
-		for (int n = 0; n < files.length; ++n)
-		{
-			File nextFile = files[n];
-
-			if (nextFile.isDirectory())
-				copyDirectory(nextFile, nextDirectory);
-
-			else
-				copyFile(nextFile, nextDirectory);
-		}
-	}
-    
-	private static void copyFile(File source, File destination)
-		throws IOException
-	{
-		FileInputStream input = null;
-		FileOutputStream output = null;
-
-        //
-        // if the destination is a dir, what we really want to do is create 
-        // a file with the same name in that dir
-        //
-		if (destination.isDirectory())
-			destination = new File(destination, source.getName());
-
-        //
-        // how is this not in the JDK?
-        //
-		try
-		{
-			input = new FileInputStream(source);
-			output = new FileOutputStream(destination);
-
-			byte[] buffer = new byte[1024];
-
-			int bytesRead = input.read(buffer);
-
-			while (bytesRead >= 0)
-			{
-				output.write(buffer, 0, bytesRead);
-				bytesRead = input.read(buffer);
-			}
-		}
-
-		finally
-		{
-			if (input != null)
-				close(input);
-			
-			if (output != null)
-				close(output);
-		}
-
-	}
-
-	/**
-     * 
-     * Copies the first file or directory to the second file or directory. 
-     * <br><br>
-     * If the first parameter is a file and the second is a file, then the 
-     * method copies the contents of the first file into the second. If the 
-     * second file does not exist, it is created.
-     * <br><br>
-     * If the first parameter is a file and the second is a directory, the 
-     * file is copied to the directory, overwriting any existing copy.
-     * <br><br>
-     * If the first parameter is a directory and the second is a directory, 
-     * the first is copied underneath the second.
-     * <br><br>
-     * If the first parameter is a directory and the second is a file name 
-     * or does not exist, a directory with that name is created, and the 
-     * contents of the first directory are copied there.
-	 *
-	 * @param source
-	 * @param destination
-     * 
-	 * @throws IOException
-     *         <ul>
-     *         <li>If the source does not exist.</li>
-     *         <li>If the user does not have permission to modify the 
-     *         destination.</li>
-     *         <li>If the copy fails for some reason related to system I/O.</li>
-     *         </ul>
-	 *
-	 */
-	public static void copy(File source, File destination)
-		throws IOException
-	{
-        if (source == null)
-            throw new NullPointerException(_MESSAGES.get("NullSource"));
-        
-        if (destination == null)
-            throw new NullPointerException(_MESSAGES.get("NullDestination"));
-        
-		if (source.isDirectory())
-			copyDirectory(source, destination);
-
-		else
-			copyFile(source, destination);
-	}
-    
-    /**
-	 *
-	 * Starts at the directory given and tests to see whether it is empty; if 
-     * so, it deletes it and moves up the directory tree, deleting empty 
-     * directories until it finds a non-empty one.
-	 *
-     * @param directory
-     *        The first directory to test.
-     * 
-     * @throws IOException
-     *         <ul>
-     *         <li>If the directory does not exist or the user does not have 
-     *         permission to delete it or its parents.</li>
-     *         </ul>
-	 *
-     */
-	public static void pruneEmptyDirectories(File directory)
-		throws IOException
-	{
-        if (directory == null)
-            throw new NullPointerException(_MESSAGES.get("NullFile"));
-    
-        if (!directory.isDirectory())
-        {
-            Object[] filler = { directory.getAbsolutePath() };
-            String message = _MESSAGES.get("NotDirectory", filler);
-            throw new IllegalArgumentException(message);
-        }
-		
-        //
-		// check to see if the directory is now empty and, if so, delete it 
-        // too, moving up the tree until we find one with stuff in it
-		//
-		while (directory != null)
-		{
-			File[] directoryFiles = directory.listFiles();
-
-			//
-			// if the directory has files, we're done
-			//
-			if (directoryFiles.length > 0)
-				break;
-
-			if (!directory.delete())
-            {
-                Object[] filler = { directory.getAbsolutePath() };
-                String message = _MESSAGES.get("DeleteFailed", filler);
-                throw new IOException(message);
-            }
-            
-			//
-			// go up the tree
-			//
-			directory = directory.getParentFile();
-		}
-	}
-    
-	private static void removeDirectory(File directory)
-		throws IOException
-	{
-		File[] files = directory.listFiles();
-
-		//
-		// for all items in the directory...
-		//
-		for (int n = 0; n < files.length; ++n)
-		{
-			File nextFile = files[n];
-
-			//
-			// if it's a directory, delete sub-directories and files before
-			// removing the empty directory
-			//
-			if (nextFile.isDirectory())
-				removeDirectory(nextFile);
-
-			//
-			// otherwise just delete the file - do NOT prune the directory
-			// in advance
-			//
-			else
-				removeFile(nextFile);
-		}
-
-		//
-		// now that everything's gone, delete the specified directory
-		//
-		if (!directory.delete())
-        {
-            Object[] filler = { directory.getAbsolutePath() };
-            String message = _MESSAGES.get("DeleteFailed", filler);
-            throw new IOException(message);
-        }
-	}
-    
-	private static void removeFile(File file)
-		throws IOException
-	{
-		//
-		// make sure the file exists, then delete it
-		//
-		
-		if (!file.exists())
-			throw new FileNotFoundException(file.getAbsolutePath());
-		
-		if (!file.delete())
-        {
-            Object[] filler = { file.getAbsolutePath() };
-            String message = _MESSAGES.get("DeleteFailed", filler);
-            throw new IOException(message);
-        }
-	}
-
-	/**
-     * 
-     * This is a convenience method that calls remove(File, boolean) with 
-     * the second parameter set to "false" (doesn't prune empty directories).
-     * 
-     * @see #remove(File, boolean)
-	 *
-	 */
-	public static void remove(File file)
-		throws IOException
-	{
-		remove(file, false);
-	}
-
-	/**
-     * 
-	 * @param file
-     *        The file or directory to delete.
-     * 
-	 * @param pruneEmptyDirectories
-     *        True if the deletion results in an empty parent directory. If 
-     *        set to true, this method will traverse up the directory tree, 
-     *        deleting directories that are made empty by the deletion.
-     * 
-	 * @throws IOException
-     *         <ul>
-     *         <li>If there was an error trying to remove the file or 
-     *         directory. The file system may be in an inconsistent state 
-     *         when the exception is thrown (directories may be partially 
-     *         deleted, etc.).</li>
-     *         </ul>
-	 *
-	 */
-	public static void remove(File file, boolean pruneEmptyDirectories)
-		throws IOException
-	{
-        if (file == null)
-            throw new NullPointerException(_MESSAGES.get("NullFile"));
-        
-		if (file.isDirectory())
-			removeDirectory(file);
-
-		else
-			removeFile(file);
-
-		if (pruneEmptyDirectories)
-			pruneEmptyDirectories(file.getParentFile());
-	}
-    
-    /**
-     * 
-     * This is a convenience method that throws an unchecked exception if 
-     * the stream could not be closed.
-     *
-     */	
-	public static void close(InputStream stream)
-	{
-		try
-		{
-			stream.close();
-		}
-		
-		catch (IOException error)
-		{
-			//
-			// what can we do?
-			//
-		}
-	}
-    
-    /**
-     * 
-     * This is a convenience method that throws an unchecked exception if 
-     * the reader could not be closed.
-     *
-     */	
-	public static void close(Reader reader)
-	{
-		try
-		{
-			reader.close();
-		}
-		
-		catch (IOException error)
-		{
-			//
-			// what can we do?
-			//
-		}
-	}
-    
-    /**
-     * 
-     * This is a convenience method that throws an unchecked exception if 
-     * the stream could not be closed.
-     *
-     */	
-	public static void close(OutputStream stream)
-	{
-		try
-		{
-			stream.close();
-		}
-		
-		catch (IOException error)
-		{
-			//
-			// what can we do?
-			//
-		}
-	}
-	
-    /**
-     * 
-     * This is a convenience method that throws an unchecked exception if 
-     * the writer could not be closed.
-     *
-     */
-	public static void close(Writer writer)
-	{
-		try
-		{
-			writer.close();
-		}
-		
-		catch (IOException error)
-		{
-			//
-			// what can we do?
-			//
-		}
-	}
-}
+/*=============================================================================*
+ *  Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *=============================================================================*/
+
+package org.apache.muse.util;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.URI;
+import java.net.URL;
+
+import org.apache.muse.util.messages.Messages;
+import org.apache.muse.util.messages.MessagesFactory;
+
+/**
+ * 
+ * FileUtils is a collection of routines for common file system operations.
+ *
+ * @author Dan Jemiolo (danj)
+ *
+ */
+
+public final class FileUtils
+{
+    //
+    // Used to lookup all exception messages
+    //
+    private static Messages _MESSAGES = MessagesFactory.get(FileUtils.class);
+    
+    /**
+     * 
+     * The application's current working directory.
+     * 
+     */
+    public static final File CURRENT_DIR = new File(".");
+    
+    /**
+     * 
+     * Merges the two paths to create a valid version of the second path. 
+     * This method should be used when you encounter a relative path in 
+     * a document and must resolve it based on the path of the current 
+     * document. An example would be:
+     * <br><br>
+     * <b>original path</b> - files/customers/Orders.xml
+     * <br><br>
+     * <b>relative path</b> - ../Accounts.xml
+     * <br><br>
+     * <b>result</b> - files/customers/Accounts.xml
+     * <br><br>
+     * The only time this method cannot be used is if the original path 
+     * is for a file that is in the root (has no directory as part of 
+     * its path) and the relative path starts with "..".
+     *
+     * @param originalPath
+     *        The path of the file that references another file.
+     * 
+     * @param relativePath
+     *        The path of the other file, which is relative to the original.
+     * 
+     * @return A proper path for the other file, one that can be used to 
+     *         open and verify the file.
+     *
+     */
+    public static String createRelativePath(String originalPath, 
+                                            String relativePath)
+    {
+        if (originalPath == null)
+            throw new NullPointerException(_MESSAGES.get("NullOriginalPath"));
+
+        if (relativePath == null)
+            throw new NullPointerException(_MESSAGES.get("NullRelativePath"));
+        
+        //
+        // remove any .. reference by taking off the last section/ of 
+        // the original path
+        //
+        if (relativePath.startsWith("../"))
+        {
+            int slash = originalPath.lastIndexOf('/');
+            originalPath = originalPath.substring(0, slash);
+            relativePath = relativePath.substring(3);
+        }
+        
+        int slash = originalPath.lastIndexOf('/');
+        
+        if (slash < 0)
+            return relativePath;
+        
+        String dir = originalPath.substring(0, slash + 1);
+        return dir + relativePath;
+    }
+    
+    /**
+     * 
+     * This is a convenience method that calls find(File, String, boolean) 
+     * with the last parameter set to "false" (does not match directories).
+     * 
+     * @see #find(File, String, boolean)
+     *
+     */
+    public static File find(File contextRoot, String fileName)
+    {
+        return find(contextRoot, fileName, false);
+    }
+    
+    /**
+     * 
+     * Searches through the directory tree under the given context directory 
+     * and finds the first file that matches the file name. If the third 
+     * parameter is true, the method will also try to match directories, not 
+     * just "regular" files.
+     * 
+     * @param contextRoot
+     *        The directory to start the search from.
+     * 
+     * @param fileName
+     *        The name of the file (or directory) to search for.
+     * 
+     * @param matchDirectories
+     *        True if the method should try and match the name against 
+     *        directory names, not just file names.
+     * 
+     * @return The java.io.File representing the <em>first</em> file or 
+     *         directory with the given name, or null if it was not found.
+     *
+     */
+    public static File find(File contextRoot, String fileName, boolean matchDirectories)
+    {
+        if (contextRoot == null)
+            throw new NullPointerException(_MESSAGES.get("NullContextRoot"));
+        
+        if (fileName == null)
+            throw new NullPointerException(_MESSAGES.get("NullFileName"));
+        
+        if (!contextRoot.isDirectory())
+        {
+            Object[] filler = { contextRoot.getAbsolutePath() };
+            String message = _MESSAGES.get("NotDirectory", filler);
+            throw new IllegalArgumentException(message);
+        }
+        
+        File[] files = contextRoot.listFiles();
+        
+        //
+        // for all children of the current directory...
+        //
+        for (int n = 0; n < files.length; ++n)
+        {
+            String nextName = files[n].getName();
+
+            //
+            // if we find a directory, there are two possibilities:
+            //
+            // 1. the names match, AND we are told to match directories.
+            //    in this case we're done
+            //
+            // 2. not told to match directories, so recurse 
+            //
+            if (files[n].isDirectory())
+            {
+                if (nextName.equals(fileName) && matchDirectories)
+                    return files[n];
+                
+                File match = find(files[n], fileName);
+                
+                if (match != null)
+                    return match;
+            }
+            
+            //
+            // in the case of regular files, just check the names
+            //
+            else if (nextName.equals(fileName))
+                return files[n];            
+        }
+        
+        return null;
+    }
+    
+	private static void copyDirectory(File source, File destination)
+		throws IOException
+	{
+		File nextDirectory = new File(destination, source.getName());
+
+		//
+		// create the directory if necessary...
+		//
+		if (!nextDirectory.exists() && !nextDirectory.mkdirs())
+        {
+            Object[] filler = { nextDirectory.getAbsolutePath() };
+            String message = _MESSAGES.get("DirCopyFailed", filler);
+			throw new IOException(message);
+        }
+
+		File[] files = source.listFiles();
+
+		//
+		// and then all the items below the directory...
+		//
+		for (int n = 0; n < files.length; ++n)
+		{
+			File nextFile = files[n];
+
+			if (nextFile.isDirectory())
+				copyDirectory(nextFile, nextDirectory);
+
+			else
+				copyFile(nextFile, nextDirectory);
+		}
+	}
+    
+	private static void copyFile(File source, File destination)
+		throws IOException
+	{
+		FileInputStream input = null;
+		FileOutputStream output = null;
+
+        //
+        // if the destination is a dir, what we really want to do is create 
+        // a file with the same name in that dir
+        //
+		if (destination.isDirectory())
+			destination = new File(destination, source.getName());
+
+        //
+        // how is this not in the JDK?
+        //
+		try
+		{
+			input = new FileInputStream(source);
+			output = new FileOutputStream(destination);
+
+			byte[] buffer = new byte[1024];
+
+			int bytesRead = input.read(buffer);
+
+			while (bytesRead >= 0)
+			{
+				output.write(buffer, 0, bytesRead);
+				bytesRead = input.read(buffer);
+			}
+		}
+
+		finally
+		{
+			if (input != null)
+				close(input);
+			
+			if (output != null)
+				close(output);
+		}
+
+	}
+
+	/**
+     * 
+     * Copies the first file or directory to the second file or directory. 
+     * <br><br>
+     * If the first parameter is a file and the second is a file, then the 
+     * method copies the contents of the first file into the second. If the 
+     * second file does not exist, it is created.
+     * <br><br>
+     * If the first parameter is a file and the second is a directory, the 
+     * file is copied to the directory, overwriting any existing copy.
+     * <br><br>
+     * If the first parameter is a directory and the second is a directory, 
+     * the first is copied underneath the second.
+     * <br><br>
+     * If the first parameter is a directory and the second is a file name 
+     * or does not exist, a directory with that name is created, and the 
+     * contents of the first directory are copied there.
+	 *
+	 * @param source
+	 * @param destination
+     * 
+	 * @throws IOException
+     *         <ul>
+     *         <li>If the source does not exist.</li>
+     *         <li>If the user does not have permission to modify the 
+     *         destination.</li>
+     *         <li>If the copy fails for some reason related to system I/O.</li>
+     *         </ul>
+	 *
+	 */
+	public static void copy(File source, File destination)
+		throws IOException
+	{
+        if (source == null)
+            throw new NullPointerException(_MESSAGES.get("NullSource"));
+        
+        if (destination == null)
+            throw new NullPointerException(_MESSAGES.get("NullDestination"));
+        
+		if (source.isDirectory())
+			copyDirectory(source, destination);
+
+		else
+			copyFile(source, destination);
+	}
+    	
+	/**
+	 * Gets a java.io.File using a path that is relative to the context
+	 * of the given Class. This uses the Class.getResource() method.
+	 * 
+	 * @param context 
+	 * 	      The classpath from which to load.
+     * 
+	 * @param path
+	 * 		  The context-relative file path.
+     * 
+	 * @return 
+	 * 		  A java.io.File representing the specified path or null if 
+     *        file could not be found.
+     * 
+	 */
+	public static File loadFromContext(Class context, String path) {
+		
+        URL url = context.getResource(path);
+        
+        if (url == null)
+            return null;
+        
+        URI uri = URI.create(url.getFile());
+        return new File(uri.getPath());
+	}
+		
+    /**
+	 *
+	 * Starts at the directory given and tests to see whether it is empty; if 
+     * so, it deletes it and moves up the directory tree, deleting empty 
+     * directories until it finds a non-empty one.
+	 *
+     * @param directory
+     *        The first directory to test.
+     * 
+     * @throws IOException
+     *         <ul>
+     *         <li>If the directory does not exist or the user does not have 
+     *         permission to delete it or its parents.</li>
+     *         </ul>
+	 *
+     */
+	public static void pruneEmptyDirectories(File directory)
+		throws IOException
+	{
+        if (directory == null)
+            throw new NullPointerException(_MESSAGES.get("NullFile"));
+    
+        if (!directory.isDirectory())
+        {
+            Object[] filler = { directory.getAbsolutePath() };
+            String message = _MESSAGES.get("NotDirectory", filler);
+            throw new IllegalArgumentException(message);
+        }
+		
+        //
+		// check to see if the directory is now empty and, if so, delete it 
+        // too, moving up the tree until we find one with stuff in it
+		//
+		while (directory != null)
+		{
+			File[] directoryFiles = directory.listFiles();
+
+			//
+			// if the directory has files, we're done
+			//
+			if (directoryFiles.length > 0)
+				break;
+
+			if (!directory.delete())
+            {
+                Object[] filler = { directory.getAbsolutePath() };
+                String message = _MESSAGES.get("DeleteFailed", filler);
+                throw new IOException(message);
+            }
+            
+			//
+			// go up the tree
+			//
+			directory = directory.getParentFile();
+		}
+	}
+    
+	private static void removeDirectory(File directory)
+		throws IOException
+	{
+		File[] files = directory.listFiles();
+
+		//
+		// for all items in the directory...
+		//
+		for (int n = 0; n < files.length; ++n)
+		{
+			File nextFile = files[n];
+
+			//
+			// if it's a directory, delete sub-directories and files before
+			// removing the empty directory
+			//
+			if (nextFile.isDirectory())
+				removeDirectory(nextFile);
+
+			//
+			// otherwise just delete the file - do NOT prune the directory
+			// in advance
+			//
+			else
+				removeFile(nextFile);
+		}
+
+		//
+		// now that everything's gone, delete the specified directory
+		//
+		if (!directory.delete())
+        {
+            Object[] filler = { directory.getAbsolutePath() };
+            String message = _MESSAGES.get("DeleteFailed", filler);
+            throw new IOException(message);
+        }
+	}
+    
+	private static void removeFile(File file)
+		throws IOException
+	{
+		//
+		// make sure the file exists, then delete it
+		//
+		
+		if (!file.exists())
+			throw new FileNotFoundException(file.getAbsolutePath());
+		
+		if (!file.delete())
+        {
+            Object[] filler = { file.getAbsolutePath() };
+            String message = _MESSAGES.get("DeleteFailed", filler);
+            throw new IOException(message);
+        }
+	}
+
+	/**
+     * 
+     * This is a convenience method that calls remove(File, boolean) with 
+     * the second parameter set to "false" (doesn't prune empty directories).
+     * 
+     * @see #remove(File, boolean)
+	 *
+	 */
+	public static void remove(File file)
+		throws IOException
+	{
+		remove(file, false);
+	}
+
+	/**
+     * 
+	 * @param file
+     *        The file or directory to delete.
+     * 
+	 * @param pruneEmptyDirectories
+     *        True if the deletion results in an empty parent directory. If 
+     *        set to true, this method will traverse up the directory tree, 
+     *        deleting directories that are made empty by the deletion.
+     * 
+	 * @throws IOException
+     *         <ul>
+     *         <li>If there was an error trying to remove the file or 
+     *         directory. The file system may be in an inconsistent state 
+     *         when the exception is thrown (directories may be partially 
+     *         deleted, etc.).</li>
+     *         </ul>
+	 *
+	 */
+	public static void remove(File file, boolean pruneEmptyDirectories)
+		throws IOException
+	{
+        if (file == null)
+            throw new NullPointerException(_MESSAGES.get("NullFile"));
+        
+		if (file.isDirectory())
+			removeDirectory(file);
+
+		else
+			removeFile(file);
+
+		if (pruneEmptyDirectories)
+			pruneEmptyDirectories(file.getParentFile());
+	}
+    
+    /**
+     * 
+     * This is a convenience method that throws an unchecked exception if 
+     * the stream could not be closed.
+     *
+     */	
+	public static void close(InputStream stream)
+	{
+		try
+		{
+			stream.close();
+		}
+		
+		catch (IOException error)
+		{
+			//
+			// what can we do?
+			//
+		}
+	}
+    
+    /**
+     * 
+     * This is a convenience method that throws an unchecked exception if 
+     * the reader could not be closed.
+     *
+     */	
+	public static void close(Reader reader)
+	{
+		try
+		{
+			reader.close();
+		}
+		
+		catch (IOException error)
+		{
+			//
+			// what can we do?
+			//
+		}
+	}
+    
+    /**
+     * 
+     * This is a convenience method that throws an unchecked exception if 
+     * the stream could not be closed.
+     *
+     */	
+	public static void close(OutputStream stream)
+	{
+		try
+		{
+			stream.close();
+		}
+		
+		catch (IOException error)
+		{
+			//
+			// what can we do?
+			//
+		}
+	}
+	
+    /**
+     * 
+     * This is a convenience method that throws an unchecked exception if 
+     * the writer could not be closed.
+     *
+     */
+	public static void close(Writer writer)
+	{
+		try
+		{
+			writer.close();
+		}
+		
+		catch (IOException error)
+		{
+			//
+			// what can we do?
+			//
+		}
+	}
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: muse-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: muse-commits-help@ws.apache.org