SerializationSource.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. using System.Collections;
  10. using System.Text;
  11. namespace System.Xml.Serialization
  12. {
  13. internal class SerializationSource
  14. {
  15. ArrayList includedTypes;
  16. string namspace;
  17. bool canBeGenerated = true;
  18. public SerializationSource (string namspace, ArrayList includedTypes)
  19. {
  20. this.namspace = namspace;
  21. this.includedTypes = includedTypes;
  22. }
  23. public override bool Equals (object o)
  24. {
  25. SerializationSource other = o as SerializationSource;
  26. if (other == null) return false;
  27. if (namspace != other.namspace) return false;
  28. if (canBeGenerated != other.canBeGenerated) return false;
  29. if (includedTypes == null)
  30. return other.includedTypes == null;
  31. if (includedTypes.Count != other.includedTypes.Count) return false;
  32. for (int n=0; n<includedTypes.Count; n++)
  33. if (!includedTypes[n].Equals (other.includedTypes[n])) return false;
  34. return true;
  35. }
  36. public virtual bool CanBeGenerated
  37. {
  38. get { return canBeGenerated; }
  39. set { canBeGenerated = value; }
  40. }
  41. }
  42. internal class XmlTypeSerializationSource: SerializationSource
  43. {
  44. string attributeOverridesHash;
  45. Type type;
  46. string rootHash;
  47. public XmlTypeSerializationSource (Type type, XmlRootAttribute root, XmlAttributeOverrides attributeOverrides, string namspace, ArrayList includedTypes)
  48. : base (namspace, includedTypes)
  49. {
  50. if (attributeOverrides != null) {
  51. StringBuilder sb = new StringBuilder ();
  52. attributeOverrides.AddKeyHash (sb);
  53. attributeOverridesHash = sb.ToString ();
  54. }
  55. if (root != null) {
  56. StringBuilder sb = new StringBuilder ();
  57. root.AddKeyHash (sb);
  58. rootHash = sb.ToString ();
  59. }
  60. this.type = type;
  61. }
  62. public override bool Equals (object o)
  63. {
  64. XmlTypeSerializationSource other = o as XmlTypeSerializationSource;
  65. if (other == null) return false;
  66. if (!type.Equals(other.type)) return false;
  67. if (rootHash != other.rootHash) return false;
  68. if (attributeOverridesHash != other.attributeOverridesHash) return false;
  69. return base.Equals (o);
  70. }
  71. public override int GetHashCode ()
  72. {
  73. return type.GetHashCode ();
  74. }
  75. }
  76. internal class SoapTypeSerializationSource: SerializationSource
  77. {
  78. string attributeOverridesHash;
  79. Type type;
  80. public SoapTypeSerializationSource (Type type, SoapAttributeOverrides attributeOverrides, string namspace, ArrayList includedTypes)
  81. : base (namspace, includedTypes)
  82. {
  83. if (attributeOverrides != null) {
  84. StringBuilder sb = new StringBuilder ();
  85. attributeOverrides.AddKeyHash (sb);
  86. attributeOverridesHash = sb.ToString ();
  87. }
  88. this.type = type;
  89. }
  90. public override bool Equals (object o)
  91. {
  92. SoapTypeSerializationSource other = o as SoapTypeSerializationSource;
  93. if (other == null) return false;
  94. if (!type.Equals(other.type)) return false;
  95. if (attributeOverridesHash != other.attributeOverridesHash) return false;
  96. return base.Equals (o);
  97. }
  98. public override int GetHashCode ()
  99. {
  100. return type.GetHashCode ();
  101. }
  102. }
  103. internal class MembersSerializationSource: SerializationSource
  104. {
  105. string elementName;
  106. bool hasWrapperElement;
  107. string membersHash;
  108. bool writeAccessors;
  109. bool literalFormat;
  110. public MembersSerializationSource (string elementName, bool hasWrapperElement, XmlReflectionMember [] members, bool writeAccessors,
  111. bool literalFormat, string namspace, ArrayList includedTypes)
  112. : base (namspace, includedTypes)
  113. {
  114. this.elementName = elementName;
  115. this.hasWrapperElement = hasWrapperElement;
  116. this.writeAccessors = writeAccessors;
  117. this.literalFormat = literalFormat;
  118. StringBuilder sb = new StringBuilder ();
  119. sb.Append (members.Length.ToString());
  120. foreach (XmlReflectionMember mem in members)
  121. mem.AddKeyHash (sb);
  122. membersHash = sb.ToString();
  123. }
  124. public override bool Equals (object o)
  125. {
  126. MembersSerializationSource other = o as MembersSerializationSource;
  127. if (other == null) return false;
  128. if (literalFormat = other.literalFormat) return false;
  129. if (elementName != other.elementName) return false;
  130. if (hasWrapperElement != other.hasWrapperElement) return false;
  131. if (membersHash != other.membersHash) return false;
  132. return base.Equals (o);
  133. }
  134. public override int GetHashCode ()
  135. {
  136. return membersHash.GetHashCode ();
  137. }
  138. }
  139. internal class KeyHelper
  140. {
  141. public static void AddField (StringBuilder sb, int n, string val)
  142. {
  143. AddField (sb, n, val, null);
  144. }
  145. public static void AddField (StringBuilder sb, int n, string val, string def)
  146. {
  147. if (val != def) {
  148. sb.Append (n.ToString());
  149. sb.Append (val.Length.ToString());
  150. sb.Append (val);
  151. }
  152. }
  153. public static void AddField (StringBuilder sb, int n, bool val)
  154. {
  155. AddField (sb, n, val, false);
  156. }
  157. public static void AddField (StringBuilder sb, int n, bool val, bool def)
  158. {
  159. if (val != def)
  160. sb.Append (n.ToString());
  161. }
  162. public static void AddField (StringBuilder sb, int n, int val, int def)
  163. {
  164. if (val != def) {
  165. sb.Append (n.ToString());
  166. sb.Append (val.ToString());
  167. }
  168. }
  169. public static void AddField (StringBuilder sb, int n, Type val)
  170. {
  171. if (val != null) {
  172. sb.Append (n.ToString());
  173. sb.Append (val.ToString());
  174. }
  175. }
  176. }
  177. }