|                                                                                                              1
 37  package org.webharvest.runtime.processors;
 38
 39  import org.webharvest.definition.WhileDef;
 40  import org.webharvest.runtime.Scraper;
 41  import org.webharvest.runtime.ScraperContext;
 42  import org.webharvest.runtime.scripting.ScriptEngine;
 43  import org.webharvest.runtime.templaters.BaseTemplater;
 44  import org.webharvest.runtime.variables.*;
 45  import org.webharvest.utils.CommonUtil;
 46  import org.webharvest.utils.Constants;
 47
 48  import java.util.ArrayList
  ; 49  import java.util.List
  ; 50
 51
 54  public class WhileProcessor extends BaseProcessor {
 55
 56      private WhileDef whileDef;
 57
 58      public WhileProcessor(WhileDef whileDef) {
 59          super(whileDef);
 60          this.whileDef = whileDef;
 61      }
 62
 63      public IVariable execute(Scraper scraper, ScraperContext context) {
 64          ScriptEngine scriptEngine = scraper.getScriptEngine();
 65          String
  index = BaseTemplater.execute( whileDef.getIndex(), scriptEngine); 66          String
  maxLoopsString = BaseTemplater.execute( whileDef.getMaxLoops(), scriptEngine); 67
 68          double maxLoops = Constants.DEFAULT_MAX_LOOPS;
 69          if (maxLoopsString != null && !"".equals(maxLoopsString.trim())) {
 70              maxLoops = Double.parseDouble(maxLoopsString);
 71          }
 72
 73          List
  resultList = new ArrayList  (); 74
 75          IVariable indexBeforeLoop = (IVariable) context.get(index);
 76
 77          int i = 1;
 78
 79                  if ( index != null && !"".equals(index) ) {
 81              context.put( index, new NodeVariable(String.valueOf(i)) );
 82          }
 83
 84          String
  condition = BaseTemplater.execute( whileDef.getCondition(), scriptEngine); 85
 86                  while ( CommonUtil.isBooleanTrue(condition) && (i <= maxLoops) ) {
 88              IVariable loopResult = getBodyListContent(whileDef, scraper, context);
 89              resultList.addAll( loopResult.toList() );
 90
 91              i++;
 92                          if ( index != null && !"".equals(index) ) {
 94                  context.put( index, new NodeVariable(String.valueOf(i)) );
 95              }
 96
 97              condition = BaseTemplater.execute(whileDef.getCondition(), scriptEngine);
 98          }
 99
 100                 if (index != null && indexBeforeLoop != null) {
 102             context.put(index, indexBeforeLoop);
 103         }
 104
 105         return new ListVariable(resultList);
 106     }
 107
 108 }
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |