1 24 package org.ofbiz.minilang.operation; 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 33 40 public class Regexp extends SimpleMapOperation { 41 42 public static final String module = Regexp.class.getName(); 43 44 static PatternMatcher matcher = new Perl5Matcher(); 45 static PatternCompiler compiler = new Perl5Compiler(); 46 Pattern pattern = null; 47 String expr; 48 49 public Regexp(Element element, SimpleMapProcess simpleMapProcess) { 50 super(element, simpleMapProcess); 51 expr = element.getAttribute("expr"); 52 try { 53 pattern = compiler.compile(expr); 54 } catch (MalformedPatternException e) { 55 Debug.logError(e, module); 56 } 57 } 58 59 public void exec(Map inMap, Map results, List messages, Locale locale, ClassLoader loader) { 60 Object obj = inMap.get(fieldName); 61 62 String fieldValue = null; 63 64 try { 65 fieldValue = (String ) ObjectType.simpleTypeConvert(obj, "String", null, locale); 66 } catch (GeneralException e) { 67 messages.add("Could not convert field value for comparison: " + e.getMessage()); 68 return; 69 } 70 71 if (pattern == null) { 72 messages.add("Could not compile regular expression \"" + expr + "\" for validation"); 73 return; 74 } 75 76 if (!matcher.matches(fieldValue, pattern)) { 77 addMessage(messages, loader, locale); 78 } 79 } 80 } 81 | Popular Tags |