New test.
[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 if(obj is XmlAnyElementAttribute)
70                                         xmlAnyElements.Add((XmlAnyElementAttribute) obj);
71                                 else if(obj is XmlArrayAttribute)
72                                         xmlArray = (XmlArrayAttribute) obj;
73                                 else if(obj is XmlArrayItemAttribute)
74                                         xmlArrayItems.Add((XmlArrayItemAttribute) obj);
75                                 else if(obj is XmlAttributeAttribute)
76                                         xmlAttribute = (XmlAttributeAttribute) obj;
77                                 else if(obj is XmlChoiceIdentifierAttribute)
78                                         xmlChoiceIdentifier = (XmlChoiceIdentifierAttribute) obj;
79                                 else if(obj is DefaultValueAttribute)
80                                         xmlDefaultValue = ((DefaultValueAttribute)obj).Value;
81                                 else if(obj is XmlElementAttribute )
82                                         xmlElements.Add((XmlElementAttribute ) obj);
83                                 else if(obj is XmlEnumAttribute)
84                                         xmlEnum = (XmlEnumAttribute) obj;
85                                 else if(obj is XmlIgnoreAttribute)
86                                         xmlIgnore = true;
87                                 else if(obj is XmlNamespaceDeclarationsAttribute)
88                                         xmlns = true;
89                                 else if(obj is XmlRootAttribute)
90                                         xmlRoot = (XmlRootAttribute) obj;
91                                 else if(obj is XmlTextAttribute)
92                                         xmlText = (XmlTextAttribute) obj;
93                                 else if(obj is XmlTypeAttribute)
94                                         xmlType = (XmlTypeAttribute) obj;
95                         }
96                 }
97
98                 #region public properties
99                 public XmlAnyAttributeAttribute XmlAnyAttribute 
100                 {
101                         get 
102                         {
103                                 return xmlAnyAttribute;
104                         }
105                         set 
106                         {
107                                 xmlAnyAttribute = value;
108                         }
109                 }
110                 public XmlAnyElementAttributes XmlAnyElements 
111                 {
112                         get 
113                         {
114                                 return xmlAnyElements;
115                         }
116                 }
117                 public XmlArrayAttribute XmlArray
118                 {
119                         get 
120                         {
121                                 return xmlArray;
122                         }
123                         set 
124                         {
125                                 xmlArray = value;
126                         }
127                 }
128                 public XmlArrayItemAttributes XmlArrayItems 
129                 {
130                         get 
131                         {
132                                 return xmlArrayItems;
133                         }
134                 }
135                 public XmlAttributeAttribute XmlAttribute 
136                 {
137                         get 
138                         {
139                                 return xmlAttribute;
140                         }
141                         set 
142                         {
143                                 xmlAttribute = value;
144                         }
145                 }
146                 public XmlChoiceIdentifierAttribute XmlChoiceIdentifier 
147                 {
148                         get 
149                         {
150                                 return xmlChoiceIdentifier;
151                         }
152                 }
153                 public object XmlDefaultValue 
154                 {
155                         get 
156                         {
157                                 return xmlDefaultValue;
158                         }
159                         set 
160                         {
161                                 xmlDefaultValue = value;
162                         }
163                 }
164                 public XmlElementAttributes XmlElements 
165                 {
166                         get 
167                         {
168                                 return xmlElements;
169                         }
170                 }
171                 public XmlEnumAttribute XmlEnum 
172                 {
173                         get 
174                         {
175                                 return xmlEnum;
176                         }
177                         set 
178                         {
179                                 xmlEnum = value;
180                         }
181                 }
182                 public bool XmlIgnore 
183                 {
184                         get 
185                         {
186                                 return xmlIgnore;
187                         }
188                         set 
189                         {
190                                 xmlIgnore = value;
191                         }
192                 }
193                 public bool Xmlns 
194                 {
195                         get 
196                         {
197                                 return xmlns;
198                         }
199                         set 
200                         {
201                                 xmlns = value;
202                         }
203                 }
204                 public XmlRootAttribute XmlRoot 
205                 {
206                         get 
207                         {
208                                 return xmlRoot;}
209                         set 
210                         {
211                                 xmlRoot = value;
212                         }
213                 }
214                 public XmlTextAttribute XmlText 
215                 {
216                         get 
217                         {
218                                 return xmlText;
219                         }
220                         set 
221                         {
222                                 xmlText = value;
223                         }
224                 }
225                 public XmlTypeAttribute XmlType 
226                 {
227                         get 
228                         {
229                                 return xmlType;
230                         }
231                         set 
232                         {
233                                 xmlType = value;
234                         }
235                 }
236                 #endregion
237                 
238                 internal void AddKeyHash (System.Text.StringBuilder sb)
239                 {
240                         sb.Append ("XA ");
241                         
242                         KeyHelper.AddField (sb, 1, xmlIgnore);
243                         KeyHelper.AddField (sb, 2, xmlns);
244                         KeyHelper.AddField (sb, 3, xmlAnyAttribute!=null);
245
246                         xmlAnyElements.AddKeyHash (sb);
247                         xmlArrayItems.AddKeyHash (sb);
248                         xmlElements.AddKeyHash (sb);
249                         
250                         if (xmlArray != null)
251                                 xmlArray.AddKeyHash (sb);
252                                 
253                         if (xmlAttribute != null)
254                                 xmlAttribute.AddKeyHash (sb);
255                                 
256                         if (xmlDefaultValue == null) {
257                                 sb.Append ("n");
258                         }
259                         else if (!(xmlDefaultValue is System.DBNull)) {
260                                 string v = XmlCustomFormatter.ToXmlString (TypeTranslator.GetTypeData (xmlDefaultValue.GetType()), xmlDefaultValue);
261                                 sb.Append ("v" + v);
262                         }
263                         
264                         if (xmlEnum != null)
265                                 xmlEnum.AddKeyHash (sb);
266                                 
267                         if (xmlRoot != null)
268                                 xmlRoot.AddKeyHash (sb);
269                                 
270                         if (xmlText != null)
271                                 xmlText.AddKeyHash (sb);
272                                 
273                         if (xmlType != null)
274                                 xmlType.AddKeyHash (sb);
275                                 
276                         if (xmlChoiceIdentifier != null)
277                                 xmlChoiceIdentifier.AddKeyHash (sb);
278                                 
279                         sb.Append ("|");
280                 }
281         }
282 }