KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > envops > Loop


1 /*
2  * $Id$
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.minilang.method.envops;
26
27 import java.util.List JavaDoc;
28 import java.util.LinkedList JavaDoc;
29
30 import org.w3c.dom.Element JavaDoc;
31
32 import org.ofbiz.minilang.method.MethodOperation;
33 import org.ofbiz.minilang.method.ContextAccessor;
34 import org.ofbiz.minilang.method.MethodContext;
35 import org.ofbiz.minilang.SimpleMethod;
36 import org.ofbiz.base.util.Debug;
37
38 /**
39  * Loop
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 6250 $
43  * @since 3.5
44  */

45 public class Loop extends MethodOperation {
46
47     public static final String JavaDoc module = Loop.class.getName();
48     protected List JavaDoc subOps = new LinkedList JavaDoc();
49     protected ContextAccessor fieldAcsr;
50     protected String JavaDoc countStr;
51
52
53     public Loop(Element JavaDoc element, SimpleMethod simpleMethod) {
54         super(element, simpleMethod);
55         this.fieldAcsr = new ContextAccessor(element.getAttribute("field"));
56         this.countStr = element.getAttribute("count");
57
58         SimpleMethod.readOperations(element, subOps, simpleMethod);
59     }
60
61     public boolean exec(MethodContext methodContext) {
62         String JavaDoc countStrExp = methodContext.expandString(this.countStr);
63         int count = 0;
64         try {
65             Double JavaDoc ctDbl = new Double JavaDoc(countStrExp);
66             if (ctDbl != null) {
67                 count = ctDbl.intValue();
68             }
69         } catch (NumberFormatException JavaDoc e) {
70             Debug.logError(e, module);
71             return false;
72         }
73
74         if (count < 1) {
75             Debug.logWarning("Count is less than one, not doing nothing: " + rawString(), module);
76             return false;
77         }
78
79         for (int i = 0; i < count; i++) {
80             fieldAcsr.put(methodContext, new Integer JavaDoc(i));
81             if (!SimpleMethod.runSubOps(subOps, methodContext)) {
82                 // only return here if it returns false, otherwise just carry on
83
return false;
84             }
85         }
86
87         return true;
88     }
89
90     public String JavaDoc rawString() {
91         return "<loop count=\"" + this.countStr + "\"/>";
92     }
93
94     public String JavaDoc expandedString(MethodContext methodContext) {
95         return this.rawString();
96     }
97 }
98
Popular Tags