You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2017/04/10 05:47:15 UTC

svn commit: r1790777 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration2/ main/java/org/apache/commons/configuration2/convert/ test/java/org/apache/commons/configuration2/

Author: ggregory
Date: Mon Apr 10 05:47:15 2017
New Revision: 1790777

URL: http://svn.apache.org/viewvc?rev=1790777&view=rev
Log:
[CONFIGURATION-658] Add API org.apache.commons.configuration2.DataConfiguration.getURI(String) methods.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
    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/TestDataConfiguration.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/DataConfiguration.java?rev=1790777&r1=1790776&r2=1790777&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/DataConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/DataConfiguration.java Mon Apr 10 05:47:15 2017
@@ -20,6 +20,7 @@ package org.apache.commons.configuration
 import java.awt.Color;
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.net.URI;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Calendar;
@@ -790,6 +791,100 @@ public class DataConfiguration extends A
     }
 
     /**
+     * Get an URI associated with the given configuration key.
+     *
+     * @param key The configuration key.
+     * @return The associated URI.
+     *
+     * @throws ConversionException is thrown if the key maps to an
+     *         object that is not an URI.
+     */
+    public URI getURI(String key)
+    {
+        return get(URI.class, key);
+    }
+
+    /**
+     * Get an URI associated with the given configuration key.
+     * If the key doesn't map to an existing object, the default value
+     * is returned.
+     *
+     * @param key          The configuration key.
+     * @param defaultValue The default value.
+     * @return The associated URI.
+     *
+     * @throws ConversionException is thrown if the key maps to an
+     *         object that is not an URI.
+     */
+    public URI getURI(String key, URI defaultValue)
+    {
+        return get(URI.class, key, defaultValue);
+    }
+
+    /**
+     * Get an array of URIs associated with the given configuration key.
+     * If the key doesn't map to an existing object an empty array is returned.
+     *
+     * @param key The configuration key.
+     * @return The associated URI array if the key is found.
+     *
+     * @throws ConversionException is thrown if the key maps to an
+     *         object that is not a list of URIs.
+     */
+    public URI[] getURIArray(String key)
+    {
+        return getURIArray(key, new URI[0]);
+    }
+
+    /**
+     * Get an array of URIs associated with the given configuration key.
+     * If the key doesn't map to an existing object an empty array is returned.
+     *
+     * @param key The configuration key.
+     * @param defaultValue the default value, which will be returned if the property is not found
+     * @return The associated URI array if the key is found.
+     *
+     * @throws ConversionException is thrown if the key maps to an
+     *         object that is not a list of URIs.
+     */
+    public URI[] getURIArray(String key, URI[] defaultValue)
+    {
+        return get(URI[].class, key, defaultValue);
+    }
+
+    /**
+     * Get a list of URIs associated with the given configuration key.
+     * If the key doesn't map to an existing object an empty list is returned.
+     *
+     * @param key The configuration key.
+     * @return The associated URI list if the key is found.
+     *
+     * @throws ConversionException is thrown if the key maps to an
+     *         object that is not a list of URIs.
+     */
+    public List<URI> getURIList(String key)
+    {
+        return getURIList(key, new ArrayList<URI>());
+    }
+
+    /**
+     * Get a list of URIs associated with the given configuration key.
+     * If the key doesn't map to an existing object, the default value is
+     * returned.
+     *
+     * @param key The configuration key.
+     * @param defaultValue The default value.
+     * @return The associated List of URIs.
+     *
+     * @throws ConversionException is thrown if the key maps to an
+     *         object that is not a list of URIs.
+     */
+    public List<URI> getURIList(String key, List<URI> defaultValue)
+    {
+        return getList(URI.class, key, defaultValue);
+    }
+
+    /**
      * Get an URL associated with the given configuration key.
      *
      * @param key The configuration key.

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=1790777&r1=1790776&r2=1790777&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 Mon Apr 10 05:47:15 2017
@@ -24,6 +24,8 @@ import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.net.InetAddress;
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.text.ParseException;
@@ -150,6 +152,10 @@ final class PropertyConverter
         {
             return toCalendar(value, convHandler.getDateFormat());
         }
+        else if (URI.class.equals(cls))
+        {
+            return toURI(value);
+        }
         else if (URL.class.equals(cls))
         {
             return toURL(value);
@@ -469,6 +475,36 @@ final class PropertyConverter
         }
     }
 
+    /**
+     * Convert the specified object into an URI.
+     *
+     * @param value the value to convert
+     * @return the converted value
+     * @throws ConversionException thrown if the value cannot be converted to an URI
+     */
+    public static URI toURI(Object value) throws ConversionException
+    {
+        if (value instanceof URI)
+        {
+            return (URI) value;
+        }
+        else if (value instanceof String)
+        {
+            try
+            {
+                return new URI((String) value);
+            }
+            catch (URISyntaxException e)
+            {
+                throw new ConversionException("The value " + value + " can't be converted to an URI", e);
+            }
+        }
+        else
+        {
+            throw new ConversionException("The value " + value + " can't be converted to an URI");
+        }
+    }
+
     /**
      * Convert the specified object into an URL.
      *

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java?rev=1790777&r1=1790776&r2=1790777&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestDataConfiguration.java Mon Apr 10 05:47:15 2017
@@ -28,6 +28,7 @@ import java.awt.Color;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.net.InetAddress;
+import java.net.URI;
 import java.net.URL;
 import java.text.DateFormat;
 import java.text.ParseException;
@@ -40,9 +41,6 @@ import java.util.List;
 import java.util.Locale;
 import java.util.NoSuchElementException;
 
-import junitx.framework.ArrayAssert;
-import junitx.framework.ListAssert;
-
 import org.apache.commons.configuration2.convert.DefaultConversionHandler;
 import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
 import org.apache.commons.configuration2.ex.ConversionException;
@@ -50,6 +48,9 @@ import org.easymock.EasyMock;
 import org.junit.Before;
 import org.junit.Test;
 
+import junitx.framework.ArrayAssert;
+import junitx.framework.ListAssert;
+
 /**
  * @author Emmanuel Bourg
  * @version $Id$
@@ -214,6 +215,24 @@ public class TestDataConfiguration
         conf.addProperty("bigdecimal.object", new BigDecimal("1"));
         conf.addProperty("bigdecimal.list.interpolated", "${bigdecimal.string},2");
 
+        // URIs
+        String uri1 = "http://jakarta.apache.org";
+        String uri2 = "http://www.apache.org";
+        conf.addProperty("uri.string", uri1);
+        conf.addProperty("uri.string.interpolated", "${uri.string}");
+        conf.addProperty("uri.object", new URI(uri1));
+        conf.addProperty("uri.list1", uri1);
+        conf.addProperty("uri.list1", uri2);
+        conf.addProperty("uri.list2", uri1 + ", " + uri2);
+        conf.addProperty("uri.list3", new URI(uri1));
+        conf.addProperty("uri.list3", new URI(uri2));
+        conf.addPropertyDirect("uri.list4", new URI[] { new URI(uri1), new URI(uri2) });
+        List<Object> uris = new ArrayList<Object>();
+        uris.add(new URI(uri1));
+        uris.add(new URI(uri2));
+        conf.addProperty("uri.list6", uris);
+        conf.addProperty("uri.list.interpolated", "${uri.string}," + uri2);
+
         // URLs
         String url1 = "http://jakarta.apache.org";
         String url2 = "http://www.apache.org";
@@ -1104,6 +1123,98 @@ public class TestDataConfiguration
     }
 
     @Test
+    public void testGetURI() throws Exception
+    {
+        // missing URI
+        URI defaultValue = new URI("http://www.google.com");
+        assertEquals(defaultValue, conf.getURI("url", defaultValue));
+
+        URI expected = new URI("http://jakarta.apache.org");
+
+        // URI string
+        assertEquals(expected, conf.getURI("uri.string"));
+
+        // URI object
+        assertEquals(expected, conf.getURI("uri.object"));
+
+        // interpolated value
+        assertEquals(expected, conf.getURI("uri.string.interpolated"));
+    }
+
+    @Test
+    public void testGetURIArray() throws Exception
+    {
+        // missing list
+        URI[] defaultValue = new URI[] { new URI("http://www.apache.org"), new URI("http://jakarta.apache.org") };
+        ArrayAssert.assertEquals(defaultValue, conf.getURIArray("url.list", defaultValue));
+
+        URI[] expected = new URI[] { new URI("http://jakarta.apache.org"), new URI("http://www.apache.org") };
+
+        // list of strings
+        ArrayAssert.assertEquals(expected, conf.getURIArray("uri.list1"));
+
+        // list of strings, comma separated
+        ArrayAssert.assertEquals(expected, conf.getURIArray("uri.list2"));
+
+        // list of URI objects
+        ArrayAssert.assertEquals(expected, conf.getURIArray("uri.list3"));
+
+        // array of URI objects
+        ArrayAssert.assertEquals(expected, conf.getURIArray("uri.list4"));
+
+        // list of URI objects
+        ArrayAssert.assertEquals(expected, conf.getURIArray("uri.list6"));
+
+        // list of interpolated values
+        ArrayAssert.assertEquals(expected, conf.getURIArray("uri.list.interpolated"));
+
+        // single URI values
+        ArrayAssert.assertEquals(new URI[] { new URI("http://jakarta.apache.org") }, conf.getURIArray("uri.string"));
+        ArrayAssert.assertEquals(new URI[] { new URI("http://jakarta.apache.org") }, conf.getURIArray("uri.object"));
+
+        // empty array
+        ArrayAssert.assertEquals(new URI[] { }, conf.getURIArray("empty"));
+    }
+
+    @Test
+    public void testGetURIList() throws Exception
+    {
+        // missing list
+        ListAssert.assertEquals(null, conf.getURIList("uri.list", null));
+
+        List<Object> expected = new ArrayList<Object>();
+        expected.add(new URI("http://jakarta.apache.org"));
+        expected.add(new URI("http://www.apache.org"));
+
+        // list of strings
+        ListAssert.assertEquals(expected, conf.getURIList("uri.list1"));
+
+        // list of strings, comma separated
+        ListAssert.assertEquals(expected, conf.getURIList("uri.list2"));
+
+        // list of URI objects
+        ListAssert.assertEquals(expected, conf.getURIList("uri.list3"));
+
+        // array of URI objects
+        ListAssert.assertEquals(expected, conf.getURIList("uri.list4"));
+
+        // list of URI objects
+        ListAssert.assertEquals(expected, conf.getURIList("uri.list6"));
+
+        // list of interpolated values
+        ListAssert.assertEquals(expected, conf.getURIList("uri.list.interpolated"));
+
+        // single URI values
+        expected = new ArrayList<Object>();
+        expected.add(new URI("http://jakarta.apache.org"));
+        ListAssert.assertEquals(expected, conf.getURIList("uri.string"));
+        ListAssert.assertEquals(expected, conf.getURIList("uri.object"));
+
+        // empty list
+        ListAssert.assertEquals(new ArrayList<Object>(), conf.getURIList("empty"));
+    }
+
+    @Test
     public void testGetURL() throws Exception
     {
         // missing URL