1 46 package org.mr.api.jms.selector.syntax; 47 48 import java.io.StringReader ; 49 50 import javax.jms.InvalidSelectorException ; 51 import javax.jms.Message ; 52 53 import org.mr.api.jms.selector.parser.SelectorLexer; 54 import org.mr.api.jms.selector.parser.SelectorParser; 55 import org.mr.api.jms.selector.parser.SelectorTreeParser; 56 57 58 213 public class Selector { 214 215 218 private final Expression evaluator; 219 220 221 228 public Selector(final String expression) throws InvalidSelectorException { 229 try { 230 231 if (expression == null || expression.length() == 0) { 232 evaluator = Literal.booleanLiteral(true); 234 } else { 236 SelectorLexer lexer = new SelectorLexer(new StringReader (expression)); 237 lexer.initialise(); 238 239 SelectorParser parser = new SelectorParser(lexer); 240 parser.initialise(); 241 parser.selector(); 243 SelectorTreeParser builder = new SelectorTreeParser(); 244 builder.initialise(new DefaultExpressionFactory()); 245 evaluator = builder.selector(parser.getAST()); 246 } } catch (Exception exception) { 249 throw new InvalidSelectorException (exception.toString()); 250 } } 253 254 260 public boolean accept(final Message message) { 261 boolean result = false; 262 try { 263 MantaObject value = evaluator.evaluate(message); 264 265 if (value instanceof MantaBoolean) { 266 result = ((MantaBoolean) value).value(); 267 } } catch (TypeMismatchException ignore) { 270 271 } return result; 273 } 275 276 277 public static void main (String [] args){ 278 try { 279 Selector s = new Selector("ticket='foo'"); 280 System.out.println(s); 281 } 282 catch (InvalidSelectorException e) { 283 e.printStackTrace(); 285 } 286 } 287 } | Popular Tags |