* XmlTypeAttribute.cs: added property AnonymousType for 2.0
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlTypeMapMemberElement.cs
index 3e36a18f517091971b4aa2e6d6bab6832f2bba1b..30b0614e97c524b9a1ea683f4e01d2306293e75d 100644 (file)
@@ -7,8 +7,30 @@
 // (C) 2002, 2003 Ximian, Inc.  http://www.ximian.com
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.Collections;
+using System.Globalization;
 
 namespace System.Xml.Serialization
 {
@@ -19,6 +41,8 @@ namespace System.Xml.Serialization
        {
                XmlTypeMapElementInfoList _elementInfo;
                string _choiceMember;
+               bool _isTextCollector;
+               TypeData _choiceTypeData;
 
                public XmlTypeMapMemberElement()
                {
@@ -26,7 +50,11 @@ namespace System.Xml.Serialization
 
                public XmlTypeMapElementInfoList ElementInfo
                {
-                       get { return _elementInfo; }
+                       get
+                       {
+                               if (_elementInfo == null) _elementInfo = new XmlTypeMapElementInfoList ();
+                               return _elementInfo;
+                       }
                        set { _elementInfo = value; }
                }
 
@@ -36,24 +64,42 @@ namespace System.Xml.Serialization
                        set { _choiceMember = value; }
                }
 
+               public TypeData ChoiceTypeData
+               {
+                       get { return _choiceTypeData; }
+                       set { _choiceTypeData = value; }
+               }
+
                public XmlTypeMapElementInfo FindElement (object ob, object memberValue)
                {
                        if (_elementInfo.Count == 1) 
                                return (XmlTypeMapElementInfo) _elementInfo[0];
                        else if (_choiceMember != null)
                        {
-                               string choiceValue = GetValue (ob, _choiceMember).ToString();
+                               object value = GetValue (ob, _choiceMember);
                                foreach (XmlTypeMapElementInfo elem in _elementInfo)
-                                       if (elem.ChoiceValue == choiceValue) return elem;
+                                       if (elem.ChoiceValue != null && elem.ChoiceValue.Equals (value)) return elem;
                        }
                        else
                        {
-                               Type type = memberValue.GetType();
+                               if (memberValue == null)
+                                       return (XmlTypeMapElementInfo) _elementInfo[0];
                                foreach (XmlTypeMapElementInfo elem in _elementInfo)
-                                       if (elem.TypeData.Type == type) return elem;
+                                       if (elem.TypeData.Type.IsInstanceOfType (memberValue)) return elem;
                        }
                        return null;
                }
+               
+               public void SetChoice (object ob, object choice)
+               {
+                       SetValue (ob, _choiceMember, choice);
+               }
+
+               public bool IsXmlTextCollector
+               {
+                       get { return _isTextCollector; }
+                       set { _isTextCollector = value; }
+               }
        }
 
        // XmlTypeMapMemberList
@@ -61,26 +107,19 @@ namespace System.Xml.Serialization
 
        internal class XmlTypeMapMemberList : XmlTypeMapMemberElement
        {
-               XmlTypeMapping _listMap;
-               string _elementName;
-               string _namespace;
-
                public XmlTypeMapping ListTypeMapping
                {
-                       get { return _listMap; }
-                       set { _listMap = value; }
+                       get { return ((XmlTypeMapElementInfo) ElementInfo[0]).MappedType; }
                }
 
                public string ElementName
                {
-                       get { return _elementName; }
-                       set { _elementName = value; }
+                       get { return ((XmlTypeMapElementInfo) ElementInfo[0]).ElementName; }
                }
 
                public string Namespace
                {
-                       get { return _namespace; }
-                       set { _namespace = value; }
+                       get { return ((XmlTypeMapElementInfo) ElementInfo[0]).Namespace; }
                }
        }
 
@@ -115,7 +154,7 @@ namespace System.Xml.Serialization
                {
                        foreach (XmlTypeMapElementInfo elem in ElementInfo)
                        {
-                               if (elem.ElementName == "")             // Default AnyElementAttribute
+                               if (elem.IsUnnamedAnyElement)           // Default AnyElementAttribute
                                        return true;
 
                                if (elem.ElementName == name && elem.Namespace == ns)
@@ -130,12 +169,20 @@ namespace System.Xml.Serialization
                        {
                                foreach (XmlTypeMapElementInfo elem in ElementInfo)
                                {
-                                       if (elem.ElementName == ""
+                                       if (elem.IsUnnamedAnyElement
                                                return true;
                                }
                                return false;
                        }
                }
+               
+               public bool CanBeText
+               {
+                       get
+                       {
+                               return (ElementInfo.Count > 0) && ((XmlTypeMapElementInfo)ElementInfo[0]).IsTextElement;
+                       }
+               }
        }
 
        internal class XmlTypeMapMemberAnyAttribute: XmlTypeMapMember