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