Merge pull request #146 from flutos/207ce0bccceec3ff7860ea986086fedb5a92d822
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlAttributes.cs
1 //
2 // XmlAttributes.cs: 
3 //
4 // Author:
5 //   John Donagher (john@webmeta.com)
6 //
7 // (C) 2002 John Donagher
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.Reflection;
32 using System;
33 using System.ComponentModel;
34 using System.Collections;
35
36 namespace System.Xml.Serialization
37 {
38         /// <summary>
39         /// Summary description for XmlAttributes.
40         /// </summary>
41         public class XmlAttributes
42         {
43 #if !MOONLIGHT
44                 private XmlAnyAttributeAttribute xmlAnyAttribute;
45 #endif
46                 private XmlAnyElementAttributes xmlAnyElements = new XmlAnyElementAttributes();
47                 private XmlArrayAttribute xmlArray;
48                 private XmlArrayItemAttributes xmlArrayItems = new XmlArrayItemAttributes();
49                 private XmlAttributeAttribute xmlAttribute;
50                 private XmlChoiceIdentifierAttribute xmlChoiceIdentifier;
51                 private object xmlDefaultValue = System.DBNull.Value;
52                 private XmlElementAttributes xmlElements = new XmlElementAttributes();
53                 private XmlEnumAttribute xmlEnum;
54                 private bool xmlIgnore;
55                 private bool xmlns;
56                 private XmlRootAttribute xmlRoot;
57                 private XmlTextAttribute xmlText;
58                 private XmlTypeAttribute xmlType;
59
60                 public XmlAttributes ()
61                 {
62                 }
63
64                 public XmlAttributes (ICustomAttributeProvider provider)
65                 {
66                         object[] attributes = provider.GetCustomAttributes(false);
67                         foreach(object obj in attributes)
68                         {
69 #if !MOONLIGHT
70                                 if(obj is XmlAnyAttributeAttribute)
71                                         xmlAnyAttribute = (XmlAnyAttributeAttribute) obj;
72                                 else
73 #endif
74                                 if(obj is XmlAnyElementAttribute)
75                                         xmlAnyElements.Add((XmlAnyElementAttribute) obj);
76                                 else if(obj is XmlArrayAttribute)
77                                         xmlArray = (XmlArrayAttribute) obj;
78                                 else if(obj is XmlArrayItemAttribute)
79                                         xmlArrayItems.Add((XmlArrayItemAttribute) obj);
80                                 else if(obj is XmlAttributeAttribute)
81                                         xmlAttribute = (XmlAttributeAttribute) obj;
82                                 else if(obj is XmlChoiceIdentifierAttribute)
83                                         xmlChoiceIdentifier = (XmlChoiceIdentifierAttribute) obj;
84                                 else if(obj is DefaultValueAttribute)
85                                         xmlDefaultValue = ((DefaultValueAttribute)obj).Value;
86                                 else if(obj is XmlElementAttribute )
87                                         xmlElements.Add((XmlElementAttribute ) obj);
88                                 else if(obj is XmlEnumAttribute)
89                                         xmlEnum = (XmlEnumAttribute) obj;
90                                 else if(obj is XmlIgnoreAttribute)
91                                         xmlIgnore = true;
92                                 else if(obj is XmlNamespaceDeclarationsAttribute)
93                                         xmlns = true;
94                                 else if(obj is XmlRootAttribute)
95                                         xmlRoot = (XmlRootAttribute) obj;
96                                 else if(obj is XmlTextAttribute)
97                                         xmlText = (XmlTextAttribute) obj;
98                                 else if(obj is XmlTypeAttribute)
99                                         xmlType = (XmlTypeAttribute) obj;
100                         }
101                 }
102
103                 #region public properties
104 #if !MOONLIGHT
105                 public XmlAnyAttributeAttribute XmlAnyAttribute 
106                 {
107                         get 
108                         {
109                                 return xmlAnyAttribute;
110                         }
111                         set 
112                         {
113                                 xmlAnyAttribute = value;
114                         }
115                 }
116 #endif
117                 public XmlAnyElementAttributes XmlAnyElements 
118                 {
119                         get 
120                         {
121                                 return xmlAnyElements;
122                         }
123                 }
124                 public XmlArrayAttribute XmlArray
125                 {
126                         get 
127                         {
128                                 return xmlArray;
129                         }
130                         set 
131                         {
132                                 xmlArray = value;
133                         }
134                 }
135                 public XmlArrayItemAttributes XmlArrayItems 
136                 {
137                         get 
138                         {
139                                 return xmlArrayItems;
140                         }
141                 }
142                 public XmlAttributeAttribute XmlAttribute 
143                 {
144                         get 
145                         {
146                                 return xmlAttribute;
147                         }
148                         set 
149                         {
150                                 xmlAttribute = value;
151                         }
152                 }
153                 public XmlChoiceIdentifierAttribute XmlChoiceIdentifier 
154                 {
155                         get 
156                         {
157                                 return xmlChoiceIdentifier;
158                         }
159                 }
160                 public object XmlDefaultValue 
161                 {
162                         get 
163                         {
164                                 return xmlDefaultValue;
165                         }
166                         set 
167                         {
168                                 xmlDefaultValue = value;
169                         }
170                 }
171                 public XmlElementAttributes XmlElements 
172                 {
173                         get 
174                         {
175                                 return xmlElements;
176                         }
177                 }
178                 public XmlEnumAttribute XmlEnum 
179                 {
180                         get 
181                         {
182                                 return xmlEnum;
183                         }
184                         set 
185                         {
186                                 xmlEnum = value;
187                         }
188                 }
189                 public bool XmlIgnore 
190                 {
191                         get 
192                         {
193                                 return xmlIgnore;
194                         }
195                         set 
196                         {
197                                 xmlIgnore = value;
198                         }
199                 }
200                 public bool Xmlns 
201                 {
202                         get 
203                         {
204                                 return xmlns;
205                         }
206                         set 
207                         {
208                                 xmlns = value;
209                         }
210                 }
211                 public XmlRootAttribute XmlRoot 
212                 {
213                         get 
214                         {
215                                 return xmlRoot;}
216                         set 
217                         {
218                                 xmlRoot = value;
219                         }
220                 }
221                 public XmlTextAttribute XmlText 
222                 {
223                         get 
224                         {
225                                 return xmlText;
226                         }
227                         set 
228                         {
229                                 xmlText = value;
230                         }
231                 }
232                 public XmlTypeAttribute XmlType 
233                 {
234                         get 
235                         {
236                                 return xmlType;
237                         }
238                         set 
239                         {
240                                 xmlType = value;
241                         }
242                 }
243                 #endregion
244                 
245                 internal void AddKeyHash (System.Text.StringBuilder sb)
246                 {
247                         sb.Append ("XA ");
248                         
249                         KeyHelper.AddField (sb, 1, xmlIgnore);
250                         KeyHelper.AddField (sb, 2, xmlns);
251 #if !MOONLIGHT
252                         KeyHelper.AddField (sb, 3, xmlAnyAttribute!=null);
253 #endif
254
255                         xmlAnyElements.AddKeyHash (sb);
256                         xmlArrayItems.AddKeyHash (sb);
257                         xmlElements.AddKeyHash (sb);
258                         
259                         if (xmlArray != null)
260                                 xmlArray.AddKeyHash (sb);
261                                 
262                         if (xmlAttribute != null)
263                                 xmlAttribute.AddKeyHash (sb);
264                                 
265                         if (xmlDefaultValue == null) {
266                                 sb.Append ("n");
267                         }
268                         else if (!(xmlDefaultValue is System.DBNull)) {
269                                 string v = XmlCustomFormatter.ToXmlString (TypeTranslator.GetTypeData (xmlDefaultValue.GetType()), xmlDefaultValue);
270                                 sb.Append ("v" + v);
271                         }
272                         
273                         if (xmlEnum != null)
274                                 xmlEnum.AddKeyHash (sb);
275                                 
276                         if (xmlRoot != null)
277                                 xmlRoot.AddKeyHash (sb);
278                                 
279                         if (xmlText != null)
280                                 xmlText.AddKeyHash (sb);
281                                 
282                         if (xmlType != null)
283                                 xmlType.AddKeyHash (sb);
284                                 
285                         if (xmlChoiceIdentifier != null)
286                                 xmlChoiceIdentifier.AddKeyHash (sb);
287                                 
288                         sb.Append ("|");
289                 }
290
291                 internal int? Order {
292                         get {
293                                 int? order = null;
294                                 if (XmlElements.Count > 0)
295                                         order = XmlElements.Order;
296                                 else if (XmlArray != null)
297                                         order = XmlArray.Order;
298                                 else if (XmlAnyElements.Count > 0)
299                                         order = XmlAnyElements.Order;
300                                 return order;
301                         }
302                 }
303                 
304                 internal int SortableOrder {
305                         get { return Order != null ? (int) Order : int.MinValue; }
306                 }
307         }
308 }