SoapAttributeOverrides.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // SoapAttributeOverrides.cs:
  3. //
  4. // Author:
  5. // John Donagher ([email protected])
  6. //
  7. // (C) 2002 John Donagher
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. namespace System.Xml.Serialization
  32. {
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. public class SoapAttributeOverrides
  37. {
  38. /// <summary>
  39. /// This class requires to store SoapAttrributes indexed by a key containg
  40. /// both Type and Member Name. There are 3 approaches to this IMO.
  41. /// 1. Make the key as "FullTypeName..MemberName", with ".." seperating Type and Member.
  42. /// 2. Use a jagged 2D hashtable. The main hashtable is indexed by Type and each value
  43. /// contains another hashtable which is indexed by member names. (Too many hashtables)
  44. /// 3. Use a new class which emcompasses the Type and MemberName. An implementation is there
  45. /// in TypeMember class in this namespace. (Too many instantiations of the class needed)
  46. ///
  47. /// Method 1 is the most elegent, but I am not sure if the seperator is language insensitive.
  48. /// What if someone writes a language which allows . in the member names.
  49. /// </summary>
  50. ///
  51. private Hashtable overrides;
  52. public SoapAttributeOverrides ()
  53. {
  54. overrides = new Hashtable();
  55. }
  56. public SoapAttributes this [Type type]
  57. {
  58. get { return this [type, string.Empty]; }
  59. }
  60. public SoapAttributes this [Type type, string member]
  61. {
  62. get
  63. {
  64. return (SoapAttributes) overrides[GetKey(type,member)];
  65. }
  66. }
  67. public void Add (Type type, SoapAttributes attributes)
  68. {
  69. Add(type, string.Empty, attributes);
  70. }
  71. public void Add (Type type, string member, SoapAttributes attributes)
  72. {
  73. if(overrides[GetKey(type, member)] != null)
  74. throw new Exception("The attributes for the given type and Member already exist in the collection");
  75. overrides.Add(GetKey(type,member), attributes);
  76. }
  77. private TypeMember GetKey(Type type, string member)
  78. {
  79. return new TypeMember(type, member);
  80. }
  81. internal void AddKeyHash (System.Text.StringBuilder sb)
  82. {
  83. sb.Append ("SAO ");
  84. foreach (DictionaryEntry entry in overrides)
  85. {
  86. SoapAttributes val = (SoapAttributes) overrides [entry.Key];
  87. sb.Append (entry.Key.ToString()).Append(' ');
  88. val.AddKeyHash (sb);
  89. }
  90. sb.Append ("|");
  91. }
  92. }
  93. }