1 16 package org.apache.cocoon.components.treeprocessor.sitemap; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNodeBuilder; 21 import org.apache.cocoon.components.treeprocessor.ProcessingNode; 22 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver; 23 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory; 24 import org.apache.cocoon.selection.Selector; 25 import org.apache.cocoon.selection.SwitchSelector; 26 27 import java.util.ArrayList ; 28 import java.util.List ; 29 30 35 36 public class SelectNodeBuilder extends AbstractParentProcessingNodeBuilder { 37 38 private static final String SELECTOR_ROLE = Selector.ROLE + "Selector"; 39 40 public ProcessingNode buildNode(Configuration config) throws Exception { 41 42 String type = this.treeBuilder.getTypeForStatement(config, SELECTOR_ROLE); 43 44 List whenChildren = new ArrayList (); 46 List whenTests = new ArrayList (); 47 48 ProcessingNode[] otherwiseNodes = null; 50 51 Configuration[] childrenConfig = config.getChildren(); 52 for (int i = 0; i < childrenConfig.length; i++) { 53 54 Configuration childConfig = childrenConfig[i]; 55 String name = childConfig.getName(); 56 57 if ("when".equals(name)) { 58 59 checkNamespace(childConfig); 60 whenTests.add( 61 VariableResolverFactory.getResolver(childConfig.getAttribute("test"), this.manager) 62 ); 63 whenChildren.add(buildChildNodes(childConfig)); 64 65 } else if ("otherwise".equals(name)) { 66 67 checkNamespace(childConfig); 68 if (otherwiseNodes != null) { 69 String msg = "Duplicate " + name + " (only one is allowed) at " + childConfig.getLocation(); 70 getLogger().error(msg); 71 throw new ConfigurationException(msg); 72 } 73 74 otherwiseNodes = buildChildNodes(childConfig); 75 76 } else if (isParameter(childConfig)) { 77 79 } else { 80 String msg = "Unknown element '" + name + "' in select at " + childConfig.getLocation(); 82 throw new ConfigurationException(msg); 83 } 84 } 85 86 ProcessingNode[][] whenChildrenNodes = (ProcessingNode[][])whenChildren.toArray(new ProcessingNode[0][0]); 87 VariableResolver[] whenResolvers = (VariableResolver[])whenTests.toArray(new VariableResolver[whenTests.size()]); 88 89 ComponentsSelector compSelector = (ComponentsSelector)this.manager.lookup(SELECTOR_ROLE); 91 92 Class clazz = null; 93 try { 94 Selector selector = (Selector)compSelector.select(type); 96 try { 97 clazz = selector.getClass(); 98 } finally { 99 compSelector.release(selector); 100 } 101 } finally { 102 this.manager.release(compSelector); 103 } 104 105 if (SwitchSelector.class.isAssignableFrom(clazz)) { 106 SwitchSelectNode node = new SwitchSelectNode(type); 107 this.treeBuilder.setupNode(node, config); 108 node.setCases(whenChildrenNodes, whenResolvers, otherwiseNodes); 109 return node; 110 } else { 111 SelectNode node = new SelectNode(type); 112 this.treeBuilder.setupNode(node, config); 113 node.setCases(whenChildrenNodes, whenResolvers, otherwiseNodes); 114 return node; 115 } 116 } 117 } 118 | Popular Tags |