KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > treeprocessor > sitemap > SwitchSelectNode


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
35
36 /**
37  *
38  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
39  * @version CVS $Id: SwitchSelectNode.java 30932 2004-07-29 17:35:38Z vgritsenko $
40  */

41 public class SwitchSelectNode extends SimpleSelectorProcessingNode
42                               implements ParameterizableProcessingNode, Composable, Disposable {
43
44     /** The parameters of this node */
45     private Map JavaDoc parameters;
46
47     /** Pre-selected selector, if it's ThreadSafe */
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 JavaDoc name) throws PatternException {
59         super(name);
60     }
61
62     public void setParameters(Map JavaDoc 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         // Get the selector, if it's ThreadSafe
78
this.threadSafeSelector = (SwitchSelector)this.getThreadSafeComponent();
79     }
80
81     public final boolean invoke(Environment env, InvokeContext context)
82     throws Exception JavaDoc {
83     
84         // Perform any common invoke functionality
85
super.invoke(env, context);
86
87         // Prepare data needed by the action
88
Map JavaDoc objectModel = env.getObjectModel();
89         Parameters resolvedParams = VariableResolver.buildParameters(this.parameters, context, objectModel);
90
91         // If selector is ThreadSafe, avoid select() and try/catch block (faster !)
92
if (this.threadSafeSelector != null) {
93
94             Object JavaDoc 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 JavaDoc 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