2006-02-06 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / TypeData.cs
1 //
2 // System.Xml.Serialization.TypeData
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //  Lluis Sanchez Gual (lluis@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34 using System.Reflection;
35 using System.Xml.Schema;
36
37 namespace System.Xml.Serialization
38 {
39         internal class TypeData
40         {
41                 Type type;
42                 string elementName;
43                 SchemaTypes sType;
44                 Type listItemType;
45                 string typeName;
46                 string fullTypeName;
47                 TypeData listItemTypeData;
48                 TypeData listTypeData;
49                 TypeData mappedType;
50                 XmlSchemaPatternFacet facet;
51                 bool hasPublicConstructor = true;
52
53                 public TypeData (Type type, string elementName, bool isPrimitive) :
54                         this(type, elementName, isPrimitive, null, null) {}
55
56                 public TypeData (Type type, string elementName, bool isPrimitive, TypeData mappedType, XmlSchemaPatternFacet facet)
57                 {
58                         this.mappedType = mappedType;
59                         this.facet = facet;
60                         this.type = type;
61                         this.typeName = type.Name;
62                         this.fullTypeName = type.FullName.Replace ('+', '.');
63
64                         if (isPrimitive)
65                                 sType = SchemaTypes.Primitive;
66                         else
67                         {
68                                 if (type.IsEnum)
69                                         sType = SchemaTypes.Enum;
70                                 else if (typeof(IXmlSerializable).IsAssignableFrom (type))
71                                         sType = SchemaTypes.XmlSerializable;
72                                 else if (typeof (System.Xml.XmlNode).IsAssignableFrom (type))
73                                         sType = SchemaTypes.XmlNode;
74                                 else if (type.IsArray || typeof(IEnumerable).IsAssignableFrom (type))
75                                         sType = SchemaTypes.Array;
76                                 else
77                                         sType = SchemaTypes.Class;
78                         }
79                         
80                         if (IsListType)
81                                 this.elementName = TypeTranslator.GetArrayName (ListItemTypeData.XmlType);
82                         else
83                                 this.elementName = elementName;
84
85                         if (sType == SchemaTypes.Array || sType == SchemaTypes.Class) {
86                                 hasPublicConstructor = !type.IsInterface && (type.IsArray || type.GetConstructor (Type.EmptyTypes) != null || type.IsAbstract || type.IsValueType);
87                         }
88                 }
89
90                 internal TypeData (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
91                 {
92                         this.elementName = xmlType;
93                         this.typeName = typeName;
94                         this.fullTypeName = fullTypeName.Replace ('+', '.');
95                         this.listItemTypeData = listItemTypeData;
96                         this.sType = schemaType;
97                         this.hasPublicConstructor = true;
98                 }
99
100                 public string TypeName
101                 {
102                         get {
103                                 return typeName;
104                         }
105                 }
106                                 
107                 public string XmlType
108                 {
109                         get {
110                                 return elementName;
111                         }
112                 }
113                                 
114                 public Type Type
115                 {
116                         get {
117                                 return type;
118                         }
119                 }
120                                 
121                 public string FullTypeName
122                 {
123                         get {
124                                 return fullTypeName;
125                         }
126                 }
127
128                 public SchemaTypes SchemaType
129                 {
130                         get {
131                                 return sType;
132                         }
133                 }
134
135                 public bool IsListType
136                 {
137                         get { return SchemaType == SchemaTypes.Array; }
138                 }
139
140                 public bool IsComplexType
141                 {
142                         get 
143                         { 
144                                 return (SchemaType == SchemaTypes.Class || 
145                                               SchemaType == SchemaTypes.Array ||
146                                               SchemaType == SchemaTypes.Enum ||
147                                               SchemaType == SchemaTypes.XmlNode ||
148                                                   SchemaType == SchemaTypes.XmlSerializable ||
149                                                   !IsXsdType); 
150                         }
151                 }
152
153                 public bool IsValueType
154                 {
155                         get
156                         {
157                                 if (type != null) return type.IsValueType;
158                                 else return (sType == SchemaTypes.Primitive || sType == SchemaTypes.Enum);
159                         }
160                 }
161
162                 public TypeData ListItemTypeData
163                 {
164                         get
165                         {
166                                 if (listItemTypeData == null && type != null)
167                                         listItemTypeData = TypeTranslator.GetTypeData (ListItemType);
168                                 return listItemTypeData;
169                         }
170                 }
171                 
172                 public Type ListItemType
173                 {
174                         get
175                         {
176                                 if (type == null) 
177                                         throw new InvalidOperationException ("Property ListItemType is not supported for custom types");
178
179                                 if (listItemType != null) return listItemType;
180
181                                 if (SchemaType != SchemaTypes.Array)
182                                         throw new InvalidOperationException (Type.FullName + " is not a collection");
183                                 else if (type.IsArray) 
184                                         listItemType = type.GetElementType ();
185                                 else if (typeof(ICollection).IsAssignableFrom (type))
186                                 {
187                                         PropertyInfo prop = GetIndexerProperty (type);
188                                         if (prop == null) 
189                                                 throw new InvalidOperationException ("You must implement a default accessor on " + type.FullName + " because it inherits from ICollection");
190                                                 
191                                         return prop.PropertyType;
192                                 }
193                                 else
194                                 {
195                                         MethodInfo met = type.GetMethod ("Add");
196                                         if (met == null)
197                                                 throw new InvalidOperationException ("The collection " + type.FullName + " must implement an Add method");
198
199                                         ParameterInfo[] pars = met.GetParameters();
200                                         if (pars.Length != 1)
201                                                 throw new InvalidOperationException ("The Add method of the collection " + type.FullName + " must have only one parameter");
202                                         
203                                         return pars[0].ParameterType;
204                                 }
205
206                                 return listItemType;
207                         }
208                 }
209
210                 public TypeData ListTypeData
211                 {
212                         get
213                         {
214                                 if (listTypeData != null) return listTypeData;
215                                 
216                                 listTypeData = new TypeData (TypeName + "[]",
217                                         FullTypeName + "[]",
218                                         TypeTranslator.GetArrayName(XmlType),
219                                         SchemaTypes.Array, this);
220
221                                 return listTypeData;
222                         }
223                 }
224
225                 public bool IsXsdType {
226                         get { return mappedType == null; }
227                 }
228
229                 public TypeData MappedType {
230                         get {
231                                 return mappedType != null ? mappedType : this;
232                         }
233                 }
234
235                 public XmlSchemaPatternFacet XmlSchemaPatternFacet {
236                         get {
237                                 return facet;
238                         }
239                 }
240                 
241                 public bool HasPublicConstructor
242                 {
243                         get { return hasPublicConstructor; }
244                 }
245
246
247                 public static PropertyInfo GetIndexerProperty (Type collectionType)
248                 {
249                         PropertyInfo[] props = collectionType.GetProperties (BindingFlags.Instance | BindingFlags.Public);
250                         foreach (PropertyInfo prop in props)
251                         {
252                                 ParameterInfo[] pi = prop.GetIndexParameters ();
253                                 if (pi != null && pi.Length == 1 && pi[0].ParameterType == typeof(int))
254                                         return prop;
255                         }
256                         return null;
257                 }
258         }
259 }