KickJava   Java API By Example, From Geeks To Geeks.

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


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.configuration.Configurable;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.cocoon.components.flow.Interpreter;
22 import org.apache.cocoon.components.treeprocessor.AbstractProcessingNodeBuilder;
23 import org.apache.cocoon.components.treeprocessor.CategoryNode;
24 import org.apache.cocoon.components.treeprocessor.CategoryNodeBuilder;
25 import org.apache.cocoon.components.treeprocessor.LinkedProcessingNodeBuilder;
26 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
27 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
28
29 /**
30  *
31  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
32  * @author <a HREF="mailto:ovidiu@apache.org">Ovidiu Predescu</a>
33  * @version CVS $Id: CallNodeBuilder.java 55145 2004-10-20 12:42:46Z vgritsenko $
34  */

35 public class CallNodeBuilder extends AbstractProcessingNodeBuilder
36                              implements LinkedProcessingNodeBuilder {
37
38     protected ProcessingNode node;
39     protected String JavaDoc resourceName;
40     protected String JavaDoc functionName;
41     protected String JavaDoc continuationId;
42
43     public ProcessingNode buildNode(Configuration config)
44     throws Exception JavaDoc {
45         resourceName = config.getAttribute("resource", null);
46         functionName = config.getAttribute("function", null);
47         continuationId = config.getAttribute("continuation", null);
48
49         if (resourceName == null) {
50             // Building a CallFunction node
51
if (functionName == null && continuationId == null) {
52                 throw new ConfigurationException(
53                     "<map:call> must have either a 'resource', 'function' or 'continuation' attribute, at " +
54                     config.getLocation());
55             }
56
57             node = new CallFunctionNode(
58                 VariableResolverFactory.getResolver(functionName, this.manager),
59                 VariableResolverFactory.getResolver(continuationId, this.manager)
60             );
61
62         } else {
63             // Building a Call(Resource)Node
64
if (functionName != null || continuationId != null) {
65                 throw new ConfigurationException(
66                     "<map:call> cannot have both a 'resource' and a 'function' or 'continuation' attribute, at "
67                     + config.getLocation()
68                 );
69             }
70             node = new CallNode();
71         }
72
73         this.treeBuilder.setupNode(this.node, config);
74         if (node instanceof Configurable) {
75             ((Configurable)this.node).configure(config);
76         }
77
78         return this.node;
79     }
80
81     public void linkNode() throws Exception JavaDoc {
82         if (resourceName != null) {
83             // We have a <map:call resource="..."/>
84
CategoryNode resources
85                     = CategoryNodeBuilder.getCategoryNode(treeBuilder, "resources");
86
87             if (resources == null)
88                 throw new ConfigurationException("This sitemap contains no resources. Cannot call at " + node.getLocation());
89
90             ((CallNode)this.node).setResource(resources, this.resourceName);
91         }
92         else {
93             // We have a <map:call> with either "function" or
94
// "continuation", or both specified
95

96             // Check to see if a flow has been defined in this sitemap
97
FlowNode flow = (FlowNode)treeBuilder.getRegisteredNode("flow");
98             if (flow == null)
99                 throw new ConfigurationException("This sitemap contains no control flows defined, cannot call at " + node.getLocation() + ". Define a control flow using <map:flow>, with embedded <map:script> elements.");
100
101             // Get the Interpreter instance and set it up in the
102
// CallFunctionNode function
103
Interpreter interpreter = flow.getInterpreter();
104             ((CallFunctionNode)node).setInterpreter(interpreter);
105         }
106     }
107 }
108
Popular Tags