UriTemplateTable.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // UriTemplateTable.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.ObjectModel;
  31. using System.Collections.Specialized;
  32. using Pair = System.Collections.Generic.KeyValuePair<System.UriTemplate, object>;
  33. namespace System
  34. {
  35. public class UriTemplateTable
  36. {
  37. public UriTemplateTable ()
  38. {
  39. }
  40. public UriTemplateTable (Uri baseAddress)
  41. {
  42. BaseAddress = baseAddress;
  43. key_value_pairs = new List<Pair> ();
  44. }
  45. public UriTemplateTable (IEnumerable<Pair> keyValuePairs)
  46. {
  47. key_value_pairs = keyValuePairs as IList<Pair>;
  48. if (key_value_pairs == null)
  49. key_value_pairs = new List<Pair> (keyValuePairs);
  50. }
  51. public UriTemplateTable (Uri baseAddress, IEnumerable<Pair> keyValuePairs)
  52. : this (keyValuePairs)
  53. {
  54. BaseAddress = baseAddress;
  55. }
  56. void CheckReadOnly ()
  57. {
  58. if (is_readonly)
  59. throw new InvalidOperationException ("This UriTemplateTable is read-only");
  60. }
  61. bool is_readonly;
  62. Uri base_address;
  63. IList<Pair> key_value_pairs;
  64. public Uri BaseAddress {
  65. get { return base_address; }
  66. set {
  67. CheckReadOnly ();
  68. base_address = value;
  69. }
  70. }
  71. public bool IsReadOnly {
  72. get { return is_readonly; }
  73. }
  74. public IList<Pair> KeyValuePairs {
  75. get { return key_value_pairs; }
  76. }
  77. [MonoTODO]
  78. public void MakeReadOnly (bool allowDuplicateEquivalentUriTemplates)
  79. {
  80. throw new NotImplementedException ();
  81. }
  82. public Collection<UriTemplateMatch> Match (Uri uri)
  83. {
  84. var c = new Collection<UriTemplateMatch> ();
  85. UriTemplateMatch ret = null;
  86. foreach (Pair p in key_value_pairs) {
  87. ret = p.Key.Match (base_address, uri);
  88. if (ret != null)
  89. c.Add (ret);
  90. }
  91. return c;
  92. }
  93. public UriTemplateMatch MatchSingle (Uri uri)
  94. {
  95. UriTemplateMatch ret = null;
  96. foreach (Pair p in key_value_pairs) {
  97. ret = p.Key.Match (base_address, uri);
  98. if (ret != null)
  99. return ret;
  100. }
  101. throw new UriTemplateMatchException (String.Format ("No template matched for Uri '{0}'", uri));
  102. }
  103. }
  104. }