SerializationSource.cs 6.5 KB

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