* roottypes.cs: Rename from tree.cs.
[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 abstract class SerializationSource 
38         {
39                 Type[] includedTypes;
40                 string namspace;
41                 bool canBeGenerated = true;
42                 
43                 public SerializationSource (string namspace, Type[] includedTypes)
44                 {
45                         this.namspace = namspace;
46                         this.includedTypes = includedTypes;
47                 }
48                 
49                 protected bool BaseEquals (SerializationSource other)
50                 {
51                         if (namspace != other.namspace) return false;
52                         if (canBeGenerated != other.canBeGenerated) return false;
53                         
54                         if (includedTypes == null)
55                                 return other.includedTypes == null;
56                         
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;
60
61                         return true;
62                 }
63                 
64                 public virtual bool CanBeGenerated
65                 {
66                         get { return canBeGenerated; }
67                         set { canBeGenerated = value; }
68                 }
69         }
70         
71         internal class XmlTypeSerializationSource: SerializationSource
72         {
73                 string attributeOverridesHash;
74                 Type type;
75                 string rootHash;
76                 
77                 public XmlTypeSerializationSource (Type type, XmlRootAttribute root, XmlAttributeOverrides attributeOverrides, string namspace, Type[] includedTypes)
78                 : base (namspace, includedTypes)
79                 {
80                         if (attributeOverrides != null) {
81                                 StringBuilder sb = new StringBuilder ();
82                                 attributeOverrides.AddKeyHash (sb);
83                                 attributeOverridesHash = sb.ToString ();
84                         }
85                         
86                         if (root != null) {
87                                 StringBuilder sb = new StringBuilder ();
88                                 root.AddKeyHash (sb);
89                                 rootHash = sb.ToString ();
90                         }
91                                 
92                         this.type = type;
93                 }
94                 
95                 public override bool Equals (object o)
96                 {
97                         XmlTypeSerializationSource other = o as XmlTypeSerializationSource;
98                         if (other == null) return false;
99                         
100                         if (!type.Equals(other.type)) return false;
101                         if (rootHash != other.rootHash) return false;
102                         if (attributeOverridesHash != other.attributeOverridesHash) return false;
103                         
104                         return base.BaseEquals (other);
105                 }
106                 
107                 public override int GetHashCode ()
108                 {
109                         return type.GetHashCode ();
110                 }
111         }
112         
113         internal class SoapTypeSerializationSource: SerializationSource
114         {
115                 string attributeOverridesHash;
116                 Type type;
117                 
118                 public SoapTypeSerializationSource (Type type, SoapAttributeOverrides attributeOverrides, string namspace, Type[] includedTypes)
119                 : base (namspace, includedTypes)
120                 {
121                         if (attributeOverrides != null) {
122                                 StringBuilder sb = new StringBuilder ();
123                                 attributeOverrides.AddKeyHash (sb);
124                                 attributeOverridesHash = sb.ToString ();
125                         }
126                         
127                         this.type = type;
128                 }
129
130                 public override bool Equals (object o)
131                 {
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;
136                         
137                         return base.BaseEquals (other);
138                 }
139                 
140                 public override int GetHashCode ()
141                 {
142                         return type.GetHashCode ();
143                 }
144         }
145         
146         internal class MembersSerializationSource: SerializationSource
147         {
148                 string elementName;
149                 bool hasWrapperElement;
150                 string membersHash;
151                 bool writeAccessors;
152                 bool literalFormat;
153                 
154                 public MembersSerializationSource (string elementName, bool hasWrapperElement, XmlReflectionMember [] members, bool writeAccessors, 
155                                                                                    bool literalFormat, string namspace, Type[] includedTypes)
156                 : base (namspace, includedTypes)
157                 {
158                         this.elementName = elementName;
159                         this.hasWrapperElement = hasWrapperElement;
160                         this.writeAccessors = writeAccessors;
161                         this.literalFormat = literalFormat;
162                         
163                         StringBuilder sb = new StringBuilder ();
164                         sb.Append (members.Length.ToString(CultureInfo.InvariantCulture));
165                         foreach (XmlReflectionMember mem in members)
166                                 mem.AddKeyHash (sb);
167                                 
168                         membersHash = sb.ToString();
169                 }
170                 
171                 
172                 public override bool Equals (object o)
173                 {
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;
180                         
181                         return base.BaseEquals (other);
182                 }
183                 
184                 public override int GetHashCode ()
185                 {
186                         return membersHash.GetHashCode ();
187                 }
188         }
189         
190         internal class KeyHelper
191         {
192                 public static void AddField (StringBuilder sb, int n, string val)
193                 {
194                         AddField (sb, n, val, null);
195                 }
196                 
197                 public static void AddField (StringBuilder sb, int n, string val, string def)
198                 {
199                         if (val != def) {
200                                 sb.Append (n.ToString());
201                                 sb.Append (val.Length.ToString(CultureInfo.InvariantCulture));
202                                 sb.Append (val);
203                         }
204                 }
205                 
206                 public static void AddField (StringBuilder sb, int n, bool val)
207                 {
208                         AddField (sb, n, val, false);
209                 }
210                 
211                 public static void AddField (StringBuilder sb, int n, bool val, bool def)
212                 {
213                         if (val != def)
214                                 sb.Append (n.ToString());
215                 }
216                 
217                 public static void AddField (StringBuilder sb, int n, int val, int def)
218                 {
219                         if (val != def) {
220                                 sb.Append (n.ToString());
221                                 sb.Append (val.ToString(CultureInfo.InvariantCulture));
222                         }
223                 }
224                 
225                 public static void AddField (StringBuilder sb, int n, Type val)
226                 {
227                         if (val != null) {
228                                 sb.Append (n.ToString(CultureInfo.InvariantCulture));
229                                 sb.Append (val.ToString());
230                         }
231                 }
232         }
233 }
234