1 17 package org.apache.forrest.locationmap.lm; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.ServiceSelector; 29 import org.apache.cocoon.components.treeprocessor.InvokeContext; 30 import org.apache.cocoon.selection.Selector; 31 32 33 36 public final class SelectNode extends AbstractNode { 37 38 private final LocatorNode m_ln; 40 41 private Selector m_selector; 43 44 private String m_type; 46 47 private AbstractNode[] m_nodes; 49 50 51 public SelectNode(LocatorNode ln, ServiceManager manager) { 52 super(manager); 53 m_ln = ln; 54 } 55 56 public void build(Configuration configuration) throws ConfigurationException { 57 58 super.build(configuration); 59 60 m_type = configuration.getAttribute("type",m_ln.getDefaultSelector()); 62 try { 63 final ServiceSelector selectors = (ServiceSelector) super.m_manager.lookup(Selector.ROLE + "Selector"); 64 m_selector = (Selector) selectors.select(m_type); 65 } catch (ServiceException e) { 66 final String message = "Unable to get Selector of type " + m_type; 67 throw new ConfigurationException(message,e); 68 } 69 70 final Configuration[] children = configuration.getChildren(); 72 final List nodes = new ArrayList (children.length); 73 for (int i = 0; i < children.length; i++) { 74 AbstractNode node = null; 75 String name = children[i].getName(); 76 if (name.equals("location")) { 77 node = new LocationNode(m_ln, super.m_manager); 78 } 79 else if (name.equals("match")) { 80 node = new MatchNode(m_ln, super.m_manager); 81 } 82 else if (name.equals("select")) { 83 node = new SelectNode(m_ln, super.m_manager); 84 } 85 else if (!name.equals("parameter")) { 86 final String message = "Unknown select node child:" + name; 87 throw new ConfigurationException(message); 88 } 89 if (node != null) { 90 node.enableLogging(getLogger()); 91 node.build(children[i]); 92 nodes.add(node); 93 } 94 } 95 m_nodes = (AbstractNode[]) nodes.toArray(new AbstractNode[nodes.size()]); 96 } 97 98 public String locate(Map om, InvokeContext context) throws Exception { 99 100 Parameters parameters = resolveParameters(context,om); 101 for (int i = 0; i < m_nodes.length; i++) { 102 String location = m_nodes[i].locate(om,context); 103 if (m_selector.select(location,om,parameters)) { 104 if (getLogger().isDebugEnabled()) { 105 getLogger().debug("selected: " + location); 106 } 107 return location; 108 } 109 } 110 return null; 111 } 112 } | Popular Tags |