commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r154517 - in jakarta/commons/proper/io/trunk/src: java/org/apache/commons/io/FileUtils.java test/org/apache/commons/io/FileUtilsTestCase.java
Date Sun, 20 Feb 2005 15:56:22 GMT
Author: scolebourne
Date: Sun Feb 20 07:56:21 2005
New Revision: 154517

URL: http://svn.apache.org/viewcvs?view=rev&rev=154517
Log:
Handle encodings in URL to File conversion
bug 32575, from Jason Madden

Modified:
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?view=diff&r1=154516&r2=154517
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Sun Feb
20 07:56:21 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2005 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.
@@ -64,7 +64,8 @@
  * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
  * @author Matthew Hawthorne
  * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
- * @version $Id: FileUtils.java,v 1.39 2004/10/29 21:34:56 bayard Exp $
+ * @author Stephen Colebourne
+ * @version $Id$
  */
 public class FileUtils {
 
@@ -306,17 +307,29 @@
     //-----------------------------------------------------------------------
     /**
      * Convert from a <code>URL</code> to a <code>File</code>.
+     * <p>
+     * From version 1.1 this method will decode the URL.
+     * Syntax such as <code>file:///my%20docs/file.txt</code> will be
+     * correctly decoded to <code>/my docs/file.txt</code>.
      *
-     * @param url  the file URL to convert
+     * @param url  the file URL to convert, null returns null
      * @return the equivalent <code>File</code> object, or <code>null</code>
      *  if the URL's protocol is not <code>file</code>
+     * @throws IllegalArgumentException if the file is incorrectly encoded
      */
     public static File toFile(URL url) {
-        if (!url.getProtocol().equals("file")) {
+        if (url == null || !url.getProtocol().equals("file")) {
             return null;
         } else {
-            String filename =
-                url.getFile().replace('/', File.separatorChar);
+            String filename = url.getFile().replace('/', File.separatorChar);
+            int pos =0;
+            while ((pos = filename.indexOf('%', pos)) >= 0) {
+                if (pos + 2 < filename.length()) {
+                    String hexStr = filename.substring(pos + 1, pos + 3);
+                    char ch = (char) Integer.parseInt(hexStr, 16);
+                    filename = filename.substring(0, pos) + ch + filename.substring(pos +
3);
+                }
+            }
             return new File(filename);
         }
     }
@@ -328,10 +341,16 @@
      * If the input is null, an empty array is returned.
      * If the input contains null, the output array contains null at the same
      * index.
+     * <p>
+     * From version 1.1 this method will decode the URL.
+     * Syntax such as <code>file:///my%20docs/file.txt</code> will be
+     * correctly decoded to <code>/my docs/file.txt</code>.
      *
      * @param urls  the file URLs to convert, null returns empty array
      * @return a non-null array of Files matching the input, with a null item
      *  if there was a null at that index in the input array
+     * @throws IllegalArgumentException if any file is not a URL file
+     * @throws IllegalArgumentException if any file is incorrectly encoded
      */
     public static File[] toFiles(URL[] urls) {
         if (urls == null || urls.length == 0) {

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?view=diff&r1=154516&r2=154517
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
(original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
Sun Feb 20 07:56:21 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2005 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.
@@ -34,7 +34,8 @@
  *
  * @author Peter Donald
  * @author Matthew Hawthorne
- * @version $Id: FileUtilsTestCase.java,v 1.24 2004/07/24 09:58:40 scolebourne Exp $
+ * @author Stephen Colebourne
+ * @version $Id$
  * @see FileUtils
  */
 public class FileUtilsTestCase extends FileBasedTestCase {
@@ -106,6 +107,32 @@
         FileUtils.waitFor(new File(""), -1);
 
         FileUtils.waitFor(new File(""), 2);
+    }
+
+    //-----------------------------------------------------------------------
+    public void testToFile1() throws Exception {
+        URL url = new URL("file", null, "a/b/c/file.txt");
+        File file = FileUtils.toFile(url);
+        assertEquals(true, file.toString().indexOf("file.txt") >= 0);
+    }
+
+    public void testToFile2() throws Exception {
+        URL url = new URL("file", null, "a/b/c/file%20n%61me.tx%74");
+        File file = FileUtils.toFile(url);
+        assertEquals(true, file.toString().indexOf("file name.txt") >= 0);
+    }
+
+    public void testToFile3() throws Exception {
+        assertEquals(null, FileUtils.toFile((URL) null));
+        assertEquals(null, FileUtils.toFile(new URL("http://jakarta.apache.org")));
+    }
+
+    public void testToFile4() throws Exception {
+        URL url = new URL("file", null, "a/b/c/file%2Xn%61me.txt");
+        try {
+            FileUtils.toFile(url);
+            fail();
+        }  catch (IllegalArgumentException ex) {}
     }
 
     // toFiles



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message