1 16 package org.apache.cocoon.components.treeprocessor.sitemap; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.component.ComponentManager; 24 import org.apache.avalon.framework.component.Composable; 25 import org.apache.avalon.framework.configuration.Configurable; 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 28 import org.apache.cocoon.ProcessingException; 29 import org.apache.cocoon.components.flow.Interpreter; 30 import org.apache.cocoon.components.treeprocessor.AbstractProcessingNode; 31 import org.apache.cocoon.components.treeprocessor.InvokeContext; 32 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver; 33 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory; 34 import org.apache.cocoon.environment.Environment; 35 import org.apache.cocoon.environment.Redirector; 36 import org.apache.cocoon.sitemap.PatternException; 37 38 46 public class CallFunctionNode extends AbstractProcessingNode implements Configurable, Composable { 47 protected List parameters; 48 protected VariableResolver functionName; 49 protected VariableResolver continuationId; 50 protected ComponentManager manager; 51 protected Interpreter interpreter; 52 53 public static List resolveList(List expressions, ComponentManager manager, InvokeContext context, Map objectModel) 54 throws PatternException { 55 int size; 56 if (expressions == null || (size = expressions.size()) == 0) 57 return Collections.EMPTY_LIST; 58 59 List result = new ArrayList (size); 60 61 for (int i = 0; i < size; i++) { 62 Interpreter.Argument arg = (Interpreter.Argument)expressions.get(i); 63 String value = VariableResolverFactory.getResolver(arg.value, manager).resolve(context, objectModel); 64 result.add(new Interpreter.Argument(arg.name, value)); 65 } 66 67 return result; 68 } 69 70 public CallFunctionNode(VariableResolver functionName, VariableResolver continuationId) { 71 this.functionName = functionName; 72 this.continuationId = continuationId; 73 } 74 75 public void setInterpreter(Interpreter interp) throws Exception { 76 this.interpreter = interp; 77 } 78 79 86 public void configure(Configuration config) throws ConfigurationException { 87 parameters = new ArrayList (); 93 94 Configuration[] params = config.getChildren("parameter"); 95 for (int i = 0; i < params.length; i++) { 96 Configuration param = params[i]; 97 String name = param.getAttribute("name", null); 98 String value = param.getAttribute("value", null); 99 parameters.add(new Interpreter.Argument(name, value)); 100 } 101 } 102 103 public void compose(ComponentManager manager) { 104 this.manager = manager; 105 } 106 107 public boolean invoke(Environment env, InvokeContext context) throws Exception { 108 List params = null; 109 110 if (parameters != null) { 112 params = resolveList(parameters, manager, context, env.getObjectModel()); 113 } 114 115 Redirector redirector = context.getRedirector(); 117 118 String continuation = continuationId.resolve(context, env.getObjectModel()); 121 if (continuation != null && continuation.length() > 0) { 122 try { 123 interpreter.handleContinuation(continuation, params, redirector); 124 } catch(Exception e) { 125 throw ProcessingException.throwLocated("Sitemap: error calling continuation", e, getLocation()); 126 } 127 if (!redirector.hasRedirected()) { 128 throw new ProcessingException("Sitemap: <map:call continuation> did not send a response", getLocation()); 129 } 130 return true; 131 } 132 133 String name = functionName.resolve(context, env.getObjectModel()); 136 if (name != null && name.length() > 0) { 137 try { 138 interpreter.callFunction(name, params, redirector); 139 } catch(Exception e) { 140 throw ProcessingException.throwLocated("Sitemap: error calling function '" + name + "'", e, getLocation()); 141 } 142 if (!redirector.hasRedirected()) { 143 throw new ProcessingException("Sitemap: <map:call function> did not send a response", getLocation()); 144 } 145 return true; 146 } 147 148 throw new ProcessingException("Sitemap: no function nor continuation given in <map:call function>", getLocation()); 150 } 151 } 152 | Popular Tags |