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.matching.Matcher; 31 32 50 public final class MatchNode extends AbstractNode { 51 52 private final LocatorNode m_ln; 54 55 private Matcher m_matcher; 57 58 private String m_type; 60 61 private String m_pattern; 63 64 private AbstractNode[] m_nodes; 66 67 public MatchNode(final LocatorNode ln, final ServiceManager manager) { 68 super(manager); 69 m_ln = ln; 70 } 71 72 public void build(final Configuration configuration) throws ConfigurationException { 73 74 super.build(configuration); 75 76 m_type = configuration.getAttribute("type",m_ln.getDefaultMatcher()); 78 try { 79 ServiceSelector matchers = (ServiceSelector) super.m_manager.lookup(Matcher.ROLE + "Selector"); 80 m_matcher = (Matcher) matchers.select(m_type); 81 } catch (ServiceException e) { 82 final String message = "Unable to get Matcher of type " + m_type; 83 throw new ConfigurationException(message,e); 84 } 85 86 m_pattern = configuration.getAttribute("pattern"); 88 89 final Configuration[] children = configuration.getChildren(); 91 final List nodes = new ArrayList (children.length); 92 for (int i = 0; i < children.length; i++) { 93 AbstractNode node = null; 94 String name = children[i].getName(); 95 if (name.equals("location")) { 96 node = new LocationNode(m_ln, super.m_manager); 97 } 98 else if (name.equals("match")) { 99 node = new MatchNode(m_ln,super.m_manager); 100 } 101 else if (name.equals("select")) { 102 node = new SelectNode(m_ln, super.m_manager); 103 } 104 else if (!name.equals("parameter")) { 105 final String message = 106 "Unknown match node child: " + name; 107 throw new ConfigurationException(message); 108 } 109 if (node != null) { 110 node.enableLogging(getLogger()); 111 node.build(children[i]); 112 nodes.add(node); 113 } 114 } 115 m_nodes = (AbstractNode[]) nodes.toArray(new AbstractNode[nodes.size()]); 116 } 117 118 public String locate(Map om, InvokeContext context) throws Exception { 119 120 Parameters parameters = resolveParameters(context,om); 121 Map substitutions = m_matcher.match(m_pattern,om,parameters); 122 if (substitutions != null) { 123 if (getLogger().isDebugEnabled()) { 124 getLogger().debug("matched: " + m_pattern); 125 } 126 context.pushMap(null,substitutions); 127 for (int i = 0; i < m_nodes.length; i++) { 128 String location = m_nodes[i].locate(om,context); 129 if (location != null) { 130 return location; 131 } 132 } 133 context.popMap(); 134 } 135 return null; 136 } 137 138 } | Popular Tags |