1 25 package org.ofbiz.minilang.method.envops; 26 27 import java.util.List ; 28 import java.util.LinkedList ; 29 30 import org.w3c.dom.Element ; 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 45 public class Loop extends MethodOperation { 46 47 public static final String module = Loop.class.getName(); 48 protected List subOps = new LinkedList (); 49 protected ContextAccessor fieldAcsr; 50 protected String countStr; 51 52 53 public Loop(Element 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 countStrExp = methodContext.expandString(this.countStr); 63 int count = 0; 64 try { 65 Double ctDbl = new Double (countStrExp); 66 if (ctDbl != null) { 67 count = ctDbl.intValue(); 68 } 69 } catch (NumberFormatException 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 (i)); 81 if (!SimpleMethod.runSubOps(subOps, methodContext)) { 82 return false; 84 } 85 } 86 87 return true; 88 } 89 90 public String rawString() { 91 return "<loop count=\"" + this.countStr + "\"/>"; 92 } 93 94 public String expandedString(MethodContext methodContext) { 95 return this.rawString(); 96 } 97 } 98 | Popular Tags |