UriTemplateQueryValue.cs 3.5 KB

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