Author: oheger
Date: Wed Mar 28 18:21:55 2018
New Revision: 1827941

URL: http://svn.apache.org/viewvc?rev=1827941&view=rev
Log:
CONFIGURATION-693: Added conversion to files and paths.

PropertyConverter now supports conversions to the types File and Path.
Thanks to l dot wolter at his dot de for the patch.

Modified:
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java
    
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/convert/TestPropertyConverter.java

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java?rev=1827941&r1=1827940&r2=1827941&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java
 Wed Mar 28 18:21:55 2018
@@ -18,6 +18,7 @@
 package org.apache.commons.configuration2.convert;
 
 import java.awt.Color;
+import java.io.File;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.math.BigDecimal;
@@ -28,6 +29,8 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.UnknownHostException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
@@ -152,6 +155,14 @@ final class PropertyConverter
         {
             return toCalendar(value, convHandler.getDateFormat());
         }
+        else if (File.class.equals(cls))
+        {
+            return toFile(value);
+        }
+        else if (Path.class.equals(cls))
+        {
+            return toPath(value);
+        }
         else if (URI.class.equals(cls))
         {
             return toURI(value);
@@ -475,6 +486,62 @@ final class PropertyConverter
         }
     }
 
+    /**
+     * Convert the specified object into a File.
+     *
+     * @param value the value to convert
+     * @return the converted value
+     * @throws ConversionException thrown if the value cannot be converted to 
a File
+     * @since 2.3
+     */
+    public static File toFile(Object value) throws ConversionException
+    {
+        if (value instanceof File)
+        {
+            return (File) value;
+        }
+        else if (value instanceof Path)
+        {
+            return ((Path) value).toFile();
+        }
+        else if (value instanceof String)
+        {
+            return new File((String) value);
+        }
+        else
+        {
+            throw new ConversionException("The value " + value + " can't be 
converted to a File");
+        }
+    }
+
+    /**
+     * Convert the specified object into a Path.
+     *
+     * @param value the value to convert
+     * @return the converted value
+     * @throws ConversionException thrown if the value cannot be converted to 
a Path
+     * @since 2.3
+     */
+    public static Path toPath(Object value) throws ConversionException
+    {
+        if (value instanceof File)
+        {
+            return ((File) value).toPath();
+        }
+        else if (value instanceof Path)
+        {
+            return (Path) value;
+        }
+        else if (value instanceof String)
+        {
+            return Paths.get((String) value);
+        }
+        else
+        {
+            throw new ConversionException("The value " + value + " can't be 
converted to a Path");
+        }
+    }
+
     /**
      * Convert the specified object into an URI.
      *

Modified: 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/convert/TestPropertyConverter.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/convert/TestPropertyConverter.java?rev=1827941&r1=1827940&r2=1827941&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/convert/TestPropertyConverter.java
 (original)
+++ 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/convert/TestPropertyConverter.java
 Wed Mar 28 18:21:55 2018
@@ -20,8 +20,11 @@ package org.apache.commons.configuration
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
 
+import java.io.File;
 import java.lang.annotation.ElementType;
 import java.math.BigDecimal;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 import org.apache.commons.configuration2.ex.ConversionException;
 import org.junit.Test;
@@ -38,6 +41,68 @@ public class TestPropertyConverter
     private static final Class<ElementType> ENUM_CLASS = ElementType.class;
 
     /**
+     * Tests conversion to files when the passed in objects are already
+     * files.
+     */
+    @Test
+    public void testToFileDirect()
+    {
+        File f = new File("dir", "file");
+        assertSame("Wrong file", f, PropertyConverter.toFile(f));
+    }
+
+    /**
+     * Tests conversion to file when the passed in objects have a compatible
+     * string representation.
+     */
+    @Test
+    public void testToFileFromString()
+    {
+        assertEquals("Wrong conversion result", new File("dir", "file"), 
PropertyConverter.toFile("dir/file"));
+    }
+
+    /**
+     * Tests conversion to file when the passed in objects are paths.
+     */
+    @Test
+    public void testToFileFromPath()
+    {
+        Path p = Paths.get("dir", "file");
+        assertEquals("Wrong conversion result", new File("dir", "file"), 
PropertyConverter.toFile(p));
+    }
+
+    /**
+     * Tests conversion to paths when the passed in objects are already
+     * paths.
+     */
+    @Test
+    public void testToPathDirect()
+    {
+        Path p = Paths.get("dir", "file");
+        assertSame("Wrong path", p, PropertyConverter.toPath(p));
+    }
+
+    /**
+     * Tests conversion to file when the passed in objects have a compatible
+     * string representation.
+     */
+    @Test
+    public void testToPathFromString()
+    {
+        assertEquals("Wrong conversion result", Paths.get("dir", "file"), 
PropertyConverter.toPath("dir/file"));
+    }
+
+    /**
+     * Tests conversion to path when the passed in objects are files.
+     */
+    @Test
+    public void testToPathFromFile()
+    {
+        File f =  new File("dir", "file");
+        assertEquals("Wrong conversion result", Paths.get("dir", "file"), 
PropertyConverter.toPath(f));
+    }
+
+    /**
      * Tests conversion to numbers when the passed in objects are already
      * numbers.
      */


Reply via email to