| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Specialized; |
| | | 5 | | using System.Text; |
| | | 6 | | using CoreWCF.Runtime; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF |
| | | 9 | | { |
| | | 10 | | internal class UriTemplateVariablePathSegment : UriTemplatePathSegment |
| | | 11 | | { |
| | | 12 | | public UriTemplateVariablePathSegment(string originalSegment, bool endsWithSlash, string varName) |
| | 52 | 13 | | : base(originalSegment, UriTemplatePartType.Variable, endsWithSlash) |
| | | 14 | | { |
| | | 15 | | Fx.Assert(!string.IsNullOrEmpty(varName), "bad variable segment"); |
| | 52 | 16 | | VarName = varName; |
| | 52 | 17 | | } |
| | | 18 | | |
| | 26 | 19 | | public string VarName { get; } |
| | | 20 | | |
| | | 21 | | public override void Bind(string[] values, ref int valueIndex, StringBuilder path) |
| | | 22 | | { |
| | | 23 | | Fx.Assert(valueIndex < values.Length, "Not enough values to bind"); |
| | 0 | 24 | | if (EndsWithSlash) |
| | | 25 | | { |
| | 0 | 26 | | path.AppendFormat("{0}/", values[valueIndex++]); |
| | | 27 | | } |
| | | 28 | | else |
| | | 29 | | { |
| | 0 | 30 | | path.Append(values[valueIndex++]); |
| | | 31 | | } |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public override bool IsEquivalentTo(UriTemplatePathSegment other, bool ignoreTrailingSlash) |
| | | 35 | | { |
| | 0 | 36 | | if (other == null) |
| | | 37 | | { |
| | | 38 | | Fx.Assert("why would we ever call this?"); |
| | 0 | 39 | | return false; |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | if (!ignoreTrailingSlash && (EndsWithSlash != other.EndsWithSlash)) |
| | | 43 | | { |
| | 0 | 44 | | return false; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | return (other.Nature == UriTemplatePartType.Variable); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public override bool IsMatch(UriTemplateLiteralPathSegment segment, bool ignoreTrailingSlash) |
| | | 51 | | { |
| | 0 | 52 | | if (!ignoreTrailingSlash && (EndsWithSlash != segment.EndsWithSlash)) |
| | | 53 | | { |
| | 0 | 54 | | return false; |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | return (!segment.IsNullOrEmpty()); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public override void Lookup(string segment, NameValueCollection boundParameters) |
| | | 61 | | { |
| | | 62 | | Fx.Assert(!string.IsNullOrEmpty(segment), "How can that be? Lookup is expected to be called after IsMatch"); |
| | | 63 | | |
| | 1 | 64 | | boundParameters.Add(VarName, segment); |
| | 1 | 65 | | } |
| | | 66 | | } |
| | | 67 | | } |