You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/07/13 21:24:36 UTC

svn commit: r1502843 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/PropertyConverter.java test/java/org/apache/commons/configuration/TestPropertyConverter.java

Author: oheger
Date: Sat Jul 13 19:24:36 2013
New Revision: 1502843

URL: http://svn.apache.org/r1502843
Log:
Removed a bunch of methods from PropertyConverter.

These methods have been moved into specific implementations of the
ListDelimiterHandlder interface.

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

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertyConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertyConverter.java?rev=1502843&r1=1502842&r2=1502843&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertyConverter.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertyConverter.java Sat Jul 13 19:24:36 2013
@@ -18,7 +18,6 @@
 package org.apache.commons.configuration;
 
 import java.awt.Color;
-import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.math.BigDecimal;
@@ -29,13 +28,8 @@ import java.net.URL;
 import java.net.UnknownHostException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.ArrayList;
 import java.util.Calendar;
-import java.util.Collection;
 import java.util.Date;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
 import java.util.Locale;
 
 import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
@@ -511,135 +505,6 @@ public final class PropertyConverter
     }
 
     /**
-     * Split a string on the specified delimiter. To be removed when
-     * commons-lang has a better replacement available (Tokenizer?).
-     *
-     * todo: replace with a commons-lang equivalent
-     *
-     * @param s          the string to split
-     * @param delimiter  the delimiter
-     * @param trim       a flag whether the single elements should be trimmed
-     * @return a list with the single tokens
-     */
-    public static List<String> split(String s, char delimiter, boolean trim)
-    {
-        if (s == null)
-        {
-            return new ArrayList<String>();
-        }
-
-        List<String> list = new ArrayList<String>();
-
-        StringBuilder token = new StringBuilder();
-        int begin = 0;
-        boolean inEscape = false;
-
-        while (begin < s.length())
-        {
-            char c = s.charAt(begin);
-            if (inEscape)
-            {
-                // last character was the escape marker
-                // can current character be escaped?
-                if (c != delimiter && c != LIST_ESC_CHAR)
-                {
-                    // no, also add escape character
-                    token.append(LIST_ESC_CHAR);
-                }
-                token.append(c);
-                inEscape = false;
-            }
-
-            else
-            {
-                if (c == delimiter)
-                {
-                    // found a list delimiter -> add token and resetDefaultFileSystem buffer
-                    String t = token.toString();
-                    if (trim)
-                    {
-                        t = t.trim();
-                    }
-                    list.add(t);
-                    token = new StringBuilder();
-                }
-                else if (c == LIST_ESC_CHAR)
-                {
-                    // eventually escape next character
-                    inEscape = true;
-                }
-                else
-                {
-                    token.append(c);
-                }
-            }
-
-            begin++;
-        }
-
-        // Trailing delimiter?
-        if (inEscape)
-        {
-            token.append(LIST_ESC_CHAR);
-        }
-        // Add last token
-        String t = token.toString();
-        if (trim)
-        {
-            t = t.trim();
-        }
-        list.add(t);
-
-        return list;
-    }
-
-    /**
-     * Split a string on the specified delimiter always trimming the elements.
-     * This is a shortcut for {@code split(s, delimiter, true)}.
-     *
-     * @param s          the string to split
-     * @param delimiter  the delimiter
-     * @return a list with the single tokens
-     */
-    public static List<String> split(String s, char delimiter)
-    {
-        return split(s, delimiter, true);
-    }
-
-    /**
-     * Escapes the delimiters that might be contained in the given string. This
-     * method works like {@link #escapeListDelimiter(String, char)}. In addition,
-     * a single backslash will also be escaped.
-     *
-     * @param s the string with the value
-     * @param delimiter the list delimiter to use
-     * @return the correctly escaped string
-     */
-    public static String escapeDelimiters(String s, char delimiter)
-    {
-        String s1 = StringUtils.replace(s, LIST_ESCAPE, LIST_ESCAPE + LIST_ESCAPE);
-        return escapeListDelimiter(s1, delimiter);
-    }
-
-    /**
-     * Escapes the list delimiter if it is contained in the given string. This
-     * method ensures that list delimiter characters that are part of a
-     * property's value are correctly escaped when a configuration is saved to a
-     * file. Otherwise when loaded again the property will be treated as a list
-     * property.
-     *
-     * @param s the string with the value
-     * @param delimiter the list delimiter to use
-     * @return the escaped string
-     * @since 1.7
-     */
-    public static String escapeListDelimiter(String s, char delimiter)
-    {
-        return StringUtils.replace(s, String.valueOf(delimiter), LIST_ESCAPE
-                + delimiter);
-    }
-
-    /**
      * Convert the specified object into a Color. If the value is a String,
      * the format allowed is (#)?[0-9A-F]{6}([0-9A-F]{2})?. Examples:
      * <ul>
@@ -904,97 +769,6 @@ public final class PropertyConverter
     }
 
     /**
-     * Returns an iterator over the simple values of a composite value. This
-     * implementation calls {@link #flatten(Object, char)} and
-     * returns an iterator over the returned collection.
-     *
-     * @param value the value to "split"
-     * @param delimiter the delimiter for String values
-     * @return an iterator for accessing the single values
-     */
-    public static Iterator<?> toIterator(Object value, char delimiter)
-    {
-        return flatten(value, delimiter).iterator();
-    }
-
-    /**
-     * Returns a collection with all values contained in the specified object.
-     * This method is used for instance by the {@code addProperty()}
-     * implementation of the default configurations to gather all values of the
-     * property to add. Depending on the type of the passed in object the
-     * following things happen:
-     * <ul>
-     * <li>Strings are checked for delimiter characters and split if necessary.</li>
-     * <li>For objects implementing the {@code Iterable} interface, the
-     * corresponding {@code Iterator} is obtained, and contained elements
-     * are added to the resulting collection.</li>
-     * <li>Arrays are treated as {@code Iterable} objects.</li>
-     * <li>All other types are directly inserted.</li>
-     * <li>Recursive combinations are supported, e.g. a collection containing
-     * an array that contains strings: The resulting collection will only
-     * contain primitive objects (hence the name &quot;flatten&quot;).</li>
-     * </ul>
-     *
-     * @param value the value to be processed
-     * @param delimiter the delimiter for String values
-     * @return a &quot;flat&quot; collection containing all primitive values of
-     *         the passed in object
-     */
-    private static Collection<?> flatten(Object value, char delimiter)
-    {
-        if (value instanceof String)
-        {
-            String s = (String) value;
-            if (s.indexOf(delimiter) > 0)
-            {
-                return split(s, delimiter);
-            }
-        }
-
-        Collection<Object> result = new LinkedList<Object>();
-        if (value instanceof Iterable)
-        {
-            flattenIterator(result, ((Iterable<?>) value).iterator(), delimiter);
-        }
-        else if (value instanceof Iterator)
-        {
-            flattenIterator(result, (Iterator<?>) value, delimiter);
-        }
-        else if (value != null)
-        {
-            if (value.getClass().isArray())
-            {
-                for (int len = Array.getLength(value), idx = 0; idx < len; idx++)
-                {
-                    result.addAll(flatten(Array.get(value, idx), delimiter));
-                }
-            }
-            else
-            {
-                result.add(value);
-            }
-        }
-
-        return result;
-    }
-
-    /**
-     * Flattens the given iterator. For each element in the iteration
-     * {@code flatten()} will be called recursively.
-     *
-     * @param target the target collection
-     * @param it the iterator to process
-     * @param delimiter the delimiter for String values
-     */
-    private static void flattenIterator(Collection<Object> target, Iterator<?> it, char delimiter)
-    {
-        while (it.hasNext())
-        {
-            target.addAll(flatten(it.next(), delimiter));
-        }
-    }
-
-    /**
      * Performs interpolation of the specified value using the given
      * {@code Configuration} object. This method checks if the given
      * {@code Configuration} has a {@link ConfigurationInterpolator} object. If

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java?rev=1502843&r1=1502842&r2=1502843&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java Sat Jul 13 19:24:36 2013
@@ -18,14 +18,10 @@
 package org.apache.commons.configuration;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
 
 import java.lang.annotation.ElementType;
 import java.math.BigDecimal;
-import java.util.Iterator;
-import java.util.List;
 
 import org.junit.Test;
 
@@ -40,118 +36,6 @@ public class TestPropertyConverter
     /** Constant for an enumeration class used by some tests. */
     private static final Class<ElementType> ENUM_CLASS = ElementType.class;
 
-    @Test
-    public void testSplit()
-    {
-        String s = "abc, xyz , 123";
-        List<String> list = PropertyConverter.split(s, ',');
-
-        assertEquals("size", 3, list.size());
-        assertEquals("1st token for '" + s + "'", "abc", list.get(0));
-        assertEquals("2nd token for '" + s + "'", "xyz", list.get(1));
-        assertEquals("3rd token for '" + s + "'", "123", list.get(2));
-    }
-
-    @Test
-    public void testSplitNoTrim()
-    {
-        String s = "abc, xyz , 123";
-        List<String> list = PropertyConverter.split(s, ',', false);
-
-        assertEquals("size", 3, list.size());
-        assertEquals("1st token for '" + s + "'", "abc", list.get(0));
-        assertEquals("2nd token for '" + s + "'", " xyz ", list.get(1));
-        assertEquals("3rd token for '" + s + "'", " 123", list.get(2));
-    }
-
-    @Test
-    public void testSplitWithEscapedSeparator()
-    {
-        String s = "abc\\,xyz, 123";
-        List<String> list = PropertyConverter.split(s, ',');
-
-        assertEquals("size", 2, list.size());
-        assertEquals("1st token for '" + s + "'", "abc,xyz", list.get(0));
-        assertEquals("2nd token for '" + s + "'", "123", list.get(1));
-    }
-
-    @Test
-    public void testSplitEmptyValues()
-    {
-        String s = ",,";
-        List<String> list = PropertyConverter.split(s, ',');
-
-        assertEquals("size", 3, list.size());
-        assertEquals("1st token for '" + s + "'", "", list.get(0));
-        assertEquals("2nd token for '" + s + "'", "", list.get(1));
-        assertEquals("3rd token for '" + s + "'", "", list.get(2));
-    }
-
-    @Test
-    public void testSplitWithEndingSlash()
-    {
-        String s = "abc, xyz\\";
-        List<String> list = PropertyConverter.split(s, ',');
-
-        assertEquals("size", 2, list.size());
-        assertEquals("1st token for '" + s + "'", "abc", list.get(0));
-        assertEquals("2nd token for '" + s + "'", "xyz\\", list.get(1));
-    }
-
-    @Test
-    public void testSplitNull()
-    {
-        List<String> list = PropertyConverter.split(null, ',');
-        assertNotNull(list);
-        assertTrue(list.isEmpty());
-    }
-
-    /**
-     * Tests whether an escape character can be itself escaped.
-     */
-    @Test
-    public void testSplitEscapeEscapeChar()
-    {
-        List<String> list = PropertyConverter.split("C:\\Temp\\\\,xyz", ',');
-        assertEquals("Wrong list size", 2, list.size());
-        assertEquals("Wrong element 1", "C:\\Temp\\", list.get(0));
-        assertEquals("Wrong element 2", "xyz", list.get(1));
-    }
-
-    /**
-     * Tests whether delimiters are correctly escaped.
-     */
-    @Test
-    public void testEscapeDelimiters()
-    {
-        assertEquals("Wrong escaped delimiters",
-                "C:\\\\Temp\\\\\\,D:\\\\Data\\\\", PropertyConverter
-                        .escapeDelimiters("C:\\Temp\\,D:\\Data\\", ','));
-    }
-
-    /**
-     * Tests whether only the list delimiter can be escaped.
-     */
-    @Test
-    public void testEscapeListDelimiter()
-    {
-        assertEquals("Wrong escaped list delimiter", "C:\\Temp\\\\,D:\\Data\\",
-                PropertyConverter.escapeListDelimiter("C:\\Temp\\,D:\\Data\\",
-                        ','));
-    }
-
-    @Test
-    public void testToIterator()
-    {
-        int[] array = new int[]{1, 2, 3};
-
-        Iterator<?> it = PropertyConverter.toIterator(array, ',');
-
-        assertEquals("1st element", new Integer(1), it.next());
-        assertEquals("2nd element", new Integer(2), it.next());
-        assertEquals("3rd element", new Integer(3), it.next());
-    }
-
     /**
      * Tests the interpolation features.
      */