示例#1
0
        private void BuildDictionaryValue(StringBuilder builder, VarSpec varSpec, IDictionary <string, string> values)
        {
            var first = true;

            CheckCompositeVariables(varSpec);

            if (op.Named && !varSpec.Exploded)
            {
                builder.Append(varSpec.Name);
                builder.Append('=');
            }

            foreach (var item in values)
            {
                if (!first)
                {
                    builder.Append(varSpec.Exploded ? op.Separator : ",");
                }

                var key = PctEncoding.Escape(item.Key, CharSpec.ExtendedSafe);

                builder.Append(key);
                builder.Append(varSpec.Exploded ? '=' : ',');
                BuildValue(builder, varSpec, item.Value ?? string.Empty);

                first = false;
            }
        }
示例#2
0
 private void CheckCompositeVariables(VarSpec varSpec)
 {
     if (varSpec.MaxLength != 0)
     {
         throw new UriTemplateException("MaxLength modifier are not applicable to composite values");
     }
 }
示例#3
0
        private void BuildValue(StringBuilder builder, VarSpec varSpec, string value)
        {
            var charSpec = op.AllowReserved ? CharSpec.ExtendedSafe : CharSpec.Safe;

            value = PctEncoding.Escape(value, charSpec);
            builder.Append(value);
        }
示例#4
0
        private bool Resolve(StringBuilder builder, VarSpec varSpec, object value, bool hasPrefix)
        {
            string stringValue = null;
            IDictionary <string, string> dictionaryValue = null;
            IEnumerable <string>         collectionValue = null;

            stringValue = value as string;

            if (stringValue == null)
            {
                collectionValue = value as IEnumerable <string>;

                if (collectionValue == null)
                {
                    dictionaryValue = value as IDictionary <string, string>;

                    if (dictionaryValue == null)
                    {
                        throw new UriTemplateException(string.Format("Invalid value type of variable \"{0}\". Expected: string or IEnumerable<string> or IDictionary<string, string>.", value.GetType()));
                    }
                    else if (dictionaryValue.Count == 0)
                    {
                        return(false);
                    }
                }
                else if (collectionValue.Count() == 0)
                {
                    return(false);
                }
            }

            if (hasPrefix)
            {
                builder.Append(op.Separator);
            }
            else
            {
                builder.Append(op.Prefix);
            }

            if (stringValue != null)
            {
                BuildStringValue(builder, varSpec, stringValue);
            }
            else if (collectionValue != null)
            {
                BuildCollectionValue(builder, varSpec, collectionValue);
            }
            else
            {
                BuildDictionaryValue(builder, varSpec, dictionaryValue);
            }

            return(true);
        }
        private void CreateVarSpec()
        {
            if (varSpecMaxLength == -1)
            {
                ThrowException("Invalid URI template modifier");
            }

            var varSpec = new VarSpec(builder.ToString(), varSpecExloded, varSpecMaxLength);

            if (varSpecs == null)
            {
                varSpecs = new List<VarSpec>();
            }

            varSpecs.Add(varSpec);

            builder.Length = 0;
            varSpecExloded = false;
            varSpecMaxLength = 0;
        }
示例#6
0
        private void CreateVarSpec()
        {
            if (varSpecMaxLength == -1)
            {
                ThrowException("Invalid URI template modifier.");
            }

            var name    = builder.ToString();
            var varSpec = new VarSpec(name, varSpecExloded, varSpecMaxLength, true);

            if (varSpecs == null)
            {
                varSpecs = new List <VarSpec>();
            }

            varSpecs.Add(varSpec);

            builder.Length   = 0;
            varSpecExloded   = false;
            varSpecMaxLength = 0;
        }
示例#7
0
        private void BuildCollectionValue(StringBuilder builder, VarSpec varSpec, IEnumerable <string> values)
        {
            var first = true;

            CheckCompositeVariables(varSpec);

            if (op.Named && !varSpec.Exploded)
            {
                builder.Append(varSpec.Name);
            }

            foreach (var value in values)
            {
                if (!first)
                {
                    builder.Append(varSpec.Exploded ? op.Separator : ",");
                }

                if (op.Named)
                {
                    if (varSpec.Exploded)
                    {
                        builder.Append(varSpec.Name);
                        builder.Append('=');
                    }
                    else if (first)
                    {
                        builder.Append('=');
                    }
                }

                BuildValue(builder, varSpec, value);
                first = false;
            }

            if (first)
            {
                builder.Append(op.Empty);
            }
        }
示例#8
0
        private void BuildStringValue(StringBuilder builder, VarSpec varSpec, string value)
        {
            if (op.Named)
            {
                builder.Append(varSpec.Name);

                if (string.IsNullOrEmpty(value))
                {
                    builder.Append(op.Empty);
                }
                else
                {
                    builder.Append("=");
                }
            }

            if (varSpec.MaxLength != 0 && varSpec.MaxLength < value.Length)
            {
                value = value.Substring(0, varSpec.MaxLength);
            }

            BuildValue(builder, varSpec, value);
        }
示例#9
0
        private bool Resolve(StringBuilder builder, VarSpec varSpec, object value, bool hasPrefix)
        {
            string stringValue = null;
            IDictionary<string, string> dictionaryValue = null;
            IEnumerable<string> collectionValue = null;

            stringValue = value as string;

            if (stringValue == null)
            {
                collectionValue = value as IEnumerable<string>;

                if (collectionValue == null)
                {
                    dictionaryValue = value as IDictionary<string, string>;

                    if (dictionaryValue == null)
                    {
                        throw new UriTemplateException(string.Format("Invalid type of variable value \"{0}\". Expected: string or IEnumerable<string> or IDictionary<string, string>", value.GetType()));
                    }
                    else if (dictionaryValue.Count == 0)
                    {
                        return false;
                    }
                }
                else if (collectionValue.Count() == 0)
                {
                    return false;
                }
            }

            if (hasPrefix)
            {
                builder.Append(op.Separator);
            }
            else
            {
                builder.Append(op.Prefix);
            }

            if (stringValue != null)
            {
                BuildStringValue(builder, varSpec, stringValue);
            }
            else if (collectionValue != null)
            {
                BuildCollectionValue(builder, varSpec, collectionValue);
            }
            else
            {
                BuildDictionaryValue(builder, varSpec, dictionaryValue);
            }

            return true;
        }
示例#10
0
 private void CheckCompositeVariables(VarSpec varSpec)
 {
     if (varSpec.MaxLength != 0)
     {
         throw new UriTemplateException("MaxLength modifier are not applicable to composite values");
     }
 }
示例#11
0
 private void BuildValue(StringBuilder builder, VarSpec varSpec, string value)
 {
     builder.Append(PctEncoding.Escape(value, op.AllowReserved));
 }
示例#12
0
        private void BuildStringValue(StringBuilder builder, VarSpec varSpec, string value)
        {
            if (op.Named)
            {
                builder.Append(varSpec.Name);

                if (string.IsNullOrEmpty(value))
                {
                    builder.Append(op.Empty);
                }
                else
                {
                    builder.Append("=");
                }
            }

            if (varSpec.MaxLength != 0 && varSpec.MaxLength < value.Length)
            {
                value = value.Substring(0, varSpec.MaxLength);
            }

            BuildValue(builder, varSpec, value);
        }
示例#13
0
        private void BuildDictionaryValue(StringBuilder builder, VarSpec varSpec, IDictionary<string, string> values)
        {
            var first = true;

            CheckCompositeVariables(varSpec);

            if (op.Named && !varSpec.Exploded)
            {
                builder.Append(varSpec.Name);
                builder.Append('=');
            }

            foreach (var item in values)
            {
                if (!first)
                {
                    builder.Append(varSpec.Exploded ? op.Separator : ",");
                }

                var key = PctEncoding.Escape(item.Key, true);

                builder.Append(key);
                builder.Append(varSpec.Exploded ? '=' : ',');
                BuildValue(builder, varSpec, item.Value ?? string.Empty);

                first = false;
            }
        }
示例#14
0
        private void BuildCollectionValue(StringBuilder builder, VarSpec varSpec, IEnumerable<string> values)
        {
            var first = true;

            CheckCompositeVariables(varSpec);

            if (op.Named && !varSpec.Exploded)
            {
                builder.Append(varSpec.Name);
            }

            foreach (var value in values)
            {
                if (!first)
                {
                    builder.Append(varSpec.Exploded ? op.Separator : ",");
                }

                if (op.Named)
                {
                    if (varSpec.Exploded)
                    {
                        builder.Append(varSpec.Name);
                        builder.Append('=');
                    }
                    else if (first)
                    {
                        builder.Append('=');
                    }
                }

                BuildValue(builder, varSpec, value);
                first = false;
            }

            if (first)
            {
                builder.Append(op.Empty);
            }
        }