2 // System.Xml.Serialization.TypeTranslator
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Erik LeBel (eriklebel@yahoo.ca)
7 // Lluis Sanchez Gual (lluis@ximian.com)
9 // (C) 2002 Ximian, Inc (http://www.ximian.com)
10 // (C) 2003 Erik Lebel
14 using System.Collections;
16 namespace System.Xml.Serialization
18 internal class TypeTranslator
20 static Hashtable nameCache;
21 static Hashtable primitiveTypes;
23 static TypeTranslator ()
25 nameCache = new Hashtable ();
27 // XSD Types with direct map to CLR types
29 nameCache.Add (typeof (bool), new TypeData (typeof (bool), "boolean", true));
30 nameCache.Add (typeof (short), new TypeData (typeof (short), "short", true));
31 nameCache.Add (typeof (ushort), new TypeData (typeof (ushort), "unsignedShort", true));
32 nameCache.Add (typeof (int), new TypeData (typeof (int), "int", true));
33 nameCache.Add (typeof (uint), new TypeData (typeof (uint), "unsignedInt", true));
34 nameCache.Add (typeof (long), new TypeData (typeof (long), "long", true));
35 nameCache.Add (typeof (ulong), new TypeData (typeof (ulong), "unsignedLong", true));
36 nameCache.Add (typeof (float), new TypeData (typeof (float), "float", true));
37 nameCache.Add (typeof (double), new TypeData (typeof (double), "double", true));
38 nameCache.Add (typeof (DateTime), new TypeData (typeof (DateTime), "dateTime", true)); // TODO: timeInstant, Xml date, xml time
39 nameCache.Add (typeof (Guid), new TypeData (typeof (Guid), "guid", true));
40 nameCache.Add (typeof (decimal), new TypeData (typeof (decimal), "decimal", true));
41 nameCache.Add (typeof (XmlQualifiedName), new TypeData (typeof (XmlQualifiedName), "QName", true));
42 nameCache.Add (typeof (string), new TypeData (typeof (string), "string", true));
43 nameCache.Add (typeof (byte), new TypeData (typeof (byte), "unsignedByte", true));
44 nameCache.Add (typeof (sbyte), new TypeData (typeof (sbyte), "byte", true));
45 nameCache.Add (typeof (char), new TypeData (typeof (char), "char", true));
46 nameCache.Add (typeof (object), new TypeData (typeof (object), "anyType", false));
47 nameCache.Add (typeof (byte[]), new TypeData (typeof (byte[]), "base64Binary", true));
48 nameCache.Add (typeof (XmlNode), new TypeData (typeof (XmlNode), "XmlNode", false));
49 nameCache.Add (typeof (XmlElement), new TypeData (typeof (XmlElement), "XmlElement", false));
50 nameCache.Add (typeof (Uri), new TypeData (typeof (Uri), "anyURI", true));
51 nameCache.Add (typeof (TimeSpan), new TypeData (typeof (TimeSpan), "duration", true));
53 primitiveTypes = new Hashtable();
54 ICollection types = nameCache.Values;
55 foreach (TypeData td in types)
56 primitiveTypes.Add (td.XmlType, td);
58 // Additional XSD types
60 primitiveTypes.Add ("date", new TypeData (typeof (DateTime), "date", true)); // TODO: timeInstant
61 primitiveTypes.Add ("time", new TypeData (typeof (DateTime), "time", true));
62 primitiveTypes.Add ("timePeriod", new TypeData (typeof (DateTime), "timePeriod", true));
63 primitiveTypes.Add ("gDay", new TypeData (typeof (string), "gDay", true));
64 primitiveTypes.Add ("gMonthDay", new TypeData (typeof (string), "gMonthDay", true));
65 primitiveTypes.Add ("gYear", new TypeData (typeof (string), "gYear", true));
66 primitiveTypes.Add ("gYearMonth", new TypeData (typeof (string), "gYearMonth", true));
67 primitiveTypes.Add ("month", new TypeData (typeof (DateTime), "month", true));
68 primitiveTypes.Add ("NMTOKEN", new TypeData (typeof (string), "NMTOKEN", true));
69 primitiveTypes.Add ("NMTOKENS", new TypeData (typeof (string), "NMTOKENS", true));
70 primitiveTypes.Add ("Name", new TypeData (typeof (string), "Name", true));
71 primitiveTypes.Add ("NCName", new TypeData (typeof (string), "NCName", true));
72 primitiveTypes.Add ("language", new TypeData (typeof (string), "language", true));
73 primitiveTypes.Add ("integer", new TypeData (typeof (string), "integer", true));
74 primitiveTypes.Add ("positiveInteger", new TypeData (typeof (string), "positiveInteger", true));
75 primitiveTypes.Add ("nonPositiveInteger", new TypeData (typeof (string), "nonPositiveInteger", true));
76 primitiveTypes.Add ("negativeInteger", new TypeData (typeof (string), "negativeInteger", true));
77 primitiveTypes.Add ("nonNegativeInteger", new TypeData (typeof (string), "nonNegativeInteger", true));
78 primitiveTypes.Add ("ENTITIES", new TypeData (typeof (string), "ENTITIES", true));
79 primitiveTypes.Add ("ENTITY", new TypeData (typeof (string), "ENTITY", true));
80 primitiveTypes.Add ("hexBinary", new TypeData (typeof (byte[]), "hexBinary", true));
81 primitiveTypes.Add ("ID", new TypeData (typeof (string), "ID", true));
82 primitiveTypes.Add ("IDREF", new TypeData (typeof (string), "IDREF", true));
83 primitiveTypes.Add ("IDREFS", new TypeData (typeof (string), "IDREFS", true));
84 primitiveTypes.Add ("NOTATION", new TypeData (typeof (string), "NOTATION", true));
85 primitiveTypes.Add ("token", new TypeData (typeof (string), "token", true));
86 primitiveTypes.Add ("normalizedString", new TypeData (typeof (string), "normalizedString", true));
89 public static TypeData GetTypeData (Type type)
91 return GetTypeData (type, null);
94 public static TypeData GetTypeData (Type type, string xmlDataType)
96 if ((xmlDataType != null) && (xmlDataType.Length != 0)) return GetPrimitiveTypeData (xmlDataType);
98 TypeData typeData = nameCache[type] as TypeData;
99 if (typeData != null) return typeData;
103 string sufix = GetTypeData (type.GetElementType ()).XmlType;
104 name = GetArrayName (sufix);
109 typeData = new TypeData (type, name, false);
110 nameCache[type] = typeData;
114 public static bool IsPrimitive (Type type)
116 return GetTypeData (type).SchemaType == SchemaTypes.Primitive;
119 public static TypeData GetPrimitiveTypeData (string typeName)
121 TypeData td = (TypeData) primitiveTypes[typeName];
122 if (td == null) throw new NotSupportedException ("Data type '" + typeName + "' not supported");
126 public static TypeData GetDefaultPrimitiveTypeData (TypeData primType)
128 // Returns the TypeData that is mapped by default to the clr type
129 // that primType represents
131 if (primType.SchemaType == SchemaTypes.Primitive)
133 TypeData newPrim = GetTypeData (primType.Type);
134 if (newPrim != primType) return newPrim;
139 public static bool IsDefaultPrimitiveTpeData (TypeData primType)
141 return GetDefaultPrimitiveTypeData (primType) == primType;
144 public static TypeData CreateCustomType (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
146 TypeData td = new TypeData (typeName, fullTypeName, xmlType, schemaType, listItemTypeData);
150 public static string GetArrayName (string elemName)
152 return "ArrayOf" + Char.ToUpper (elemName [0]) + elemName.Substring (1);
155 public static string GetArrayName (string elemName, int dimensions)
157 string aname = GetArrayName (elemName);
158 for ( ; dimensions > 1; dimensions--)
159 aname = "ArrayOf" + aname;
163 public static void ParseArrayType (string arrayType, out string type, out string ns, out string dimensions)
165 int i = arrayType.LastIndexOf (":");
166 if (i == -1) ns = "";
167 else ns = arrayType.Substring (0,i);
169 int j = arrayType.IndexOf ("[", i+1);
170 if (j == -1) throw new InvalidOperationException ("Cannot parse WSDL array type: " + arrayType);
171 type = arrayType.Substring (i+1, j-i-1);
172 dimensions = arrayType.Substring (j);