1 16 package org.apache.cocoon.components.treeprocessor.sitemap; 17 18 import java.util.Map ; 19 20 import org.apache.avalon.framework.activity.Disposable; 21 import org.apache.avalon.framework.component.ComponentException; 22 import org.apache.avalon.framework.component.ComponentManager; 23 import org.apache.avalon.framework.component.ComponentSelector; 24 import org.apache.avalon.framework.component.Composable; 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.cocoon.components.treeprocessor.InvokeContext; 27 import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode; 28 import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode; 29 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver; 30 import org.apache.cocoon.environment.Environment; 31 import org.apache.cocoon.matching.Matcher; 32 import org.apache.cocoon.sitemap.PatternException; 33 34 39 40 public class MatchNode extends SimpleSelectorProcessingNode 41 implements ParameterizableProcessingNode, Composable, Disposable { 42 43 44 private VariableResolver pattern; 45 46 47 private String name; 48 49 50 private Matcher threadSafeMatcher; 51 52 private Map parameters; 53 54 private ComponentManager manager; 55 56 public MatchNode(String type, VariableResolver pattern, String name) throws PatternException { 57 super(type); 58 this.pattern = pattern; 59 this.name = name; 60 } 61 62 public void setParameters(Map parameterMap) { 63 this.parameters = parameterMap; 64 } 65 66 public void compose(ComponentManager manager) throws ComponentException { 67 this.manager = manager; 68 this.setSelector((ComponentSelector)manager.lookup(Matcher.ROLE + "Selector")); 69 70 this.threadSafeMatcher = (Matcher)this.getThreadSafeComponent(); 72 } 73 74 public final boolean invoke(Environment env, InvokeContext context) 75 throws Exception { 76 77 super.invoke(env, context); 79 80 Map objectModel = env.getObjectModel(); 81 82 String resolvedPattern = pattern.resolve(context, objectModel); 83 Parameters resolvedParams = VariableResolver.buildParameters(this.parameters, context, objectModel); 84 85 Map result = null; 86 87 if (this.threadSafeMatcher != null) { 88 result = this.threadSafeMatcher.match(resolvedPattern, objectModel, resolvedParams); 90 } else { 91 Matcher matcher = (Matcher)this.selector.select(this.componentName); 93 try { 94 result = matcher.match(resolvedPattern, objectModel, resolvedParams); 95 } finally { 96 this.selector.release(matcher); 97 } 98 } 99 100 if (result != null) { 101 if (getLogger().isDebugEnabled()) { 102 getLogger().debug("Matcher '" + this.componentName + "' matched pattern '" + this.pattern + 103 "' at " + this.getLocation()); 104 } 105 106 return this.invokeNodes(children, env, context, name, result); 108 } else { 109 return false; 111 } 112 } 113 114 117 public void dispose() { 118 this.manager.release(this.selector); 119 } 120 } 121 | Popular Tags |