예제 #1
0
            private void ProcessExpression(StringBuilder currentExpression)
            {
                if (currentExpression.Length == 0)
                {
                    _ErrorDetected = true;
                    _Result.Append("{}");
                    return;
                }

                OperatorInfo op = GetOperator(currentExpression[0]);

                var firstChar = op.Default ? 0 : 1;


                var varSpec = new VarSpec(op);

                for (int i = firstChar; i < currentExpression.Length; i++)
                {
                    char currentChar = currentExpression[i];
                    switch (currentChar)
                    {
                    case '*':
                        varSpec.Explode = true;
                        break;

                    case ':':      // Parse Prefix Modifier
                        var prefixText = new StringBuilder();
                        currentChar = currentExpression[++i];
                        while (currentChar >= '0' && currentChar <= '9' && i < currentExpression.Length)
                        {
                            prefixText.Append(currentChar);
                            i++;
                            if (i < currentExpression.Length)
                            {
                                currentChar = currentExpression[i];
                            }
                        }
                        varSpec.PrefixLength = int.Parse(prefixText.ToString());
                        i--;
                        break;

                    case ',':
                        var  success = ProcessVariable(varSpec);
                        bool isFirst = varSpec.First;
                        // Reset for new variable
                        varSpec = new VarSpec(op);
                        if (success || !isFirst)
                        {
                            varSpec.First = false;
                        }

                        break;

                    default:
                        varSpec.VarName.Append(currentChar);
                        break;
                    }
                }
                ProcessVariable(varSpec);
            }
예제 #2
0
        private bool ProcessVariable(VarSpec varSpec, Result result, bool multiVariableExpression = false)
        {
            var varname = varSpec.VarName.ToString();

            result.ParameterNames.Add(varname);

            if (!_Parameters.ContainsKey(varname) ||
                _Parameters[varname] == null ||
                (_Parameters[varname] is IList && ((IList)_Parameters[varname]).Count == 0) ||
                (_Parameters[varname] is IDictionary && ((IDictionary)_Parameters[varname]).Count == 0))
            {
                if (_resolvePartially == true)
                {
                    if (multiVariableExpression)
                    {
                        if (varSpec.First)
                        {
                            result.Append("{");
                        }

                        result.Append(varSpec.ToString());
                    }
                    else
                    {
                        result.Append("{");
                        result.Append(varSpec.ToString());
                        result.Append("}");
                    }
                    return(false);
                }
                return(false);
            }

            if (varSpec.First)
            {
                result.Append(varSpec.OperatorInfo.First);
            }
            else
            {
                result.Append(varSpec.OperatorInfo.Seperator);
            }

            object value = _Parameters[varname];

            // Handle Strings
            if (value is string)
            {
                var stringValue = (string)value;
                if (varSpec.OperatorInfo.Named)
                {
                    result.AppendName(varname, varSpec.OperatorInfo, string.IsNullOrEmpty(stringValue));
                }
                result.AppendValue(stringValue, varSpec.PrefixLength, varSpec.OperatorInfo.AllowReserved);
            }
            else
            {
                // Handle Lists
                var list = value as IList;
                if (list == null && value is IEnumerable <string> )
                {
                    list = ((IEnumerable <string>)value).ToList <string>();
                }
                ;
                if (list != null)
                {
                    if (varSpec.OperatorInfo.Named && !varSpec.Explode)      // exploding will prefix with list name
                    {
                        result.AppendName(varname, varSpec.OperatorInfo, list.Count == 0);
                    }

                    result.AppendList(varSpec.OperatorInfo, varSpec.Explode, varname, list);
                }
                else
                {
                    // Handle associative arrays
                    var dictionary = value as IDictionary <string, string>;
                    if (dictionary != null)
                    {
                        if (varSpec.OperatorInfo.Named && !varSpec.Explode)      // exploding will prefix with list name
                        {
                            result.AppendName(varname, varSpec.OperatorInfo, dictionary.Count() == 0);
                        }
                        result.AppendDictionary(varSpec.OperatorInfo, varSpec.Explode, dictionary);
                    }
                    else
                    {
                        // If above all fails, convert the object to string using the default object.ToString() implementation
                        var stringValue = value.ToString();
                        if (varSpec.OperatorInfo.Named)
                        {
                            result.AppendName(varname, varSpec.OperatorInfo, string.IsNullOrEmpty(stringValue));
                        }
                        result.AppendValue(stringValue, varSpec.PrefixLength, varSpec.OperatorInfo.AllowReserved);
                    }
                }
            }
            return(true);
        }
예제 #3
0
            private bool ProcessVariable(VarSpec varSpec, Result result, bool multiVariableExpression = false)
            {
                var varname = varSpec.VarName.ToString();
                result.ParameterNames.Add(varname);

                if (!_Parameters.ContainsKey(varname)
                    || _Parameters[varname] == null
                    || (_Parameters[varname] is IList && ((IList) _Parameters[varname]).Count == 0)
                    || (_Parameters[varname] is IDictionary && ((IDictionary) _Parameters[varname]).Count == 0))
                {
                    if (_resolvePartially == true)
                    {
                        if (multiVariableExpression)
                        {
                            if (varSpec.First)
                            {
                                result.Append("{");
                            }

                            result.Append(varSpec.ToString());
                        }
                        else
                        {
                            result.Append("{");
                            result.Append(varSpec.ToString());
                            result.Append("}");
                        }
                        return false;
                    }
                    return false;
                }

                if (varSpec.First)
                {
                    result.Append(varSpec.OperatorInfo.First);
                }
                else
                {
                    result.Append(varSpec.OperatorInfo.Seperator);
                }

                object value = _Parameters[varname];

                // Handle Strings
                if (value is string)
                {
                    var stringValue = (string)value;
                    if (varSpec.OperatorInfo.Named)
                    {
                        result.AppendName(varname, varSpec.OperatorInfo, string.IsNullOrEmpty(stringValue));
                    }
                    result.AppendValue(stringValue, varSpec.PrefixLength, varSpec.OperatorInfo.AllowReserved);
                }
                else
                {
                    // Handle Lists
                    var list = value as IList;
                    if (list == null && value is IEnumerable<string>)
                    {
                        list = ((IEnumerable<string>)value).ToList<string>();
                    } ;
                    if (list != null)
                    {
                        if (varSpec.OperatorInfo.Named && !varSpec.Explode)  // exploding will prefix with list name
                        {
                            result.AppendName(varname, varSpec.OperatorInfo, list.Count == 0);
                        }

                        result.AppendList(varSpec.OperatorInfo, varSpec.Explode, varname, list);
                    }
                    else
                    {

                        // Handle associative arrays
                        var dictionary = value as IDictionary<string, string>;
                        if (dictionary != null)
                        {
                            if (varSpec.OperatorInfo.Named && !varSpec.Explode)  // exploding will prefix with list name
                            {
                                result.AppendName(varname, varSpec.OperatorInfo, dictionary.Count() == 0);
                            }
                            result.AppendDictionary(varSpec.OperatorInfo, varSpec.Explode, dictionary);
                        }
                        else
                        {
                            // If above all fails, convert the object to string using the default object.ToString() implementation
                            var stringValue = value.ToString();
                            if (varSpec.OperatorInfo.Named)
                            {
                                result.AppendName(varname, varSpec.OperatorInfo, string.IsNullOrEmpty(stringValue));
                            }
                            result.AppendValue(stringValue, varSpec.PrefixLength, varSpec.OperatorInfo.AllowReserved);
                        }

                    }

                }
                return true;
            }
예제 #4
0
            private void ProcessExpression(StringBuilder currentExpression, Result result)
            {

                if (currentExpression.Length == 0)
                {
                    result.ErrorDetected = true;
                    result.Append("{}");
                    return;
                }

                OperatorInfo op = GetOperator(currentExpression[0]);

                var firstChar = op.Default ? 0 : 1;
                bool multivariableExpression = false;

                var varSpec = new VarSpec(op);
                for (int i = firstChar; i < currentExpression.Length; i++)
                {
                    char currentChar = currentExpression[i];
                    switch (currentChar)
                    {
                        case '*':
                            varSpec.Explode = true;
                            break;

                        case ':':  // Parse Prefix Modifier
                            var prefixText = new StringBuilder();
                            currentChar = currentExpression[++i];
                            while (currentChar >= '0' && currentChar <= '9' && i < currentExpression.Length)
                            {
                                prefixText.Append(currentChar);
                                i++;
                                if (i < currentExpression.Length) currentChar = currentExpression[i];
                            }
                            varSpec.PrefixLength = int.Parse(prefixText.ToString());
                            i--;
                            break;

                        case ',':
                            multivariableExpression = true;
                            var success = ProcessVariable(varSpec, result, multivariableExpression);
                            bool isFirst = varSpec.First;
                            // Reset for new variable
                            varSpec = new VarSpec(op);
                            if (success || !isFirst || _resolvePartially) varSpec.First = false;
                            if (!success && _resolvePartially) {result.Append(",") ; }
                            break; 
                        

                        default:
                            if (IsVarNameChar(currentChar))
                            {
                                varSpec.VarName.Append(currentChar);
                            }
                            else
                            {
                                result.ErrorDetected = true;
                            }
                            break;
                    }
                }

                ProcessVariable(varSpec, result, multivariableExpression);
                if (multivariableExpression && _resolvePartially) result.Append("}");
            }
예제 #5
0
            private bool ProcessVariable(VarSpec varSpec)
            {
                var varname = varSpec.VarName.ToString();

                if (_ParameterNames != null)
                {
                    _ParameterNames.Add(varname);
                }

                if (!_Parameters.ContainsKey(varname) ||
                    _Parameters[varname] == null ||
                    (_Parameters[varname] is IList && ((IList)_Parameters[varname]).Count == 0) ||
                    (_Parameters[varname] is IDictionary && ((IDictionary)_Parameters[varname]).Count == 0))
                {
                    return(false);
                }

                if (varSpec.First)
                {
                    _Result.Append(varSpec.OperatorInfo.First);
                }
                else
                {
                    _Result.Append(varSpec.OperatorInfo.Seperator);
                }

                object value = _Parameters[varname];

                // Handle Strings
                if (value is string)
                {
                    var stringValue = (string)value;
                    if (varSpec.OperatorInfo.Named)
                    {
                        AppendName(varname, varSpec.OperatorInfo, string.IsNullOrEmpty(stringValue));
                    }
                    AppendValue(stringValue, varSpec.PrefixLength, varSpec.OperatorInfo.AllowReserved);
                }
                else
                {
                    // Handle Lists
                    var list = value as IEnumerable <string>;
                    if (list != null)
                    {
                        if (varSpec.OperatorInfo.Named && !varSpec.Explode)  // exploding will prefix with list name
                        {
                            AppendName(varname, varSpec.OperatorInfo, list.Count() == 0);
                        }

                        AppendList(varSpec.OperatorInfo, varSpec.Explode, varname, list);
                    }
                    else
                    {
                        // Handle associative arrays
                        var dictionary = value as IDictionary <string, string>;
                        if (dictionary != null)
                        {
                            if (varSpec.OperatorInfo.Named && !varSpec.Explode)  // exploding will prefix with list name
                            {
                                AppendName(varname, varSpec.OperatorInfo, dictionary.Count() == 0);
                            }
                            AppendDictionary(varSpec.OperatorInfo, varSpec.Explode, dictionary);
                        }
                    }
                }
                return(true);
            }
예제 #6
0
        private void ProcessExpression(StringBuilder currentExpression, Result result)
        {
            if (currentExpression.Length == 0)
            {
                result.ErrorDetected = true;
                result.Append("{}");
                return;
            }

            var op = GetOperator(currentExpression[0]);

            var firstChar = op.Default ? 0 : 1;
            var multivariableExpression = false;

            var varSpec = new VarSpec(op);

            for (var i = firstChar; i < currentExpression.Length; i++)
            {
                var currentChar = currentExpression[i];
                switch (currentChar)
                {
                case '*':
                    varSpec.Explode = true;
                    break;

                case ':':     // Parse Prefix Modifier
                    var prefixText = new StringBuilder();
                    currentChar = currentExpression[++i];
                    while (currentChar >= '0' && currentChar <= '9' && i < currentExpression.Length)
                    {
                        prefixText.Append(currentChar);
                        i++;
                        if (i < currentExpression.Length)
                        {
                            currentChar = currentExpression[i];
                        }
                    }
                    varSpec.PrefixLength = int.Parse(prefixText.ToString());
                    i--;
                    break;

                case ',':
                    multivariableExpression = true;
                    var success = ProcessVariable(varSpec, result, multivariableExpression);
                    var isFirst = varSpec.First;
                    // Reset for new variable
                    varSpec = new VarSpec(op);
                    if (success || !isFirst || _resolvePartially)
                    {
                        varSpec.First = false;
                    }

                    if (!success && _resolvePartially)
                    {
                        result.Append(",");
                    }

                    break;

                default:
                    if (IsVarNameChar(currentChar))
                    {
                        varSpec.VarName.Append(currentChar);
                    }
                    else
                    {
                        result.ErrorDetected = true;
                    }

                    break;
                }
            }

            ProcessVariable(varSpec, result, multivariableExpression);
            if (multivariableExpression && _resolvePartially)
            {
                result.Append("}");
            }
        }