com.hotels.styx.server.routing.antlr.ExpressionVisitor Maven / Gradle / Ivy
/*
Copyright (C) 2013-2018 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.server.routing.antlr;
import com.hotels.styx.server.routing.ConditionBaseVisitor;
import com.hotels.styx.server.routing.ConditionParser;
import com.hotels.styx.server.routing.ConditionParser.StringIsPresentContext;
import java.util.Map;
import java.util.regex.Pattern;
import static com.google.common.base.Strings.nullToEmpty;
import static com.hotels.styx.server.routing.antlr.Strings.stripFirstAndLastCharacter;
class ExpressionVisitor extends ConditionBaseVisitor> {
private final StringCompareVisitor stringVisitor;
public ExpressionVisitor(Map zeroArgumentFunctions, Map oneArgumentFunctions) {
this.stringVisitor = new StringCompareVisitor(
new FunctionResolver(zeroArgumentFunctions, oneArgumentFunctions));
}
@Override
public Expression visitAndExpression(ConditionParser.AndExpressionContext ctx) {
Expression left = visit(ctx.expression(0));
Expression right = visit(ctx.expression(1));
return (request, context) -> left.evaluate(request, context) && right.evaluate(request, context);
}
@Override
public Expression visitOrExpression(ConditionParser.OrExpressionContext ctx) {
Expression left = visit(ctx.expression(0));
Expression right = visit(ctx.expression(1));
return (request, context) -> left.evaluate(request, context) || right.evaluate(request, context);
}
@Override
public Expression visitNotExpression(ConditionParser.NotExpressionContext ctx) {
Expression expression = visit(ctx.expression());
return (request, context) -> !expression.evaluate(request, context);
}
@Override
public Expression visitSubExpression(ConditionParser.SubExpressionContext ctx) {
Expression expression = visit(ctx.expression());
return expression::evaluate;
}
@Override
public Expression visitStringIsPresent(StringIsPresentContext ctx) {
Expression stringExpression = stringVisitor.visitStringExpression(ctx.stringExpression());
return (request, context) -> nullToEmpty(stringExpression.evaluate(request, context)).length() > 0;
}
@Override
public Expression visitStringEqualsString(ConditionParser.StringEqualsStringContext ctx) {
Expression left = stringVisitor.visitStringExpression(ctx.stringExpression(0));
Expression right = stringVisitor.visitStringExpression(ctx.stringExpression(1));
return (request, context) -> nullToEmpty(left.evaluate(request, context)).equals(right.evaluate(request, context));
}
@Override
public Expression visitStringMatchesRegexp(ConditionParser.StringMatchesRegexpContext ctx) {
Expression stringExpression = stringVisitor.visitStringExpression(ctx.stringExpression());
Pattern pattern = Pattern.compile(stripFirstAndLastCharacter(ctx.string().getText()));
return (request, context) -> {
String evaluate = stringExpression.evaluate(request, context);
return pattern.matcher(evaluate).matches();
};
}
}