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.WhileDef;
040: import org.webharvest.runtime.Scraper;
041: import org.webharvest.runtime.ScraperContext;
042: import org.webharvest.runtime.scripting.ScriptEngine;
043: import org.webharvest.runtime.templaters.BaseTemplater;
044: import org.webharvest.runtime.variables.*;
045: import org.webharvest.utils.CommonUtil;
046: import org.webharvest.utils.Constants;
047:
048: import java.util.ArrayList;
049: import java.util.List;
050:
051: /**
052: * Conditional processor.
053: */
054: public class WhileProcessor extends BaseProcessor {
055:
056: private WhileDef whileDef;
057:
058: public WhileProcessor(WhileDef whileDef) {
059: super (whileDef);
060: this .whileDef = whileDef;
061: }
062:
063: public IVariable execute(Scraper scraper, ScraperContext context) {
064: ScriptEngine scriptEngine = scraper.getScriptEngine();
065: String index = BaseTemplater.execute(whileDef.getIndex(),
066: scriptEngine);
067: String maxLoopsString = BaseTemplater.execute(whileDef
068: .getMaxLoops(), scriptEngine);
069:
070: double maxLoops = Constants.DEFAULT_MAX_LOOPS;
071: if (maxLoopsString != null && !"".equals(maxLoopsString.trim())) {
072: maxLoops = Double.parseDouble(maxLoopsString);
073: }
074:
075: List resultList = new ArrayList();
076:
077: IVariable indexBeforeLoop = (IVariable) context.get(index);
078:
079: int i = 1;
080:
081: // define first value of index variable
082: if (index != null && !"".equals(index)) {
083: context.put(index, new NodeVariable(String.valueOf(i)));
084: }
085:
086: String condition = BaseTemplater.execute(whileDef
087: .getCondition(), scriptEngine);
088:
089: // iterates while testing variable represents boolean true or loop limit is exceeded
090: while (CommonUtil.isBooleanTrue(condition) && (i <= maxLoops)) {
091: IVariable loopResult = getBodyListContent(whileDef,
092: scraper, context);
093: resultList.addAll(loopResult.toList());
094:
095: i++;
096: // define current value of index variable
097: if (index != null && !"".equals(index)) {
098: context.put(index, new NodeVariable(String.valueOf(i)));
099: }
100:
101: condition = BaseTemplater.execute(whileDef.getCondition(),
102: scriptEngine);
103: }
104:
105: // restores previous value of index variable
106: if (index != null && indexBeforeLoop != null) {
107: context.put(index, indexBeforeLoop);
108: }
109:
110: return new ListVariable(resultList);
111: }
112:
113: }
|