commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections ExtendedProperties.java
Date Mon, 21 Jun 2004 23:39:25 GMT
scolebourne    2004/06/21 16:39:25

  Modified:    collections/src/test/org/apache/commons/collections
                        TestExtendedProperties.java
               collections/src/java/org/apache/commons/collections
                        ExtendedProperties.java
  Log:
  Handle multiple keys better,
  bug reported by Wolfgang Hoschek
  
  Revision  Changes    Path
  1.13      +82 -1     jakarta-commons/collections/src/test/org/apache/commons/collections/TestExtendedProperties.java
  
  Index: TestExtendedProperties.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestExtendedProperties.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- TestExtendedProperties.java	18 Feb 2004 01:20:35 -0000	1.12
  +++ TestExtendedProperties.java	21 Jun 2004 23:39:25 -0000	1.13
  @@ -171,4 +171,85 @@
           }
       }
       
  +    public void testMultipleSameKey1() throws Exception {
  +        ExtendedProperties ep1 = new ExtendedProperties();
  +
  +        /*
  +        initialize using:
  +        one=a
  +        one=b,c
  +        */
  +        String s1 = "one=a\none=b,c\n";
  +        byte[] bytes = s1.getBytes();
  +        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  +        ep1.load(bais);
  +        assertEquals(1, ep1.size());
  +        assertEquals(3, ep1.getVector("one").size());
  +        assertEquals("a", ep1.getVector("one").get(0));
  +        assertEquals("b", ep1.getVector("one").get(1));
  +        assertEquals("c", ep1.getVector("one").get(2));
  +    }
  +    
  +    public void testMultipleSameKey2() throws Exception {
  +        ExtendedProperties ep1 = new ExtendedProperties();
  +
  +        /*
  +        initialize using:
  +        one=a,b
  +        one=c,d
  +        */
  +        String s1 = "one=a,b\none=c,d\n";
  +        byte[] bytes = s1.getBytes();
  +        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  +        ep1.load(bais);
  +        assertEquals(1, ep1.size());
  +        assertEquals(4, ep1.getVector("one").size());
  +        assertEquals("a", ep1.getVector("one").get(0));
  +        assertEquals("b", ep1.getVector("one").get(1));
  +        assertEquals("c", ep1.getVector("one").get(2));
  +        assertEquals("d", ep1.getVector("one").get(3));
  +    }
  +    
  +    public void testMultipleSameKey3() throws Exception {
  +        ExtendedProperties ep1 = new ExtendedProperties();
  +
  +        /*
  +        initialize using:
  +        one=a,b
  +        one=c
  +        */
  +        String s1 = "one=a,b\none=c\n";
  +        byte[] bytes = s1.getBytes();
  +        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  +        ep1.load(bais);
  +        assertEquals(1, ep1.size());
  +        assertEquals(3, ep1.getVector("one").size());
  +        assertEquals("a", ep1.getVector("one").get(0));
  +        assertEquals("b", ep1.getVector("one").get(1));
  +        assertEquals("c", ep1.getVector("one").get(2));
  +    }
  +    
  +    public void testMultipleSameKeyByCode() throws Exception {
  +        ExtendedProperties ep1 = new ExtendedProperties();
  +
  +        ep1.addProperty("one", "a");
  +        assertEquals(1, ep1.size());
  +        assertEquals(1, ep1.getVector("one").size());
  +        assertEquals("a", ep1.getVector("one").get(0));
  +        
  +        ep1.addProperty("one", Boolean.TRUE);
  +        assertEquals(1, ep1.size());
  +        assertEquals(2, ep1.getVector("one").size());
  +        assertEquals("a", ep1.getVector("one").get(0));
  +        assertEquals(Boolean.TRUE, ep1.getVector("one").get(1));
  +        
  +        ep1.addProperty("one", "c,d");
  +        assertEquals(1, ep1.size());
  +        assertEquals(4, ep1.getVector("one").size());
  +        assertEquals("a", ep1.getVector("one").get(0));
  +        assertEquals(Boolean.TRUE, ep1.getVector("one").get(1));
  +        assertEquals("c", ep1.getVector("one").get(2));
  +        assertEquals("d", ep1.getVector("one").get(3));
  +    }
  +    
   }
  
  
  
  1.23      +35 -97    jakarta-commons/collections/src/java/org/apache/commons/collections/ExtendedProperties.java
  
  Index: ExtendedProperties.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ExtendedProperties.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- ExtendedProperties.java	17 Mar 2004 21:09:08 -0000	1.22
  +++ ExtendedProperties.java	21 Jun 2004 23:39:25 -0000	1.23
  @@ -639,78 +639,24 @@
        * <code>["file", "classpath"]</code>
        *
        * @param key  the key to add
  -     * @param token  the value to add
  +     * @param value  the value to add
        */
  -    public void addProperty(String key, Object token) {
  -        Object obj = this.get(key);
  -
  -        /*
  -         *  $$$ GMJ
  -         *  FIXME : post 1.0 release, we need to not assume
  -         *  that a scalar is a String - it can be an Object
  -         *  so we should make a little vector-like class
  -         *  say, Foo that wraps (not extends Vector),
  -         *  so we can do things like
  -         *  if ( !( o instanceof Foo) )
  -         *  so we know it's our 'vector' container
  -         *
  -         *  This applies throughout
  -         */
  -
  -        if (obj instanceof String) {
  -            Vector v = new Vector(2);
  -            v.addElement(obj);
  -            v.addElement(token);
  -            put(key, v);
  -            
  -        } else if (obj instanceof Vector) {
  -            ((Vector) obj).addElement(token);
  -            
  -        } else {
  -            /*
  -             * This is the first time that we have seen
  -             * request to place an object in the 
  -             * configuration with the key 'key'. So
  -             * we just want to place it directly into
  -             * the configuration ... but we are going to
  -             * make a special exception for String objects
  -             * that contain "," characters. We will take
  -             * CSV lists and turn the list into a vector of
  -             * Strings before placing it in the configuration.
  -             * This is a concession for Properties and the
  -             * like that cannot parse multiple same key
  -             * values.
  -             */
  -            if (token instanceof String &&
  -                ((String) token).indexOf(PropertiesTokenizer.DELIMITER) > 0) {
  -                    
  -                PropertiesTokenizer tokenizer = new PropertiesTokenizer((String) token);
  -
  +    public void addProperty(String key, Object value) {
  +        if (value instanceof String) {
  +            String str = (String) value;
  +            if (str.indexOf(PropertiesTokenizer.DELIMITER) > 0) {
  +                // token contains commas, so must be split apart then added
  +                PropertiesTokenizer tokenizer = new PropertiesTokenizer(str);
                   while (tokenizer.hasMoreTokens()) {
  -                    String value = tokenizer.nextToken();
  -
  -                    /*
  -                     * We know this is a string, so make sure it
  -                     * just goes in rather than risking vectorization
  -                     * if it contains an escaped comma
  -                     */
  -                    addStringProperty(key, unescape(value));
  +                    String token = tokenizer.nextToken();
  +                    addPropertyInternal(key, unescape(token));
                   }
               } else {
  -                /*
  -                 * We want to keep track of the order the keys
  -                 * are parsed, or dynamically entered into
  -                 * the configuration. So when we see a key
  -                 * for the first time we will place it in
  -                 * an ArrayList so that if a client class needs
  -                 * to perform operations with configuration
  -                 * in a definite order it will be possible.
  -                 */
  -                if (token instanceof String) {
  -                    token = unescape((String) token);
  -                }
  -                addPropertyDirect(key, token);
  +                // token contains no commas, so can be simply added
  +                addPropertyInternal(key, unescape(str));
               }
  +        } else {
  +            addPropertyInternal(key, value);
           }
   
           // Adding a property connotes initialization
  @@ -721,56 +667,48 @@
        * Adds a key/value pair to the map.  This routine does
        * no magic morphing.  It ensures the keylist is maintained
        *
  -     * @param key key to use for mapping
  -     * @param obj object to store
  +     * @param key  the key to store at
  +     * @param value  the decoded object to store
        */
  -    private void addPropertyDirect(String key, Object obj) {
  +    private void addPropertyDirect(String key, Object value) {
           // safety check
           if (!containsKey(key)) {
               keysAsListed.add(key);
           }
  -        put(key, obj);
  +        put(key, value);
       }
   
       /**
  -     * Sets a string property w/o checking for commas - used
  +     * Adds a decoded property to the map w/o checking for commas - used
        * internally when a property has been broken up into
        * strings that could contain escaped commas to prevent
        * the inadvertent vectorization.
        * <p>
        * Thanks to Leon Messerschmidt for this one.
        *
  +     * @param key  the key to store at
  +     * @param value  the decoded object to store
        */
  -    private void addStringProperty(String key, String token) {
  -        Object obj = this.get(key);
  -
  -        /*
  -         *  $$$ GMJ
  -         *  FIXME : post 1.0 release, we need to not assume
  -         *  that a scalar is a String - it can be an Object
  -         *  so we should make a little vector-like class
  -         *  say, Foo that wraps (not extends Vector),
  -         *  so we can do things like
  -         *  if ( !( o instanceof Foo) )
  -         *  so we know it's our 'vector' container
  -         *
  -         *  This applies throughout
  -         */
  +    private void addPropertyInternal(String key, Object value) {
  +        Object current = this.get(key);
   
  -        // do the usual thing - if we have a value and 
  -        // it's scalar, make a vector, otherwise add
  -        // to the vector
  -        if (obj instanceof String) {
  +        if (current instanceof String) {
  +            // one object already in map - convert it to a vector
               Vector v = new Vector(2);
  -            v.addElement(obj);
  -            v.addElement(token);
  +            v.addElement(current);
  +            v.addElement(value);
               put(key, v);
               
  -        } else if (obj instanceof Vector) {
  -            ((Vector) obj).addElement(token);
  +        } else if (current instanceof Vector) {
  +            // already a vector - just add the new token
  +            ((Vector) current).addElement(value);
               
           } else {
  -            addPropertyDirect(key, token);
  +            // brand new key - store in keysAsListed to retain order
  +            if (!containsKey(key)) {
  +                keysAsListed.add(key);
  +            }
  +            put(key, value);
           }
       }
   
  
  
  

---------------------------------------------------------------------
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