2 // System.Xml.Serialization.SerializationSource.cs
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // Copyright (C) 2004 Novell, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Globalization;
35 namespace System.Xml.Serialization
37 internal abstract class SerializationSource
41 bool canBeGenerated = true;
43 public SerializationSource (string namspace, Type[] includedTypes)
45 this.namspace = namspace;
46 this.includedTypes = includedTypes;
49 protected bool BaseEquals (SerializationSource other)
51 if (namspace != other.namspace) return false;
52 if (canBeGenerated != other.canBeGenerated) return false;
54 if (includedTypes == null)
55 return other.includedTypes == null;
57 if (other.includedTypes == null || includedTypes.Length != other.includedTypes.Length) return false;
58 for (int n=0; n<includedTypes.Length; n++)
59 if (!includedTypes[n].Equals (other.includedTypes[n])) return false;
64 public virtual bool CanBeGenerated
66 get { return canBeGenerated; }
67 set { canBeGenerated = value; }
71 internal class XmlTypeSerializationSource: SerializationSource
73 string attributeOverridesHash;
77 public XmlTypeSerializationSource (Type type, XmlRootAttribute root, XmlAttributeOverrides attributeOverrides, string namspace, Type[] includedTypes)
78 : base (namspace, includedTypes)
80 if (attributeOverrides != null) {
81 StringBuilder sb = new StringBuilder ();
82 attributeOverrides.AddKeyHash (sb);
83 attributeOverridesHash = sb.ToString ();
87 StringBuilder sb = new StringBuilder ();
89 rootHash = sb.ToString ();
95 public override bool Equals (object o)
97 XmlTypeSerializationSource other = o as XmlTypeSerializationSource;
98 if (other == null) return false;
100 if (!type.Equals(other.type)) return false;
101 if (rootHash != other.rootHash) return false;
102 if (attributeOverridesHash != other.attributeOverridesHash) return false;
104 return base.BaseEquals (other);
107 public override int GetHashCode ()
109 return type.GetHashCode ();
113 internal class SoapTypeSerializationSource: SerializationSource
115 string attributeOverridesHash;
118 public SoapTypeSerializationSource (Type type, SoapAttributeOverrides attributeOverrides, string namspace, Type[] includedTypes)
119 : base (namspace, includedTypes)
121 if (attributeOverrides != null) {
122 StringBuilder sb = new StringBuilder ();
123 attributeOverrides.AddKeyHash (sb);
124 attributeOverridesHash = sb.ToString ();
130 public override bool Equals (object o)
132 SoapTypeSerializationSource other = o as SoapTypeSerializationSource;
133 if (other == null) return false;
134 if (!type.Equals(other.type)) return false;
135 if (attributeOverridesHash != other.attributeOverridesHash) return false;
137 return base.BaseEquals (other);
140 public override int GetHashCode ()
142 return type.GetHashCode ();
146 internal class MembersSerializationSource: SerializationSource
149 bool hasWrapperElement;
154 public MembersSerializationSource (string elementName, bool hasWrapperElement, XmlReflectionMember [] members, bool writeAccessors,
155 bool literalFormat, string namspace, Type[] includedTypes)
156 : base (namspace, includedTypes)
158 this.elementName = elementName;
159 this.hasWrapperElement = hasWrapperElement;
160 this.writeAccessors = writeAccessors;
161 this.literalFormat = literalFormat;
163 StringBuilder sb = new StringBuilder ();
164 sb.Append (members.Length.ToString(CultureInfo.InvariantCulture));
165 foreach (XmlReflectionMember mem in members)
168 membersHash = sb.ToString();
172 public override bool Equals (object o)
174 MembersSerializationSource other = o as MembersSerializationSource;
175 if (other == null) return false;
176 if (literalFormat = other.literalFormat) return false;
177 if (elementName != other.elementName) return false;
178 if (hasWrapperElement != other.hasWrapperElement) return false;
179 if (membersHash != other.membersHash) return false;
181 return base.BaseEquals (other);
184 public override int GetHashCode ()
186 return membersHash.GetHashCode ();
190 internal class KeyHelper
192 public static void AddField (StringBuilder sb, int n, string val)
194 AddField (sb, n, val, null);
197 public static void AddField (StringBuilder sb, int n, string val, string def)
200 sb.Append (n.ToString());
201 sb.Append (val.Length.ToString(CultureInfo.InvariantCulture));
206 public static void AddField (StringBuilder sb, int n, bool val)
208 AddField (sb, n, val, false);
211 public static void AddField (StringBuilder sb, int n, bool val, bool def)
214 sb.Append (n.ToString());
217 public static void AddField (StringBuilder sb, int n, int val, int def)
220 sb.Append (n.ToString());
221 sb.Append (val.ToString(CultureInfo.InvariantCulture));
225 public static void AddField (StringBuilder sb, int n, Type val)
228 sb.Append (n.ToString(CultureInfo.InvariantCulture));
229 sb.Append (val.ToString());