Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3136,6 +3136,24 @@ public void testDistinct() {
assertEquals("[]", s);
}

@Test
public void testNot() {
exchange.getMessage().setBody("");
Expression expression = context.resolveLanguage("csimple").createExpression("${not()}");
assertTrue(expression.evaluate(exchange, Boolean.class));

exchange.getMessage().setBody("Hello");
expression = context.resolveLanguage("csimple").createExpression("${not()}");
assertFalse(expression.evaluate(exchange, Boolean.class));

expression = context.resolveLanguage("csimple").createExpression("${not(${body} == 'Hello')}");
assertFalse(expression.evaluate(exchange, Boolean.class));

exchange.getMessage().setBody("Bye");
expression = context.resolveLanguage("csimple").createExpression("${not(${body} == 'Hello')}");
assertTrue(expression.evaluate(exchange, Boolean.class));
}

@Override
protected String getLanguageName() {
return "csimple";
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ TIP:
|`isAlphaNumeric(exp)` | `boolean` | Whether the expression is alphanumeric value (A..Z0-9). For more advanced checks use the `regex` operator.
|`isNumeric()` | `boolean` | Whether the message body is numeric value (0..9). For more advanced checks use the `regex` operator.
|`isNumeric(exp)` | `boolean` | Whether the expression is numeric value (0..9). For more advanced checks use the `regex` operator.
|`not(predicate)` | `boolean` | Evaluates the predicate and returns the opposite.
|====

=== Date & Time Functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1184,4 +1184,28 @@ public static Object setVariable(Exchange exchange, String name, Class<?> type,
return null;
}

public static boolean isNot(Exchange exchange, Object value) {
if (value == null) {
return true;
}
if (value instanceof String s) {
if (s.isEmpty()) {
return true;
}
if ("false".equalsIgnoreCase(s)) {
return true;
} else if ("true".equalsIgnoreCase(s)) {
return false;
} else {
return false;
}
}
Boolean b = convertTo(exchange, Boolean.class, value);
if (b == null) {
return true;
} else {
return !b;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ public final class SimpleConstants {
label = "function", javaType = "Object", displayName = "Create Empty Object")
public static final String NEW_EMPTY = "newEmpty(type)";

@Metadata(description = "Evaluates the predicate and returns the opposite.", label = "function", javaType = "boolean")
public static final String NOT = "not";

@Metadata(description = "Represents a null value", label = "function", javaType = "Object")
public static final String NULL = "null";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.apache.camel.support.ShortUuidGenerator;
import org.apache.camel.support.SimpleUuidGenerator;
import org.apache.camel.support.builder.ExpressionBuilder;
import org.apache.camel.support.builder.PredicateBuilder;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
Expand Down Expand Up @@ -557,6 +558,34 @@ public String toString() {
};
}

/**
* Returns the opposite result of the predicate
*/
public static Expression isNotPredicate(final String predicate) {
return new ExpressionAdapter() {
private Predicate pred;

@Override
public void init(CamelContext context) {
pred = PredicateBuilder.not(context.resolveLanguage("simple").createPredicate(predicate));
pred.init(context);
}

public Object evaluate(Exchange exchange) {
return pred.matches(exchange);
}

@Override
public String toString() {
if (predicate != null) {
return "not(" + predicate + ")";
} else {
return "not()";
}
}
};
}

/**
* Trims the given expressions (uses message body if expression is null)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.language.simple.BaseSimpleParser;
import org.apache.camel.language.simple.SimpleExpressionBuilder;
import org.apache.camel.language.simple.SimplePredicateParser;
import org.apache.camel.language.simple.types.SimpleParserException;
import org.apache.camel.language.simple.types.SimpleToken;
import org.apache.camel.spi.Language;
Expand Down Expand Up @@ -1085,6 +1086,16 @@ private Expression createSimpleExpressionMisc(String function) {
}
return SimpleExpressionBuilder.isNumericExpression(exp);
}
// not function
remainder = ifStartsWithReturnRemainder("not(", function);
if (remainder != null) {
String exp = "${body}";
String value = StringHelper.beforeLast(remainder, ")");
if (ObjectHelper.isNotEmpty(value)) {
exp = value;
}
return SimpleExpressionBuilder.isNotPredicate(exp);
}

// trim function
remainder = ifStartsWithReturnRemainder("trim(", function);
Expand Down Expand Up @@ -1654,7 +1665,7 @@ private String doCreateCode(CamelContext camelContext, String expression) throws
}

// miscellaneous functions
String misc = createCodeExpressionMisc(function);
String misc = createCodeExpressionMisc(camelContext, function);
if (misc != null) {
return misc;
}
Expand Down Expand Up @@ -2314,7 +2325,7 @@ private String createCodeBase64(CamelContext camelContext, String function) {
return factory.get().createCode(camelContext, function, token.getIndex());
}

private String createCodeExpressionMisc(String function) {
private String createCodeExpressionMisc(CamelContext camelContext, String function) {
String remainder;

// setHeader function
Expand Down Expand Up @@ -2667,8 +2678,6 @@ private String createCodeExpressionMisc(String function) {
// pad function
remainder = ifStartsWithReturnRemainder("pad(", function);
if (remainder != null) {
String exp;
String len;
String separator = null;
String values = StringHelper.beforeLast(remainder, ")");
if (values == null || ObjectHelper.isEmpty(values)) {
Expand Down Expand Up @@ -2824,6 +2833,25 @@ private String createCodeExpressionMisc(String function) {
}
return "Object o = " + exp + ";\n return isNumeric(exchange, o);";
}
// not function
remainder = ifStartsWithReturnRemainder("not(", function);
if (remainder != null) {
String exp = "body";
String values = StringHelper.beforeLast(remainder, ")");
if (ObjectHelper.isNotEmpty(values)) {
String[] tokens = codeSplitSafe(values, ',', true, true);
if (tokens.length != 1) {
throw new SimpleParserException(
"Valid syntax: ${not(exp)} was: " + function, token.getIndex());
}

// Parse the condition as a predicate and generate code
SimplePredicateParser predicateParser
= new SimplePredicateParser(camelContext, tokens[0], true, skipFileFunctions, null);
exp = predicateParser.parseCode();
}
return "Object o = " + exp + ";\n return isNot(exchange, o);";
}

// capitalize function
remainder = ifStartsWithReturnRemainder("capitalize(", function);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3417,6 +3417,24 @@ public void testDistinct() {
assertEquals("[]", s);
}

@Test
public void testNot() {
exchange.getMessage().setBody("");
Expression expression = context.resolveLanguage("simple").createExpression("${not()}");
assertTrue(expression.evaluate(exchange, Boolean.class));

exchange.getMessage().setBody("Hello");
expression = context.resolveLanguage("simple").createExpression("${not()}");
assertFalse(expression.evaluate(exchange, Boolean.class));

expression = context.resolveLanguage("simple").createExpression("${not(${body} == 'Hello')}");
assertFalse(expression.evaluate(exchange, Boolean.class));

exchange.getMessage().setBody("Bye");
expression = context.resolveLanguage("simple").createExpression("${not(${body} == 'Hello')}");
assertTrue(expression.evaluate(exchange, Boolean.class));
}

@Override
protected String getLanguageName() {
return "simple";
Expand Down
Loading