Author: lluis
Date: 2005-03-31 03:38:38 -0500 (Thu, 31 Mar 2005)
New Revision: 42416

Modified:
   trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog
   
trunk/mcs/class/System.XML/System.Xml.Serialization/SerializationCodeGenerator.cs
   trunk/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs
   
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs
   trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapElementInfo.cs
   trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapMember.cs
   
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapMemberElement.cs
Log:
2005-03-30  Lluis Sanchez Gual  <[EMAIL PROTECTED]>

        * SerializationCodeGenerator.cs: 
        * XmlSerializationReaderInterpreter.cs: If the element being read is
        bound to a choice member, set the corresponding value.
        * XmlTypeMapMember.cs: Added helper SetValue method.
        * XmlTypeMapElementInfo.cs: Changed the type of ChoiceValue to Object,
        since now stores the enum value.
        * XmlReflectionImporter.cs: Properly import choice values.
        * XmlTypeMapMemberElement.cs: Added setter for the choice.



Modified: trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog
===================================================================
--- trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog       
2005-03-31 06:19:04 UTC (rev 42415)
+++ trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog       
2005-03-31 08:38:38 UTC (rev 42416)
@@ -1,3 +1,14 @@
+2005-03-30  Lluis Sanchez Gual  <[EMAIL PROTECTED]>
+
+       * SerializationCodeGenerator.cs: 
+       * XmlSerializationReaderInterpreter.cs: If the element being read is
+       bound to a choice member, set the corresponding value.
+       * XmlTypeMapMember.cs: Added helper SetValue method.
+       * XmlTypeMapElementInfo.cs: Changed the type of ChoiceValue to Object,
+       since now stores the enum value.
+       * XmlReflectionImporter.cs: Properly import choice values.
+       * XmlTypeMapMemberElement.cs: Added setter for the choice.
+
 2005-03-29  Lluis Sanchez Gual  <[EMAIL PROTECTED]>
 
        * XmlReflectionImporter.cs: Added support for subclasses of XmlNode.

Modified: 
trunk/mcs/class/System.XML/System.Xml.Serialization/SerializationCodeGenerator.cs
===================================================================
--- 
trunk/mcs/class/System.XML/System.Xml.Serialization/SerializationCodeGenerator.cs
   2005-03-31 06:19:04 UTC (rev 42415)
+++ 
trunk/mcs/class/System.XML/System.Xml.Serialization/SerializationCodeGenerator.cs
   2005-03-31 08:38:38 UTC (rev 42416)
@@ -1775,6 +1775,10 @@
                                                        WriteLineUni ("}");
                                                }
                                                else if 
(!GenerateReadMemberHook (xmlMapType, info.Member)) {
+                                                       if (info.ChoiceValue != 
null) {
+                                                               
XmlTypeMapMemberElement imem = (XmlTypeMapMemberElement) info.Member;
+                                                               WriteLine (ob + 
".@" + imem.ChoiceMember + " = " + GetLiteral(info.ChoiceValue) + ";");
+                                                       }
                                                        GenerateSetMemberValue 
(info.Member, ob, GenerateReadObjectElement (info), isValueList);
                                                        GenerateEndHook ();
                                                }

Modified: 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs
===================================================================
--- 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs    
    2005-03-31 06:19:04 UTC (rev 42415)
+++ 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs    
    2005-03-31 08:38:38 UTC (rev 42416)
@@ -109,7 +109,7 @@
                        XmlMemberMapping[] mapping = new 
XmlMemberMapping[members.Length];
                        for (int n=0; n<members.Length; n++)
                        {
-                               XmlTypeMapMember mapMem = CreateMapMember 
(members[n], ns);
+                               XmlTypeMapMember mapMem = CreateMapMember 
(null, members[n], ns);
                                mapping[n] = new XmlMemberMapping 
(members[n].MemberName, ns, mapMem, false);
                        }
                        XmlMembersMapping mps = new XmlMembersMapping 
(elementName, ns, hasWrapperElement, false, mapping);
@@ -280,7 +280,7 @@
                                                ns = bmap.XmlTypeNamespace;
                                        }
                                        
-                                       XmlTypeMapMember mem = CreateMapMember 
(rmember, ns);
+                                       XmlTypeMapMember mem = CreateMapMember 
(type, rmember, ns);
                                        mem.CheckOptionalValueType (type);
                                        classMap.AddMember (mem);
                                }
@@ -665,7 +665,7 @@
                        return members;         
                }
                
-               private XmlTypeMapMember CreateMapMember (XmlReflectionMember 
rmember, string defaultNamespace)
+               private XmlTypeMapMember CreateMapMember (Type declaringType, 
XmlReflectionMember rmember, string defaultNamespace)
                {
                        XmlTypeMapMember mapMember;
                        XmlAttributes atts = rmember.XmlAttributes;
@@ -748,7 +748,7 @@
                                        // TODO: check that it does not have 
XmlArrayAttribute
                                        XmlTypeMapMemberFlatList member = new 
XmlTypeMapMemberFlatList ();
                                        member.ListMap = new ListMap ();
-                                       member.ListMap.ItemInfo = 
ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, 
member, atts);
+                                       member.ListMap.ItemInfo = 
ImportElementInfo (declaringType, rmember.MemberName, defaultNamespace, 
typeData.ListItemType, member, atts);
                                        member.ElementInfo = 
member.ListMap.ItemInfo;
                                        mapMember = member;
                                }
@@ -776,7 +776,7 @@
                                // An element
 
                                XmlTypeMapMemberElement member = new 
XmlTypeMapMemberElement ();
-                               member.ElementInfo = ImportElementInfo 
(rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts);
+                               member.ElementInfo = ImportElementInfo 
(declaringType, rmember.MemberName, defaultNamespace, rmember.MemberType, 
member, atts);
                                mapMember = member;
                        }
 
@@ -787,12 +787,30 @@
                        return mapMember;
                }
 
-               XmlTypeMapElementInfoList ImportElementInfo (string 
defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement 
member, XmlAttributes atts)
+               XmlTypeMapElementInfoList ImportElementInfo (Type cls, string 
defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement 
member, XmlAttributes atts)
                {
+                       EnumMap choiceEnumMap = null;
+                       Type choiceEnumType = null;
+                       
                        XmlTypeMapElementInfoList list = new 
XmlTypeMapElementInfoList();
-
                        ImportTextElementInfo (list, defaultType, member, atts);
                        
+                       if (atts.XmlChoiceIdentifier != null) {
+                               if (cls == null)
+                                       throw new InvalidOperationException 
("XmlChoiceIdentifierAttribute not supported in this context.");
+                                       
+                               member.ChoiceMember = 
atts.XmlChoiceIdentifier.MemberName;
+                               MemberInfo[] mems = cls.GetMember 
(member.ChoiceMember, BindingFlags.Instance|BindingFlags.Public);
+                               
+                               if (mems.Length == 0)
+                                       throw new InvalidOperationException 
("Choice member '" + member.ChoiceMember + "' not found in class '" + cls);
+                                       
+                               if (mems[0] is PropertyInfo) choiceEnumType = 
((PropertyInfo)mems[0]).PropertyType;
+                               else choiceEnumType = 
((FieldInfo)mems[0]).FieldType;
+                               
+                               choiceEnumMap = (EnumMap) ImportTypeMapping 
(choiceEnumType).ObjectMap;
+                       }
+                       
                        if (atts.XmlElements.Count == 0 && list.Count == 0)
                        {
                                XmlTypeMapElementInfo elem = new 
XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
@@ -831,6 +849,12 @@
                                else
                                        elem.ElementName = defaultName;
 
+                               if (choiceEnumMap != null) {
+                                       string cname = 
choiceEnumMap.GetEnumName (elem.ElementName);
+                                       if (cname == null) throw new 
InvalidOperationException ("The '" + choiceEnumType + "' enumeration does not 
have a value for the element '" + elem.ElementName + "'");
+                                       elem.ChoiceValue = Enum.Parse 
(choiceEnumType, cname);
+                               }
+                                       
                                list.Add (elem);
                        }
                        return list;

Modified: 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs
===================================================================
--- 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs
    2005-03-31 06:19:04 UTC (rev 42415)
+++ 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs
    2005-03-31 08:38:38 UTC (rev 42416)
@@ -402,8 +402,13 @@
                                                                else if (val != 
null)
                                                                        
SetMemberValue (info.Member, ob, val, isValueList);
                                                        }
-                                                       else 
+                                                       else {
                                                                SetMemberValue 
(info.Member, ob, ReadObjectElement (info), isValueList);
+                                                               if 
(info.ChoiceValue != null) {
+                                                                       
XmlTypeMapMemberElement imem = (XmlTypeMapMemberElement) info.Member;
+                                                                       
imem.SetChoice (ob, info.ChoiceValue);
+                                                               }
+                                                       }
                                                }
                                                else
                                                        throw new 
InvalidOperationException ("Unknown member type");

Modified: 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapElementInfo.cs
===================================================================
--- 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapElementInfo.cs    
    2005-03-31 06:19:04 UTC (rev 42415)
+++ 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapElementInfo.cs    
    2005-03-31 08:38:38 UTC (rev 42416)
@@ -43,7 +43,7 @@
                string _namespace = "";
                XmlSchemaForm _form;
                XmlTypeMapMember _member;
-               string _choiceValue;
+               object _choiceValue;
                bool _isNullable;
                int _nestingLevel;      // Only for array items
                XmlTypeMapping _mappedType;
@@ -62,7 +62,7 @@
                        set { _type = value; }
                }
 
-               public string ChoiceValue
+               public object ChoiceValue
                {
                        get { return _choiceValue; }
                        set { _choiceValue = value; }
@@ -158,9 +158,10 @@
                        if (_type.XmlType != oinfo._type.XmlType) return false;
                        if (_namespace != oinfo._namespace) return false;
                        if (_form != oinfo._form) return false;
-                       if (_choiceValue != oinfo._choiceValue) return false;
                        if (_type != oinfo._type) return false;
                        if (_isNullable != oinfo._isNullable) return false;
+                       if (_choiceValue != null && !_choiceValue.Equals 
(oinfo._choiceValue)) return false;
+                       if (_choiceValue != oinfo._choiceValue) return false;
                        return true;
                }
 

Modified: 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapMember.cs
===================================================================
--- trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapMember.cs     
2005-03-31 06:19:04 UTC (rev 42415)
+++ trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapMember.cs     
2005-03-31 08:38:38 UTC (rev 42416)
@@ -101,6 +101,13 @@
                        else ((FieldInfo)_member).SetValue (ob, value);
                }
 
+               public static void SetValue (object ob, string name, object 
value)
+               {
+                       MemberInfo[] mems = ob.GetType().GetMember (name, 
BindingFlags.Instance|BindingFlags.Public);
+                       if (mems[0] is PropertyInfo) 
((PropertyInfo)mems[0]).SetValue (ob, value, null);
+                       else ((FieldInfo)mems[0]).SetValue (ob, value);
+               }
+
                void InitMember (Type type)
                {
                        MemberInfo[] mems = type.GetMember (_name, 
BindingFlags.Instance|BindingFlags.Public);

Modified: 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapMemberElement.cs
===================================================================
--- 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapMemberElement.cs  
    2005-03-31 06:19:04 UTC (rev 42415)
+++ 
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapMemberElement.cs  
    2005-03-31 08:38:38 UTC (rev 42416)
@@ -70,9 +70,8 @@
                        else if (_choiceMember != null)
                        {
                                object value = GetValue (ob, _choiceMember);
-                               string choiceValue = value is IFormattable ? 
((IFormattable) value).ToString (null, CultureInfo.InvariantCulture) : 
value.ToString();
                                foreach (XmlTypeMapElementInfo elem in 
_elementInfo)
-                                       if (elem.ChoiceValue == choiceValue) 
return elem;
+                                       if (elem.ChoiceValue != null && 
elem.ChoiceValue.Equals (value)) return elem;
                        }
                        else
                        {
@@ -84,6 +83,11 @@
                        }
                        return null;
                }
+               
+               public void SetChoice (object ob, object choice)
+               {
+                       SetValue (ob, _choiceMember, choice);
+               }
 
                public bool IsXmlTextCollector
                {

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to