* @return initialized document
*/
public SrxDocument parse(Reader reader) {
Srx srx = (Srx) bind.unmarshal(reader);
SrxDocument document = new SrxDocument();
document.setCascade("yes".equals(srx.getHeader().getCascade()));
Body body = srx.getBody();
Map<String, LanguageRule> languageRuleMap = new HashMap<String, LanguageRule>();
for (Languagerule lr : body.getLanguagerules().getLanguagerule()) {
LanguageRule languageRule = new LanguageRule(lr
.getLanguagerulename());
for (net.sourceforge.segment.srx.io.bind.Rule r : lr.getRule()) {
boolean breakRule = !"no".equals(r.getBreak());
String before;
if (r.getBeforebreak() != null) {
before = r.getBeforebreak().getContent();
} else {
before = "";
}
String after;
if (r.getAfterbreak() != null) {
after = r.getAfterbreak().getContent();
} else {
after = "";
}
Rule rule = new Rule(breakRule, before, after);
languageRule.addRule(rule);
}
languageRuleMap.put(languageRule.getName(), languageRule);
}
for (Languagemap lm : body.getMaprules().getLanguagemap()) {
LanguageRule languageRule = languageRuleMap.get(lm
.getLanguagerulename());
if (languageRule == null) {
log.warn("Language map \"" + lm.getLanguagepattern()
+ "\": language rule \"" + lm.getLanguagerulename()
+ "\" not found.");
} else {
document.addLanguageMap(lm.getLanguagepattern(), languageRule);
}
}
return document;
}