001    package jcolibri.connector.plaintextutils;
002    
003    import java.math.BigDecimal;
004    import java.net.URL;
005    import java.text.SimpleDateFormat;
006    import java.util.Date;
007    
008    import jcolibri.connector.TypeAdaptor;
009    
010    
011    /**
012     * Converts data types between its textual representation and Java types. By
013     * default it only manages a few data types:
014     * <ul>
015     * <li>BigDecimal
016     * <li>Boolean
017     * <li>Byte
018     * <li>Date
019     * <li>Double
020     * <li>Float
021     * <li>Int
022     * <li>Long
023     * <li>Object
024     * <li>Short
025     * <li>String
026     * <li>URL
027     * </ul>
028     * Even so, developers can store any class in the text file if the class
029     * implements the jcolibri.connectors.TypeAdaptor interface.
030     * 
031     * @author Juan Antonio Recio García
032     * @version 2.0
033     * @see jcolibri.connector.TypeAdaptor
034     */
035    public class PlainTextTypeConverter {
036    
037            /**
038             * Coverts a string representation of the data in an object.
039             * 
040             * @param data
041             *            string representation of the data.
042             * @param type
043             *            type of the data.
044             * @return the object trepresented by data and type.
045             */
046            @SuppressWarnings("unchecked")
047            public static Object convert(String data, Class type) {
048                    try {
049                            if(data == null)
050                                return null;
051                            else if(data.equals("null"))
052                                return null;
053                            else if (type.equals(BigDecimal.class))
054                                    return new BigDecimal(data);
055                            else if (type.equals(Boolean.class))
056                                    return new Boolean(data);
057                            else if (type.equals(Byte.class))
058                                    return new Byte(data);
059                            else if (type.equals(Date.class))
060                                    return new SimpleDateFormat().parse(data);
061                            else if (type.equals(Double.class))
062                                    return new Double(data);
063                            else if (type.equals(Float.class))
064                                    return new Float(data);
065                            else if (type.equals(Integer.class))
066                                    return new Integer(data);
067                            else if (type.equals(Long.class))
068                                    return new Long(data);
069                            else if (type.equals(Object.class))
070                                    return data;
071                            else if (type.equals(Short.class))
072                                    return new Short(data);
073                            else if (type.equals(String.class))
074                                    return data;
075                            else if (type.equals(URL.class))
076                                    return data;
077                            else if (Enum.class.isAssignableFrom(type))
078                            {
079                                    return Enum.valueOf(type, data);
080                            }
081                            else {
082                                    TypeAdaptor adaptor = (TypeAdaptor)type.newInstance();
083                                    adaptor.fromString(data);
084                                    return adaptor;
085                            }
086                    } catch (Exception e) {
087                            org.apache.commons.logging.LogFactory.getLog(PlainTextTypeConverter.class).error("Error converting types: " + e.getMessage());
088                    }
089    
090                    return null;
091            }
092    }