1 24 package org.ofbiz.minilang.method.envops; 25 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.ofbiz.minilang.SimpleMethod; 32 import org.ofbiz.minilang.method.ContextAccessor; 33 import org.ofbiz.minilang.method.MethodContext; 34 import org.ofbiz.minilang.method.MethodOperation; 35 import org.ofbiz.base.util.Debug; 36 import org.w3c.dom.Element ; 37 38 45 public class IterateMap extends MethodOperation { 46 47 public static final String module = IterateMap.class.getName(); 48 49 List subOps = new LinkedList (); 50 51 ContextAccessor keyAcsr; 52 ContextAccessor valueAcsr; 53 ContextAccessor mapAcsr; 54 55 public IterateMap(Element element, SimpleMethod simpleMethod) { 56 super(element, simpleMethod); 57 this.keyAcsr = new ContextAccessor(element.getAttribute("key-name")); 58 this.valueAcsr = new ContextAccessor(element.getAttribute("value-name")); 59 this.mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 60 61 SimpleMethod.readOperations(element, subOps, simpleMethod); 62 } 63 64 public boolean exec(MethodContext methodContext) { 65 Object fieldVal = null; 66 67 if (mapAcsr.isEmpty()) { 68 Debug.logWarning("No map-name specified in iterate tag, doing nothing: " + rawString(), module); 69 return true; 70 } 71 72 Object oldKey = keyAcsr.get(methodContext); 73 Object oldValue = valueAcsr.get(methodContext); 74 if (oldKey != null) { 75 Debug.logWarning("In iterate-map the key had a non-null value before entering the loop for the operation: " + this.rawString(), module); 76 } 77 if (oldValue != null) { 78 Debug.logWarning("In iterate-map the value had a non-null value before entering the loop for the operation: " + this.rawString(), module); 79 } 80 81 Map theMap = (Map ) mapAcsr.get(methodContext); 82 if (theMap == null) { 83 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", doing nothing: " + rawString(), module); 84 return true; 85 } 86 if (theMap.size() == 0) { 87 if (Debug.verboseOn()) Debug.logVerbose("Map with name " + mapAcsr + " has zero entries, doing nothing: " + rawString(), module); 88 return true; 89 } 90 91 Iterator theIterator = theMap.entrySet().iterator(); 92 while (theIterator.hasNext()) { 93 Map.Entry theEntry = (Map.Entry ) theIterator.next(); 94 keyAcsr.put(methodContext, theEntry.getKey()); 95 valueAcsr.put(methodContext, theEntry.getValue()); 96 97 if (!SimpleMethod.runSubOps(subOps, methodContext)) { 98 return false; 100 } 101 } 102 103 return true; 104 } 105 106 public String rawString() { 107 return "<iterate-map map-name=\"" + this.mapAcsr + "\" key=\"" + this.keyAcsr + "\" value=\"" + this.valueAcsr + "\"/>"; 108 } 109 public String expandedString(MethodContext methodContext) { 110 return this.rawString(); 112 } 113 } 114 | Popular Tags |