This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / SerializationSource.cs
1 // 
2 // System.Xml.Serialization.SerializationSource.cs 
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // Copyright (C) 2004 Novell, Inc.
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Collections;
32 using System.Globalization;
33 using System.Text;
34
35 namespace System.Xml.Serialization 
36 {
37         internal class SerializationSource 
38         {
39                 ArrayList includedTypes;
40                 string namspace;
41                 bool canBeGenerated = true;
42                 
43                 public SerializationSource (string namspace, ArrayList includedTypes)
44                 {
45                         this.namspace = namspace;
46                         this.includedTypes = includedTypes;
47                 }
48                 
49                 public override bool Equals (object o)
50                 {
51                         SerializationSource other = o as SerializationSource;
52                         if (other == null) return false;
53                         if (namspace != other.namspace) return false;
54                         if (canBeGenerated != other.canBeGenerated) return false;
55                         
56                         if (includedTypes == null)
57                                 return other.includedTypes == null;
58                         
59                         if (includedTypes.Count != other.includedTypes.Count) return false;
60                         for (int n=0; n<includedTypes.Count; n++)
61                                 if (!includedTypes[n].Equals (other.includedTypes[n])) return false;
62
63                         return true;
64                 }
65                 
66                 public virtual bool CanBeGenerated
67                 {
68                         get { return canBeGenerated; }
69                         set { canBeGenerated = value; }
70                 }
71         }
72         
73         internal class XmlTypeSerializationSource: SerializationSource
74         {
75                 string attributeOverridesHash;
76                 Type type;
77                 string rootHash;
78                 
79                 public XmlTypeSerializationSource (Type type, XmlRootAttribute root, XmlAttributeOverrides attributeOverrides, string namspace, ArrayList includedTypes)
80                 : base (namspace, includedTypes)
81                 {
82                         if (attributeOverrides != null) {
83                                 StringBuilder sb = new StringBuilder ();
84                                 attributeOverrides.AddKeyHash (sb);
85                                 attributeOverridesHash = sb.ToString ();
86                         }
87                         
88                         if (root != null) {
89                                 StringBuilder sb = new StringBuilder ();
90                                 root.AddKeyHash (sb);
91                                 rootHash = sb.ToString ();
92                         }
93                                 
94                         this.type = type;
95                 }
96                 
97                 public override bool Equals (object o)
98                 {
99                         XmlTypeSerializationSource other = o as XmlTypeSerializationSource;
100                         if (other == null) return false;
101                         
102                         if (!type.Equals(other.type)) return false;
103                         if (rootHash != other.rootHash) return false;
104                         if (attributeOverridesHash != other.attributeOverridesHash) return false;
105                         
106                         return base.Equals (o);
107                 }
108                 
109                 public override int GetHashCode ()
110                 {
111                         return type.GetHashCode ();
112                 }
113         }
114         
115         internal class SoapTypeSerializationSource: SerializationSource
116         {
117                 string attributeOverridesHash;
118                 Type type;
119                 
120                 public SoapTypeSerializationSource (Type type, SoapAttributeOverrides attributeOverrides, string namspace, ArrayList includedTypes)
121                 : base (namspace, includedTypes)
122                 {
123                         if (attributeOverrides != null) {
124                                 StringBuilder sb = new StringBuilder ();
125                                 attributeOverrides.AddKeyHash (sb);
126                                 attributeOverridesHash = sb.ToString ();
127                         }
128                         
129                         this.type = type;
130                 }
131
132                 public override bool Equals (object o)
133                 {
134                         SoapTypeSerializationSource other = o as SoapTypeSerializationSource;
135                         if (other == null) return false;
136                         if (!type.Equals(other.type)) return false;
137                         if (attributeOverridesHash != other.attributeOverridesHash) return false;
138                         
139                         return base.Equals (o);
140                 }
141                 
142                 public override int GetHashCode ()
143                 {
144                         return type.GetHashCode ();
145                 }
146         }
147         
148         internal class MembersSerializationSource: SerializationSource
149         {
150                 string elementName;
151                 bool hasWrapperElement;
152                 string membersHash;
153                 bool writeAccessors;
154                 bool literalFormat;
155                 
156                 public MembersSerializationSource (string elementName, bool hasWrapperElement, XmlReflectionMember [] members, bool writeAccessors, 
157                                                                                    bool literalFormat, string namspace, ArrayList includedTypes)
158                 : base (namspace, includedTypes)
159                 {
160                         this.elementName = elementName;
161                         this.hasWrapperElement = hasWrapperElement;
162                         this.writeAccessors = writeAccessors;
163                         this.literalFormat = literalFormat;
164                         
165                         StringBuilder sb = new StringBuilder ();
166                         sb.Append (members.Length.ToString(CultureInfo.InvariantCulture));
167                         foreach (XmlReflectionMember mem in members)
168                                 mem.AddKeyHash (sb);
169                                 
170                         membersHash = sb.ToString();
171                 }
172                 
173                 
174                 public override bool Equals (object o)
175                 {
176                         MembersSerializationSource other = o as MembersSerializationSource;
177                         if (other == null) return false;
178                         if (literalFormat = other.literalFormat) return false;
179                         if (elementName != other.elementName) return false;
180                         if (hasWrapperElement != other.hasWrapperElement) return false;
181                         if (membersHash != other.membersHash) return false;
182                         
183                         return base.Equals (o);
184                 }
185                 
186                 public override int GetHashCode ()
187                 {
188                         return membersHash.GetHashCode ();
189                 }
190         }
191         
192         internal class KeyHelper
193         {
194                 public static void AddField (StringBuilder sb, int n, string val)
195                 {
196                         AddField (sb, n, val, null);
197                 }
198                 
199                 public static void AddField (StringBuilder sb, int n, string val, string def)
200                 {
201                         if (val != def) {
202                                 sb.Append (n.ToString());
203                                 sb.Append (val.Length.ToString(CultureInfo.InvariantCulture));
204                                 sb.Append (val);
205                         }
206                 }
207                 
208                 public static void AddField (StringBuilder sb, int n, bool val)
209                 {
210                         AddField (sb, n, val, false);
211                 }
212                 
213                 public static void AddField (StringBuilder sb, int n, bool val, bool def)
214                 {
215                         if (val != def)
216                                 sb.Append (n.ToString());
217                 }
218                 
219                 public static void AddField (StringBuilder sb, int n, int val, int def)
220                 {
221                         if (val != def) {
222                                 sb.Append (n.ToString());
223                                 sb.Append (val.ToString(CultureInfo.InvariantCulture));
224                         }
225                 }
226                 
227                 public static void AddField (StringBuilder sb, int n, Type val)
228                 {
229                         if (val != null) {
230                                 sb.Append (n.ToString(CultureInfo.InvariantCulture));
231                                 sb.Append (val.ToString());
232                         }
233                 }
234         }
235 }
236