Пример #1
0
        public static UriTemplatePathSegment CreateFromUriTemplate(string segment, UriTemplate template)
        {
            // Identifying the type of segment - Literal|Compound|Variable
            switch (UriTemplateHelpers.IdentifyPartType(segment))
            {
            case UriTemplatePartType.Literal:
                return(UriTemplateLiteralPathSegment.CreateFromUriTemplate(segment, template));

            case UriTemplatePartType.Compound:
                return(UriTemplateCompoundPathSegment.CreateFromUriTemplate(segment, template));

            case UriTemplatePartType.Variable:
                if (segment.EndsWith("/", StringComparison.Ordinal))
                {
                    string varName = template.AddPathVariable(UriTemplatePartType.Variable,
                                                              segment.Substring(1, segment.Length - 3));
                    return(new UriTemplateVariablePathSegment(segment, true, varName));
                }
                else
                {
                    string varName = template.AddPathVariable(UriTemplatePartType.Variable,
                                                              segment.Substring(1, segment.Length - 2));
                    return(new UriTemplateVariablePathSegment(segment, false, varName));
                }

            default:
                Fx.Assert("Invalid value from IdentifyStringNature");
                return(null);
            }
        }
        public static UriTemplatePathSegment CreateFromUriTemplate(string segment, UriTemplate template)
        {
            // Identifying the type of segment - Literal|Compound|Variable
            switch (UriTemplateHelpers.IdentifyPartType(segment))
            {
                case UriTemplatePartType.Literal:
                    return UriTemplateLiteralPathSegment.CreateFromUriTemplate(segment, template);

                case UriTemplatePartType.Compound:
                    return UriTemplateCompoundPathSegment.CreateFromUriTemplate(segment, template);

                case UriTemplatePartType.Variable:
                    if (segment.EndsWith("/", StringComparison.Ordinal))
                    {
                        string varName = template.AddPathVariable(UriTemplatePartType.Variable,
                            segment.Substring(1, segment.Length - 3));
                        return new UriTemplateVariablePathSegment(segment, true, varName);
                    }
                    else
                    {
                        string varName = template.AddPathVariable(UriTemplatePartType.Variable,
                            segment.Substring(1, segment.Length - 2));
                        return new UriTemplateVariablePathSegment(segment, false, varName);
                    }

                default:
                    Fx.Assert("Invalid value from IdentifyStringNature");
                    return null;
            }
        }
        public static UriTemplatePathSegment CreateFromUriTemplate(string segment, UriTemplate template)
        {
            switch (UriTemplateHelpers.IdentifyPartType(segment))
            {
                case UriTemplatePartType.Literal:
                    return UriTemplateLiteralPathSegment.CreateFromUriTemplate(segment, template);

                case UriTemplatePartType.Compound:
                    return UriTemplateCompoundPathSegment.CreateFromUriTemplate(segment, template);

                case UriTemplatePartType.Variable:
                    if (!segment.EndsWith("/", StringComparison.Ordinal))
                    {
                        return new UriTemplateVariablePathSegment(segment, false, template.AddPathVariable(UriTemplatePartType.Variable, segment.Substring(1, segment.Length - 2)));
                    }
                    return new UriTemplateVariablePathSegment(segment, true, template.AddPathVariable(UriTemplatePartType.Variable, segment.Substring(1, segment.Length - 3)));
            }
            return null;
        }
        public static UriTemplatePathSegment CreateFromUriTemplate(string segment, UriTemplate template)
        {
            switch (UriTemplateHelpers.IdentifyPartType(segment))
            {
            case UriTemplatePartType.Literal:
                return(UriTemplateLiteralPathSegment.CreateFromUriTemplate(segment, template));

            case UriTemplatePartType.Compound:
                return(UriTemplateCompoundPathSegment.CreateFromUriTemplate(segment, template));

            case UriTemplatePartType.Variable:
                if (!segment.EndsWith("/", StringComparison.Ordinal))
                {
                    return(new UriTemplateVariablePathSegment(segment, false, template.AddPathVariable(UriTemplatePartType.Variable, segment.Substring(1, segment.Length - 2))));
                }
                return(new UriTemplateVariablePathSegment(segment, true, template.AddPathVariable(UriTemplatePartType.Variable, segment.Substring(1, segment.Length - 3))));
            }
            return(null);
        }
        public static new UriTemplateCompoundPathSegment CreateFromUriTemplate(string segment, UriTemplate template)
        {
            string origSegment   = segment;
            bool   endsWithSlash = segment.EndsWith("/", StringComparison.Ordinal);

            if (endsWithSlash)
            {
                segment = segment.Remove(segment.Length - 1);
            }

            int nextVarStart = segment.IndexOf("{", StringComparison.Ordinal);

            Fx.Assert(nextVarStart >= 0, "The method is only called after identifying a '{' character in the segment");
            string firstLiteral = ((nextVarStart > 0) ? segment.Substring(0, nextVarStart) : string.Empty);

            if (firstLiteral.IndexOf(UriTemplate.WildcardPath, StringComparison.Ordinal) != -1)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(
                                                                              SR.GetString(SR.UTInvalidWildcardInVariableOrLiteral, template.originalTemplate, UriTemplate.WildcardPath)));
            }
            UriTemplateCompoundPathSegment result = new UriTemplateCompoundPathSegment(origSegment, endsWithSlash,
                                                                                       ((firstLiteral != string.Empty) ? Uri.UnescapeDataString(firstLiteral) : string.Empty));

            do
            {
                int nextVarEnd = segment.IndexOf("}", nextVarStart + 1, StringComparison.Ordinal);
                if (nextVarEnd < nextVarStart + 2)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(
                                                                                  SR.GetString(SR.UTInvalidFormatSegmentOrQueryPart, segment)));
                }
                bool   hasDefault;
                string varName = template.AddPathVariable(UriTemplatePartType.Compound,
                                                          segment.Substring(nextVarStart + 1, nextVarEnd - nextVarStart - 1), out hasDefault);
                if (hasDefault)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
                                                                                  SR.GetString(SR.UTDefaultValueToCompoundSegmentVar, template, origSegment, varName)));
                }
                nextVarStart = segment.IndexOf("{", nextVarEnd + 1, StringComparison.Ordinal);
                string literal;
                if (nextVarStart > 0)
                {
                    if (nextVarStart == nextVarEnd + 1)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("template",
                                                                                     SR.GetString(SR.UTDoesNotSupportAdjacentVarsInCompoundSegment, template, segment));
                    }
                    literal = segment.Substring(nextVarEnd + 1, nextVarStart - nextVarEnd - 1);
                }
                else if (nextVarEnd + 1 < segment.Length)
                {
                    literal = segment.Substring(nextVarEnd + 1);
                }
                else
                {
                    literal = string.Empty;
                }
                if (literal.IndexOf(UriTemplate.WildcardPath, StringComparison.Ordinal) != -1)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(
                                                                                  SR.GetString(SR.UTInvalidWildcardInVariableOrLiteral, template.originalTemplate, UriTemplate.WildcardPath)));
                }
                if (literal.IndexOf('}') != -1)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(
                                                                                  SR.GetString(SR.UTInvalidFormatSegmentOrQueryPart, segment)));
                }
                result.varLitPairs.Add(new VarAndLitPair(varName, ((literal == string.Empty) ? string.Empty : Uri.UnescapeDataString(literal))));
            } while (nextVarStart > 0);

            if (string.IsNullOrEmpty(result.firstLiteral))
            {
                if (string.IsNullOrEmpty(result.varLitPairs[result.varLitPairs.Count - 1].Literal))
                {
                    result.csClass = CompoundSegmentClass.HasNoPrefixNorSuffix;
                }
                else
                {
                    result.csClass = CompoundSegmentClass.HasOnlySuffix;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(result.varLitPairs[result.varLitPairs.Count - 1].Literal))
                {
                    result.csClass = CompoundSegmentClass.HasOnlyPrefix;
                }
                else
                {
                    result.csClass = CompoundSegmentClass.HasPrefixAndSuffix;
                }
            }

            return(result);
        }
Пример #6
0
        public static UriTemplateCompoundPathSegment CreateFromUriTemplate(string segment, UriTemplate template)
        {
            string originalSegment = segment;
            bool   endsWithSlash   = segment.EndsWith("/", StringComparison.Ordinal);

            if (endsWithSlash)
            {
                segment = segment.Remove(segment.Length - 1);
            }
            int    index            = segment.IndexOf("{", StringComparison.Ordinal);
            string stringToUnescape = (index > 0) ? segment.Substring(0, index) : string.Empty;

            if (stringToUnescape.IndexOf("*", StringComparison.Ordinal) != -1)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(System.ServiceModel.SR.GetString("UTInvalidWildcardInVariableOrLiteral", new object[] { template.originalTemplate, "*" })));
            }
            UriTemplateCompoundPathSegment segment2 = new UriTemplateCompoundPathSegment(originalSegment, endsWithSlash, (stringToUnescape != string.Empty) ? Uri.UnescapeDataString(stringToUnescape) : string.Empty);

            do
            {
                bool   flag2;
                string str4;
                int    num2 = segment.IndexOf("}", index + 1, StringComparison.Ordinal);
                if (num2 < (index + 2))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(System.ServiceModel.SR.GetString("UTInvalidFormatSegmentOrQueryPart", new object[] { segment })));
                }
                string varName = template.AddPathVariable(UriTemplatePartType.Compound, segment.Substring(index + 1, (num2 - index) - 1), out flag2);
                if (flag2)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("UTDefaultValueToCompoundSegmentVar", new object[] { template, originalSegment, varName })));
                }
                index = segment.IndexOf("{", num2 + 1, StringComparison.Ordinal);
                if (index > 0)
                {
                    if (index == (num2 + 1))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("template", System.ServiceModel.SR.GetString("UTDoesNotSupportAdjacentVarsInCompoundSegment", new object[] { template, segment }));
                    }
                    str4 = segment.Substring(num2 + 1, (index - num2) - 1);
                }
                else if ((num2 + 1) < segment.Length)
                {
                    str4 = segment.Substring(num2 + 1);
                }
                else
                {
                    str4 = string.Empty;
                }
                if (str4.IndexOf("*", StringComparison.Ordinal) != -1)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(System.ServiceModel.SR.GetString("UTInvalidWildcardInVariableOrLiteral", new object[] { template.originalTemplate, "*" })));
                }
                if (str4.IndexOf('}') != -1)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(System.ServiceModel.SR.GetString("UTInvalidFormatSegmentOrQueryPart", new object[] { segment })));
                }
                segment2.varLitPairs.Add(new VarAndLitPair(varName, (str4 == string.Empty) ? string.Empty : Uri.UnescapeDataString(str4)));
            }while (index > 0);
            if (string.IsNullOrEmpty(segment2.firstLiteral))
            {
                VarAndLitPair pair = segment2.varLitPairs[segment2.varLitPairs.Count - 1];
                if (string.IsNullOrEmpty(pair.Literal))
                {
                    segment2.csClass = CompoundSegmentClass.HasNoPrefixNorSuffix;
                    return(segment2);
                }
                segment2.csClass = CompoundSegmentClass.HasOnlySuffix;
                return(segment2);
            }
            VarAndLitPair pair2 = segment2.varLitPairs[segment2.varLitPairs.Count - 1];

            if (string.IsNullOrEmpty(pair2.Literal))
            {
                segment2.csClass = CompoundSegmentClass.HasOnlyPrefix;
                return(segment2);
            }
            segment2.csClass = CompoundSegmentClass.HasPrefixAndSuffix;
            return(segment2);
        }