| | | 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; |
| | | 5 | | using System.Collections.Specialized; |
| | | 6 | | using System.Text; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF |
| | | 10 | | { |
| | | 11 | | internal abstract class UriTemplateQueryValue |
| | | 12 | | { |
| | 29 | 13 | | protected UriTemplateQueryValue(UriTemplatePartType nature) |
| | | 14 | | { |
| | 29 | 15 | | Nature = nature; |
| | 29 | 16 | | } |
| | | 17 | | |
| | 0 | 18 | | public static UriTemplateQueryValue Empty { get; } = new EmptyUriTemplateQueryValue(); |
| | | 19 | | |
| | 2 | 20 | | public UriTemplatePartType Nature { get; } |
| | | 21 | | |
| | | 22 | | public static UriTemplateQueryValue CreateFromUriTemplate(string value, UriTemplate template) |
| | | 23 | | { |
| | | 24 | | // Checking for empty value |
| | 29 | 25 | | if (value == null) |
| | | 26 | | { |
| | 0 | 27 | | return Empty; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | // Identifying the type of value - Literal|Compound|Variable |
| | 29 | 31 | | switch (UriTemplateHelpers.IdentifyPartType(value)) |
| | | 32 | | { |
| | | 33 | | case UriTemplatePartType.Literal: |
| | 0 | 34 | | return UriTemplateLiteralQueryValue.CreateFromUriTemplate(value); |
| | | 35 | | |
| | | 36 | | case UriTemplatePartType.Compound: |
| | 0 | 37 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format( |
| | 0 | 38 | | SR.UTQueryCannotHaveCompoundValue, template._originalTemplate))); |
| | | 39 | | |
| | | 40 | | case UriTemplatePartType.Variable: |
| | 29 | 41 | | return new UriTemplateVariableQueryValue(template.AddQueryVariable(value.Substring(1, value.Length - |
| | | 42 | | |
| | | 43 | | default: |
| | | 44 | | Fx.Assert("Invalid value from IdentifyStringNature"); |
| | 0 | 45 | | return null; |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public static bool IsNullOrEmpty(UriTemplateQueryValue utqv) |
| | | 50 | | { |
| | 0 | 51 | | if (utqv == null) |
| | | 52 | | { |
| | 0 | 53 | | return true; |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | if (utqv == Empty) |
| | | 57 | | { |
| | 0 | 58 | | return true; |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | return false; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public abstract void Bind(string keyName, string[] values, ref int valueIndex, StringBuilder query); |
| | | 65 | | |
| | | 66 | | public abstract bool IsEquivalentTo(UriTemplateQueryValue other); |
| | | 67 | | |
| | | 68 | | public abstract void Lookup(string value, NameValueCollection boundParameters); |
| | | 69 | | |
| | | 70 | | internal class EmptyUriTemplateQueryValue : UriTemplateQueryValue |
| | | 71 | | { |
| | | 72 | | public EmptyUriTemplateQueryValue() |
| | 0 | 73 | | : base(UriTemplatePartType.Literal) |
| | | 74 | | { |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | public override void Bind(string keyName, string[] values, ref int valueIndex, StringBuilder query) |
| | | 78 | | { |
| | 0 | 79 | | query.AppendFormat("&{0}", UrlUtility.UrlEncode(keyName, Encoding.UTF8)); |
| | 0 | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | public override bool IsEquivalentTo(UriTemplateQueryValue other) => other == Empty; |
| | | 83 | | |
| | | 84 | | public override void Lookup(string value, NameValueCollection boundParameters) |
| | | 85 | | { |
| | | 86 | | Fx.Assert(string.IsNullOrEmpty(value), "shouldn't have a value"); |
| | 0 | 87 | | } |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |