private static final QName CASE_Q = new QName(Constants.SYNAPSE_NAMESPACE, "case");
private static final QName DEFAULT_Q = new QName(Constants.SYNAPSE_NAMESPACE, "default");
public Mediator createMediator(OMElement elem) {
SwitchMediator switchMediator = new SwitchMediator();
OMAttribute source = elem.getAttribute(new QName(Constants.NULL_NAMESPACE, "source"));
if (source == null) {
String msg = "A 'source' XPath attribute is required for a switch mediator";
log.error(msg);
throw new SynapseException(msg);
} else {
try {
AXIOMXPath sourceXPath = new AXIOMXPath(source.getAttributeValue());
org.apache.synapse.config.xml.OMElementUtils.addNameSpaces(sourceXPath, elem, log);
switchMediator.setSource(sourceXPath);
} catch (JaxenException e) {
String msg = "Invalid XPath for attribute 'source' : " + source.getAttributeValue();
log.error(msg);
throw new SynapseException(msg);
}
}
// after successfully creating the mediator
// set its common attributes such as tracing etc
initMediator(switchMediator, elem);
Iterator iter = elem.getChildrenWithName(CASE_Q);
while (iter.hasNext()) {
OMElement caseElem = (OMElement) iter.next();
SwitchCase aCase = new SwitchCase();
OMAttribute regex = caseElem.getAttribute(new QName(Constants.NULL_NAMESPACE, "regex"));
if (regex == null) {
String msg = "The 'regex' attribute is required for a switch case definition";
log.error(msg);
throw new SynapseException(msg);
}
try {
aCase.setRegex(Pattern.compile(regex.getAttributeValue()));
} catch (PatternSyntaxException pse) {
String msg = "Invalid Regular Expression for attribute 'regex' : " + regex.getAttributeValue();
log.error(msg);
throw new SynapseException(msg);
}
aCase.setCaseMediator(AnonymousListMediatorFactory.createAnonymousListMediator(caseElem));
switchMediator.addCase(aCase);
}
iter = elem.getChildrenWithName(DEFAULT_Q);
while (iter.hasNext()) {
SwitchCase aCase = new SwitchCase();
aCase.setCaseMediator(AnonymousListMediatorFactory.createAnonymousListMediator((OMElement) iter.next()));
switchMediator.setDefaultCase(aCase);
break; // add only the *first* default if multiple are specified, ignore rest if any
}
return switchMediator;
}