**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / System.Xml.Serialization / TypeTranslator.cs
blobfc71eeea981f378058c4908567497f5e0da23c23
1 //
2 // System.Xml.Serialization.TypeTranslator
3 //
4 // Authors:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Erik LeBel (eriklebel@yahoo.ca)
7 // Lluis Sanchez Gual (lluis@ximian.com)
8 //
9 // (C) 2002 Ximian, Inc (http://www.ximian.com)
10 // (C) 2003 Erik Lebel
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System;
35 using System.Collections;
36 using System.Globalization;
38 namespace System.Xml.Serialization
40 internal class TypeTranslator
42 static Hashtable nameCache;
43 static Hashtable primitiveTypes;
45 static TypeTranslator ()
47 nameCache = new Hashtable ();
49 // XSD Types with direct map to CLR types
51 nameCache.Add (typeof (bool), new TypeData (typeof (bool), "boolean", true));
52 nameCache.Add (typeof (short), new TypeData (typeof (short), "short", true));
53 nameCache.Add (typeof (ushort), new TypeData (typeof (ushort), "unsignedShort", true));
54 nameCache.Add (typeof (int), new TypeData (typeof (int), "int", true));
55 nameCache.Add (typeof (uint), new TypeData (typeof (uint), "unsignedInt", true));
56 nameCache.Add (typeof (long), new TypeData (typeof (long), "long", true));
57 nameCache.Add (typeof (ulong), new TypeData (typeof (ulong), "unsignedLong", true));
58 nameCache.Add (typeof (float), new TypeData (typeof (float), "float", true));
59 nameCache.Add (typeof (double), new TypeData (typeof (double), "double", true));
60 nameCache.Add (typeof (DateTime), new TypeData (typeof (DateTime), "dateTime", true)); // TODO: timeInstant, Xml date, xml time
61 nameCache.Add (typeof (Guid), new TypeData (typeof (Guid), "guid", true));
62 nameCache.Add (typeof (decimal), new TypeData (typeof (decimal), "decimal", true));
63 nameCache.Add (typeof (XmlQualifiedName), new TypeData (typeof (XmlQualifiedName), "QName", true));
64 nameCache.Add (typeof (string), new TypeData (typeof (string), "string", true));
65 nameCache.Add (typeof (byte), new TypeData (typeof (byte), "unsignedByte", true));
66 nameCache.Add (typeof (sbyte), new TypeData (typeof (sbyte), "byte", true));
67 nameCache.Add (typeof (char), new TypeData (typeof (char), "char", true));
68 nameCache.Add (typeof (object), new TypeData (typeof (object), "anyType", false));
69 nameCache.Add (typeof (byte[]), new TypeData (typeof (byte[]), "base64Binary", true));
70 nameCache.Add (typeof (XmlNode), new TypeData (typeof (XmlNode), "XmlNode", false));
71 nameCache.Add (typeof (XmlElement), new TypeData (typeof (XmlElement), "XmlElement", false));
72 nameCache.Add (typeof (TimeSpan), new TypeData (typeof (TimeSpan), "duration", true));
74 primitiveTypes = new Hashtable();
75 ICollection types = nameCache.Values;
76 foreach (TypeData td in types)
77 primitiveTypes.Add (td.XmlType, td);
79 // Additional XSD types
81 primitiveTypes.Add ("date", new TypeData (typeof (DateTime), "date", true)); // TODO: timeInstant
82 primitiveTypes.Add ("time", new TypeData (typeof (DateTime), "time", true));
83 primitiveTypes.Add ("timePeriod", new TypeData (typeof (DateTime), "timePeriod", true));
84 primitiveTypes.Add ("gDay", new TypeData (typeof (string), "gDay", true));
85 primitiveTypes.Add ("gMonthDay", new TypeData (typeof (string), "gMonthDay", true));
86 primitiveTypes.Add ("gYear", new TypeData (typeof (string), "gYear", true));
87 primitiveTypes.Add ("gYearMonth", new TypeData (typeof (string), "gYearMonth", true));
88 primitiveTypes.Add ("month", new TypeData (typeof (DateTime), "month", true));
89 primitiveTypes.Add ("NMTOKEN", new TypeData (typeof (string), "NMTOKEN", true));
90 primitiveTypes.Add ("NMTOKENS", new TypeData (typeof (string), "NMTOKENS", true));
91 primitiveTypes.Add ("Name", new TypeData (typeof (string), "Name", true));
92 primitiveTypes.Add ("NCName", new TypeData (typeof (string), "NCName", true));
93 primitiveTypes.Add ("language", new TypeData (typeof (string), "language", true));
94 primitiveTypes.Add ("integer", new TypeData (typeof (string), "integer", true));
95 primitiveTypes.Add ("positiveInteger", new TypeData (typeof (string), "positiveInteger", true));
96 primitiveTypes.Add ("nonPositiveInteger", new TypeData (typeof (string), "nonPositiveInteger", true));
97 primitiveTypes.Add ("negativeInteger", new TypeData (typeof (string), "negativeInteger", true));
98 primitiveTypes.Add ("nonNegativeInteger", new TypeData (typeof (string), "nonNegativeInteger", true));
99 primitiveTypes.Add ("ENTITIES", new TypeData (typeof (string), "ENTITIES", true));
100 primitiveTypes.Add ("ENTITY", new TypeData (typeof (string), "ENTITY", true));
101 primitiveTypes.Add ("hexBinary", new TypeData (typeof (byte[]), "hexBinary", true));
102 primitiveTypes.Add ("ID", new TypeData (typeof (string), "ID", true));
103 primitiveTypes.Add ("IDREF", new TypeData (typeof (string), "IDREF", true));
104 primitiveTypes.Add ("IDREFS", new TypeData (typeof (string), "IDREFS", true));
105 primitiveTypes.Add ("NOTATION", new TypeData (typeof (string), "NOTATION", true));
106 primitiveTypes.Add ("token", new TypeData (typeof (string), "token", true));
107 primitiveTypes.Add ("normalizedString", new TypeData (typeof (string), "normalizedString", true));
108 primitiveTypes.Add ("anyURI", new TypeData (typeof (string), "anyURI", true));
109 primitiveTypes.Add ("base64", new TypeData (typeof (byte[]), "base64", true));
112 public static TypeData GetTypeData (Type type)
114 return GetTypeData (type, null);
117 public static TypeData GetTypeData (Type type, string xmlDataType)
119 if ((xmlDataType != null) && (xmlDataType.Length != 0)) return GetPrimitiveTypeData (xmlDataType);
121 TypeData typeData = nameCache[type] as TypeData;
122 if (typeData != null) return typeData;
124 string name;
125 if (type.IsArray) {
126 string sufix = GetTypeData (type.GetElementType ()).XmlType;
127 name = GetArrayName (sufix);
129 else
130 name = type.Name;
132 typeData = new TypeData (type, name, false);
133 nameCache[type] = typeData;
134 return typeData;
137 public static bool IsPrimitive (Type type)
139 return GetTypeData (type).SchemaType == SchemaTypes.Primitive;
142 public static TypeData GetPrimitiveTypeData (string typeName)
144 TypeData td = (TypeData) primitiveTypes[typeName];
145 if (td == null) throw new NotSupportedException ("Data type '" + typeName + "' not supported");
146 return td;
149 public static TypeData FindPrimitiveTypeData (string typeName)
151 return (TypeData) primitiveTypes[typeName];
154 public static TypeData GetDefaultPrimitiveTypeData (TypeData primType)
156 // Returns the TypeData that is mapped by default to the clr type
157 // that primType represents
159 if (primType.SchemaType == SchemaTypes.Primitive)
161 TypeData newPrim = GetTypeData (primType.Type);
162 if (newPrim != primType) return newPrim;
164 return primType;
167 public static bool IsDefaultPrimitiveTpeData (TypeData primType)
169 return GetDefaultPrimitiveTypeData (primType) == primType;
172 public static TypeData CreateCustomType (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
174 TypeData td = new TypeData (typeName, fullTypeName, xmlType, schemaType, listItemTypeData);
175 return td;
178 public static string GetArrayName (string elemName)
180 return "ArrayOf" + Char.ToUpper (elemName [0], CultureInfo.InvariantCulture) + elemName.Substring (1);
183 public static string GetArrayName (string elemName, int dimensions)
185 string aname = GetArrayName (elemName);
186 for ( ; dimensions > 1; dimensions--)
187 aname = "ArrayOf" + aname;
188 return aname;
191 public static void ParseArrayType (string arrayType, out string type, out string ns, out string dimensions)
193 int i = arrayType.LastIndexOf (":");
194 if (i == -1) ns = "";
195 else ns = arrayType.Substring (0,i);
197 int j = arrayType.IndexOf ("[", i+1);
198 if (j == -1) throw new InvalidOperationException ("Cannot parse WSDL array type: " + arrayType);
199 type = arrayType.Substring (i+1, j-i-1);
200 dimensions = arrayType.Substring (j);