2 // XmlFormatterDeserializer.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
33 using System.Reflection;
34 using System.Runtime.Serialization.Formatters.Binary;
36 using System.Xml.Schema;
38 using QName = System.Xml.XmlQualifiedName;
40 namespace System.Runtime.Serialization
42 internal class XmlFormatterDeserializer
44 KnownTypeCollection types;
45 IDataContractSurrogate surrogate;
46 DataContractResolver resolver; // new in 4.0.
47 // 3.5 SP1 supports deserialization by reference (id->obj).
48 // Though unlike XmlSerializer, it does not support forward-
49 // reference resolution i.e. a referenced object must appear
50 // before any references to it.
51 Hashtable references = new Hashtable ();
53 public static object Deserialize (XmlReader reader, Type type,
54 KnownTypeCollection knownTypes, IDataContractSurrogate surrogate, DataContractResolver resolver,
55 string name, string ns, bool verifyObjectName)
57 reader.MoveToContent ();
59 if (reader.NodeType != XmlNodeType.Element ||
60 reader.LocalName != name ||
61 reader.NamespaceURI != ns)
62 throw new SerializationException (String.Format ("Expected element '{0}' in namespace '{1}', but found {2} node '{3}' in namespace '{4}'", name, ns, reader.NodeType, reader.LocalName, reader.NamespaceURI));
63 // Verify (knownTypes, type, name, ns, reader);
64 return new XmlFormatterDeserializer (knownTypes, surrogate, resolver).Deserialize (type, reader);
67 // Verify the top element name and namespace.
68 private static void Verify (KnownTypeCollection knownTypes, Type type, string name, string Namespace, XmlReader reader)
70 QName graph_qname = new QName (reader.Name, reader.NamespaceURI);
71 if (graph_qname.Name == name && graph_qname.Namespace == Namespace)
74 // <BClass .. i:type="EClass" >..</BClass>
75 // Expecting type EClass : allowed
76 // See test Serialize1b, and Serialize1c (for
79 // Run through inheritance heirarchy ..
80 for (Type baseType = type; baseType != null; baseType = baseType.BaseType)
81 if (knownTypes.GetQName (baseType) == graph_qname)
84 QName typeQName = knownTypes.GetQName (type);
85 throw new SerializationException (String.Format (
86 "Expecting element '{0}' from namespace '{1}'. Encountered 'Element' with name '{2}', namespace '{3}'",
87 typeQName.Name, typeQName.Namespace, graph_qname.Name, graph_qname.Namespace));
90 private XmlFormatterDeserializer (
91 KnownTypeCollection knownTypes,
92 IDataContractSurrogate surrogate,
93 DataContractResolver resolver)
95 this.types = knownTypes;
96 this.surrogate = surrogate;
97 this.resolver = resolver;
100 public Hashtable References {
101 get { return references; }
104 // This method handles z:Ref, xsi:nil and primitive types, and then delegates to DeserializeByMap() for anything else.
105 public object Deserialize (Type type, XmlReader reader)
107 QName graph_qname = types.GetQName (type);
108 string itype = reader.GetAttribute ("type", XmlSchema.InstanceNamespace);
110 string[] parts = itype.Split (':');
111 if (parts.Length > 1)
112 graph_qname = new QName (parts [1], reader.LookupNamespace (reader.NameTable.Get (parts[0])));
114 graph_qname = new QName (itype, reader.NamespaceURI);
117 string label = reader.GetAttribute ("Ref", KnownTypeCollection.MSSimpleNamespace);
119 object o = references [label];
121 throw new SerializationException (String.Format ("Deserialized object with reference Id '{0}' was not found", label));
126 bool isNil = reader.GetAttribute ("nil", XmlSchema.InstanceNamespace) == "true";
130 if (!type.IsValueType)
132 else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>))
135 throw new SerializationException (String.Format ("Value type {0} cannot be null.", type));
138 if (KnownTypeCollection.GetPrimitiveTypeFromName (graph_qname.Name) != null) {
139 string id = reader.GetAttribute ("Id", KnownTypeCollection.MSSimpleNamespace);
142 if (reader.IsEmptyElement) {
143 reader.Read (); // advance
144 if (type.IsValueType)
145 return Activator.CreateInstance (type);
147 // FIXME: Workaround for creating empty objects of the correct type.
148 value = String.Empty;
151 value = reader.ReadElementContentAsString ();
152 object ret = KnownTypeCollection.PredefinedTypeStringToObject (value, graph_qname.Name, reader);
154 if (references.ContainsKey (id))
155 throw new InvalidOperationException (String.Format ("Object with Id '{0}' already exists as '{1}'", id, references [id]));
156 references.Add (id, ret);
161 return DeserializeByMap (graph_qname, type, reader);
164 object DeserializeByMap (QName name, Type type, XmlReader reader)
166 SerializationMap map = types.FindUserMap (name);
167 if (map == null && (name.Name.StartsWith ("ArrayOf", StringComparison.Ordinal) ||
168 name.Namespace == KnownTypeCollection.MSArraysNamespace ||
169 name.Namespace.StartsWith (KnownTypeCollection.DefaultClrNamespaceBase, StringComparison.Ordinal))) {
170 var it = GetTypeFromNamePair (name.Name, name.Namespace);
171 types.TryRegister (it);
172 map = types.FindUserMap (name);
175 throw new SerializationException (String.Format ("Unknown type {0} is used for DataContract with reference of name {1}. Any derived types of a data contract or a data member should be added to KnownTypes.", type, name));
177 return map.DeserializeObject (reader, this);
180 Type GetTypeFromNamePair (string name, string ns)
182 Type p = KnownTypeCollection.GetPrimitiveTypeFromName (name); // FIXME: namespace?
185 bool makeArray = false;
186 if (name.StartsWith ("ArrayOf", StringComparison.Ordinal)) {
187 name = name.Substring (7); // strip "ArrayOf"
188 if (ns == KnownTypeCollection.MSArraysNamespace)
189 return GetTypeFromNamePair (name, String.Empty).MakeArrayType ();
193 string dnsb = KnownTypeCollection.DefaultClrNamespaceBase;
194 string clrns = ns.StartsWith (dnsb, StringComparison.Ordinal) ? ns.Substring (dnsb.Length) : ns;
196 foreach (var ass in AppDomain.CurrentDomain.GetAssemblies ()) {
201 types = ass.GetTypes ();
202 } catch (System.Reflection.ReflectionTypeLoadException rtle) {
206 types = ass.GetTypes ();
211 foreach (var t in types) {
212 // there can be null entries or exception throw to access the attribute -
213 // at least when some referenced assemblies could not be loaded (affects moonlight)
218 var dca = t.GetCustomAttribute<DataContractAttribute> (true);
219 if (dca != null && dca.Name == name && dca.Namespace == ns)
220 return makeArray ? t.MakeArrayType () : t;
222 catch (TypeLoadException tle) {
223 Console.Error.WriteLine (tle);
226 catch (FileNotFoundException fnfe) {
227 Console.Error.WriteLine (fnfe);
231 if (clrns != null && t.Name == name && t.Namespace == clrns)
232 return makeArray ? t.MakeArrayType () : t;
235 throw new XmlException (String.Format ("Type not found; name: {0}, namespace: {1}", name, ns));