//
// $Id: ValueMarshaller.java,v 1.4 2002/03/08 02:40:02 mdb Exp $

package com.samskivert.util;

import java.awt.Color;
import java.util.HashMap;

/**
 * Provides a mechanism for converting a string representation of a value
 * into a Java object when provided with the type of the target object.
 * This is used to do things like populate object fields with values
 * parsed from an XML file and the like.
 */
public class ValueMarshaller
{
    /**
     * Attempts to convert the specified value to an instance of the
     * specified object type.
     *
     * @exception Exception thrown if no field parser exists for the
     * target type or if an error occurs while parsing the value.
     */
    public static Object unmarshal (Class type, String source)
        throws Exception
    {
        // look up an argument parser for the field type
        Parser parser = (Parser)_parsers.get(type);
        if (parser == null) {
            String errmsg = "Don't know how to convert strings into " +
                "values of type '" + type + "'.";
            throw new Exception(errmsg);
        }
        return parser.parse(source);
    }

    protected static interface Parser
    {
        public Object parse (String source) throws Exception;
    }

    protected static HashMap _parsers;

    protected static final int[] INT_ARRAY_PROTOTYPE = new int[0];
    protected static final float[] FLOAT_ARRAY_PROTOTYPE = new float[0];
    protected static final String[] STRING_ARRAY_PROTOTYPE = new String[0];

    static {
        _parsers = new HashMap();

        // we can parse strings
        _parsers.put(String.class, new Parser() {
            public Object parse (String source) throws Exception {
                return source;
            }
        });

        // and ints
        _parsers.put(Integer.TYPE, new Parser() {
            public Object parse (String source) throws Exception {
                return Integer.valueOf(source);
            }
        });

        // and integers
        _parsers.put(Integer.class, new Parser() {
            public Object parse (String source) throws Exception {
                return Integer.valueOf(source);
            }
        });

        // and booleans
        _parsers.put(Boolean.TYPE, new Parser() {
            public Object parse (String source) throws Exception {
                return Boolean.valueOf(source);
            }
        });

        // and int arrays
        _parsers.put(INT_ARRAY_PROTOTYPE.getClass(), new Parser() {
            public Object parse (String source) throws Exception {
                return StringUtil.parseIntArray(source);
            }
        });

        // and float arrays
        _parsers.put(FLOAT_ARRAY_PROTOTYPE.getClass(), new Parser() {
            public Object parse (String source) throws Exception {
                return StringUtil.parseFloatArray(source);
            }
        });

        // and string arrays, oh my!
        _parsers.put(STRING_ARRAY_PROTOTYPE.getClass(), new Parser() {
            public Object parse (String source) throws Exception {
                return StringUtil.parseStringArray(source);
            }
        });

        // and Color objects
        _parsers.put(Color.class, new Parser() {
            public Object parse (String source) throws Exception {
                if (source.length() == 0 || source.charAt(0) != '#') {
                    return new Color(Integer.parseInt(source, 16));
                } else {
                    return new Color(Integer.parseInt(source.substring(1), 16));
                }
            }
        });
    }
}

@@ -29,17 +29,17 @@
 ava,v 1.
-4
+5
  2002/03
@@ -47,15 +47,15 @@
 08 0
-2:40:02
+6:13:21
  mdb
@@ -2000,15 +2000,13 @@
 and 
-integer
+float
 s%0A  
@@ -2028,21 +2028,18 @@
 put(
-Integer.class
+Float.TYPE
 , ne
@@ -2133,39 +2133,37 @@
          return 
-Integer
+Float
 .valueOf(source)
@@ -2324,32 +2324,32 @@
 ows Exception %7B%0A
-
                 
@@ -2399,32 +2399,249 @@
  %7D%0A        %7D);%0A%0A
+        // and integers%0A        _parsers.put(Integer.class, new Parser() %7B%0A            public Object parse (String source) throws Exception %7B%0A                return Integer.valueOf(source);%0A            %7D%0A        %7D);%0A%0A
         // and i

Add ability to parse floats.