1 25 package org.ofbiz.minilang.method.ifops; 26 27 import java.util.List ; 28 import java.util.LinkedList ; 29 import java.util.Map ; 30 31 import org.ofbiz.minilang.method.MethodOperation; 32 import org.ofbiz.minilang.method.MethodContext; 33 import org.ofbiz.minilang.method.ContextAccessor; 34 import org.ofbiz.minilang.SimpleMethod; 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.ObjectType; 37 import org.ofbiz.base.util.UtilXml; 38 39 import org.w3c.dom.Element ; 40 41 47 public class IfInstanceOf extends MethodOperation { 48 49 public static final String module = IfInstanceOf.class.getName(); 50 51 protected List subOps = new LinkedList (); 52 protected List elseSubOps = null; 53 54 protected ContextAccessor mapAcsr = null; 55 protected ContextAccessor fieldAcsr = null; 56 protected String className = null; 57 58 public IfInstanceOf(Element element, SimpleMethod simpleMethod) { 59 super(element, simpleMethod); 60 this.mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 61 this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); 62 this.className = element.getAttribute("class"); 63 64 SimpleMethod.readOperations(element, subOps, simpleMethod); 65 66 Element elseElement = UtilXml.firstChildElement(element, "else"); 67 if (elseElement != null) { 68 elseSubOps = new LinkedList (); 69 SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod); 70 } 71 } 72 73 public boolean exec(MethodContext methodContext) { 74 boolean runSubOps = false; 76 Object fieldVal = null; 77 78 if (!mapAcsr.isEmpty()) { 79 Map fromMap = (Map ) mapAcsr.get(methodContext); 80 if (fromMap == null) { 81 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", running operations", module); 82 } else { 83 fieldVal = fieldAcsr.get(fromMap, methodContext); 84 } 85 } else { 86 fieldVal = fieldAcsr.get(methodContext); 88 } 89 90 runSubOps = ObjectType.instanceOf(fieldVal, className); 91 92 if (runSubOps) { 93 return SimpleMethod.runSubOps(subOps, methodContext); 94 } else { 95 if (elseSubOps != null) { 96 return SimpleMethod.runSubOps(elseSubOps, methodContext); 97 } else { 98 return true; 99 } 100 } 101 } 102 103 public String rawString() { 104 return "<if-instance-of field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; 106 } 107 public String expandedString(MethodContext methodContext) { 108 return this.rawString(); 110 } 111 } 112 | Popular Tags |