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.selection.SwitchSelector; 32 import org.apache.cocoon.sitemap.PatternException; 33 34 import java.util.Map ; 35 36 41 public class SwitchSelectNode extends SimpleSelectorProcessingNode 42 implements ParameterizableProcessingNode, Composable, Disposable { 43 44 45 private Map parameters; 46 47 48 protected SwitchSelector threadSafeSelector; 49 50 private ProcessingNode[][] whenNodes; 51 52 private VariableResolver[] whenTests; 53 54 private ProcessingNode[] otherwhiseNodes; 55 56 private ComponentManager manager; 57 58 public SwitchSelectNode(String name) throws PatternException { 59 super(name); 60 } 61 62 public void setParameters(Map parameterMap) { 63 this.parameters = parameterMap; 64 } 65 66 public void setCases(ProcessingNode[][] whenNodes, VariableResolver[] whenTests, ProcessingNode[] otherwhiseNodes) { 67 this.whenNodes = whenNodes; 68 this.whenTests = whenTests; 69 this.otherwhiseNodes = otherwhiseNodes; 70 } 71 72 public void compose(ComponentManager manager) throws ComponentException { 73 this.manager = manager; 74 75 setSelector((ComponentSelector)manager.lookup(Selector.ROLE + "Selector")); 76 77 this.threadSafeSelector = (SwitchSelector)this.getThreadSafeComponent(); 79 } 80 81 public final boolean invoke(Environment env, InvokeContext context) 82 throws Exception { 83 84 super.invoke(env, context); 86 87 Map objectModel = env.getObjectModel(); 89 Parameters resolvedParams = VariableResolver.buildParameters(this.parameters, context, objectModel); 90 91 if (this.threadSafeSelector != null) { 93 94 Object ctx = this.threadSafeSelector.getSelectorContext(objectModel, resolvedParams); 95 96 for (int i = 0; i < this.whenTests.length; i++) { 97 if (this.threadSafeSelector.select(whenTests[i].resolve(context, objectModel), ctx)) { 98 return invokeNodes(this.whenNodes[i], env, context); 99 } 100 } 101 102 if (this.otherwhiseNodes != null) { 103 return invokeNodes(this.otherwhiseNodes, env, context); 104 } 105 106 return false; 107 108 } else { 109 SwitchSelector selector = (SwitchSelector)this.selector.select(this.componentName); 110 111 Object ctx = selector.getSelectorContext(objectModel, resolvedParams); 112 113 try { 114 for (int i = 0; i < this.whenTests.length; i++) { 115 if (selector.select(whenTests[i].resolve(context, objectModel), ctx)) { 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 |