class ExpressionLanguageUtil {

    public static boolean isExpressionLanguagePresent(final PropertyDescriptor descriptor, final String value) {
        final ExpressionLanguageScope scope = descriptor.getExpressionLanguageScope();
        if (scope == null || scope == ExpressionLanguageScope.NONE) {
            return false;
        }

        char lastChar = 0;
        int embeddedCount = 0;
        int expressionStart = -1;
        boolean oddDollarCount = false;
        int backslashCount = 0;

        for (int i = 0; i < value.length(); i++) {
            final char c = value.charAt(i);

            if (expressionStart > -1 && (c == '\'' || c == '"') && (lastChar != '\\' || backslashCount % 2 == 0)) {
                final int endQuoteIndex = findEndQuoteChar(value, i);
                if (endQuoteIndex < 0) {
                    break;
                }

                i = endQuoteIndex;
                continue;
            }

            if (c == '{') {
                if (oddDollarCount && lastChar == '$') {
                    if (embeddedCount == 0) {
                        expressionStart = i - 1;
                    }
                }

                // Keep track of the number of opening curly braces that we are embedded within,
                // if we are within an Expression. If we are outside of an Expression, we can just ignore
                // curly braces. This allows us to ignore the first character if the value is something
                // like: { ${abc} }
                // However, we will count the curly braces if we have something like: ${ $${abc} }
                if (expressionStart > -1) {
                    embeddedCount++;
                }
            } else if (c == '}') {
                if (embeddedCount <= 0) {
                    continue;
                }

                if (--embeddedCount == 0) {
                    if (expressionStart > -1) {
                        // ended expression. EL is present
                        return true;
                    }

                    expressionStart = -1;
                }
            } else if (c == '$') {
                oddDollarCount = !oddDollarCount;
            } else if (c == '\\') {
                backslashCount++;
            } else {
                oddDollarCount = false;
            }

            lastChar = c;
        }

        return false;
    }

}