private String createCodeBody()

in core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java [1207:1374]


    private String createCodeBody(final String function) {
        // bodyAsIndex
        String remainder = ifStartsWithReturnRemainder("bodyAsIndex(", function);
        if (remainder != null) {
            String typeAndIndex = StringHelper.before(remainder, ")");
            if (typeAndIndex == null) {
                throw new SimpleParserException(
                        "Valid syntax: ${bodyAsIndex(type, index).OGNL} was: " + function, token.getIndex());
            }

            String type = StringHelper.before(typeAndIndex, ",");
            String index = StringHelper.after(typeAndIndex, ",");
            remainder = StringHelper.after(remainder, ")");
            if (ObjectHelper.isEmpty(type) || ObjectHelper.isEmpty(index)) {
                throw new SimpleParserException(
                        "Valid syntax: ${bodyAsIndex(type, index).OGNL} was: " + function, token.getIndex());
            }
            type = type.trim();
            type = appendClass(type);
            type = type.replace('$', '.');
            index = StringHelper.removeQuotes(index);
            index = index.trim();
            if (ObjectHelper.isNotEmpty(remainder)) {
                boolean invalid = OgnlHelper.isInvalidValidOgnlExpression(remainder);
                if (invalid) {
                    throw new SimpleParserException(
                            "Valid syntax: ${bodyAsIndex(type, index).OGNL} was: " + function, token.getIndex());
                }
                return "bodyAsIndex(message, " + type + ", \"" + index + "\")" + ognlCodeMethods(remainder, type);
            } else {
                return "bodyAsIndex(message, " + type + ", \"" + index + "\")";
            }
        }

        // bodyAs
        remainder = ifStartsWithReturnRemainder("bodyAs(", function);
        if (remainder != null) {
            String type = StringHelper.before(remainder, ")");
            if (type == null) {
                throw new SimpleParserException("Valid syntax: ${bodyAs(type)} was: " + function, token.getIndex());
            }
            type = appendClass(type);
            type = type.replace('$', '.');
            type = type.trim();
            remainder = StringHelper.after(remainder, ")");
            if (ObjectHelper.isNotEmpty(remainder)) {
                boolean invalid = OgnlHelper.isInvalidValidOgnlExpression(remainder);
                if (invalid) {
                    throw new SimpleParserException("Valid syntax: ${bodyAs(type).OGNL} was: " + function, token.getIndex());
                }
                if (remainder.startsWith("[")) {
                    // is there any index, then we should use bodyAsIndex function instead
                    // (use splitOgnl which assembles multiple indexes into a single part)
                    List<String> parts = splitOgnl(remainder);
                    if (!parts.isEmpty()) {
                        String func = "bodyAsIndex(" + type + ", \"" + parts.remove(0) + "\")";
                        String last = String.join("", parts);
                        if (!last.isEmpty()) {
                            func += "." + last;
                        }
                        return createCodeBody(func);
                    }
                }
                return "bodyAs(message, " + type + ")" + ognlCodeMethods(remainder, type);
            } else {
                return "bodyAs(message, " + type + ")";
            }
        }

        // mandatoryBodyAsIndex
        remainder = ifStartsWithReturnRemainder("mandatoryBodyAsIndex(", function);
        if (remainder != null) {
            String typeAndIndex = StringHelper.before(remainder, ")");
            if (typeAndIndex == null) {
                throw new SimpleParserException(
                        "Valid syntax: ${mandatoryBodyAsIndex(type, index).OGNL} was: " + function, token.getIndex());
            }

            String type = StringHelper.before(typeAndIndex, ",");
            String index = StringHelper.after(typeAndIndex, ",");
            remainder = StringHelper.after(remainder, ")");
            if (ObjectHelper.isEmpty(type) || ObjectHelper.isEmpty(index)) {
                throw new SimpleParserException(
                        "Valid syntax: ${mandatoryBodyAsIndex(type, index).OGNL} was: " + function, token.getIndex());
            }
            type = type.trim();
            type = appendClass(type);
            type = type.replace('$', '.');
            index = StringHelper.removeQuotes(index);
            index = index.trim();
            if (ObjectHelper.isNotEmpty(remainder)) {
                boolean invalid = OgnlHelper.isInvalidValidOgnlExpression(remainder);
                if (invalid) {
                    throw new SimpleParserException(
                            "Valid syntax: ${mandatoryBodyAsIndex(type, index).OGNL} was: " + function, token.getIndex());
                }
                return "mandatoryBodyAsIndex(message, " + type + ", \"" + index + "\")" + ognlCodeMethods(remainder, type);
            } else {
                return "mandatoryBodyAsIndex(message, " + type + ", \"" + index + "\")";
            }
        }

        // mandatoryBodyAs
        remainder = ifStartsWithReturnRemainder("mandatoryBodyAs(", function);
        if (remainder != null) {
            String type = StringHelper.before(remainder, ")");
            if (type == null) {
                throw new SimpleParserException("Valid syntax: ${mandatoryBodyAs(type)} was: " + function, token.getIndex());
            }
            type = appendClass(type);
            type = type.replace('$', '.');
            type = type.trim();
            remainder = StringHelper.after(remainder, ")");
            if (ObjectHelper.isNotEmpty(remainder)) {
                boolean invalid = OgnlHelper.isInvalidValidOgnlExpression(remainder);
                if (invalid) {
                    throw new SimpleParserException(
                            "Valid syntax: ${mandatoryBodyAs(type).OGNL} was: " + function, token.getIndex());
                }
                if (remainder.startsWith("[")) {
                    // is there any index, then we should use mandatoryBodyAsIndex function instead
                    // (use splitOgnl which assembles multiple indexes into a single part)
                    List<String> parts = splitOgnl(remainder);
                    if (!parts.isEmpty()) {
                        String func = "mandatoryBodyAsIndex(" + type + ", \"" + parts.remove(0) + "\")";
                        String last = String.join("", parts);
                        if (!last.isEmpty()) {
                            func += "." + last;
                        }
                        return createCodeBody(func);
                    }
                }
                return "mandatoryBodyAs(message, " + type + ")" + ognlCodeMethods(remainder, type);
            } else {
                return "mandatoryBodyAs(message, " + type + ")";
            }
        }

        // body OGNL
        remainder = ifStartsWithReturnRemainder("body", function);
        if (remainder == null) {
            remainder = ifStartsWithReturnRemainder("in.body", function);
        }
        if (remainder != null) {
            // OGNL must start with a . ? or [
            boolean ognlStart = remainder.startsWith(".") || remainder.startsWith("?") || remainder.startsWith("[");
            boolean invalid = !ognlStart || OgnlHelper.isInvalidValidOgnlExpression(remainder);
            if (invalid) {
                throw new SimpleParserException("Valid syntax: ${body.OGNL} was: " + function, token.getIndex());
            }
            if (remainder.startsWith("[")) {
                // is there any index, then we should use bodyAsIndex function instead
                // (use splitOgnl which assembles multiple indexes into a single part)
                List<String> parts = splitOgnl(remainder);
                if (!parts.isEmpty()) {
                    String func = "bodyAsIndex(Object.class, \"" + parts.remove(0) + "\")";
                    String last = String.join("", parts);
                    if (!last.isEmpty()) {
                        func += "." + last;
                    }
                    return createCodeBody(func);
                }
            }
            return "body" + ognlCodeMethods(remainder, null);
        }

        return null;
    }