//
// $Id: ValueMarshaller.java,v 1.2 2001/12/11 01:38:08 mdb Exp $

package com.samskivert.util;

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 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 string arrays, oh my!
        _parsers.put(STRING_ARRAY_PROTOTYPE.getClass(), new Parser() {
            public Object parse (String source) throws Exception {
                return StringUtil.parseStringArray(source);
            }
        });
    }
}

@@ -33,28 +33,28 @@
 v 1.
-2
+3
  200
-1/12/11 01:38:0
+2/03/08 02:07:5
 8 md
@@ -92,16 +92,39 @@
 .util;%0A%0A
+import java.awt.Color;%0A
 import j
@@ -2846,16 +2846,451 @@
      %7D);
+%0A%0A        // and Color objects%0A        _parsers.put(Color.class, new Parser() %7B%0A            public Object parse (String source) throws Exception %7B%0A                if (source.length() == 0 %7C%7C source.charAt(0) != '#') %7B%0A                    return new Color(Integer.parseInt(source, 16));%0A                %7D else %7B%0A                    return new Color(Integer.parseInt(source.substring(1), 16));%0A                %7D%0A            %7D%0A        %7D);
 %0A    %7D%0A%7D

Add a parser for Color fields.