1 package fr.jayasoft.ivy.event; 2 3 import fr.jayasoft.ivy.filter.AndFilter; 4 import fr.jayasoft.ivy.filter.Filter; 5 import fr.jayasoft.ivy.filter.NoFilter; 6 import fr.jayasoft.ivy.filter.NotFilter; 7 import fr.jayasoft.ivy.filter.OrFilter; 8 import fr.jayasoft.ivy.matcher.ExactPatternMatcher; 9 import fr.jayasoft.ivy.matcher.Matcher; 10 import fr.jayasoft.ivy.matcher.PatternMatcher; 11 12 43 public class IvyEventFilter implements Filter { 44 private static final String NOT = "NOT "; 45 private static final String OR = " OR "; 46 private static final String AND = " AND "; 47 private PatternMatcher _matcher; 48 private Filter _nameFilter; 49 private Filter _attFilter; 50 51 public IvyEventFilter(String event, String filterExpression, PatternMatcher matcher) { 52 _matcher = matcher == null ? ExactPatternMatcher.getInstance() : matcher; 53 if (event == null) { 54 _nameFilter = NoFilter.INSTANCE; 55 } else { 56 final Matcher eventNameMatcher = _matcher.getMatcher(event); 57 _nameFilter = new Filter() { 58 public boolean accept(Object o) { 59 IvyEvent e = (IvyEvent) o; 60 return eventNameMatcher.matches(e.getName()); 61 } 62 }; 63 } 64 _attFilter = filterExpression == null || filterExpression.trim().length() == 0 ? 65 NoFilter.INSTANCE 66 : parseExpression(filterExpression); 67 } 68 69 private Filter parseExpression(String filterExpression) { 70 filterExpression = filterExpression.trim(); 78 int index = filterExpression.indexOf(AND); 79 if (index == -1) { 80 index = filterExpression.indexOf(OR); 81 if (index == -1) { 82 if (filterExpression.startsWith(NOT)) { 83 return new NotFilter(parseExpression(filterExpression.substring(NOT.length()))); 84 } else { 85 index = filterExpression.indexOf("="); 86 if (index == -1) { 87 throw new IllegalArgumentException ("bad filter expression: "+filterExpression+": no equal sign found"); 88 } 89 final String attname = filterExpression.substring(0, index).trim(); 90 String [] values = filterExpression.substring(index+1).trim().split(","); 91 final Matcher[] matchers = new Matcher[values.length]; 92 for (int i = 0; i < values.length; i++) { 93 matchers[i] = _matcher.getMatcher(values[i].trim()); 94 } 95 return new Filter() { 96 public boolean accept(Object o) { 97 IvyEvent e = (IvyEvent) o; 98 String val = (String ) e.getAttributes().get(attname); 99 if (val == null) { 100 return false; 101 } 102 for (int i = 0; i < matchers.length; i++) { 103 if (matchers[i].matches(val)) { 104 return true; 105 } 106 } 107 return false; 108 } 109 }; 110 } 111 } else { 112 return new OrFilter(parseExpression(filterExpression.substring(0, index)), parseExpression(filterExpression.substring(index+OR.length()))); 113 } 114 } else { 115 return new AndFilter(parseExpression(filterExpression.substring(0, index)), parseExpression(filterExpression.substring(index+AND.length()))); 116 } 117 } 118 119 public boolean accept(Object o) { 120 if (! (o instanceof IvyEvent)) { 121 return false; 122 } 123 return _nameFilter.accept(o) && _attFilter.accept(o); 124 } 125 126 } 127 | Popular Tags |