Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.activemq.artemis.selector.filter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
/**
* A filter performing a comparison of two objects
*
* @version $Revision: 1.2 $
*/
public abstract class ComparisonExpression extends BinaryExpression implements BooleanExpression {
public static final ThreadLocal CONVERT_STRING_EXPRESSIONS = new ThreadLocal<>();
boolean convertStringExpressions = false;
private static final Set REGEXP_CONTROL_CHARS = new HashSet<>();
/**
* @param left
* @param right
*/
public ComparisonExpression(Expression left, Expression right) {
super(left, right);
convertStringExpressions = CONVERT_STRING_EXPRESSIONS.get() != null;
}
public static BooleanExpression createBetween(Expression value, Expression left, Expression right) {
return LogicExpression.createAND(createGreaterThanEqual(value, left), createLessThanEqual(value, right));
}
public static BooleanExpression createNotBetween(Expression value, Expression left, Expression right) {
return LogicExpression.createOR(createLessThan(value, left), createGreaterThan(value, right));
}
static {
REGEXP_CONTROL_CHARS.add(Character.valueOf('.'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('\\'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('['));
REGEXP_CONTROL_CHARS.add(Character.valueOf(']'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('^'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('$'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('?'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('*'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('+'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('{'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('}'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('|'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('('));
REGEXP_CONTROL_CHARS.add(Character.valueOf(')'));
REGEXP_CONTROL_CHARS.add(Character.valueOf(':'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('&'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('<'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('>'));
REGEXP_CONTROL_CHARS.add(Character.valueOf('='));
REGEXP_CONTROL_CHARS.add(Character.valueOf('!'));
}
static class LikeExpression extends UnaryExpression implements BooleanExpression {
Pattern likePattern;
/**
*/
LikeExpression(Expression right, String like, int escape) {
super(right);
StringBuffer regexp = new StringBuffer(like.length() * 2);
regexp.append("\\A"); // The beginning of the input
for (int i = 0; i < like.length(); i++) {
char c = like.charAt(i);
if (escape == (0xFFFF & c) && shouldEscapeNext(like, i, c)) {
i++;
char t = like.charAt(i);
regexp.append("\\x");
regexp.append(Integer.toHexString(0xFFFF & t));
} else {
append(regexp, c);
}
}
regexp.append("\\z"); // The end of the input
likePattern = Pattern.compile(regexp.toString(), Pattern.DOTALL);
}
private boolean shouldEscapeNext(String selector, int i, char escape) {
int next = i + 1;
if (next < selector.length()) {
final char c = selector.charAt(next);
return (c == '_' || c == '%' || c == escape);
}
return false;
}
private void append(StringBuffer regexp, char c) {
if (c == '%') {
regexp.append(".*?"); // Do a non-greedy match
} else if (c == '_') {
regexp.append("."); // match one
} else if (REGEXP_CONTROL_CHARS.contains(new Character(c))) {
regexp.append("\\x");
regexp.append(Integer.toHexString(0xFFFF & c));
} else {
regexp.append(c);
}
}
/**
* @see org.apache.activemq.filter.UnaryExpression#getExpressionSymbol()
*/
@Override
public String getExpressionSymbol() {
return "LIKE";
}
/**
* @see org.apache.activemq.filter.Expression#evaluate(Filterable)
*/
@Override
public Object evaluate(Filterable message) throws FilterException {
Object rv = this.getRight().evaluate(message);
if (rv == null) {
return null;
}
if (!(rv instanceof String)) {
return Boolean.FALSE;
// throw new RuntimeException("LIKE can only operate on String
// identifiers. LIKE attempted on: '" + rv.getClass());
}
return likePattern.matcher((String) rv).matches() ? Boolean.TRUE : Boolean.FALSE;
}
@Override
public boolean matches(Filterable message) throws FilterException {
Object object = evaluate(message);
return object == Boolean.TRUE;
}
}
public static BooleanExpression createLike(Expression left, String right, String escape) {
if (escape != null && escape.length() != 1) {
throw new RuntimeException("The ESCAPE string literal is invalid. It can only be one character. Literal used: " + escape);
}
int c = -1;
if (escape != null) {
c = 0xFFFF & escape.charAt(0);
}
return new LikeExpression(left, right, c);
}
public static BooleanExpression createNotLike(Expression left, String right, String escape) {
return UnaryExpression.createNOT(createLike(left, right, escape));
}
public static BooleanExpression createInFilter(Expression left, List