Search in sources :

Example 1 with BooleanExpression

use of org.apache.activemq.artemis.selector.filter.BooleanExpression in project activemq-artemis by apache.

the class SelectorParserTest method testParseWithParensAround.

@Test
public void testParseWithParensAround() throws Exception {
    String[] values = { "x = 1 and y = 2", "(x = 1) and (y = 2)", "((x = 1) and (y = 2))" };
    for (int i = 0; i < values.length; i++) {
        String value = values[i];
        info("Parsing: " + value);
        BooleanExpression andExpression = parse(value);
        Assert.assertTrue("Created LogicExpression expression", andExpression instanceof LogicExpression);
        LogicExpression logicExpression = (LogicExpression) andExpression;
        Expression left = logicExpression.getLeft();
        Expression right = logicExpression.getRight();
        Assert.assertTrue("Left is a binary filter", left instanceof ComparisonExpression);
        Assert.assertTrue("Right is a binary filter", right instanceof ComparisonExpression);
        ComparisonExpression leftCompare = (ComparisonExpression) left;
        ComparisonExpression rightCompare = (ComparisonExpression) right;
        assertPropertyExpression("left", leftCompare.getLeft(), "x");
        assertPropertyExpression("right", rightCompare.getLeft(), "y");
    }
}
Also used : ComparisonExpression(org.apache.activemq.artemis.selector.filter.ComparisonExpression) BooleanExpression(org.apache.activemq.artemis.selector.filter.BooleanExpression) Expression(org.apache.activemq.artemis.selector.filter.Expression) LogicExpression(org.apache.activemq.artemis.selector.filter.LogicExpression) ComparisonExpression(org.apache.activemq.artemis.selector.filter.ComparisonExpression) PropertyExpression(org.apache.activemq.artemis.selector.filter.PropertyExpression) XPathExpression(org.apache.activemq.artemis.selector.filter.XPathExpression) BooleanExpression(org.apache.activemq.artemis.selector.filter.BooleanExpression) LogicExpression(org.apache.activemq.artemis.selector.filter.LogicExpression) Test(org.junit.Test)

Example 2 with BooleanExpression

use of org.apache.activemq.artemis.selector.filter.BooleanExpression in project activemq-artemis by apache.

the class SelectorParserTest method testParseXPath.

@Test
public void testParseXPath() throws Exception {
    BooleanExpression filter = parse("XPATH '//title[@lang=''eng'']'");
    Assert.assertTrue("Created XPath expression", filter instanceof XPathExpression);
    info("Expression: " + filter);
}
Also used : XPathExpression(org.apache.activemq.artemis.selector.filter.XPathExpression) BooleanExpression(org.apache.activemq.artemis.selector.filter.BooleanExpression) Test(org.junit.Test)

Example 3 with BooleanExpression

use of org.apache.activemq.artemis.selector.filter.BooleanExpression in project activemq-artemis by apache.

the class SelectorParser method parse.

public static BooleanExpression parse(String sql) throws FilterException {
    Object result = cache.get(sql);
    if (result instanceof FilterException) {
        throw (FilterException) result;
    } else if (result instanceof BooleanExpression) {
        return (BooleanExpression) result;
    } else {
        String actual = sql;
        boolean convertStringExpressions = false;
        boolean hyphenatedProps = false;
        while (true) {
            if (actual.startsWith(CONVERT_STRING_EXPRESSIONS_PREFIX)) {
                convertStringExpressions = true;
                actual = actual.substring(CONVERT_STRING_EXPRESSIONS_PREFIX.length());
                continue;
            }
            if (actual.startsWith(HYPHENATED_PROPS_PREFIX)) {
                hyphenatedProps = true;
                actual = actual.substring(HYPHENATED_PROPS_PREFIX.length());
                continue;
            }
            if (actual.startsWith(NO_CONVERT_STRING_EXPRESSIONS_PREFIX)) {
                convertStringExpressions = false;
                actual = actual.substring(NO_CONVERT_STRING_EXPRESSIONS_PREFIX.length());
                continue;
            }
            if (actual.startsWith(NO_HYPHENATED_PROPS_PREFIX)) {
                hyphenatedProps = false;
                actual = actual.substring(NO_HYPHENATED_PROPS_PREFIX.length());
                continue;
            }
            break;
        }
        if (convertStringExpressions) {
            ComparisonExpression.CONVERT_STRING_EXPRESSIONS.set(true);
        }
        try {
            BooleanExpression e = null;
            if (hyphenatedProps) {
                HyphenatedParser parser = new HyphenatedParser(new StringReader(actual));
                e = parser.JmsSelector();
            } else {
                StrictParser parser = new StrictParser(new StringReader(actual));
                e = parser.JmsSelector();
            }
            cache.put(sql, e);
            return e;
        } catch (Throwable e) {
            FilterException fe = new FilterException(actual, e);
            cache.put(sql, fe);
            throw fe;
        } finally {
            if (convertStringExpressions) {
                ComparisonExpression.CONVERT_STRING_EXPRESSIONS.remove();
            }
        }
    }
}
Also used : BooleanExpression(org.apache.activemq.artemis.selector.filter.BooleanExpression) HyphenatedParser(org.apache.activemq.artemis.selector.hyphenated.HyphenatedParser) StringReader(java.io.StringReader) FilterException(org.apache.activemq.artemis.selector.filter.FilterException) StrictParser(org.apache.activemq.artemis.selector.strict.StrictParser)

Example 4 with BooleanExpression

use of org.apache.activemq.artemis.selector.filter.BooleanExpression in project activemq-artemis by apache.

the class SelectorTest method assertSelector.

protected void assertSelector(MockMessage message, String text, boolean expected) throws FilterException {
    BooleanExpression selector = SelectorParser.parse(text);
    Assert.assertTrue("Created a valid selector", selector != null);
    boolean value = selector.matches(message);
    Assert.assertEquals("Selector for: " + text, expected, value);
}
Also used : BooleanExpression(org.apache.activemq.artemis.selector.filter.BooleanExpression)

Aggregations

BooleanExpression (org.apache.activemq.artemis.selector.filter.BooleanExpression)4 XPathExpression (org.apache.activemq.artemis.selector.filter.XPathExpression)2 Test (org.junit.Test)2 StringReader (java.io.StringReader)1 ComparisonExpression (org.apache.activemq.artemis.selector.filter.ComparisonExpression)1 Expression (org.apache.activemq.artemis.selector.filter.Expression)1 FilterException (org.apache.activemq.artemis.selector.filter.FilterException)1 LogicExpression (org.apache.activemq.artemis.selector.filter.LogicExpression)1 PropertyExpression (org.apache.activemq.artemis.selector.filter.PropertyExpression)1 HyphenatedParser (org.apache.activemq.artemis.selector.hyphenated.HyphenatedParser)1 StrictParser (org.apache.activemq.artemis.selector.strict.StrictParser)1