예제 #1
0
        private static object CallCustomFunction(object[] parameters, JUSTContext localContext)
        {
            object[] customParameters = new object[parameters.Length - 3];
            string   functionString   = string.Empty;
            string   dllName          = string.Empty;
            int      i = 0;

            foreach (object parameter in parameters)
            {
                if (i == 0)
                {
                    dllName = parameter.ToString();
                }
                else if (i == 1)
                {
                    functionString = parameter.ToString();
                }
                else
                if (i != (parameters.Length - 1))
                {
                    customParameters[i - 2] = parameter;
                }

                i++;
            }

            int index = functionString.LastIndexOf(".");

            string className    = functionString.Substring(0, index);
            string functionName = functionString.Substring(index + 1, functionString.Length - index - 1);

            className = className + "," + dllName;

            return(ReflectionHelper.caller(null, className, functionName, customParameters, true, localContext ?? GlobalContext));
        }
        private static object ParseFunction(string functionString, string inputJson, JArray array, JToken currentArrayElement, JUSTContext localContext)
        {
            try
            {
                object output = null;

                string functionName, argumentString;
                if (!ExpressionHelper.TryParseFunctionNameAndArguments(functionString, out functionName, out argumentString))
                {
                    return(functionName);
                }

                string[] arguments      = ExpressionHelper.GetArguments(argumentString);
                var      listParameters = new List <object>();

                if (functionName == "ifcondition")
                {
                    var condition = ParseArgument(inputJson, array, currentArrayElement, arguments[0], localContext);
                    var value     = ParseArgument(inputJson, array, currentArrayElement, arguments[1], localContext);
                    var index     = condition.ToString().ToLower() == value.ToString().ToLower() ? 2 : 3;
                    output = ParseArgument(inputJson, array, currentArrayElement, arguments[index], localContext);
                }
                else
                {
                    int i = 0;
                    for (; i < (arguments?.Length ?? 0); i++)
                    {
                        listParameters.Add(ParseArgument(inputJson, array, currentArrayElement, arguments[i], localContext));
                    }

                    listParameters.Add(localContext ?? GlobalContext);
                    var parameters = listParameters.ToArray();

                    if (functionName == "currentvalue" || functionName == "currentindex" || functionName == "lastindex" ||
                        functionName == "lastvalue")
                    {
                        output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement }, true, localContext ?? GlobalContext);
                    }
                    else if (functionName == "currentvalueatpath" || functionName == "lastvalueatpath")
                    {
                        output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement, arguments[0] }, true, localContext ?? GlobalContext);
                    }
                    else if (functionName == "customfunction")
                    {
                        output = CallCustomFunction(parameters, localContext);
                    }
                    else if (localContext?.IsRegisteredCustomFunction(functionName) ?? false)
                    {
                        var methodInfo = localContext.GetCustomMethod(functionName);
                        output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true, localContext ?? GlobalContext);
                    }
                    else if (GlobalContext.IsRegisteredCustomFunction(functionName))
                    {
                        var methodInfo = GlobalContext.GetCustomMethod(functionName);
                        output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true, localContext ?? GlobalContext);
                    }
                    else if (Regex.IsMatch(functionName, ReflectionHelper.EXTERNAL_ASSEMBLY_REGEX))
                    {
                        output = ReflectionHelper.CallExternalAssembly(functionName, parameters, localContext ?? GlobalContext);
                    }
                    else if (functionName == "xconcat" || functionName == "xadd" ||
                             functionName == "mathequals" || functionName == "mathgreaterthan" || functionName == "mathlessthan" ||
                             functionName == "mathgreaterthanorequalto" ||
                             functionName == "mathlessthanorequalto" || functionName == "stringcontains" ||
                             functionName == "stringequals")
                    {
                        object[] oParams = new object[1];
                        oParams[0] = parameters;
                        output     = ReflectionHelper.caller(null, "JUST.Transformer", functionName, oParams, true, localContext ?? GlobalContext);
                    }
                    else
                    {
                        var input = ((JUSTContext)parameters.Last()).Input;
                        if (currentArrayElement != null && functionName != "valueof")
                        {
                            ((JUSTContext)parameters.Last()).Input = JsonConvert.SerializeObject(currentArrayElement);
                        }
                        output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, parameters, true, localContext ?? GlobalContext);
                        ((JUSTContext)parameters.Last()).Input = input;
                    }
                }

                return(output);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while calling function : " + functionString + " - " + ex.Message, ex);
            }
        }
예제 #3
0
        private static object caller(string myclass, string mymethod, object[] parameters)
        {
            Assembly assembly = null;

            return(ReflectionHelper.caller(assembly, myclass, mymethod, parameters, true, new JUSTContext()));
        }
예제 #4
0
        private static object ParseFunction(string functionString, string inputJson, JArray array, JToken currentArrayElement)
        {
            try
            {
                object output = null;

                string functionName, argumentString;
                if (!TryParseFunctionNameAndArguments(functionString, out functionName, out argumentString))
                {
                    return(functionName);
                }

                string[] arguments  = GetArguments(argumentString);
                object[] parameters = new object[arguments.Length + 1];

                int i = 0;
                if (arguments != null && arguments.Length > 0)
                {
                    foreach (string argument in arguments)
                    {
                        string trimmedArgument = argument;

                        if (argument.Contains("#"))
                        {
                            trimmedArgument = argument.Trim();
                        }

                        if (trimmedArgument.StartsWith("#"))
                        {
                            parameters[i] = ParseFunction(trimmedArgument, inputJson, array, currentArrayElement);
                        }
                        else
                        {
                            parameters[i] = trimmedArgument;
                        }
                        i++;
                    }
                }

                parameters[i] = inputJson;

                if (functionName == "currentvalue" || functionName == "currentindex" || functionName == "lastindex" ||
                    functionName == "lastvalue")
                {
                    output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement });
                }
                else if (functionName == "currentvalueatpath" || functionName == "lastvalueatpath")
                {
                    output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement, arguments[0] });
                }
                else if (functionName == "customfunction")
                {
                    output = CallCustomFunction(parameters);
                }
                else if (JUSTContext.IsRegisteredCustomFunction(functionName))
                {
                    var methodInfo = JUSTContext.GetCustomMethod(functionName);
                    output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true);
                }
                else if (Regex.IsMatch(functionName, ReflectionHelper.EXTERNAL_ASSEMBLY_REGEX))
                {
                    output = ReflectionHelper.CallExternalAssembly(functionName, parameters);
                }
                else if (functionName == "xconcat" || functionName == "xadd" ||
                         functionName == "mathequals" || functionName == "mathgreaterthan" || functionName == "mathlessthan" ||
                         functionName == "mathgreaterthanorequalto" ||
                         functionName == "mathlessthanorequalto" || functionName == "stringcontains" ||
                         functionName == "stringequals")
                {
                    object[] oParams = new object[1];
                    oParams[0] = parameters;
                    output     = ReflectionHelper.caller(null, "JUST.Transformer", functionName, oParams);
                }
                else
                {
                    if (currentArrayElement != null && functionName != "valueof")
                    {
                        parameters[i] = JsonConvert.SerializeObject(currentArrayElement);
                    }
                    output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, parameters);
                }

                return(output);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while calling function : " + functionString + " - " + ex.Message, ex);
            }
        }
예제 #5
0
        private static object ParseFunction(string functionString, string inputJson, JArray array, JToken currentArrayElement, JUSTContext localContext)
        {
            try
            {
                object output;

                string functionName, argumentString;
                if (!ExpressionHelper.TryParseFunctionNameAndArguments(functionString, out functionName, out argumentString))
                {
                    return(functionName);
                }

                string[] arguments = ExpressionHelper.SplitArguments(argumentString);

                if (functionName == "ifcondition")
                {
                    var condition = ParseArgument(inputJson, array, currentArrayElement, arguments[0], localContext);
                    var value     = ParseArgument(inputJson, array, currentArrayElement, arguments[1], localContext);
                    var index     = condition.ToString().ToLower() == value.ToString().ToLower() ? 2 : 3;
                    output = ParseArgument(inputJson, array, currentArrayElement, arguments[index], localContext);
                }
                else
                {
                    var listParameters = new List <object>();
                    int i = 0;
                    for (; i < (arguments?.Length ?? 0); i++)
                    {
                        listParameters.Add(ParseArgument(inputJson, array, currentArrayElement, arguments[i], localContext));
                    }

                    listParameters.Add(localContext ?? GlobalContext);
                    var parameters = listParameters.ToArray();

                    if (new[] { "currentvalue", "currentindex", "lastindex", "lastvalue" }.Contains(functionName))
                    {
                        output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement }, true, localContext ?? GlobalContext);
                    }
                    else if (new[] { "currentvalueatpath", "lastvalueatpath" }.Contains(functionName))
                    {
                        output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement, parameters[0] }, true, localContext ?? GlobalContext);
                    }
                    else if (functionName == "currentproperty")
                    {
                        output = ReflectionHelper.caller(null, "JUST.Transformer", functionName,
                                                         new object[] { array, currentArrayElement, localContext ?? GlobalContext },
                                                         false, localContext ?? GlobalContext);
                    }
                    else if (functionName == "customfunction")
                    {
                        output = CallCustomFunction(parameters, localContext);
                    }
                    else if (localContext?.IsRegisteredCustomFunction(functionName) ?? false)
                    {
                        var methodInfo = localContext.GetCustomMethod(functionName);
                        output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true, localContext ?? GlobalContext);
                    }
                    else if (GlobalContext.IsRegisteredCustomFunction(functionName))
                    {
                        var methodInfo = GlobalContext.GetCustomMethod(functionName);
                        output = ReflectionHelper.InvokeCustomMethod(methodInfo, parameters, true, localContext ?? GlobalContext);
                    }
                    else if (ReflectionHelper.IsExternalFunction(functionName))
                    {
                        output = ReflectionHelper.CallExternalAssembly(functionName, parameters, localContext ?? GlobalContext);
                    }
                    else if (new[] { "xconcat", "xadd",
                                     "mathequals", "mathgreaterthan", "mathlessthan", "mathgreaterthanorequalto", "mathlessthanorequalto",
                                     "stringcontains", "stringequals" }.Contains(functionName))
                    {
                        object[] oParams = new object[1];
                        oParams[0] = parameters;
                        output     = ReflectionHelper.caller(null, "JUST.Transformer", functionName, oParams, true, localContext ?? GlobalContext);
                    }
                    else if (functionName == "applyover")
                    {
                        var contextInput = GetInputToken(localContext);
                        var input        = JToken.Parse(Transform(parameters[0].ToString(), contextInput.ToString(), localContext));
                        (localContext ?? GlobalContext).Input = input;
                        output = ParseFunction(parameters[1].ToString().Trim(' ', '\''), inputJson, array, currentArrayElement, localContext ?? GlobalContext);
                        (localContext ?? GlobalContext).Input = contextInput;
                    }
                    else
                    {
                        var input = ((JUSTContext)parameters.Last()).Input;
                        if (currentArrayElement != null && functionName != "valueof")
                        {
                            ((JUSTContext)parameters.Last()).Input = currentArrayElement;
                        }
                        output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, parameters, true, localContext ?? GlobalContext);
                        ((JUSTContext)parameters.Last()).Input = input;
                    }
                }

                return(output);
            }
            catch (Exception ex)
            {
                //TODO Exception with full function string and specific function string (first and last level of recursion)
                throw new Exception("Error while calling function : " + functionString + " - " + ex.Message, ex);
            }
        }
예제 #6
0
        private static object ParseFunction(string functionString, string inputJson, JArray array, JToken currentArrayElement)
        {
            try
            {
                object output = null;
                functionString = functionString.Trim();
                output         = functionString.Substring(1);

                int indexOfStart = output.ToString().IndexOf("(", 0);
                int indexOfEnd   = output.ToString().LastIndexOf(")");

                if (indexOfStart == -1 || indexOfEnd == -1)
                {
                    return(functionString);
                }

                string functionName = output.ToString().Substring(0, indexOfStart);

                string argumentString = output.ToString().Substring(indexOfStart + 1, indexOfEnd - indexOfStart - 1);

                string[] arguments  = GetArguments(argumentString);
                object[] parameters = new object[arguments.Length + 1];

                int i = 0;
                if (arguments != null && arguments.Length > 0)
                {
                    foreach (string argument in arguments)
                    {
                        string trimmedArgument = argument;

                        if (argument.Contains("#"))
                        {
                            trimmedArgument = argument.Trim();
                        }

                        if (trimmedArgument.StartsWith("#"))
                        {
                            parameters[i] = ParseFunction(trimmedArgument, inputJson, array, currentArrayElement);
                        }
                        else
                        {
                            parameters[i] = trimmedArgument;
                        }
                        i++;
                    }
                }

                parameters[i] = inputJson;

                if (functionName == "currentvalue" || functionName == "currentindex" || functionName == "lastindex" ||
                    functionName == "lastvalue")
                {
                    output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement });
                }
                else if (functionName == "currentvalueatpath" || functionName == "lastvalueatpath")
                {
                    output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, new object[] { array, currentArrayElement, arguments[0] });
                }
                else if (functionName == "customfunction")
                {
                    output = CallCustomFunction(parameters);
                }
                else if (Regex.IsMatch(functionName, ReflectionHelper.EXTERNAL_ASSEMBLY_REGEX))
                {
                    output = ReflectionHelper.CallExternalAssembly(functionName, parameters);
                }
                else if (functionName == "xconcat" || functionName == "xadd" ||
                         functionName == "mathequals" || functionName == "mathgreaterthan" || functionName == "mathlessthan" ||
                         functionName == "mathgreaterthanorequalto" ||
                         functionName == "mathlessthanorequalto" || functionName == "stringcontains" ||
                         functionName == "stringequals")
                {
                    object[] oParams = new object[1];
                    oParams[0] = parameters;
                    output     = ReflectionHelper.caller(null, "JUST.Transformer", functionName, oParams);
                }
                else
                {
                    if (currentArrayElement != null && functionName != "valueof")
                    {
                        parameters[i] = JsonConvert.SerializeObject(currentArrayElement);
                    }
                    output = ReflectionHelper.caller(null, "JUST.Transformer", functionName, parameters);
                }

                return(output);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while calling function : " + functionString + " - " + ex.Message, ex);
            }
        }