in core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java [697:923]
private Expression createSimpleExpressionMisc(String function) {
String remainder;
// replace function
remainder = ifStartsWithReturnRemainder("replace(", function);
if (remainder != null) {
String values = StringHelper.before(remainder, ")");
if (values == null || ObjectHelper.isEmpty(values)) {
throw new SimpleParserException(
"Valid syntax: ${replace(from,to)} or ${replace(from,to,expression)} was: " + function,
token.getIndex());
}
String[] tokens = StringQuoteHelper.splitSafeQuote(values, ',', false);
if (tokens.length > 3) {
throw new SimpleParserException(
"Valid syntax: ${replace(from,to,expression)} was: " + function, token.getIndex());
}
String from = StringHelper.xmlDecode(tokens[0]);
String to = StringHelper.xmlDecode(tokens[1]);
// special to make it easy to replace to an empty value (ie remove)
if ("∅".equals(to)) {
to = "";
}
String exp = "${body}";
if (tokens.length == 3) {
exp = tokens[2];
}
return SimpleExpressionBuilder.replaceExpression(exp, from, to);
}
// substring function
remainder = ifStartsWithReturnRemainder("substring(", function);
if (remainder != null) {
String values = StringHelper.before(remainder, ")");
if (values == null || ObjectHelper.isEmpty(values)) {
throw new SimpleParserException(
"Valid syntax: ${substring(num)}, ${substring(num,num)}, or ${substring(num,num,expression)} was: "
+ function,
token.getIndex());
}
String[] tokens = StringQuoteHelper.splitSafeQuote(values, ',', false);
if (tokens.length > 3) {
throw new SimpleParserException(
"Valid syntax: ${substring(num,num,expression)} was: " + function, token.getIndex());
}
String num1 = tokens[0];
String num2 = "0";
if (tokens.length > 1) {
num2 = tokens[1];
}
String exp = "${body}";
if (tokens.length == 3) {
exp = tokens[2];
}
return SimpleExpressionBuilder.substringExpression(exp, num1, num2);
}
// random function
remainder = ifStartsWithReturnRemainder("random(", function);
if (remainder != null) {
String values = StringHelper.before(remainder, ")");
if (values == null || ObjectHelper.isEmpty(values)) {
throw new SimpleParserException(
"Valid syntax: ${random(min,max)} or ${random(max)} was: " + function, token.getIndex());
}
if (values.contains(",")) {
String[] tokens = values.split(",", 3);
if (tokens.length > 2) {
throw new SimpleParserException(
"Valid syntax: ${random(min,max)} or ${random(max)} was: " + function, token.getIndex());
}
return SimpleExpressionBuilder.randomExpression(tokens[0].trim(), tokens[1].trim());
} else {
return SimpleExpressionBuilder.randomExpression("0", values.trim());
}
}
// skip function
remainder = ifStartsWithReturnRemainder("skip(", function);
if (remainder != null) {
String values = StringHelper.before(remainder, ")");
if (values == null || ObjectHelper.isEmpty(values)) {
throw new SimpleParserException("Valid syntax: ${skip(number)} was: " + function, token.getIndex());
}
String exp = "${body}";
int num = Integer.parseInt(values.trim());
return SimpleExpressionBuilder.skipExpression(exp, num);
}
// collate function
remainder = ifStartsWithReturnRemainder("collate(", function);
if (remainder != null) {
String values = StringHelper.before(remainder, ")");
if (values == null || ObjectHelper.isEmpty(values)) {
throw new SimpleParserException("Valid syntax: ${collate(group)} was: " + function, token.getIndex());
}
String exp = "${body}";
int num = Integer.parseInt(values.trim());
return SimpleExpressionBuilder.collateExpression(exp, num);
}
// join function
remainder = ifStartsWithReturnRemainder("join(", function);
if (remainder != null) {
String values = StringHelper.before(remainder, ")");
String separator = ",";
String prefix = null;
String exp = "${body}";
if (ObjectHelper.isNotEmpty(values)) {
String[] tokens = StringQuoteHelper.splitSafeQuote(values, ',', false);
if (tokens.length > 3) {
throw new SimpleParserException(
"Valid syntax: ${join(separator,prefix,expression)} was: " + function, token.getIndex());
}
if (tokens.length == 3) {
separator = tokens[0];
prefix = tokens[1];
exp = tokens[2];
} else if (tokens.length == 2) {
separator = tokens[0];
prefix = tokens[1];
} else {
separator = tokens[0];
}
}
return SimpleExpressionBuilder.joinExpression(exp, separator, prefix);
}
// messageHistory function
remainder = ifStartsWithReturnRemainder("messageHistory", function);
if (remainder != null) {
boolean detailed;
String values = StringHelper.between(remainder, "(", ")");
if (values == null || ObjectHelper.isEmpty(values)) {
detailed = true;
} else {
detailed = Boolean.parseBoolean(values);
}
return SimpleExpressionBuilder.messageHistoryExpression(detailed);
} else if (ObjectHelper.equal(function, "messageHistory")) {
return SimpleExpressionBuilder.messageHistoryExpression(true);
}
// uuid function
remainder = ifStartsWithReturnRemainder("uuid", function);
if (remainder != null) {
String values = StringHelper.between(remainder, "(", ")");
return SimpleExpressionBuilder.uuidExpression(values);
} else if (ObjectHelper.equal(function, "uuid")) {
return SimpleExpressionBuilder.uuidExpression(null);
}
// hash function
remainder = ifStartsWithReturnRemainder("hash(", function);
if (remainder != null) {
String values = StringHelper.before(remainder, ")");
if (values == null || ObjectHelper.isEmpty(values)) {
throw new SimpleParserException(
"Valid syntax: ${hash(value,algorithm)} or ${hash(value)} was: " + function, token.getIndex());
}
if (values.contains(",")) {
String[] tokens = values.split(",", 2);
if (tokens.length > 2) {
throw new SimpleParserException(
"Valid syntax: ${hash(value,algorithm)} or ${hash(value)} was: " + function, token.getIndex());
}
return SimpleExpressionBuilder.hashExpression(tokens[0].trim(), tokens[1].trim());
} else {
return SimpleExpressionBuilder.hashExpression(values.trim(), "SHA-256");
}
}
// empty function
remainder = ifStartsWithReturnRemainder("empty(", function);
if (remainder != null) {
String value = StringHelper.before(remainder, ")");
if (ObjectHelper.isEmpty(value)) {
throw new SimpleParserException(
"Valid syntax: ${empty(<type>)} but was: " + function, token.getIndex());
}
return SimpleExpressionBuilder.newEmptyExpression(value);
}
// iif function
remainder = ifStartsWithReturnRemainder("iif(", function);
if (remainder != null) {
String values = StringHelper.beforeLast(remainder, ")");
if (values == null || ObjectHelper.isEmpty(values)) {
throw new SimpleParserException(
"Valid syntax: ${iif(predicate,trueExpression,falseExpression)} was: " + function, token.getIndex());
}
String[] tokens = StringQuoteHelper.splitSafeQuote(values, ',');
if (tokens.length > 3) {
throw new SimpleParserException(
"Valid syntax: ${iif(predicate,trueExpression,falseExpression)} was: " + function, token.getIndex());
}
return SimpleExpressionBuilder.iifExpression(tokens[0].trim(), tokens[1].trim(), tokens[2].trim());
}
// list function
remainder = ifStartsWithReturnRemainder("list(", function);
if (remainder != null) {
String values = StringHelper.beforeLast(remainder, ")");
String[] tokens = null;
if (ObjectHelper.isNotEmpty(values)) {
tokens = StringQuoteHelper.splitSafeQuote(values, ',');
}
return SimpleExpressionBuilder.listExpression(tokens);
}
// map function
remainder = ifStartsWithReturnRemainder("map(", function);
if (remainder != null) {
String values = StringHelper.beforeLast(remainder, ")");
String[] tokens = null;
if (ObjectHelper.isNotEmpty(values)) {
tokens = StringQuoteHelper.splitSafeQuote(values, ',');
}
// there must be an even number of tokens as each map element is a pair
if (tokens != null && tokens.length % 2 == 1) {
throw new SimpleParserException(
"Map function must have an even number of values, was: " + tokens.length + " values.",
token.getIndex());
}
return SimpleExpressionBuilder.mapExpression(tokens);
}
return null;
}