001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.treeprocessor.sitemap;
018:
019: import org.apache.avalon.framework.activity.Initializable;
020: import org.apache.avalon.framework.component.ComponentException;
021: import org.apache.avalon.framework.component.ComponentManager;
022: import org.apache.avalon.framework.component.Composable;
023: import org.apache.cocoon.components.treeprocessor.AbstractProcessingNode;
024: import org.apache.cocoon.components.treeprocessor.CategoryNode;
025: import org.apache.cocoon.components.treeprocessor.InvokeContext;
026: import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode;
027: import org.apache.cocoon.components.treeprocessor.ProcessingNode;
028: import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
029: import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
030: import org.apache.cocoon.environment.Environment;
031:
032: import java.util.Map;
033:
034: /**
035: *
036: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
037: * @version CVS $Id: CallNode.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039:
040: public class CallNode extends AbstractProcessingNode implements
041: Initializable, ParameterizableProcessingNode, Composable {
042:
043: private ComponentManager manager;
044:
045: /** The parameters of this node */
046: private Map parameters;
047:
048: /** The 'resource' attribute */
049: private String resourceName;
050:
051: private VariableResolver resourceResolver;
052:
053: /** The category node */
054: private CategoryNode resources;
055:
056: private ProcessingNode resourceNode;
057:
058: public void compose(ComponentManager manager)
059: throws ComponentException {
060: this .manager = manager;
061: }
062:
063: public void setParameters(Map parameterMap) {
064: this .parameters = parameterMap;
065: }
066:
067: public void setResource(CategoryNode resources, String resourceName)
068: throws Exception {
069: this .resourceName = resourceName;
070: this .resources = resources;
071: }
072:
073: public void initialize() throws Exception {
074: if (VariableResolverFactory.needsResolve(this .resourceName)) {
075: // Will always be resolved at invoke time
076: this .resourceResolver = VariableResolverFactory
077: .getResolver(this .resourceName, this .manager);
078: } else {
079: // Static name : get it now
080: this .resourceNode = this .resources
081: .getNodeByName(VariableResolverFactory
082: .unescape(this .resourceName));
083: }
084: }
085:
086: public final boolean invoke(Environment env, InvokeContext context)
087: throws Exception {
088:
089: Map objectModel = env.getObjectModel();
090: // Resolve parameters, but push them only once the resource name has been
091: // resolved, otherwise it adds an unwanted nesting level
092: Map params = VariableResolver.buildMap(this .parameters,
093: context, objectModel);
094:
095: if (this .resourceNode != null) {
096: // Static resource name
097: context.pushMap(null, params);
098:
099: try {
100: return this .resourceNode.invoke(env, context);
101: } finally {
102: context.popMap();
103: }
104:
105: } else {
106: // Resolved resource name
107: String name = this .resourceResolver.resolve(context,
108: objectModel);
109: if (getLogger().isDebugEnabled()) {
110: getLogger().debug("Calling resource " + name);
111: }
112:
113: // and only now push the parameters
114: context.pushMap(null, params);
115:
116: try {
117: return this.resources.invokeByName(name, env, context);
118: } finally {
119: context.popMap();
120: }
121: }
122: }
123: }
|