svn path=/branches/mono-1-1-9/mono/; revision=51217
[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
46                 public XmlTypeMapMemberElement()
47                 {
48                 }
49
50                 public XmlTypeMapElementInfoList ElementInfo
51                 {
52                         get
53                         {
54                                 if (_elementInfo == null) _elementInfo = new XmlTypeMapElementInfoList ();
55                                 return _elementInfo;
56                         }
57                         set { _elementInfo = value; }
58                 }
59
60                 public string ChoiceMember
61                 {
62                         get { return _choiceMember; }
63                         set { _choiceMember = value; }
64                 }
65
66                 public XmlTypeMapElementInfo FindElement (object ob, object memberValue)
67                 {
68                         if (_elementInfo.Count == 1) 
69                                 return (XmlTypeMapElementInfo) _elementInfo[0];
70                         else if (_choiceMember != null)
71                         {
72                                 object value = GetValue (ob, _choiceMember);
73                                 foreach (XmlTypeMapElementInfo elem in _elementInfo)
74                                         if (elem.ChoiceValue != null && elem.ChoiceValue.Equals (value)) return elem;
75                         }
76                         else
77                         {
78                                 if (memberValue == null)
79                                         return (XmlTypeMapElementInfo) _elementInfo[0];
80                                 Type type = memberValue.GetType();
81                                 foreach (XmlTypeMapElementInfo elem in _elementInfo)
82                                         if (elem.TypeData.Type == type) return elem;
83                         }
84                         return null;
85                 }
86                 
87                 public void SetChoice (object ob, object choice)
88                 {
89                         SetValue (ob, _choiceMember, choice);
90                 }
91
92                 public bool IsXmlTextCollector
93                 {
94                         get { return _isTextCollector; }
95                         set { _isTextCollector = value; }
96                 }
97         }
98
99         // XmlTypeMapMemberList
100         // A member of a class that must be serialized as a list
101
102         internal class XmlTypeMapMemberList : XmlTypeMapMemberElement
103         {
104                 public XmlTypeMapping ListTypeMapping
105                 {
106                         get { return ((XmlTypeMapElementInfo) ElementInfo[0]).MappedType; }
107                 }
108
109                 public string ElementName
110                 {
111                         get { return ((XmlTypeMapElementInfo) ElementInfo[0]).ElementName; }
112                 }
113
114                 public string Namespace
115                 {
116                         get { return ((XmlTypeMapElementInfo) ElementInfo[0]).Namespace; }
117                 }
118         }
119
120         // XmlTypeMapMemberFlatList
121         // A member of a class that must be serialized as a flat list
122
123         internal class XmlTypeMapMemberExpandable : XmlTypeMapMemberElement
124         {
125                 int _flatArrayIndex;
126
127                 public int FlatArrayIndex
128                 {
129                         get { return _flatArrayIndex; }
130                         set { _flatArrayIndex = value; }
131                 }
132         }
133
134         internal class XmlTypeMapMemberFlatList : XmlTypeMapMemberExpandable
135         {
136                 ListMap _listMap;
137
138                 public ListMap ListMap
139                 {
140                         get { return _listMap; }
141                         set { _listMap = value; }
142                 }
143         }
144
145         internal class XmlTypeMapMemberAnyElement : XmlTypeMapMemberExpandable
146         {
147                 public bool IsElementDefined (string name, string ns)
148                 {
149                         foreach (XmlTypeMapElementInfo elem in ElementInfo)
150                         {
151                                 if (elem.IsUnnamedAnyElement)           // Default AnyElementAttribute
152                                         return true;
153
154                                 if (elem.ElementName == name && elem.Namespace == ns)
155                                         return true;
156                         }
157                         return false;
158                 }
159
160                 public bool IsDefaultAny
161                 {
162                         get
163                         {
164                                 foreach (XmlTypeMapElementInfo elem in ElementInfo)
165                                 {
166                                         if (elem.IsUnnamedAnyElement) 
167                                                 return true;
168                                 }
169                                 return false;
170                         }
171                 }
172                 
173                 public bool CanBeText
174                 {
175                         get
176                         {
177                                 return (ElementInfo.Count > 0) && ((XmlTypeMapElementInfo)ElementInfo[0]).IsTextElement;
178                         }
179                 }
180         }
181
182         internal class XmlTypeMapMemberAnyAttribute: XmlTypeMapMember
183         {
184         }
185 }