KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.cocoon.components.treeprocessor.InvokeContext;
23 import org.apache.cocoon.components.treeprocessor.NamedProcessingNode;
24 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
25 import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode;
26 import org.apache.cocoon.environment.Environment;
27
28 /**
29  *
30  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
31  * @version CVS $Id: ActionSetNode.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  */

33
34 public class ActionSetNode extends SimpleSelectorProcessingNode
35   implements NamedProcessingNode {
36       
37     public static final String JavaDoc CALLER_PARAMETERS = ActionSetNode.class.getName() + "/CallerParameters";
38     public static final String JavaDoc ACTION_RESULTS = ActionSetNode.class.getName() + "/ActionResults";
39
40     /** The action nodes */
41     private ProcessingNode[] nodes;
42
43     /** The 'action' attribute for each action */
44     private String JavaDoc[] actionNames;
45
46     public ActionSetNode(
47       String JavaDoc name, ProcessingNode[] nodes, String JavaDoc[] actionNames) {
48         super(name);
49         this.nodes = nodes;
50         this.actionNames = actionNames;
51     }
52
53     public final boolean invoke(Environment env, InvokeContext context)
54       throws Exception JavaDoc {
55     
56         // Perform any common invoke functionalty
57
// super.invoke(env, context);
58
String JavaDoc msg = "An action-set cannot be invoked, at " + this.getLocation();
59         throw new UnsupportedOperationException JavaDoc(msg);
60     }
61
62     /**
63      * Call the actions composing the action-set and return the combined result of
64      * these actions.
65      */

66     public final Map JavaDoc call(Environment env, InvokeContext context, Parameters params) throws Exception JavaDoc {
67
68         String JavaDoc cocoonAction = env.getAction();
69
70         // Store the parameters from the caller into the environment so that they can be merged with
71
// each action's parameters.
72

73
74         Map JavaDoc result = null;
75
76         // Call each action that either has no cocoonAction, or whose cocoonAction equals
77
// the one from the environment.
78
env.setAttribute(CALLER_PARAMETERS, params);
79
80         for (int i = 0; i < nodes.length; i++) {
81
82
83             String JavaDoc actionName = actionNames[i];
84             if (actionName == null || actionName.equals(cocoonAction)) {
85                 
86                 this.nodes[i].invoke(env, context);
87                 
88                 // Get action results. They're passed back through the environment since action-sets
89
// "violate" the tree hierarchy (the returned Map is visible outside of the node)
90
Map JavaDoc actionResult = (Map JavaDoc)env.getAttribute(ACTION_RESULTS);
91                 // Don't forget to clear it
92
env.removeAttribute(ACTION_RESULTS);
93                 
94                 if (actionResult != null) {
95                     // Merge the result in the global result, creating it if necessary.
96
if (result == null) {
97                         result = new HashMap JavaDoc(actionResult);
98                     } else {
99                         result.putAll(actionResult);
100                     }
101                 }
102                 
103             } // if (actionName...
104
} // for (int i...
105

106         return result;
107     }
108
109     /**
110      * Implementation of <code>NamedProcessingNode</code>.
111      */

112
113     public String JavaDoc getName() {
114         return this.componentName;
115     }
116 }
117
Popular Tags