File parser to Java type design The basic requirement of my code is to accept a file input, parse it, convert it to a java type (A DTO essentially) for use in another component of the system.

I should also note that I am not actually parsing a file from a filesystem, the FileParser will be used by a Message Driven Bean that has the contents of the file as a String already.

I am looking for input and general design/code review, thanks for your time.

I have 4 classes:


FileParser   
Message   
MessageFactory   
MessageFieldTypes


The format of the file is as follows:

FIELDONETYPE/FIELDONEVALUE
FIELDTWOTYPE/FIELDTWOVALUE
FIELDTHREETYPE/FIELDTHREEVALUE
FIELDFOURTYPE/FIELDFOURVALUE
FIELDFIVETYPE/FIELDFIVEVALUE


etc.  

There are roughly 20 fields.

My initial thought was to parse the file into a hashmap and then create the object. Here is the code listing for the parser:

final public class FileParser 
{
    public static final Message parse(String text) {    

    Map&lt;MessageFieldTypes, String&gt; map = new LinkedHashMap&lt;MessageFieldTypes, String&gt;();

    for(String line : text.split(" *\n *")) {
        String[] keyValuePair = line.split(" */ *");
        map.put(MessageFieldTypes.valueOf(keyValuePair[0]),
                keyValuePair.length == 1 ? "" : keyValuePair[1]);
    }

    return MessageFactory.createFromHashMap(map);
    }
}


Here is the code listing for the factory:

public class MessageFactory {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormat.forPattern("ddMMMyyyy");

    public static Message createFromHashMap(Map&lt;MessageFieldTypes, String&gt; map) {
        Message message = new Message();

        message.setFieldOne(new FieldOneType(map.get(MessageFieldTypes.FIELDONE),
                            map.get(MessageFieldTypes.FIELDTWO), 
                            DATE_TIME_FORMATTER.parseDateTime(map.get(MessageFieldTypes.FIELDTHREE)).toLocalDate()));       
        message.setFieldFour(new FieldFourType(map.get(MessageFieldTypes.FIELDFOUR)));
        message.setFieldFive(map.get(MessageFieldTypes.FIELDFIVE)));

        return message;
    }
}


And the Enum:

public enum MessageFieldTypes {
    FIELDONE,
    FIELDTWO,
    FIELDTHREE,
    FIELDFOUR,
    FIELDFIVE
}
If you can change your file format slightly (to type=value or type:value) I'd suggest using the Java Properties class. It can parse the file from a stream for you (Untested code):

final public class FileParser 
{
    public static final Message parse(String text) {
        text = text.replace("/", ":");   
        Properties typeValues = new Properties();
        typeValues.load(new StringReader(text));

        return MessageFactory.createFromHashTable(typeValues);
    }
}


Note that Properties extends HashTable, not HashMap. They chose HashTable because HashMap is not thread-safe. They work pretty much the same way, though.

On structure: I like the factory format, and it makes sense to use it here. You didn't mention whether the field types are known; if they are your factory code is fine. (It looks like they are, since you call several different constructors with fixed numbers of arguments.)