1 24 package org.ofbiz.minilang.method.ifops; 25 26 import java.util.*; 27 28 import org.apache.oro.text.regex.*; 29 import org.w3c.dom.*; 30 31 import org.ofbiz.base.util.*; 32 import org.ofbiz.base.util.string.FlexibleStringExpander; 33 import org.ofbiz.minilang.*; 34 import org.ofbiz.minilang.method.*; 35 36 43 public class IfRegexp extends MethodOperation { 44 45 public static final String module = IfRegexp.class.getName(); 46 47 static PatternMatcher matcher = new Perl5Matcher(); 48 static PatternCompiler compiler = new Perl5Compiler(); 49 50 List subOps = new LinkedList(); 51 List elseSubOps = null; 52 53 ContextAccessor mapAcsr; 54 ContextAccessor fieldAcsr; 55 56 FlexibleStringExpander exprExdr; 57 58 public IfRegexp(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 63 this.exprExdr = new FlexibleStringExpander(element.getAttribute("expr")); 64 65 SimpleMethod.readOperations(element, subOps, simpleMethod); 66 67 Element elseElement = UtilXml.firstChildElement(element, "else"); 68 69 if (elseElement != null) { 70 elseSubOps = new LinkedList(); 71 SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod); 72 } 73 } 74 75 public boolean exec(MethodContext methodContext) { 76 79 String fieldString = null; 80 Object fieldVal = null; 81 82 if (!mapAcsr.isEmpty()) { 83 Map fromMap = (Map) mapAcsr.get(methodContext); 84 if (fromMap == null) { 85 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", using empty string for comparison", module); 86 } else { 87 fieldVal = fieldAcsr.get(fromMap, methodContext); 88 } 89 } else { 90 fieldVal = fieldAcsr.get(methodContext); 92 } 93 94 if (fieldVal != null) { 95 try { 96 fieldString = (String ) ObjectType.simpleTypeConvert(fieldVal, "String", null, null); 97 } catch (GeneralException e) { 98 Debug.logError(e, "Could not convert object to String, using empty String", module); 99 } 100 } 101 if (fieldString == null) fieldString = ""; 103 104 Pattern pattern = null; 105 try { 106 pattern = compiler.compile(methodContext.expandString(this.exprExdr)); 107 } catch (MalformedPatternException e) { 108 Debug.logError(e, "Regular Expression [" + this.exprExdr + "] is mal-formed: " + e.toString(), module); 109 } 110 111 if (matcher.matches(fieldString, pattern)) { 112 return SimpleMethod.runSubOps(subOps, methodContext); 113 } else { 114 if (elseSubOps != null) { 115 return SimpleMethod.runSubOps(elseSubOps, methodContext); 116 } else { 117 return true; 118 } 119 } 120 } 121 122 public String rawString() { 123 return "<if-regexp field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; 125 } 126 public String expandedString(MethodContext methodContext) { 127 return this.rawString(); 129 } 130 } 131 | Popular Tags |