2 // XmlTypeMapMemberElement.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.Collections;
33 using System.Globalization;
35 namespace System.Xml.Serialization
37 // XmlTypeMapMemberElement
38 // A member of a class that must be serialized as an XmlElement
40 internal class XmlTypeMapMemberElement: XmlTypeMapMember
42 XmlTypeMapElementInfoList _elementInfo;
44 bool _isTextCollector;
45 TypeData _choiceTypeData;
47 public XmlTypeMapMemberElement()
51 public XmlTypeMapElementInfoList ElementInfo
55 if (_elementInfo == null) _elementInfo = new XmlTypeMapElementInfoList ();
58 set { _elementInfo = value; }
61 public string ChoiceMember
63 get { return _choiceMember; }
64 set { _choiceMember = value; }
67 public TypeData ChoiceTypeData
69 get { return _choiceTypeData; }
70 set { _choiceTypeData = value; }
73 public XmlTypeMapElementInfo FindElement (object ob, object memberValue)
75 if (_elementInfo.Count == 1)
76 return (XmlTypeMapElementInfo) _elementInfo[0];
77 else if (_choiceMember != null)
79 object value = GetValue (ob, _choiceMember);
80 foreach (XmlTypeMapElementInfo elem in _elementInfo)
81 if (elem.ChoiceValue != null && elem.ChoiceValue.Equals (value)) return elem;
85 if (memberValue == null)
86 return (XmlTypeMapElementInfo) _elementInfo [0];
89 XmlTypeMapElementInfo bestTypeElem = null;
90 // Select the most-specific type for the given memberValue
91 foreach (XmlTypeMapElementInfo elem in _elementInfo)
93 if (elem.TypeData.Type.IsInstanceOfType (memberValue))
95 if (bestTypeElem == null || elem.TypeData.Type.IsSubclassOf (bestTypeElem.TypeData.Type))
107 public void SetChoice (object ob, object choice)
109 SetValue (ob, _choiceMember, choice);
112 public bool IsXmlTextCollector
114 get { return _isTextCollector; }
115 set { _isTextCollector = value; }
118 public override bool RequiresNullable {
120 foreach (XmlTypeMapElementInfo einfo in ElementInfo)
121 if (einfo.IsNullable)
128 // XmlTypeMapMemberList
129 // A member of a class that must be serialized as a list
131 internal class XmlTypeMapMemberList : XmlTypeMapMemberElement
133 public XmlTypeMapping ListTypeMapping
135 get { return ((XmlTypeMapElementInfo) ElementInfo[0]).MappedType; }
138 public string ElementName
140 get { return ((XmlTypeMapElementInfo) ElementInfo[0]).ElementName; }
143 public string Namespace
145 get { return ((XmlTypeMapElementInfo) ElementInfo[0]).Namespace; }
149 // XmlTypeMapMemberFlatList
150 // A member of a class that must be serialized as a flat list
152 internal class XmlTypeMapMemberExpandable : XmlTypeMapMemberElement
156 public int FlatArrayIndex
158 get { return _flatArrayIndex; }
159 set { _flatArrayIndex = value; }
163 internal class XmlTypeMapMemberFlatList : XmlTypeMapMemberExpandable
167 public ListMap ListMap
169 get { return _listMap; }
170 set { _listMap = value; }
174 internal class XmlTypeMapMemberAnyElement : XmlTypeMapMemberExpandable
176 public bool IsElementDefined (string name, string ns)
178 foreach (XmlTypeMapElementInfo elem in ElementInfo)
180 if (elem.IsUnnamedAnyElement) // Default AnyElementAttribute
183 if (elem.ElementName == name && elem.Namespace == ns)
189 public bool IsDefaultAny
193 foreach (XmlTypeMapElementInfo elem in ElementInfo)
195 if (elem.IsUnnamedAnyElement)
202 public bool CanBeText
206 return (ElementInfo.Count > 0) && ((XmlTypeMapElementInfo)ElementInfo[0]).IsTextElement;
211 internal class XmlTypeMapMemberAnyAttribute: XmlTypeMapMember