in core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java [1777:2068]
private String createCodeExpressionMisc(String function) {
String remainder;
// 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)} was: "
+ function,
token.getIndex());
}
String[] tokens = codeSplitSafe(values, ',', true, true);
if (tokens.length > 2) {
throw new SimpleParserException(
"Valid syntax: ${substring(num,num)} was: " + function, token.getIndex());
}
String num1 = tokens[0];
String num2 = "0";
if (tokens.length > 1) {
num2 = tokens[1];
}
num1 = num1.trim();
num2 = num2.trim();
return "substring(exchange, " + num1 + ", " + num2 + ")";
}
// random function
remainder = ifStartsWithReturnRemainder("random(", function);
if (remainder != null) {
String values = StringHelper.beforeLast(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 before = StringHelper.before(remainder, ",");
before = before.trim();
String after = StringHelper.after(remainder, ",");
after = after.trim();
if (after.endsWith(")")) {
after = after.substring(0, after.length() - 1);
}
return "random(exchange, " + before + ", " + after + ")";
} else {
return "random(exchange, 0, " + values.trim() + ")";
}
}
// 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)} was: " + function,
token.getIndex());
}
String[] tokens = codeSplitSafe(values, ',', true, false);
if (tokens.length > 2) {
throw new SimpleParserException(
"Valid syntax: ${replace(from,to)} 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 = "";
}
if ("\"".equals(from)) {
from = "\\\"";
}
if ("\"".equals(to)) {
to = "\\\"";
}
from = StringQuoteHelper.doubleQuote(from);
to = StringQuoteHelper.doubleQuote(to);
return "replace(exchange, " + from + ", " + to + ")";
}
// skip function
remainder = ifStartsWithReturnRemainder("skip(", function);
if (remainder != null) {
String values = StringHelper.beforeLast(remainder, ")");
if (values == null || ObjectHelper.isEmpty(values)) {
throw new SimpleParserException("Valid syntax: ${skip(number)} was: " + function, token.getIndex());
}
return "skip(exchange, " + values.trim() + ")";
}
// collate function
remainder = ifStartsWithReturnRemainder("collate(", function);
if (remainder != null) {
String values = StringHelper.beforeLast(remainder, ")");
if (values == null || ObjectHelper.isEmpty(values)) {
throw new SimpleParserException("Valid syntax: ${collate(group)} was: " + function, token.getIndex());
}
return "collate(exchange, " + values.trim() + ")";
}
// 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 "messageHistory(exchange, " + (detailed ? "true" : "false") + ")";
} else if (ObjectHelper.equal(function, "messageHistory")) {
return "messageHistory(exchange, true)";
}
// join
remainder = ifStartsWithReturnRemainder("join(", function);
if (remainder != null) {
String values = StringHelper.beforeLast(remainder, ")");
String separator = "\",\"";
String prefix = null;
String exp = "body";
if (ObjectHelper.isNotEmpty(values)) {
String[] tokens = codeSplitSafe(values, ',', true, true);
if (tokens.length > 3) {
throw new SimpleParserException(
"Valid syntax: ${join(separator,prefix,expression)} was: " + function, token.getIndex());
}
// single quotes should be double quotes
for (int i = 0; i < tokens.length; i++) {
String s = tokens[i];
if (StringHelper.isSingleQuoted(s)) {
s = StringHelper.removeLeadingAndEndingQuotes(s);
s = StringQuoteHelper.doubleQuote(s);
tokens[i] = s;
} else if (i < 2 && !StringHelper.isDoubleQuoted(s)) {
s = StringQuoteHelper.doubleQuote(s);
tokens[i] = s;
}
}
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 "var val = " + exp + ";\n return join(exchange, val, " + separator + ", " + prefix + ");";
}
// empty function
remainder = ifStartsWithReturnRemainder("empty(", function);
if (remainder != null) {
String value = StringHelper.beforeLast(remainder, ")");
if (ObjectHelper.isEmpty(value)) {
throw new SimpleParserException(
"Valid syntax: ${empty(<type>)} but was: " + function, token.getIndex());
}
value = StringQuoteHelper.doubleQuote(value);
return "empty(exchange, " + value + ")";
}
// list function
remainder = ifStartsWithReturnRemainder("list(", function);
if (remainder != null) {
String values = StringHelper.beforeLast(remainder, ")");
String[] tokens = null;
if (ObjectHelper.isNotEmpty(values)) {
tokens = codeSplitSafe(values, ',', true, true);
}
StringJoiner sj = new StringJoiner(", ");
for (int i = 0; tokens != null && i < tokens.length; i++) {
sj.add(tokens[i]);
}
String p = sj.length() > 0 ? sj.toString() : "null";
return "list(exchange, " + p + ")";
}
// map function
remainder = ifStartsWithReturnRemainder("map(", function);
if (remainder != null) {
String values = StringHelper.beforeLast(remainder, ")");
String[] tokens = null;
if (ObjectHelper.isNotEmpty(values)) {
tokens = codeSplitSafe(values, ',', true, true);
}
StringJoiner sj = new StringJoiner(", ");
for (int i = 0; tokens != null && i < tokens.length; i++) {
sj.add(tokens[i]);
}
String p = sj.length() > 0 ? sj.toString() : "null";
return "map(exchange, " + p + ")";
}
// hash function
remainder = ifStartsWithReturnRemainder("hash(", function);
if (remainder != null) {
String values = StringHelper.beforeLast(remainder, ")");
if (values == null || ObjectHelper.isEmpty(values)) {
throw new SimpleParserException(
"Valid syntax: ${hash(value,algorithm)} or ${hash(value)} was: " + function, token.getIndex());
}
String[] tokens = codeSplitSafe(values, ',', true, true);
if (tokens.length > 2) {
throw new SimpleParserException(
"Valid syntax: ${hash(value,algorithm)} or ${hash(value)} was: " + function, token.getIndex());
}
// single quotes should be double quotes
for (int i = 0; i < tokens.length; i++) {
String s = tokens[i];
if (StringHelper.isSingleQuoted(s)) {
s = StringHelper.removeLeadingAndEndingQuotes(s);
s = StringQuoteHelper.doubleQuote(s);
tokens[i] = s;
}
}
String algo = "\"SHA-256\"";
if (tokens.length == 2) {
algo = tokens[1];
if (!StringHelper.isQuoted(algo)) {
algo = StringQuoteHelper.doubleQuote(algo);
}
}
return "var val = " + tokens[0] + ";\n return hash(exchange, val, " + algo + ");";
}
// uuid function
remainder = ifStartsWithReturnRemainder("uuid", function);
if (remainder == null && "uuid".equals(function)) {
remainder = "(default)";
}
if (remainder != null) {
String generator = StringHelper.between(remainder, "(", ")");
if (generator == null) {
generator = "default";
}
StringBuilder sb = new StringBuilder(128);
if ("classic".equals(generator)) {
sb.append(" UuidGenerator uuid = new org.apache.camel.support.ClassicUuidGenerator();\n");
sb.append("return uuid.generateUuid();");
} else if ("short".equals(generator)) {
sb.append(" UuidGenerator uuid = new org.apache.camel.support.ShortUuidGenerator();\n");
sb.append("return uuid.generateUuid();");
} else if ("simple".equals(generator)) {
sb.append(" UuidGenerator uuid = new org.apache.camel.support.SimpleUuidGenerator();\n");
sb.append("return uuid.generateUuid();");
} else if ("default".equals(generator)) {
sb.append(" UuidGenerator uuid = new org.apache.camel.support.DefaultUuidGenerator();\n");
sb.append("return uuid.generateUuid();");
} else {
generator = StringQuoteHelper.doubleQuote(generator);
sb.append("if (uuid == null) uuid = customUuidGenerator(exchange, ").append(generator)
.append("); return uuid.generateUuid();");
}
return sb.toString();
}
// 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 = codeSplitSafe(values, ',', true, true);
if (tokens.length != 3) {
throw new SimpleParserException(
"Valid syntax: ${iif(predicate,trueExpression,falseExpression)} was: " + function, token.getIndex());
}
// single quotes should be double quotes
for (int i = 0; i < 3; i++) {
String s = tokens[i];
if (StringHelper.isSingleQuoted(s)) {
s = StringHelper.removeLeadingAndEndingQuotes(s);
s = StringQuoteHelper.doubleQuote(s);
tokens[i] = s;
}
}
return "Object o = " + tokens[0]
+ ";\n boolean b = convertTo(exchange, boolean.class, o);\n return b ? "
+ tokens[1] + " : " + tokens[2];
}
return null;
}