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. internal class SerializationSource
  35. {
  36. Type[] includedTypes;
  37. string namspace;
  38. bool canBeGenerated = true;
  39. public SerializationSource (string namspace, Type[] includedTypes)
  40. {
  41. this.namspace = namspace;
  42. this.includedTypes = includedTypes;
  43. }
  44. public override bool Equals (object o)
  45. {
  46. SerializationSource other = o as SerializationSource;
  47. if (other == null) return false;
  48. if (namspace != other.namspace) return false;
  49. if (canBeGenerated != other.canBeGenerated) return false;
  50. if (includedTypes == null)
  51. return other.includedTypes == null;
  52. if (includedTypes.Length != other.includedTypes.Length) return false;
  53. for (int n=0; n<includedTypes.Length; n++)
  54. if (!includedTypes[n].Equals (other.includedTypes[n])) return false;
  55. return true;
  56. }
  57. public virtual bool CanBeGenerated
  58. {
  59. get { return canBeGenerated; }
  60. set { canBeGenerated = value; }
  61. }
  62. }
  63. internal class XmlTypeSerializationSource: SerializationSource
  64. {
  65. string attributeOverridesHash;
  66. Type type;
  67. string rootHash;
  68. public XmlTypeSerializationSource (Type type, XmlRootAttribute root, XmlAttributeOverrides attributeOverrides, string namspace, Type[] includedTypes)
  69. : base (namspace, includedTypes)
  70. {
  71. if (attributeOverrides != null) {
  72. StringBuilder sb = new StringBuilder ();
  73. attributeOverrides.AddKeyHash (sb);
  74. attributeOverridesHash = sb.ToString ();
  75. }
  76. if (root != null) {
  77. StringBuilder sb = new StringBuilder ();
  78. root.AddKeyHash (sb);
  79. rootHash = sb.ToString ();
  80. }
  81. this.type = type;
  82. }
  83. public override bool Equals (object o)
  84. {
  85. XmlTypeSerializationSource other = o as XmlTypeSerializationSource;
  86. if (other == null) return false;
  87. if (!type.Equals(other.type)) return false;
  88. if (rootHash != other.rootHash) return false;
  89. if (attributeOverridesHash != other.attributeOverridesHash) return false;
  90. return base.Equals (o);
  91. }
  92. public override int GetHashCode ()
  93. {
  94. return type.GetHashCode ();
  95. }
  96. }
  97. internal class SoapTypeSerializationSource: SerializationSource
  98. {
  99. string attributeOverridesHash;
  100. Type type;
  101. public SoapTypeSerializationSource (Type type, SoapAttributeOverrides attributeOverrides, string namspace, Type[] includedTypes)
  102. : base (namspace, includedTypes)
  103. {
  104. if (attributeOverrides != null) {
  105. StringBuilder sb = new StringBuilder ();
  106. attributeOverrides.AddKeyHash (sb);
  107. attributeOverridesHash = sb.ToString ();
  108. }
  109. this.type = type;
  110. }
  111. public override bool Equals (object o)
  112. {
  113. SoapTypeSerializationSource other = o as SoapTypeSerializationSource;
  114. if (other == null) return false;
  115. if (!type.Equals(other.type)) return false;
  116. if (attributeOverridesHash != other.attributeOverridesHash) return false;
  117. return base.Equals (o);
  118. }
  119. public override int GetHashCode ()
  120. {
  121. return type.GetHashCode ();
  122. }
  123. }
  124. internal class MembersSerializationSource: SerializationSource
  125. {
  126. string elementName;
  127. bool hasWrapperElement;
  128. string membersHash;
  129. bool writeAccessors;
  130. bool literalFormat;
  131. public MembersSerializationSource (string elementName, bool hasWrapperElement, XmlReflectionMember [] members, bool writeAccessors,
  132. bool literalFormat, string namspace, Type[] includedTypes)
  133. : base (namspace, includedTypes)
  134. {
  135. this.elementName = elementName;
  136. this.hasWrapperElement = hasWrapperElement;
  137. this.writeAccessors = writeAccessors;
  138. this.literalFormat = literalFormat;
  139. StringBuilder sb = new StringBuilder ();
  140. sb.Append (members.Length.ToString(CultureInfo.InvariantCulture));
  141. foreach (XmlReflectionMember mem in members)
  142. mem.AddKeyHash (sb);
  143. membersHash = sb.ToString();
  144. }
  145. public override bool Equals (object o)
  146. {
  147. MembersSerializationSource other = o as MembersSerializationSource;
  148. if (other == null) return false;
  149. if (literalFormat = other.literalFormat) return false;
  150. if (elementName != other.elementName) return false;
  151. if (hasWrapperElement != other.hasWrapperElement) return false;
  152. if (membersHash != other.membersHash) return false;
  153. return base.Equals (o);
  154. }
  155. public override int GetHashCode ()
  156. {
  157. return membersHash.GetHashCode ();
  158. }
  159. }
  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. }