|                                                                                                              1
 37  package org.webharvest.runtime.processors;
 38
 39  import org.webharvest.definition.*;
 40  import org.webharvest.exception.FunctionException;
 41  import org.webharvest.runtime.Scraper;
 42  import org.webharvest.runtime.ScraperContext;
 43  import org.webharvest.runtime.scripting.ScriptEngine;
 44  import org.webharvest.runtime.templaters.BaseTemplater;
 45  import org.webharvest.runtime.variables.IVariable;
 46  import org.webharvest.runtime.variables.NodeVariable;
 47
 48
 51  public class CallProcessor extends BaseProcessor {
 52
 53      private CallDef callDef;
 54
 55      ScraperContext functionContext = new ScraperContext();
 56      ScriptEngine scriptEngine = new ScriptEngine(functionContext);
 57
 58      private IVariable functionResult = new NodeVariable("");
 59
 60      public CallProcessor(CallDef callDef) {
 61          super(callDef);
 62          this.callDef = callDef;
 63      }
 64
 65      public IVariable execute(Scraper scraper, ScraperContext context) {
 66                  scraper.addRunningFunction(this);
 68
 69          String
  functionName = BaseTemplater.execute( callDef.getName(), scraper.getScriptEngine() ); 70          FunctionDef functionDef = scraper.getConfiguration().getFunctionDef(functionName);
 71
 72          if (functionDef == null) {
 73              throw new FunctionException("Function \"" + functionName + "\" is undefined!");
 74          }
 75
 76                  executeBody(callDef, scraper, context);
 78
 79                  executeBody(functionDef, scraper, functionContext);
 81
 82                  scraper.removeRunningFunction();
 84
 85          return functionResult;
 86      }
 87
 88      public void setFunctionResult(IVariable result) {
 89          this.functionResult = result;
 90      }
 91
 92      public void addContextVariable(String
  name, IVariable variable) { 93          functionContext.put(name, variable);
 94      }
 95
 96
 97      public ScriptEngine getScriptEngine() {
 98          return scriptEngine;
 99      }
 100
 101 }
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |