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