Beispiel #1
0
        public static object EvaluateBinding(JObject data, string bindingExpression)
        {
            // If it's all one single expression
            // (This is a temporary hack for resolving correct data types)
            if (Regex.IsMatch(bindingExpression, "^" + REGEX_BINDING_EXPRESSION + "$"))
            {
                return(EvaluateSingleBinding(data, bindingExpression));
            }

            Regex regex     = new Regex(REGEX_BINDING_EXPRESSION);
            var   evaluator = new MyMatchEvaluator(data);

            string replaced = regex.Replace(bindingExpression, evaluator.ReplaceBinding);

            return(replaced);
        }
Beispiel #2
0
        public static JToken EvaluateBinding(string bindingExpression, JsonTransformerContext context)
        {
            string remainingBindingExpression = bindingExpression;
            string answer = "";
            bool   first  = true;
            bool   found  = false;

            while (true)
            {
                int startIndex = remainingBindingExpression.IndexOf('{');
                if (startIndex != -1)
                {
                    string substr      = remainingBindingExpression.Substring(startIndex);
                    var    inputStream = new AntlrInputStream(substr);
                    var    lexer       = new BindingExpressionsLexer(inputStream);
                    var    tokenStream = new CommonTokenStream(lexer);
                    var    parser      = new BindingExpressionsParser(tokenStream);
                    parser.AddErrorListener(new ErrorListener());

                    var foundExpression = parser.bindingExpression();

                    // If it actually found something
                    if (foundExpression.Stop.StopIndex < substr.Length && parser.NumberOfSyntaxErrors == 0)
                    {
                        var    visitor = new BindingExpressionsVisitor(context);
                        JToken result  = visitor.Visit(foundExpression);

                        answer += remainingBindingExpression.Substring(0, startIndex);
                        answer += result;

                        remainingBindingExpression = substr;
                        remainingBindingExpression = remainingBindingExpression.Substring(foundExpression.Stop.StopIndex + 1);

                        // If whole expression was the binding expression, don't do any string concatenation
                        if (first && startIndex == 0 && remainingBindingExpression.Length == 0)
                        {
                            return(result);
                        }

                        found = true;
                    }

                    else
                    {
                        // Otherwise, increment past there and try again
                        answer += remainingBindingExpression.Substring(startIndex + 1);
                        remainingBindingExpression = remainingBindingExpression.Substring(startIndex + 1);
                    }
                }
                else
                {
                    answer += remainingBindingExpression;
                    break;
                }

                first = false;
            }

            if (found)
            {
                return(answer);
            }

            return(bindingExpression);

            //var inputStream = new AntlrInputStream(bindingExpression);
            //var lexer = new BindingExpressionsLexer(inputStream);
            //var tokenStream = new CommonTokenStream(lexer);
            //var parser = new BindingExpressionsParser(tokenStream);

            //var foundExpression = parser.input_string();
            //if (foundExpression.IsEmpty)
            //{
            //    //return bindingExpression;
            //}
            //var visitor = new BindingExpressionsVisitor(context);
            //JToken result = visitor.Visit(foundExpression);
            //return result;


            // If it's all one single expression, we don't do any string concatenation and we return the actual type
            var match = Regex.Match(bindingExpression, "^" + REGEX_BINDING_EXPRESSION + "$");

            if (match.Success && match.Groups[1].Success)
            {
                return(EvaluateComplexExpression(match.Groups[1].Value, context));
            }

            Regex regex     = new Regex(REGEX_BINDING_EXPRESSION);
            var   evaluator = new MyMatchEvaluator(context);

            string replaced = regex.Replace(bindingExpression, evaluator.ReplaceBinding);

            return(replaced);
        }