SerializationSource.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // System.Xml.Serialization.SerializationSource.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) 2004 Novell, Inc.
  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.Collections;
  30. using System.Globalization;
  31. using System.Text;
  32. namespace System.Xml.Serialization
  33. {
  34. #if !NET_2_1
  35. internal abstract class SerializationSource
  36. {
  37. Type[] includedTypes;
  38. string namspace;
  39. bool canBeGenerated = true;
  40. public SerializationSource (string namspace, Type[] includedTypes)
  41. {
  42. this.namspace = namspace;
  43. this.includedTypes = includedTypes;
  44. }
  45. protected bool BaseEquals (SerializationSource other)
  46. {
  47. if (namspace != other.namspace) return false;
  48. if (canBeGenerated != other.canBeGenerated) return false;
  49. if (includedTypes == null)
  50. return other.includedTypes == null;
  51. if (other.includedTypes == null || includedTypes.Length != other.includedTypes.Length) return false;
  52. for (int n=0; n<includedTypes.Length; n++)
  53. if (!includedTypes[n].Equals (other.includedTypes[n])) return false;
  54. return true;
  55. }
  56. public virtual bool CanBeGenerated
  57. {
  58. get { return canBeGenerated; }
  59. set { canBeGenerated = value; }
  60. }
  61. }
  62. internal class XmlTypeSerializationSource: SerializationSource
  63. {
  64. string attributeOverridesHash;
  65. Type type;
  66. string rootHash;
  67. public XmlTypeSerializationSource (Type type, XmlRootAttribute root, XmlAttributeOverrides attributeOverrides, string namspace, Type[] includedTypes)
  68. : base (namspace, includedTypes)
  69. {
  70. if (attributeOverrides != null) {
  71. StringBuilder sb = new StringBuilder ();
  72. attributeOverrides.AddKeyHash (sb);
  73. attributeOverridesHash = sb.ToString ();
  74. }
  75. if (root != null) {
  76. StringBuilder sb = new StringBuilder ();
  77. root.AddKeyHash (sb);
  78. rootHash = sb.ToString ();
  79. }
  80. this.type = type;
  81. }
  82. public override bool Equals (object o)
  83. {
  84. XmlTypeSerializationSource other = o as XmlTypeSerializationSource;
  85. if (other == null) return false;
  86. if (!type.Equals(other.type)) return false;
  87. if (rootHash != other.rootHash) return false;
  88. if (attributeOverridesHash != other.attributeOverridesHash) return false;
  89. return base.BaseEquals (other);
  90. }
  91. public override int GetHashCode ()
  92. {
  93. return type.GetHashCode ();
  94. }
  95. }
  96. internal class SoapTypeSerializationSource: SerializationSource
  97. {
  98. string attributeOverridesHash;
  99. Type type;
  100. public SoapTypeSerializationSource (Type type, SoapAttributeOverrides attributeOverrides, string namspace, Type[] includedTypes)
  101. : base (namspace, includedTypes)
  102. {
  103. if (attributeOverrides != null) {
  104. StringBuilder sb = new StringBuilder ();
  105. attributeOverrides.AddKeyHash (sb);
  106. attributeOverridesHash = sb.ToString ();
  107. }
  108. this.type = type;
  109. }
  110. public override bool Equals (object o)
  111. {
  112. SoapTypeSerializationSource other = o as SoapTypeSerializationSource;
  113. if (other == null) return false;
  114. if (!type.Equals(other.type)) return false;
  115. if (attributeOverridesHash != other.attributeOverridesHash) return false;
  116. return base.BaseEquals (other);
  117. }
  118. public override int GetHashCode ()
  119. {
  120. return type.GetHashCode ();
  121. }
  122. }
  123. internal class MembersSerializationSource: SerializationSource
  124. {
  125. string elementName;
  126. bool hasWrapperElement;
  127. string membersHash;
  128. bool writeAccessors;
  129. bool literalFormat;
  130. public MembersSerializationSource (string elementName, bool hasWrapperElement, XmlReflectionMember [] members, bool writeAccessors,
  131. bool literalFormat, string namspace, Type[] includedTypes)
  132. : base (namspace, includedTypes)
  133. {
  134. this.elementName = elementName;
  135. this.hasWrapperElement = hasWrapperElement;
  136. this.writeAccessors = writeAccessors;
  137. this.literalFormat = literalFormat;
  138. StringBuilder sb = new StringBuilder ();
  139. sb.Append (members.Length.ToString(CultureInfo.InvariantCulture));
  140. foreach (XmlReflectionMember mem in members)
  141. mem.AddKeyHash (sb);
  142. membersHash = sb.ToString();
  143. }
  144. public override bool Equals (object o)
  145. {
  146. MembersSerializationSource other = o as MembersSerializationSource;
  147. if (other == null) return false;
  148. if (literalFormat = other.literalFormat) return false;
  149. if (elementName != other.elementName) return false;
  150. if (hasWrapperElement != other.hasWrapperElement) return false;
  151. if (membersHash != other.membersHash) return false;
  152. return base.BaseEquals (other);
  153. }
  154. public override int GetHashCode ()
  155. {
  156. return membersHash.GetHashCode ();
  157. }
  158. }
  159. #endif
  160. internal class KeyHelper
  161. {
  162. public static void AddField (StringBuilder sb, int n, string val)
  163. {
  164. AddField (sb, n, val, null);
  165. }
  166. public static void AddField (StringBuilder sb, int n, string val, string def)
  167. {
  168. if (val != def) {
  169. sb.Append (n.ToString());
  170. sb.Append (val.Length.ToString(CultureInfo.InvariantCulture));
  171. sb.Append (val);
  172. }
  173. }
  174. public static void AddField (StringBuilder sb, int n, bool val)
  175. {
  176. AddField (sb, n, val, false);
  177. }
  178. public static void AddField (StringBuilder sb, int n, bool val, bool def)
  179. {
  180. if (val != def)
  181. sb.Append (n.ToString());
  182. }
  183. public static void AddField (StringBuilder sb, int n, int val, int def)
  184. {
  185. if (val != def) {
  186. sb.Append (n.ToString());
  187. sb.Append (val.ToString(CultureInfo.InvariantCulture));
  188. }
  189. }
  190. public static void AddField (StringBuilder sb, int n, Type val)
  191. {
  192. if (val != null) {
  193. sb.Append (n.ToString(CultureInfo.InvariantCulture));
  194. sb.Append (val.ToString());
  195. }
  196. }
  197. }
  198. }