I have a chunk of code here:
public class ParserFactory
{
public static ParserClass getParserClass(InputStream is, CustomMap customMap,
DataHandler recordHandler, DataHandler columnHandler) throws JsonProcessingException
{
ParserClassType ParserClassType = ParserClassType.getParserClassType(customMap.getParser());
switch (ParserClassType)
{
case SPECIFIC_XML:
return new SpecificXMLParser(is, customMap.getProperties(), recordHandler, columnHandler);
case XML:
return new GenericXMLParser(is, customMap.getProperties(), recordHandler, columnHandler);
case JSON:
return new GenericJSONParser(is, customMap.getProperties(), recordHandler, columnHandler);
case SPECIAL:
return new SpecialParser(is, customMap.getProperties(), recordHandler, columnHandler);
case FW:
return new GenericFixedWidthParser(is, customMap.getProperties(), recordHandler, columnHandler);
case CSV:
return new GenericCSVParser(is, customMap.getProperties(), recordHandler, columnHandler);
default:
throw new IllegalArgumentException("ParserClassType not found.");
}
}
public static String getFileExtension(CustomMap customMap)
{
ParserClassType ParserClassType = ParserClassType.getParserClassType(customMap.getParser());
switch (ParserClassType)
{
case SPECIFIC_XML:
return SpecificXMLParser.FILE_TYPE_OUTPUT;
case XML:
return GenericXMLParser.FILE_TYPE_OUTPUT;
case JSON:
return GenericJSONParser.FILE_TYPE_OUTPUT;
case SPECIAL:
return SpecialParser.FILE_TYPE_OUTPUT;
case FW:
return GenericFixedWidthParser.FILE_TYPE_OUTPUT;
case CSV:
return GenericCSVParser.FILE_TYPE_OUTPUT;
default:
throw new IllegalArgumentException("ParserClassType not found.");
}
}
}
Now, it's not exactly too important what all these arguments are. In getParserClass
, I retrieve an enum from ParserClassType
. The switch handles all the possible values. A certain value will return a ParserClass
.
The other function, getFileExtension
, retrieves a static string from a parser class.
As my project grows, I will have many new parsers that I add. I want to make this as flexible and less-hectic as possible. I don't want to keep adding cases to this function. So.
I was looking into EnumMap. I was thinking of mapping an ENUM to a ParserClass name, so for example "XML" would make to "GenericXMLParser.class", and then I could use a ReflectionUtil to create the class with the arguments necessary or retrieve that string. Therefore, if I have to make any additions, all I need to do is add the ENUM to the enum class and make an addition to the map. But, ReflectionUtils seems to require me to provide the path of the parser. Something like "com.package.service.more.bs.SpecificXMLParser". I would rather not do that...
Any ideas?