KickJava   Java API By Example, From Geeks To Geeks.

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


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.Initializable;
19 import org.apache.avalon.framework.component.ComponentException;
20 import org.apache.avalon.framework.component.ComponentManager;
21 import org.apache.avalon.framework.component.Composable;
22 import org.apache.cocoon.components.treeprocessor.AbstractProcessingNode;
23 import org.apache.cocoon.components.treeprocessor.CategoryNode;
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.variables.VariableResolver;
28 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
29 import org.apache.cocoon.environment.Environment;
30
31 import java.util.Map JavaDoc;
32
33 /**
34  *
35  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
36  * @version CVS $Id: CallNode.java 30932 2004-07-29 17:35:38Z vgritsenko $
37  */

38
39 public class CallNode extends AbstractProcessingNode
40     implements Initializable, ParameterizableProcessingNode, Composable {
41
42     private ComponentManager manager;
43
44     /** The parameters of this node */
45     private Map JavaDoc parameters;
46
47     /** The 'resource' attribute */
48     private String JavaDoc resourceName;
49
50     private VariableResolver resourceResolver;
51
52     /** The category node */
53     private CategoryNode resources;
54
55     private ProcessingNode resourceNode;
56
57     public void compose(ComponentManager manager) throws ComponentException {
58         this.manager = manager;
59     }
60
61     public void setParameters(Map JavaDoc parameterMap) {
62         this.parameters = parameterMap;
63     }
64
65     public void setResource(CategoryNode resources, String JavaDoc resourceName) throws Exception JavaDoc {
66         this.resourceName = resourceName;
67         this.resources = resources;
68     }
69
70     public void initialize() throws Exception JavaDoc {
71         if (VariableResolverFactory.needsResolve(this.resourceName)) {
72             // Will always be resolved at invoke time
73
this.resourceResolver = VariableResolverFactory.getResolver(this.resourceName, this.manager);
74         } else {
75             // Static name : get it now
76
this.resourceNode = this.resources.getNodeByName(VariableResolverFactory.unescape(this.resourceName));
77         }
78     }
79
80     public final boolean invoke(Environment env, InvokeContext context)
81       throws Exception JavaDoc {
82
83         Map JavaDoc objectModel = env.getObjectModel();
84         // Resolve parameters, but push them only once the resource name has been
85
// resolved, otherwise it adds an unwanted nesting level
86
Map JavaDoc params = VariableResolver.buildMap(this.parameters, context, objectModel);
87
88         if (this.resourceNode != null) {
89             // Static resource name
90
context.pushMap(null,params);
91             
92             try {
93                 return this.resourceNode.invoke(env, context);
94             } finally {
95                 context.popMap();
96             }
97     
98         } else {
99             // Resolved resource name
100
String JavaDoc name = this.resourceResolver.resolve(context, objectModel);
101             if (getLogger().isDebugEnabled()) {
102                 getLogger().debug("Calling resource " + name);
103             }
104             
105             // and only now push the parameters
106
context.pushMap(null,params);
107             
108             try {
109                 return this.resources.invokeByName(name, env, context);
110             } finally {
111                 context.popMap();
112             }
113         }
114     }
115 }
116
Popular Tags