KickJava   Java API By Example, From Geeks To Geeks.

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


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.sitemap.PatternException;
32
33 import java.util.Map JavaDoc;
34
35 /**
36  *
37  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
38  * @version CVS $Id: SelectNode.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

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