public ProcessingNode buildNode(Configuration config) throws Exception {
String type = this.treeBuilder.getTypeForStatement(config, SELECTOR_ROLE);
// Lists of ProcessingNode[] and test resolvers for each "when"
List whenChildren = new ArrayList();
List whenTests = new ArrayList();
// Nodes for otherwise (if any)
ProcessingNode[] otherwiseNodes = null;
Configuration[] childrenConfig = config.getChildren();
for (int i = 0; i < childrenConfig.length; i++) {
Configuration childConfig = childrenConfig[i];
String name = childConfig.getName();
if ("when".equals(name)) {
checkNamespace(childConfig);
whenTests.add(
VariableResolverFactory.getResolver(childConfig.getAttribute("test"), this.manager)
);
whenChildren.add(buildChildNodes(childConfig));
} else if ("otherwise".equals(name)) {
checkNamespace(childConfig);
if (otherwiseNodes != null) {
String msg = "Duplicate " + name + " (only one is allowed) at " + childConfig.getLocation();
getLogger().error(msg);
throw new ConfigurationException(msg);
}
otherwiseNodes = buildChildNodes(childConfig);
} else if (isParameter(childConfig)) {
// ignore it. It is handled automatically in setupNode()
} else {
// Unknown element
String msg = "Unknown element '" + name + "' in select at " + childConfig.getLocation();
throw new ConfigurationException(msg);
}
}
ProcessingNode[][] whenChildrenNodes = (ProcessingNode[][])whenChildren.toArray(new ProcessingNode[0][0]);
VariableResolver[] whenResolvers = (VariableResolver[])whenTests.toArray(new VariableResolver[whenTests.size()]);
// Get the type and class for this selector
ComponentsSelector compSelector = (ComponentsSelector)this.manager.lookup(SELECTOR_ROLE);
Class clazz = null;
try {
// Find selector class
Selector selector = (Selector)compSelector.select(type);
try {
clazz = selector.getClass();
} finally {
compSelector.release(selector);
}
} finally {
this.manager.release(compSelector);
}
if (SwitchSelector.class.isAssignableFrom(clazz)) {
SwitchSelectNode node = new SwitchSelectNode(type);
this.treeBuilder.setupNode(node, config);
node.setCases(whenChildrenNodes, whenResolvers, otherwiseNodes);
return node;
} else {
SelectNode node = new SelectNode(type);
this.treeBuilder.setupNode(node, config);
node.setCases(whenChildrenNodes, whenResolvers, otherwiseNodes);
return node;
}
}
|