1 16 package org.apache.cocoon.components.treeprocessor.sitemap; 17 18 import org.apache.avalon.framework.activity.Disposable; 19 import org.apache.avalon.framework.component.ComponentException; 20 import org.apache.avalon.framework.component.ComponentManager; 21 import org.apache.avalon.framework.component.ComponentSelector; 22 import org.apache.avalon.framework.component.Composable; 23 import org.apache.avalon.framework.parameters.Parameters; 24 import org.apache.cocoon.components.treeprocessor.InvokeContext; 25 import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode; 26 import org.apache.cocoon.components.treeprocessor.ProcessingNode; 27 import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode; 28 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver; 29 import org.apache.cocoon.environment.Environment; 30 import org.apache.cocoon.selection.Selector; 31 import org.apache.cocoon.sitemap.PatternException; 32 33 import java.util.Map ; 34 35 40 public class SelectNode extends SimpleSelectorProcessingNode 41 implements ParameterizableProcessingNode, Composable, Disposable { 42 43 44 private Map parameters; 45 46 47 protected Selector threadSafeSelector; 48 49 private ProcessingNode[][] whenNodes; 50 51 private VariableResolver[] whenTests; 52 53 private ProcessingNode[] otherwhiseNodes; 54 55 private ComponentManager manager; 56 57 public SelectNode(String name) throws PatternException { 58 super(name); 59 } 60 61 public void setParameters(Map parameterMap) { 62 this.parameters = parameterMap; 63 } 64 65 public void setCases(ProcessingNode[][] whenNodes, VariableResolver[] whenTests, ProcessingNode[] otherwhiseNodes) { 66 this.whenNodes = whenNodes; 67 this.whenTests = whenTests; 68 this.otherwhiseNodes = otherwhiseNodes; 69 } 70 71 public void compose(ComponentManager manager) throws ComponentException { 72 this.manager = manager; 73 setSelector((ComponentSelector)manager.lookup(Selector.ROLE + "Selector")); 74 75 this.threadSafeSelector = (Selector)this.getThreadSafeComponent(); 77 } 78 79 public final boolean invoke(Environment env, InvokeContext context) 80 throws Exception { 81 82 super.invoke(env, context); 84 85 Map objectModel = env.getObjectModel(); 87 Parameters resolvedParams = VariableResolver.buildParameters(this.parameters, context, objectModel); 88 89 if (this.threadSafeSelector != null) { 91 92 for (int i = 0; i < this.whenTests.length; i++) { 93 if (this.threadSafeSelector.select( 94 whenTests[i].resolve(context, objectModel), 95 objectModel, 96 resolvedParams)) { 97 return invokeNodes(this.whenNodes[i], env, context); 98 } 99 } 100 101 if (this.otherwhiseNodes != null) { 102 return invokeNodes(this.otherwhiseNodes, env, context); 103 } 104 105 return false; 106 107 } else { 108 Selector selector = (Selector)this.selector.select(this.componentName); 109 try { 110 111 for (int i = 0; i < this.whenTests.length; i++) { 112 if (selector.select( 113 whenTests[i].resolve(context, objectModel), 114 objectModel, 115 resolvedParams)) { 116 return invokeNodes(this.whenNodes[i], env, context); 117 } 118 } 119 120 if (this.otherwhiseNodes != null) { 121 return invokeNodes(this.otherwhiseNodes, env, context); 122 } 123 124 return false; 125 } finally { 126 this.selector.release(selector); 127 } 128 } 129 } 130 131 public void dispose() { 132 if (this.threadSafeSelector != null) { 133 this.selector.release(this.threadSafeSelector); 134 this.threadSafeSelector = null; 135 } 136 if (this.selector != null) { 137 this.manager.release(this.selector); 138 this.selector = null; 139 } 140 this.manager = null; 141 } 142 } 143 | Popular Tags |