Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaType.cs
1 //
2 // XmlSchemaType.cs
3 //
4 // Authors:
5 //      Dwivedi, Ajay kumar  Adwiv@Yahoo.com
6 //      Atsushi Enomoto  atsushi@ximian.com
7 //
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.Xml;
31 using System.ComponentModel;
32 using Mono.Xml.Schema;
33 using System.Xml.Serialization;
34 #if NET_2_0_in_the_future
35 using MS.Internal.Xml;
36 #endif
37
38 namespace System.Xml.Schema
39 {
40         /// <summary>
41         /// Summary description for XmlSchemaType.
42         /// </summary>
43         public class XmlSchemaType : XmlSchemaAnnotated
44         {
45                 private XmlSchemaDerivationMethod final;
46                 private bool isMixed;
47                 private string name;
48                 bool recursed;
49
50                 internal XmlQualifiedName BaseSchemaTypeName;
51                 internal XmlSchemaType BaseXmlSchemaTypeInternal;
52                 internal XmlSchemaDatatype DatatypeInternal;
53                 internal XmlSchemaDerivationMethod resolvedDerivedBy;
54                 internal XmlSchemaDerivationMethod finalResolved;
55                 internal XmlQualifiedName QNameInternal;
56
57                 public XmlSchemaType ()
58                 {
59                         final = XmlSchemaDerivationMethod.None;
60                         QNameInternal = XmlQualifiedName.Empty;
61                 }
62
63                 #region Attributes
64                 [System.Xml.Serialization.XmlAttribute("name")]
65                 public string Name {
66                         get{ return name; }
67                         set{ name = value; }
68                 }
69
70                 [DefaultValue(XmlSchemaDerivationMethod.None)]
71                 [System.Xml.Serialization.XmlAttribute("final")]
72                 public XmlSchemaDerivationMethod Final {
73                         get{ return  final; }
74                         set{ final = value; }
75                 }
76                 #endregion
77
78                 #region Post Compilation Schema Information
79                 [XmlIgnore]
80                 public XmlQualifiedName QualifiedName {
81                         get{ return QNameInternal; }
82                 }
83
84                 [XmlIgnore]
85                 public XmlSchemaDerivationMethod FinalResolved {
86                         get{ return finalResolved; }
87                 }
88
89                 [XmlIgnore]
90 #if NET_2_0
91                 [Obsolete ("This property is going away. Use BaseXmlSchemaType instead")]
92 #endif
93                 public object BaseSchemaType {
94                         get{
95                                 if (BaseXmlSchemaType != null)
96                                         return BaseXmlSchemaType;
97                                 else if (this == XmlSchemaComplexType.AnyType)
98                                         return null; // This property is designed so.
99                                 else
100                                         return Datatype;
101                         }
102                 }
103
104                 [XmlIgnore]
105                 // FIXME:This property works as always returning a valid schema type.
106                 [MonoTODO]
107                 // In .NET 2.0, all schema types used in schema documents must
108                 // be XmlSchemaType, even if it is primitive type, in terms of
109                 // non-obsolete System.Xml.Schema members.
110                 
111                 // To modify this property, we have to make sure that it does
112                 // not affect to any compilation/validation logic.
113 #if NET_2_0
114                 public XmlSchemaType BaseXmlSchemaType {
115 #else
116                 internal XmlSchemaType BaseXmlSchemaType {
117 #endif
118                         get { return  BaseXmlSchemaTypeInternal; }
119                 }
120
121                 [XmlIgnore]
122                 public XmlSchemaDerivationMethod DerivedBy {
123                         get{ return resolvedDerivedBy; }
124                 }
125
126                 [XmlIgnore]
127                 public XmlSchemaDatatype Datatype {
128                         get{ return DatatypeInternal; }
129                 }
130
131                 [XmlIgnore]
132                 public virtual bool IsMixed {  
133                         get{ return  isMixed; }
134                         set{ isMixed = value; } 
135                 }
136
137 #if NET_2_0
138                 // LAMESPEC: for IDREFS it returns Idref. for ENTITIES 
139                 // it returns Entity. for NMTOKENS it returns NmToken.
140                 [XmlIgnore]
141                 public XmlTypeCode TypeCode {
142                         get {
143                                 if (this == XmlSchemaComplexType.AnyType)
144                                         return XmlTypeCode.Item;
145                                 if (DatatypeInternal == XmlSchemaSimpleType.AnySimpleType)
146                                         return XmlTypeCode.AnyAtomicType;
147                                 if (this == XmlSchemaSimpleType.XsIDRefs)
148                                         return XmlTypeCode.Idref;
149                                 if (this == XmlSchemaSimpleType.XsEntities)
150                                         return XmlTypeCode.Entity;
151                                 if (this == XmlSchemaSimpleType.XsNMTokens)
152                                         return XmlTypeCode.NmToken;
153                                 if (DatatypeInternal != null)
154                                         return DatatypeInternal.TypeCode;
155                                 return BaseXmlSchemaType.TypeCode;
156                         }
157                 }
158 #endif
159                 #endregion
160
161 #if NET_2_0
162                 internal static XmlSchemaType GetBuiltInType (XmlQualifiedName qualifiedName)
163                 {
164                         XmlSchemaType t = GetBuiltInSimpleType (qualifiedName);
165                         if (t == null)
166                                 t = GetBuiltInComplexType (qualifiedName);
167                         return t;
168                 }
169
170                 internal static XmlSchemaType GetBuiltInType (XmlTypeCode typecode)
171                 {
172                         if (typecode == XmlTypeCode.Item)
173                                 return XmlSchemaComplexType.AnyType;
174                         return GetBuiltInSimpleType (typecode);
175                 }
176 #endif
177
178 #if NET_2_0
179                 public static XmlSchemaComplexType GetBuiltInComplexType (XmlQualifiedName qualifiedName)
180 #else
181                 internal static XmlSchemaComplexType GetBuiltInComplexType (XmlQualifiedName qualifiedName)
182 #endif
183                 {
184                         if (qualifiedName.Name == "anyType" && qualifiedName.Namespace == XmlSchema.Namespace)
185                                 return XmlSchemaComplexType.AnyType;
186
187                         return null;
188                 }
189
190 #if NET_2_0
191                 public static XmlSchemaComplexType GetBuiltInComplexType (XmlTypeCode typeCode)
192                 {
193                         switch (typeCode) {
194                         case XmlTypeCode.Item:
195                                 return XmlSchemaComplexType.AnyType;
196                         }
197                         return null;
198                 }
199 #endif
200
201 #if NET_2_0
202                 [MonoTODO]
203                 public static XmlSchemaSimpleType GetBuiltInSimpleType (XmlQualifiedName qualifiedName)
204                 {
205                         if (qualifiedName.Namespace == "http://www.w3.org/2003/11/xpath-datatypes") {
206                                 switch (qualifiedName.Name) {
207                                 case "untypedAtomic":
208                                         return XmlSchemaSimpleType.XdtUntypedAtomic;
209                                 case "anyAtomicType":
210                                         return XmlSchemaSimpleType.XdtAnyAtomicType;
211                                 case "yearMonthDuration":
212                                         return XmlSchemaSimpleType.XdtYearMonthDuration;
213                                 case "dayTimeDuration":
214                                         return XmlSchemaSimpleType.XdtDayTimeDuration;
215                                 }
216                                 return null;
217                         }
218                         else if (qualifiedName.Namespace != XmlSchema.Namespace)
219                                 return null;
220                         switch (qualifiedName.Name) {
221                         case "anySimpleType":
222                                 return XmlSchemaSimpleType.XsAnySimpleType;
223                         case "string":
224                                 return XmlSchemaSimpleType.XsString;
225                         case "boolean":
226                                 return XmlSchemaSimpleType.XsBoolean;
227                         case "decimal":
228                                 return XmlSchemaSimpleType.XsDecimal;
229                         case "float":
230                                 return XmlSchemaSimpleType.XsFloat;
231                         case "double":
232                                 return XmlSchemaSimpleType.XsDouble;
233                         case "duration":
234                                 return XmlSchemaSimpleType.XsDuration;
235                         case "dateTime":
236                                 return XmlSchemaSimpleType.XsDateTime;
237                         case "time":
238                                 return XmlSchemaSimpleType.XsTime;
239                         case "date":
240                                 return XmlSchemaSimpleType.XsDate;
241                         case "gYearMonth":
242                                 return XmlSchemaSimpleType.XsGYearMonth;
243                         case "gYear":
244                                 return XmlSchemaSimpleType.XsGYear;
245                         case "gMonthDay":
246                                 return XmlSchemaSimpleType.XsGMonthDay;
247                         case "gDay":
248                                 return XmlSchemaSimpleType.XsGDay;
249                         case "gMonth":
250                                 return XmlSchemaSimpleType.XsGMonth;
251                         case "hexBinary":
252                                 return XmlSchemaSimpleType.XsHexBinary;
253                         case "base64Binary":
254                                 return XmlSchemaSimpleType.XsBase64Binary;
255                         case "anyURI":
256                                 return XmlSchemaSimpleType.XsAnyUri;
257                         case "QName":
258                                 return XmlSchemaSimpleType.XsQName;
259                         case "NOTATION":
260                                 return XmlSchemaSimpleType.XsNotation;
261                         case "normalizedString":
262                                 return XmlSchemaSimpleType.XsNormalizedString;
263                         case "token":
264                                 return XmlSchemaSimpleType.XsToken;
265                         case "language":
266                                 return XmlSchemaSimpleType.XsLanguage;
267                         case "NMTOKEN":
268                                 return XmlSchemaSimpleType.XsNMToken;
269                         case "NMTOKENS":
270                                 return XmlSchemaSimpleType.XsNMTokens;
271                         case "Name":
272                                 return XmlSchemaSimpleType.XsName;
273                         case "NCName":
274                                 return XmlSchemaSimpleType.XsNCName;
275                         case "ID":
276                                 return XmlSchemaSimpleType.XsID;
277                         case "IDREF":
278                                 return XmlSchemaSimpleType.XsIDRef;
279                         case "IDREFS":
280                                 return XmlSchemaSimpleType.XsIDRefs;
281                         case "ENTITY":
282                                 return XmlSchemaSimpleType.XsEntity;
283                         case "ENTITIES":
284                                 return XmlSchemaSimpleType.XsEntities;
285                         case "integer":
286                                 return XmlSchemaSimpleType.XsInteger;
287                         case "nonPositiveInteger":
288                                 return XmlSchemaSimpleType.XsNonPositiveInteger;
289                         case "negativeInteger":
290                                 return XmlSchemaSimpleType.XsNegativeInteger;
291                         case "long":
292                                 return XmlSchemaSimpleType.XsLong;
293                         case "int":
294                                 return XmlSchemaSimpleType.XsInt;
295                         case "short":
296                                 return XmlSchemaSimpleType.XsShort;
297                         case "byte":
298                                 return XmlSchemaSimpleType.XsByte;
299                         case "nonNegativeInteger":
300                                 return XmlSchemaSimpleType.XsNonNegativeInteger;
301                         case "positiveInteger":
302                                 return XmlSchemaSimpleType.XsPositiveInteger;
303                         case "unsignedLong":
304                                 return XmlSchemaSimpleType.XsUnsignedLong;
305                         case "unsignedInt":
306                                 return XmlSchemaSimpleType.XsUnsignedInt;
307                         case "unsignedShort":
308                                 return XmlSchemaSimpleType.XsUnsignedShort;
309                         case "unsignedByte":
310                                 return XmlSchemaSimpleType.XsUnsignedByte;
311                         }
312                         return null;
313                 }
314
315                 internal static XmlSchemaSimpleType GetBuiltInSimpleType (XmlSchemaDatatype type)
316                 {
317                         if (type is XsdEntities)
318                                 return XmlSchemaSimpleType.XsEntities;
319                         else if (type is XsdNMTokens)
320                                 return XmlSchemaSimpleType.XsNMTokens;
321                         else if (type is XsdIDRefs)
322                                 return XmlSchemaSimpleType.XsIDRefs;
323                         else
324                                 return GetBuiltInSimpleType (type.TypeCode);
325                 }
326
327                 [MonoTODO]
328                 // Don't use this method to cover all XML Schema datatypes.
329                 public static XmlSchemaSimpleType GetBuiltInSimpleType (XmlTypeCode typeCode)
330                 {
331                         switch (typeCode) {
332                         case XmlTypeCode.None:
333                         case XmlTypeCode.Item:
334                         case XmlTypeCode.Node:
335                         case XmlTypeCode.Document: // node
336                         case XmlTypeCode.Element: // node
337                         case XmlTypeCode.Attribute: // node
338                         case XmlTypeCode.Namespace: // node
339                         case XmlTypeCode.ProcessingInstruction: // node
340                         case XmlTypeCode.Comment: // node
341                         case XmlTypeCode.Text:  // node
342                                 return null;
343                         case XmlTypeCode.AnyAtomicType:
344                                 return XmlSchemaSimpleType.XdtAnyAtomicType;
345                         case XmlTypeCode.UntypedAtomic:
346                                 return XmlSchemaSimpleType.XdtUntypedAtomic;
347                         case XmlTypeCode.String:
348                                 return XmlSchemaSimpleType.XsString;
349                         case XmlTypeCode.Boolean:
350                                 return XmlSchemaSimpleType.XsBoolean;
351                         case XmlTypeCode.Decimal:
352                                 return XmlSchemaSimpleType.XsDecimal;
353                         case XmlTypeCode.Float:
354                                 return XmlSchemaSimpleType.XsFloat;
355                         case XmlTypeCode.Double:
356                                 return XmlSchemaSimpleType.XsDouble;
357                         case XmlTypeCode.Duration:
358                                 return XmlSchemaSimpleType.XsDuration;
359                         case XmlTypeCode.DateTime:
360                                 return XmlSchemaSimpleType.XsDateTime;
361                         case XmlTypeCode.Time:
362                                 return XmlSchemaSimpleType.XsTime;
363                         case XmlTypeCode.Date:
364                                 return XmlSchemaSimpleType.XsDate;
365                         case XmlTypeCode.GYearMonth:
366                                 return XmlSchemaSimpleType.XsGYearMonth;
367                         case XmlTypeCode.GYear:
368                                 return XmlSchemaSimpleType.XsGYear;
369                         case XmlTypeCode.GMonthDay:
370                                 return XmlSchemaSimpleType.XsGMonthDay;
371                         case XmlTypeCode.GDay:
372                                 return XmlSchemaSimpleType.XsGDay;
373                         case XmlTypeCode.GMonth:
374                                 return XmlSchemaSimpleType.XsGMonth;
375                         case XmlTypeCode.HexBinary:
376                                 return XmlSchemaSimpleType.XsHexBinary;
377                         case XmlTypeCode.Base64Binary:
378                                 return XmlSchemaSimpleType.XsBase64Binary;
379                         case XmlTypeCode.AnyUri:
380                                 return XmlSchemaSimpleType.XsAnyUri;
381                         case XmlTypeCode.QName:
382                                 return XmlSchemaSimpleType.XsQName;
383                         case XmlTypeCode.Notation:
384                                 return XmlSchemaSimpleType.XsNotation;
385                         case XmlTypeCode.NormalizedString:
386                                 return XmlSchemaSimpleType.XsNormalizedString;
387                         case XmlTypeCode.Token:
388                                 return XmlSchemaSimpleType.XsToken;
389                         case XmlTypeCode.Language:
390                                 return XmlSchemaSimpleType.XsLanguage;
391                         case XmlTypeCode.NmToken: // NmTokens is not primitive
392                                 return XmlSchemaSimpleType.XsNMToken;
393                         case XmlTypeCode.Name:
394                                 return XmlSchemaSimpleType.XsName;
395                         case XmlTypeCode.NCName:
396                                 return XmlSchemaSimpleType.XsNCName;
397                         case XmlTypeCode.Id:
398                                 return XmlSchemaSimpleType.XsID;
399                         case XmlTypeCode.Idref: // Idrefs is not primitive
400                                 return XmlSchemaSimpleType.XsIDRef;
401                         case XmlTypeCode.Entity: // Entities is not primitive
402                                 return XmlSchemaSimpleType.XsEntity;
403                         case XmlTypeCode.Integer:
404                                 return XmlSchemaSimpleType.XsInteger;
405                         case XmlTypeCode.NonPositiveInteger:
406                                 return XmlSchemaSimpleType.XsNonPositiveInteger;
407                         case XmlTypeCode.NegativeInteger:
408                                 return XmlSchemaSimpleType.XsNegativeInteger;
409                         case XmlTypeCode.Long:
410                                 return XmlSchemaSimpleType.XsLong;
411                         case XmlTypeCode.Int:
412                                 return XmlSchemaSimpleType.XsInt;
413                         case XmlTypeCode.Short:
414                                 return XmlSchemaSimpleType.XsShort;
415                         case XmlTypeCode.Byte:
416                                 return XmlSchemaSimpleType.XsByte;
417                         case XmlTypeCode.NonNegativeInteger:
418                                 return XmlSchemaSimpleType.XsNonNegativeInteger;
419                         case XmlTypeCode.UnsignedLong:
420                                 return XmlSchemaSimpleType.XsUnsignedLong;
421                         case XmlTypeCode.UnsignedInt:
422                                 return XmlSchemaSimpleType.XsUnsignedInt;
423                         case XmlTypeCode.UnsignedShort:
424                                 return XmlSchemaSimpleType.XsUnsignedShort;
425                         case XmlTypeCode.UnsignedByte:
426                                 return XmlSchemaSimpleType.XsUnsignedByte;
427                         case XmlTypeCode.PositiveInteger:
428                                 return XmlSchemaSimpleType.XsPositiveInteger;
429                         case XmlTypeCode.YearMonthDuration:
430                                 return XmlSchemaSimpleType.XdtYearMonthDuration;
431                         case XmlTypeCode.DayTimeDuration:
432                                 return XmlSchemaSimpleType.XdtDayTimeDuration;
433                         }
434                         return null;
435                 }
436
437                 public static bool IsDerivedFrom (XmlSchemaType derivedType, XmlSchemaType baseType, XmlSchemaDerivationMethod except)
438                 {
439                         if (derivedType.BaseXmlSchemaType == null)
440                                 return false;
441                         if ((derivedType.DerivedBy & except) != 0)
442                                 return false;
443                         if (derivedType.BaseXmlSchemaType == baseType)
444                                 return true;
445                         return IsDerivedFrom (derivedType.BaseXmlSchemaType,
446                                 baseType, except);
447                 }
448 #endif
449
450                 internal bool ValidateRecursionCheck ()
451                 {
452                         if (recursed)
453                                 return (this != XmlSchemaComplexType.AnyType);
454                         recursed = true;
455                         XmlSchemaType baseType = this.BaseXmlSchemaType as XmlSchemaType;
456                         bool result = false;
457                         if (baseType != null)
458                                 result = baseType.ValidateRecursionCheck ();
459                         recursed = false;
460                         return result;
461                 }
462         }
463 }