001: /* Copyright (c) 2006-2007, Vladimir Nikic
002: All rights reserved.
003:
004: Redistribution and use of this software in source and binary forms,
005: with or without modification, are permitted provided that the following
006: conditions are met:
007:
008: * Redistributions of source code must retain the above
009: copyright notice, this list of conditions and the
010: following disclaimer.
011:
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the
014: following disclaimer in the documentation and/or other
015: materials provided with the distribution.
016:
017: * The name of Web-Harvest may not be used to endorse or promote
018: products derived from this software without specific prior
019: written permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: POSSIBILITY OF SUCH DAMAGE.
032:
033: You can contact Vladimir Nikic by sending e-mail to
034: nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
035: subject line.
036: */
037: package org.webharvest.runtime.processors;
038:
039: import org.webharvest.definition.*;
040: import org.webharvest.exception.FunctionException;
041: import org.webharvest.runtime.Scraper;
042: import org.webharvest.runtime.ScraperContext;
043: import org.webharvest.runtime.scripting.ScriptEngine;
044: import org.webharvest.runtime.templaters.BaseTemplater;
045: import org.webharvest.runtime.variables.IVariable;
046: import org.webharvest.runtime.variables.NodeVariable;
047:
048: /**
049: * Function call processor.
050: */
051: public class CallProcessor extends BaseProcessor {
052:
053: private CallDef callDef;
054:
055: ScraperContext functionContext = new ScraperContext();
056: ScriptEngine scriptEngine = new ScriptEngine(functionContext);
057:
058: private IVariable functionResult = new NodeVariable("");
059:
060: public CallProcessor(CallDef callDef) {
061: super (callDef);
062: this .callDef = callDef;
063: }
064:
065: public IVariable execute(Scraper scraper, ScraperContext context) {
066: // adds this runtiome info to the running functions stack
067: scraper.addRunningFunction(this );
068:
069: String functionName = BaseTemplater.execute(callDef.getName(),
070: scraper.getScriptEngine());
071: FunctionDef functionDef = scraper.getConfiguration()
072: .getFunctionDef(functionName);
073:
074: if (functionDef == null) {
075: throw new FunctionException("Function \"" + functionName
076: + "\" is undefined!");
077: }
078:
079: // executes body of call processor
080: executeBody(callDef, scraper, context);
081:
082: // executes body of function using new context
083: executeBody(functionDef, scraper, functionContext);
084:
085: // remove running function from the stack
086: scraper.removeRunningFunction();
087:
088: return functionResult;
089: }
090:
091: public void setFunctionResult(IVariable result) {
092: this .functionResult = result;
093: }
094:
095: public void addContextVariable(String name, IVariable variable) {
096: functionContext.put(name, variable);
097: }
098:
099: public ScriptEngine getScriptEngine() {
100: return scriptEngine;
101: }
102:
103: }
|