KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webharvest > runtime > processors > WhileProcessor


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16
17     * The name of Web-Harvest may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
35     subject line.
36 */

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 JavaDoc;
49 import java.util.List JavaDoc;
50
51 /**
52  * Conditional processor.
53  */

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 JavaDoc index = BaseTemplater.execute( whileDef.getIndex(), scriptEngine);
66         String JavaDoc 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 JavaDoc resultList = new ArrayList JavaDoc();
74
75         IVariable indexBeforeLoop = (IVariable) context.get(index);
76
77         int i = 1;
78         
79         // define first value of index variable
80
if ( index != null && !"".equals(index) ) {
81             context.put( index, new NodeVariable(String.valueOf(i)) );
82         }
83
84         String JavaDoc condition = BaseTemplater.execute( whileDef.getCondition(), scriptEngine);
85
86         // iterates while testing variable represents boolean true or loop limit is exceeded
87
while ( CommonUtil.isBooleanTrue(condition) && (i <= maxLoops) ) {
88             IVariable loopResult = getBodyListContent(whileDef, scraper, context);
89             resultList.addAll( loopResult.toList() );
90
91             i++;
92             // define current value of index variable
93
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         // restores previous value of index variable
101
if (index != null && indexBeforeLoop != null) {
102             context.put(index, indexBeforeLoop);
103         }
104
105         return new ListVariable(resultList);
106     }
107
108 }
Popular Tags