2 // XmlSerializationReaderInterpreter.cs:
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // (C) 2002, 2003 Ximian, Inc. http://www.ximian.com
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Reflection;
33 using System.Collections;
35 namespace System.Xml.Serialization
37 internal class XmlSerializationReaderInterpreter: XmlSerializationReader
40 SerializationFormat _format;
41 static readonly XmlQualifiedName AnyType = new XmlQualifiedName("anyType", System.Xml.Schema.XmlSchema.Namespace);
42 static readonly object [] empty_array = new object [0];
44 public XmlSerializationReaderInterpreter(XmlMapping typeMap)
47 _format = typeMap.Format;
50 protected override void InitCallbacks ()
52 ArrayList maps = _typeMap.RelatedMaps;
55 foreach (XmlTypeMapping map in maps)
57 if (map.TypeData.SchemaType == SchemaTypes.Class || map.TypeData.SchemaType == SchemaTypes.Enum)
59 ReaderCallbackInfo info = new ReaderCallbackInfo (this, map);
60 AddReadCallback (map.XmlType, map.Namespace, map.TypeData.Type, new XmlSerializationReadCallback (info.ReadObject));
66 protected override void InitIDs ()
70 protected XmlTypeMapping GetTypeMap (Type type)
72 ArrayList maps = _typeMap.RelatedMaps;
75 foreach (XmlTypeMapping map in maps)
76 if (map.TypeData.Type == type) return map;
78 throw new InvalidOperationException ("Type " + type + " not mapped");
81 public object ReadRoot ()
83 Reader.MoveToContent();
84 if (_typeMap is XmlTypeMapping)
86 if (_format == SerializationFormat.Literal)
87 return ReadRoot ((XmlTypeMapping)_typeMap);
89 return ReadEncodedObject ((XmlTypeMapping)_typeMap);
92 return ReadMessage ((XmlMembersMapping)_typeMap);
95 object ReadEncodedObject (XmlTypeMapping typeMap)
98 throw new NotSupportedException ();
101 Reader.MoveToContent();
102 if (Reader.NodeType == System.Xml.XmlNodeType.Element)
104 if (Reader.LocalName == typeMap.ElementName && Reader.NamespaceURI == typeMap.Namespace)
105 ob = ReadReferencedElement();
107 throw CreateUnknownNodeException();
112 ReadReferencedElements();
117 protected virtual object ReadMessage (XmlMembersMapping typeMap)
120 throw new NotSupportedException ();
122 object[] parameters = new object[typeMap.Count];
124 if (typeMap.HasWrapperElement)
126 // bug #79988: out parameters need to be initialized if they
128 ArrayList members = ((ClassMap) typeMap.ObjectMap).AllMembers;
129 for (int n = 0; n < members.Count; n++) {
130 XmlTypeMapMember mem = (XmlTypeMapMember) members [n];
131 if (!mem.IsReturnValue && mem.TypeData.IsValueType)
132 SetMemberValueFromAttr (mem, parameters, CreateInstance (
133 mem.TypeData.Type), true);
136 if (_format == SerializationFormat.Encoded)
138 while (Reader.NodeType == System.Xml.XmlNodeType.Element)
140 string root = Reader.GetAttribute ("root", XmlSerializer.EncodingNamespace);
141 if (root == null || System.Xml.XmlConvert.ToBoolean(root)) break;
142 ReadReferencedElement ();
143 Reader.MoveToContent ();
147 while (Reader.NodeType != System.Xml.XmlNodeType.EndElement &&
148 // it could be an empty root element
149 Reader.ReadState == ReadState.Interactive)
151 if (Reader.IsStartElement(typeMap.ElementName, typeMap.Namespace)
152 || _format == SerializationFormat.Encoded)
154 ReadAttributeMembers ((ClassMap)typeMap.ObjectMap, parameters, true);
155 if (Reader.IsEmptyElement) {
157 Reader.MoveToContent();
160 Reader.ReadStartElement();
161 ReadMembers ((ClassMap)typeMap.ObjectMap, parameters, true, false);
168 Reader.MoveToContent();
172 ReadMembers ((ClassMap)typeMap.ObjectMap, parameters, true, _format == SerializationFormat.Encoded);
174 if (_format == SerializationFormat.Encoded)
175 ReadReferencedElements();
181 object ReadRoot (XmlTypeMapping rootMap)
183 if (rootMap.TypeData.SchemaType == SchemaTypes.XmlNode)
185 return ReadXmlNodeElement (rootMap, true);
189 if (Reader.LocalName != rootMap.ElementName || Reader.NamespaceURI != rootMap.Namespace)
190 throw CreateUnknownNodeException();
192 return ReadObject (rootMap, rootMap.IsNullable, true);
197 protected virtual object ReadObject (XmlTypeMapping typeMap, bool isNullable, bool checkType)
199 switch (typeMap.TypeData.SchemaType)
201 case SchemaTypes.Class: return ReadClassInstance (typeMap, isNullable, checkType);
202 case SchemaTypes.Array: return ReadListElement (typeMap, isNullable, null, true);
203 case SchemaTypes.XmlNode: return ReadXmlNodeElement (typeMap, isNullable);
204 case SchemaTypes.Primitive: return ReadPrimitiveElement (typeMap, isNullable);
205 case SchemaTypes.Enum: return ReadEnumElement (typeMap, isNullable);
206 case SchemaTypes.XmlSerializable: return ReadXmlSerializableElement (typeMap, isNullable);
207 default: throw new Exception ("Unsupported map type");
211 protected virtual object ReadClassInstance (XmlTypeMapping typeMap, bool isNullable, bool checkType)
213 if (isNullable && ReadNull()) return null;
217 System.Xml.XmlQualifiedName t = GetXsiType();
220 XmlTypeMapping realMap = typeMap.GetRealElementMap (t.Name, t.Namespace);
221 if (realMap == null) {
222 if (typeMap.TypeData.Type == typeof(object))
223 return ReadTypedPrimitive (t);
225 throw CreateUnknownTypeException((System.Xml.XmlQualifiedName)t);
227 if (realMap != typeMap)
228 return ReadObject (realMap, false, false);
230 else if (typeMap.TypeData.Type == typeof(object))
231 return ReadTypedPrimitive (AnyType);
234 object ob = CreateInstance (typeMap.TypeData.Type, true);
236 Reader.MoveToElement();
237 bool isEmpty = Reader.IsEmptyElement;
238 ReadClassInstanceMembers (typeMap, ob);
240 if (isEmpty) Reader.Skip();
241 else ReadEndElement();
246 protected virtual void ReadClassInstanceMembers (XmlTypeMapping typeMap, object ob)
248 ReadMembers ((ClassMap) typeMap.ObjectMap, ob, false, false);
251 void ReadAttributeMembers (ClassMap map, object ob, bool isValueList)
253 XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
254 int anyAttributeIndex = 0;
255 object anyAttributeArray = null;
257 while (Reader.MoveToNextAttribute())
259 XmlTypeMapMemberAttribute member = map.GetAttribute (Reader.LocalName, Reader.NamespaceURI);
263 SetMemberValue (member, ob, GetValueFromXmlString (Reader.Value, member.TypeData, member.MappedType), isValueList);
265 else if (IsXmlnsAttribute(Reader.Name))
267 // If the map has NamespaceDeclarations,
268 // then store this xmlns to the given member.
269 // If the instance doesn't exist, then create.
270 if (map.NamespaceDeclarations != null) {
271 XmlSerializerNamespaces nss = this.GetMemberValue (map.NamespaceDeclarations, ob, isValueList) as XmlSerializerNamespaces;
273 nss = new XmlSerializerNamespaces ();
274 SetMemberValue (map.NamespaceDeclarations, ob, nss, isValueList);
276 if (Reader.Prefix == "xmlns")
277 nss.Add (Reader.LocalName, Reader.Value);
279 nss.Add ("", Reader.Value);
283 else if (anyAttrMember != null)
285 XmlAttribute attr = (XmlAttribute) Document.ReadNode(Reader);
286 ParseWsdlArrayType (attr);
287 AddListValue (anyAttrMember.TypeData, ref anyAttributeArray, anyAttributeIndex++, attr, true);
290 ProcessUnknownAttribute(ob);
295 if (anyAttrMember != null)
297 anyAttributeArray = ShrinkArray ((Array)anyAttributeArray, anyAttributeIndex, anyAttrMember.TypeData.Type.GetElementType(), true);
298 SetMemberValue (anyAttrMember, ob, anyAttributeArray, isValueList);
301 Reader.MoveToElement ();
304 void ReadMembers (ClassMap map, object ob, bool isValueList, bool readBySoapOrder)
307 ReadAttributeMembers (map, ob, isValueList);
311 Reader.MoveToElement();
312 if (Reader.IsEmptyElement) {
313 SetListMembersDefaults (map, ob, isValueList);
317 Reader.ReadStartElement();
322 bool[] readFlag = new bool[(map.ElementMembers != null) ? map.ElementMembers.Count : 0];
324 bool hasAnyReturnMember = (isValueList && _format == SerializationFormat.Encoded && map.ReturnMember != null);
326 Reader.MoveToContent();
328 int[] indexes = null;
329 object[] flatLists = null;
330 object[] flatListsChoices = null;
335 if (readBySoapOrder) {
336 if (map.ElementMembers != null) maxInd = map.ElementMembers.Count;
340 maxInd = int.MaxValue;
342 if (map.FlatLists != null)
344 indexes = new int[map.FlatLists.Count];
345 flatLists = new object[map.FlatLists.Count];
346 foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) {
347 if (IsReadOnly (mem, mem.TypeData, ob, isValueList))
348 flatLists [mem.FlatArrayIndex] = mem.GetValue (ob);
349 else if (mem.TypeData.Type.IsArray) {
350 flatLists [mem.FlatArrayIndex] = InitializeList (mem.TypeData);
353 object list = mem.GetValue (ob);
355 list = InitializeList (mem.TypeData);
356 SetMemberValue (mem, ob, list, isValueList);
358 flatLists [mem.FlatArrayIndex] = list;
361 if (mem.ChoiceMember != null) {
362 if (flatListsChoices == null)
363 flatListsChoices = new object [map.FlatLists.Count];
364 flatListsChoices [mem.FlatArrayIndex] = InitializeList (mem.ChoiceTypeData);
369 if (_format == SerializationFormat.Encoded && map.ElementMembers != null)
371 FixupCallbackInfo info = new FixupCallbackInfo (this, map, isValueList);
372 fixup = new Fixup(ob, new XmlSerializationFixupCallback(info.FixupMembers), map.ElementMembers.Count);
376 XmlTypeMapMember previousMember = null;
377 while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && (ind < maxInd - 1))
379 if (Reader.NodeType == System.Xml.XmlNodeType.Element)
381 XmlTypeMapElementInfo info;
383 if (readBySoapOrder) {
384 info = map.GetElement (Reader.LocalName, Reader.NamespaceURI);
386 else if (hasAnyReturnMember) {
387 info = (XmlTypeMapElementInfo) ((XmlTypeMapMemberElement)map.ReturnMember).ElementInfo[0];
388 hasAnyReturnMember = false;
391 if (map.IsOrderDependentMap) {
392 info = map.GetElement (Reader.LocalName, Reader.NamespaceURI);
395 info = map.GetElement (Reader.LocalName, Reader.NamespaceURI, -1);
398 if (info != null && !readFlag[info.Member.Index] )
400 if (info.Member != previousMember)
403 previousMember = info.Member;
406 if (readBySoapOrder && info.ExplicitOrder != ind)
407 throw new InvalidOperationException(string.Format("Element '{0}' has wrong order in sequence (expected - {1}, actual - {2}", Reader.LocalName, info.ExplicitOrder, ind));
410 if (info.Member.GetType() == typeof (XmlTypeMapMemberList))
412 if (_format == SerializationFormat.Encoded && info.MultiReferenceType)
414 object list = ReadReferencingElement (out fixup.Ids[info.Member.Index]);
415 if (fixup.Ids[info.Member.Index] == null) // Already read
417 if (IsReadOnly (info.Member, info.TypeData, ob, isValueList)) throw CreateReadOnlyCollectionException (info.TypeData.FullTypeName);
418 else SetMemberValue (info.Member, ob, list, isValueList);
420 else if (!info.MappedType.TypeData.Type.IsArray)
422 if (IsReadOnly (info.Member, info.TypeData, ob, isValueList))
423 list = GetMemberValue (info.Member, ob, isValueList);
425 list = CreateList (info.MappedType.TypeData.Type);
426 SetMemberValue (info.Member, ob, list, isValueList);
428 AddFixup (new CollectionFixup (list, new XmlSerializationCollectionFixupCallback (FillList), fixup.Ids[info.Member.Index]));
429 fixup.Ids[info.Member.Index] = null; // The member already has the value, no further fix needed.
434 if (IsReadOnly (info.Member, info.TypeData, ob, isValueList)) {
435 ReadListElement (info.MappedType, info.IsNullable, GetMemberValue (info.Member, ob, isValueList), false);
436 } else if (info.MappedType.TypeData.Type.IsArray) {
437 object list = ReadListElement (info.MappedType, info.IsNullable, null, true);
438 if (list != null || info.IsNullable)
439 SetMemberValue (info.Member, ob, list, isValueList);
441 // If the member already has a list, reuse that list. No need to create a new one.
442 object list = GetMemberValue (info.Member, ob, isValueList);
444 list = CreateList (info.MappedType.TypeData.Type);
445 SetMemberValue (info.Member, ob, list, isValueList);
447 ReadListElement (info.MappedType, info.IsNullable, list, true);
450 readFlag[info.Member.Index] = true;
452 else if (info.Member.GetType() == typeof (XmlTypeMapMemberFlatList))
454 XmlTypeMapMemberFlatList mem = (XmlTypeMapMemberFlatList)info.Member;
455 AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadObjectElement (info), !IsReadOnly (info.Member, info.TypeData, ob, isValueList));
456 if (mem.ChoiceMember != null) {
457 AddListValue (mem.ChoiceTypeData, ref flatListsChoices [mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]-1, info.ChoiceValue, true);
460 else if (info.Member.GetType() == typeof (XmlTypeMapMemberAnyElement))
462 XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement)info.Member;
463 if (mem.TypeData.IsListType) AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode (mem.TypeData.ListItemTypeData, false), true);
464 else SetMemberValue (mem, ob, ReadXmlNode (mem.TypeData, false), isValueList);
466 else if (info.Member.GetType() == typeof(XmlTypeMapMemberElement))
469 readFlag[info.Member.Index] = true;
470 if (_format == SerializationFormat.Encoded)
472 if (info.Member.TypeData.SchemaType != SchemaTypes.Primitive)
473 val = ReadReferencingElement (out fixup.Ids[info.Member.Index]);
475 val = ReadReferencingElement (info.Member.TypeData.XmlType, System.Xml.Schema.XmlSchema.Namespace, out fixup.Ids[info.Member.Index]);
477 if (info.MultiReferenceType) {
478 if (fixup.Ids[info.Member.Index] == null) // already read
479 SetMemberValue (info.Member, ob, val, isValueList);
481 else if (val != null)
482 SetMemberValue (info.Member, ob, val, isValueList);
485 SetMemberValue (info.Member, ob, ReadObjectElement (info), isValueList);
486 if (info.ChoiceValue != null) {
487 XmlTypeMapMemberElement imem = (XmlTypeMapMemberElement) info.Member;
488 imem.SetChoice (ob, info.ChoiceValue);
493 throw new InvalidOperationException ("Unknown member type");
495 else if (map.DefaultAnyElementMember != null)
497 XmlTypeMapMemberAnyElement mem = map.DefaultAnyElementMember;
498 if (mem.TypeData.IsListType) AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode (mem.TypeData.ListItemTypeData, false), true);
499 else SetMemberValue (mem, ob, ReadXmlNode (mem.TypeData, false), isValueList);
502 ProcessUnknownElement(ob);
504 else if ((Reader.NodeType == System.Xml.XmlNodeType.Text || Reader.NodeType == System.Xml.XmlNodeType.CDATA) && map.XmlTextCollector != null)
506 if (map.XmlTextCollector is XmlTypeMapMemberExpandable)
508 XmlTypeMapMemberExpandable mem = (XmlTypeMapMemberExpandable)map.XmlTextCollector;
509 XmlTypeMapMemberFlatList flatl = mem as XmlTypeMapMemberFlatList;
510 TypeData itype = (flatl == null) ? mem.TypeData.ListItemTypeData : flatl.ListMap.FindTextElement().TypeData;
512 object val = (itype.Type == typeof (string)) ? (object) Reader.ReadString() : (object) ReadXmlNode (itype, false);
513 AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, val, true);
517 XmlTypeMapMemberElement mem = (XmlTypeMapMemberElement) map.XmlTextCollector;
518 XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) mem.ElementInfo [0];
519 if (info.TypeData.Type == typeof (string))
520 SetMemberValue (mem, ob, ReadString ((string) GetMemberValue (mem, ob, isValueList)), isValueList);
522 SetMemberValue (mem, ob, GetValueFromXmlString (Reader.ReadString(), info.TypeData, info.MappedType), isValueList);
529 Reader.MoveToContent();
532 if (flatLists != null)
534 foreach (XmlTypeMapMemberExpandable mem in map.FlatLists)
536 Object list = flatLists[mem.FlatArrayIndex];
537 if (mem.TypeData.Type.IsArray)
538 list = ShrinkArray ((Array)list, indexes[mem.FlatArrayIndex], mem.TypeData.Type.GetElementType(), true);
539 if (!IsReadOnly (mem, mem.TypeData, ob, isValueList) && mem.TypeData.Type.IsArray)
540 SetMemberValue (mem, ob, list, isValueList);
544 if (flatListsChoices != null)
546 foreach (XmlTypeMapMemberExpandable mem in map.FlatLists)
548 Object list = flatListsChoices[mem.FlatArrayIndex];
549 if (list == null) continue;
550 list = ShrinkArray ((Array)list, indexes[mem.FlatArrayIndex], mem.ChoiceTypeData.Type.GetElementType(), true);
551 XmlTypeMapMember.SetValue (ob, mem.ChoiceMember, list);
554 SetListMembersDefaults (map, ob, isValueList);
557 void SetListMembersDefaults (ClassMap map, object ob, bool isValueList)
559 if (map.ListMembers != null)
561 ArrayList members = map.ListMembers;
562 for (int n=0; n<members.Count; n++) {
563 XmlTypeMapMember mem = (XmlTypeMapMember) members[n];
564 if (IsReadOnly (mem, mem.TypeData, ob, isValueList))
566 if (GetMemberValue (mem, ob, isValueList) == null)
567 SetMemberValue (mem, ob, InitializeList (mem.TypeData), isValueList);
572 internal void FixupMembers (ClassMap map, object obfixup, bool isValueList)
574 Fixup fixup = (Fixup)obfixup;
575 ICollection members = map.ElementMembers;
576 string[] ids = fixup.Ids;
577 foreach (XmlTypeMapMember member in members)
579 if (ids[member.Index] != null)
580 SetMemberValue (member, fixup.Source, GetTarget(ids[member.Index]), isValueList);
584 protected virtual void ProcessUnknownAttribute (object target)
586 UnknownNode (target);
589 protected virtual void ProcessUnknownElement (object target)
591 UnknownNode (target);
594 bool IsReadOnly (XmlTypeMapMember member, TypeData memType, object ob, bool isValueList)
596 if (isValueList) return !memType.HasPublicConstructor;
597 else return member.IsReadOnly (ob.GetType()) || !memType.HasPublicConstructor;
600 void SetMemberValue (XmlTypeMapMember member, object ob, object value, bool isValueList)
603 ((object[])ob)[member.GlobalIndex] = value;
605 member.SetValue (ob, value);
606 if (member.IsOptionalValueType)
607 member.SetValueSpecified (ob, true);
610 void SetMemberValueFromAttr (XmlTypeMapMember member, object ob, object value, bool isValueList)
612 // Enumeration values specified in custom attributes are stored as integer
613 // values if the custom attribute property is of type object. So, it is
614 // necessary to convert to the enum type before asigning the value to the field.
616 if (member.TypeData.Type.IsEnum)
617 value = Enum.ToObject (member.TypeData.Type, value);
618 SetMemberValue (member, ob, value, isValueList);
621 object GetMemberValue (XmlTypeMapMember member, object ob, bool isValueList)
623 if (isValueList) return ((object[])ob)[member.GlobalIndex];
624 else return member.GetValue (ob);
627 object ReadObjectElement (XmlTypeMapElementInfo elem)
629 switch (elem.TypeData.SchemaType)
631 case SchemaTypes.XmlNode:
632 return ReadXmlNode (elem.TypeData, true);
634 case SchemaTypes.Primitive:
635 case SchemaTypes.Enum:
636 return ReadPrimitiveValue (elem);
638 case SchemaTypes.Array:
639 return ReadListElement (elem.MappedType, elem.IsNullable, null, true);
641 case SchemaTypes.Class:
642 return ReadObject (elem.MappedType, elem.IsNullable, true);
644 case SchemaTypes.XmlSerializable:
645 object ob = CreateInstance (elem.TypeData.Type, true);
646 return ReadSerializable ((IXmlSerializable)ob);
649 throw new NotSupportedException ("Invalid value type");
653 object ReadPrimitiveValue (XmlTypeMapElementInfo elem)
655 if (elem.TypeData.Type == typeof (XmlQualifiedName)) {
656 if (elem.IsNullable) return ReadNullableQualifiedName ();
657 else return ReadElementQualifiedName ();
659 else if (elem.IsNullable)
660 return GetValueFromXmlString (ReadNullableString (), elem.TypeData, elem.MappedType);
662 return GetValueFromXmlString (Reader.ReadElementString (), elem.TypeData, elem.MappedType);
665 object GetValueFromXmlString (string value, TypeData typeData, XmlTypeMapping typeMap)
667 if (typeData.SchemaType == SchemaTypes.Array)
668 return ReadListString (typeMap, value);
669 else if (typeData.SchemaType == SchemaTypes.Enum)
670 return GetEnumValue (typeMap, value);
671 else if (typeData.Type == typeof (XmlQualifiedName))
672 return ToXmlQualifiedName (value);
674 return XmlCustomFormatter.FromXmlString (typeData, value);
677 object ReadListElement (XmlTypeMapping typeMap, bool isNullable, object list, bool canCreateInstance)
679 Type listType = typeMap.TypeData.Type;
680 ListMap listMap = (ListMap)typeMap.ObjectMap;
682 if (listType.IsArray && ReadNull()) return null;
685 if (canCreateInstance && typeMap.TypeData.HasPublicConstructor) list = CreateList (listType);
686 else throw CreateReadOnlyCollectionException (typeMap.TypeFullName);
689 if (Reader.IsEmptyElement) {
691 if (listType.IsArray)
692 list = ShrinkArray ((Array)list, 0, listType.GetElementType(), false);
697 Reader.ReadStartElement();
698 Reader.MoveToContent();
700 while (Reader.NodeType != System.Xml.XmlNodeType.EndElement)
702 if (Reader.NodeType == System.Xml.XmlNodeType.Element)
704 XmlTypeMapElementInfo elemInfo = listMap.FindElement (Reader.LocalName, Reader.NamespaceURI);
705 if (elemInfo != null)
706 AddListValue (typeMap.TypeData, ref list, index++, ReadObjectElement (elemInfo), false);
713 Reader.MoveToContent();
717 if (listType.IsArray)
718 list = ShrinkArray ((Array)list, index, listType.GetElementType(), false);
723 object ReadListString (XmlTypeMapping typeMap, string values)
725 Type listType = typeMap.TypeData.Type;
726 ListMap listMap = (ListMap)typeMap.ObjectMap;
727 values = values.Trim ();
729 if (values == string.Empty)
731 return Array.CreateInstance (listType.GetElementType(), 0);
734 string[] valueArray = values.Split (' ');
735 Array list = Array.CreateInstance (listType.GetElementType(), valueArray.Length);
737 XmlTypeMapElementInfo info = (XmlTypeMapElementInfo)listMap.ItemInfo[0];
739 for (int index = 0; index < valueArray.Length; index++)
740 list.SetValue (GetValueFromXmlString (valueArray[index], info.TypeData, info.MappedType), index);
745 void AddListValue (TypeData listType, ref object list, int index, object value, bool canCreateInstance)
747 Type type = listType.Type;
750 list = EnsureArrayIndex ((Array)list, index, type.GetElementType());
751 ((Array)list).SetValue (value, index);
753 else // Must be IEnumerable
756 if (canCreateInstance) list = CreateInstance (type, true);
757 else throw CreateReadOnlyCollectionException (type.FullName);
760 MethodInfo mi = type.GetMethod ("Add", new Type[] {listType.ListItemType} );
761 mi.Invoke (list, new object[] { value });
765 static object CreateInstance (Type type, bool nonPublic)
768 return Activator.CreateInstance (type); // always false
770 return Activator.CreateInstance (type, nonPublic);
774 object CreateInstance (Type type)
776 return Activator.CreateInstance (type, empty_array);
779 object CreateList (Type listType)
781 if (listType.IsArray)
782 return EnsureArrayIndex (null, 0, listType.GetElementType());
784 return CreateInstance (listType, true);
787 object InitializeList (TypeData listType)
789 if (listType.Type.IsArray)
792 return CreateInstance (listType.Type, true);
795 void FillList (object list, object items)
797 CopyEnumerableList (items, list);
800 void CopyEnumerableList (object source, object dest)
802 if (dest == null) throw CreateReadOnlyCollectionException (source.GetType().FullName);
804 object[] param = new object[1];
805 MethodInfo mi = dest.GetType().GetMethod ("Add");
806 foreach (object ob in (IEnumerable)source)
809 mi.Invoke (dest, param);
813 object ReadXmlNodeElement (XmlTypeMapping typeMap, bool isNullable)
815 return ReadXmlNode (typeMap.TypeData, false);
818 object ReadXmlNode (TypeData type, bool wrapped)
821 throw new NotSupportedException ();
823 if (type.Type == typeof (XmlDocument))
824 return ReadXmlDocument (wrapped);
826 return ReadXmlNode (wrapped);
830 object ReadPrimitiveElement (XmlTypeMapping typeMap, bool isNullable)
832 XmlQualifiedName t = GetXsiType();
833 if (t == null) t = new XmlQualifiedName (typeMap.XmlType, typeMap.Namespace);
834 return ReadTypedPrimitive (t);
837 object ReadEnumElement (XmlTypeMapping typeMap, bool isNullable)
839 Reader.ReadStartElement ();
840 object o = GetEnumValue (typeMap, Reader.ReadString());
845 object GetEnumValue (XmlTypeMapping typeMap, string val)
849 EnumMap map = (EnumMap) typeMap.ObjectMap;
850 string ev = map.GetEnumName (typeMap.TypeFullName, val);
851 if (ev == null) throw CreateUnknownConstantException (val, typeMap.TypeData.Type);
852 return Enum.Parse (typeMap.TypeData.Type, ev, false);
855 object ReadXmlSerializableElement (XmlTypeMapping typeMap, bool isNullable)
857 Reader.MoveToContent ();
858 if (Reader.NodeType == XmlNodeType.Element)
860 if (Reader.LocalName == typeMap.ElementName && Reader.NamespaceURI == typeMap.Namespace)
862 object ob = CreateInstance (typeMap.TypeData.Type, true);
863 return ReadSerializable ((IXmlSerializable)ob);
866 throw CreateUnknownNodeException ();
875 class FixupCallbackInfo
877 XmlSerializationReaderInterpreter _sri;
881 public FixupCallbackInfo (XmlSerializationReaderInterpreter sri, ClassMap map, bool isValueList)
885 _isValueList = isValueList;
888 public void FixupMembers (object fixup)
890 _sri.FixupMembers (_map, fixup, _isValueList);
894 class ReaderCallbackInfo
896 XmlSerializationReaderInterpreter _sri;
897 XmlTypeMapping _typeMap;
899 public ReaderCallbackInfo (XmlSerializationReaderInterpreter sri, XmlTypeMapping typeMap)
905 internal object ReadObject ()
907 return _sri.ReadObject (_typeMap, true, true);