Java Reference
In-Depth Information
for (Entry<Pattern, LineTokenizer> entry : patternTokenizers.entrySet()) {
Matcher matcher = entry.getKey().matcher(input);
if(matcher.find()) {
tokenizer = entry.getValue();
break;
}
}
if(tokenizer != null) {
return tokenizer;
} else {
throw new ParseException("Unable to locate a tokenizer for " + input);
}
}
public void afterPropertiesSet() throws Exception {
patternTokenizers = new HashMap<Pattern, LineTokenizer>();
patternMappers = new HashMap<LineTokenizer, FieldSetMapper<Object>>();
for (Map.Entry<String, LineTokenizer> entry : tokenizers.entrySet()) {
Pattern pattern = Pattern.compile(entry.getKey());
patternTokenizers.put(pattern, entry.getValue());
patternMappers.put(entry.getValue(), mappers.get(entry.getKey()));
}
}
public void setLineTokenizers(Map<String, LineTokenizer> lineTokenizers) {
this.tokenizers = lineTokenizers;
}
public void setFieldSetMappers(Map<String, FieldSetMapper<Object>> fieldSetMappers) {
this.mappers = fieldSetMappers;
}
}
RegularExpressionLineMapper is a basic implementation of the LineMapper interface. It implements
the one required method, mapLine , to convert a String read in from a flat file to an object. The mapLine
method begins by obtaining a LineTokenizer implementation based on the String the method received
as input (more on that in a bit). It then uses that LineTokenizer implementation to parse the String into
a FieldSet. With the String divided into its individual fields, the appropriate FieldSetMapper is retrieved,
and the fields are mapped into a new instance of the object required. The new object is then returned.
To determine which LineTokenizer to use, you do two things. First, the afterPropertiesSet method
(from the InitializerBean interface) creates two Map s. The first consists of regular expression keys to
LineTokenizer values. The second consists of LineTokenizer keys to FieldSetMapper values. These two
Map s are used in delegating to the appropriate implementations. You use these maps by looping through
the keys of the patternTokenizers Map , applying each regular expression to the String you're trying to
parse. When you find a regular expression that matches, you use the associated LineTokenizer to parse
the String . The LineTokenizer from the previous step allows you to get the correct FieldSetMapper from
the patternMappers Map and map the FieldSet to the correct object. If for some reason a LineTokenizer or
 
Search WWH ::




Custom Search