You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Lars W (JIRA)" <ji...@apache.org> on 2018/03/23 13:27:00 UTC

[jira] [Created] (CONFIGURATION-693) Conversion to File is missing

Lars W created CONFIGURATION-693:
------------------------------------

             Summary: Conversion to File is missing
                 Key: CONFIGURATION-693
                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-693
             Project: Commons Configuration
          Issue Type: Improvement
          Components: Type conversion
    Affects Versions: 2.2
            Reporter: Lars W
         Attachments: Converter.patch.txt

PropertyConverter.java misses type conversion to File although a string could be easily converted to a File.

Attaching the patch as a file does not work - I don't know why. So I'll paste it here:

 

Index: src/main/java/org/apache/commons/configuration/PropertyConverter.java
===================================================================
--- src/main/java/org/apache/commons/configuration/PropertyConverter.java
+++ src/main/java/org/apache/commons/configuration/PropertyConverter.java
@@ -19,6 +19,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;
@@ -29,6 +30,7 @@
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.UnknownHostException;
+import java.nio.file.Path;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
@@ -153,6 +155,10 @@
         {
             return toCalendar(value, convHandler.getDateFormat());
         }
+        else if (File.class.equals(cls))
+        {
+            return toFile(value);
+        }
         else if (URI.class.equals(cls))
         {
             return toURI(value);
@@ -477,6 +483,33 @@
     }
 
     /**
+     * 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
+     */
+    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 an URI.
      *
      * @param value the value to convert



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)