Merge pull request #1156 from felfert/master
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlTypeMapMemberElement.cs
1 //
2 // XmlTypeMapMemberElement.cs: 
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // (C) 2002, 2003 Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Globalization;
34
35 namespace System.Xml.Serialization
36 {
37         // XmlTypeMapMemberElement
38         // A member of a class that must be serialized as an XmlElement
39
40         internal class XmlTypeMapMemberElement: XmlTypeMapMember
41         {
42                 XmlTypeMapElementInfoList _elementInfo;
43                 string _choiceMember;
44                 bool _isTextCollector;
45                 TypeData _choiceTypeData;
46
47                 public XmlTypeMapMemberElement()
48                 {
49                 }
50
51                 public XmlTypeMapElementInfoList ElementInfo
52                 {
53                         get
54                         {
55                                 if (_elementInfo == null) _elementInfo = new XmlTypeMapElementInfoList ();
56                                 return _elementInfo;
57                         }
58                         set { _elementInfo = value; }
59                 }
60
61                 public string ChoiceMember
62                 {
63                         get { return _choiceMember; }
64                         set { _choiceMember = value; }
65                 }
66
67                 public TypeData ChoiceTypeData
68                 {
69                         get { return _choiceTypeData; }
70                         set { _choiceTypeData = value; }
71                 }
72
73                 public XmlTypeMapElementInfo FindElement (object ob, object memberValue)
74                 {
75                         if (_elementInfo.Count == 1) 
76                                 return (XmlTypeMapElementInfo) _elementInfo[0];
77                         else if (_choiceMember != null)
78                         {
79                                 object value = GetValue (ob, _choiceMember);
80                                 foreach (XmlTypeMapElementInfo elem in _elementInfo)
81                                         if (elem.ChoiceValue != null && elem.ChoiceValue.Equals (value)) return elem;
82                         }
83                         else
84                         {
85                                 if (memberValue == null)
86                                         return (XmlTypeMapElementInfo) _elementInfo[0];
87                                 foreach (XmlTypeMapElementInfo elem in _elementInfo)
88                                         if (elem.TypeData.Type.IsInstanceOfType (memberValue)) return elem;
89                         }
90                         return null;
91                 }
92                 
93                 public void SetChoice (object ob, object choice)
94                 {
95                         SetValue (ob, _choiceMember, choice);
96                 }
97
98                 public bool IsXmlTextCollector
99                 {
100                         get { return _isTextCollector; }
101                         set { _isTextCollector = value; }
102                 }
103                 
104                 public override bool RequiresNullable {
105                         get {
106                                 foreach (XmlTypeMapElementInfo einfo in ElementInfo)
107                                         if (einfo.IsNullable)
108                                                 return true;
109                                 return false;
110                         }
111                 }
112         }
113
114         // XmlTypeMapMemberList
115         // A member of a class that must be serialized as a list
116
117         internal class XmlTypeMapMemberList : XmlTypeMapMemberElement
118         {
119                 public XmlTypeMapping ListTypeMapping
120                 {
121                         get { return ((XmlTypeMapElementInfo) ElementInfo[0]).MappedType; }
122                 }
123
124                 public string ElementName
125                 {
126                         get { return ((XmlTypeMapElementInfo) ElementInfo[0]).ElementName; }
127                 }
128
129                 public string Namespace
130                 {
131                         get { return ((XmlTypeMapElementInfo) ElementInfo[0]).Namespace; }
132                 }
133         }
134
135         // XmlTypeMapMemberFlatList
136         // A member of a class that must be serialized as a flat list
137
138         internal class XmlTypeMapMemberExpandable : XmlTypeMapMemberElement
139         {
140                 int _flatArrayIndex;
141
142                 public int FlatArrayIndex
143                 {
144                         get { return _flatArrayIndex; }
145                         set { _flatArrayIndex = value; }
146                 }
147         }
148
149         internal class XmlTypeMapMemberFlatList : XmlTypeMapMemberExpandable
150         {
151                 ListMap _listMap;
152
153                 public ListMap ListMap
154                 {
155                         get { return _listMap; }
156                         set { _listMap = value; }
157                 }
158         }
159
160         internal class XmlTypeMapMemberAnyElement : XmlTypeMapMemberExpandable
161         {
162                 public bool IsElementDefined (string name, string ns)
163                 {
164                         foreach (XmlTypeMapElementInfo elem in ElementInfo)
165                         {
166                                 if (elem.IsUnnamedAnyElement)           // Default AnyElementAttribute
167                                         return true;
168
169                                 if (elem.ElementName == name && elem.Namespace == ns)
170                                         return true;
171                         }
172                         return false;
173                 }
174
175                 public bool IsDefaultAny
176                 {
177                         get
178                         {
179                                 foreach (XmlTypeMapElementInfo elem in ElementInfo)
180                                 {
181                                         if (elem.IsUnnamedAnyElement) 
182                                                 return true;
183                                 }
184                                 return false;
185                         }
186                 }
187                 
188                 public bool CanBeText
189                 {
190                         get
191                         {
192                                 return (ElementInfo.Count > 0) && ((XmlTypeMapElementInfo)ElementInfo[0]).IsTextElement;
193                         }
194                 }
195         }
196
197         internal class XmlTypeMapMemberAnyAttribute: XmlTypeMapMember
198         {
199         }
200 }