2002-05-25 Dwivedi, Ajay kumar <adwiv@yahoo.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleType.cs
1 // Author: Dwivedi, Ajay kumar\r
2 //            Adwiv@Yahoo.com\r
3 using System;\r
4 using System.Xml.Serialization;\r
5 using System.Xml;\r
6 \r
7 namespace System.Xml.Schema\r
8 {\r
9         /// <summary>\r
10         /// Summary description for XmlSchemaSimpleType.\r
11         /// </summary>\r
12         public class XmlSchemaSimpleType : XmlSchemaType\r
13         {\r
14                 private XmlSchemaSimpleTypeContent content;\r
15                 //compilation vars\r
16                 internal bool islocal = true; // Assuming local means we have to specify islocal=false only in XmlSchema\r
17                 private int errorCount;\r
18 \r
19                 public XmlSchemaSimpleType()\r
20                 {\r
21                 }\r
22 \r
23                 [XmlElement("restriction",typeof(XmlSchemaSimpleTypeRestriction),Namespace="http://www.w3.org/2001/XMLSchema")]\r
24                 [XmlElement("list",typeof(XmlSchemaSimpleTypeList),Namespace="http://www.w3.org/2001/XMLSchema")]\r
25                 [XmlElement("union",typeof(XmlSchemaSimpleTypeUnion),Namespace="http://www.w3.org/2001/XMLSchema")]\r
26                 public XmlSchemaSimpleTypeContent Content\r
27                 {\r
28                         get{ return  content; } \r
29                         set{ content = value; }\r
30                 }\r
31 \r
32                 /// <remarks>\r
33                 /// For a simple Type:\r
34                 ///             1. Content must be present\r
35                 ///             2. id if present, must have be a valid ID\r
36                 ///             a) If the simpletype is local\r
37                 ///                     1-      are from <xs:complexType name="simpleType"> and <xs:complexType name="localSimpleType">\r
38                 ///                     1. name  is prohibited\r
39                 ///                     2. final is prohibited\r
40                 ///             b) If the simpletype is toplevel\r
41                 ///                     1-  are from <xs:complexType name="simpleType"> and <xs:complexType name="topLevelSimpleType">\r
42                 ///                     1. name is required, type must be NCName\r
43                 ///                     2. Content is required\r
44                 ///                     3. final can have values : #all | (list | union | restriction)\r
45                 ///                     4. If final is set, finalResolved is same as final (but within the values of b.3)\r
46                 ///                     5. If final is not set, the finalDefault of the schema (ie. only #all and restriction)\r
47                 ///                     6. Base type is:\r
48                 ///                             4.1 If restriction is chosen,the base type of restriction or elements\r
49                 ///                             4.2 otherwise simple ur-type\r
50                 /// </remarks>\r
51                 [MonoTODO]\r
52                 internal int Compile(ValidationEventHandler h, XmlSchemaInfo info)\r
53                 {\r
54                         errorCount = 0;\r
55 \r
56                         if(this.islocal) // a\r
57                         {\r
58                                 if(this.Name != null) // a.1\r
59                                         error(h,"Name is prohibited in a local simpletype");\r
60                                 if(this.Final != XmlSchemaDerivationMethod.None) //a.2\r
61                                         error(h,"Final is prohibited in a local simpletype");\r
62                         }\r
63                         else //b\r
64                         {\r
65                                 if(this.Name == null) //b.1\r
66                                         error(h,"Name is required in top level simpletype");\r
67                                 else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2\r
68                                         error(h,"name attribute of a simpleType must be NCName");\r
69                                 else\r
70                                         this.qName = new XmlQualifiedName(this.Name,info.targetNS);\r
71                                 \r
72                                 //NOTE: Although the FinalResolved can be Empty, it is not a valid value for Final\r
73                                 //DEVIATION: If an error occurs, the finaldefault is always consulted. This deviates\r
74                                 //                       from the way MS implementation works.\r
75                                 switch(this.Final) //b.3, b.4\r
76                                 {\r
77                                                 // Invalid values: Throw error and use "prohibited substitutions"\r
78                                         case XmlSchemaDerivationMethod.Substitution:\r
79                                                 error(h,"substition is not a valid value for final in a simpletype");\r
80                                                 goto case XmlSchemaDerivationMethod.None;\r
81                                         case XmlSchemaDerivationMethod.Extension:\r
82                                                 error(h,"extension is not a valid value for final in a simpletype");\r
83                                                 goto case XmlSchemaDerivationMethod.None;\r
84                                         case XmlSchemaDerivationMethod.Empty:\r
85                                                 error(h,"empty is not a valid value for final in simpletype");\r
86                                                 goto case XmlSchemaDerivationMethod.None;\r
87                                                 //valid cases:\r
88                                         case XmlSchemaDerivationMethod.All:\r
89                                                 this.finalResolved = XmlSchemaDerivationMethod.All;\r
90                                                 break;\r
91                                         case XmlSchemaDerivationMethod.List:\r
92                                                 this.finalResolved = XmlSchemaDerivationMethod.List;\r
93                                                 break;\r
94                                         case XmlSchemaDerivationMethod.Union:\r
95                                                 this.finalResolved = XmlSchemaDerivationMethod.Union;\r
96                                                 break;\r
97                                         case XmlSchemaDerivationMethod.Restriction:\r
98                                                 this.finalResolved = XmlSchemaDerivationMethod.Restriction;\r
99                                                 break;\r
100                                                 // If mutliple values are specified\r
101                                         default:\r
102                                                 error(h,"simpletype can't have more than one value for final");\r
103                                                 goto case XmlSchemaDerivationMethod.None;\r
104                                                 // use assignment from finaldefault on schema.\r
105                                                 // The possible values of finalDefault on schema are #all | List of (extension | restriction)\r
106                                                 // Of these, the only possible values for us are #all | restriction.\r
107                                         case XmlSchemaDerivationMethod.None: // b.5\r
108                                                 if(info.finalDefault == XmlSchemaDerivationMethod.All)\r
109                                                         finalResolved = XmlSchemaDerivationMethod.All;\r
110                                                 else // Either Restriction or Empty\r
111                                                         finalResolved = info.finalDefault & XmlSchemaDerivationMethod.Restriction;\r
112                                                 break;\r
113                                 }\r
114                         }\r
115 \r
116                         if(this.Id != null && !XmlSchemaUtil.CheckID(this.Id))\r
117                                 error(h,"id must be a valid ID");\r
118 \r
119                         if(this.Content == null) //a.3,b.2\r
120                                 error(h,"Content is required in a simpletype");\r
121                         else if(Content is XmlSchemaSimpleTypeRestriction)\r
122                         {\r
123                                 errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,info);\r
124                         }\r
125                         else if(Content is XmlSchemaSimpleTypeList)\r
126                         {\r
127                                 errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,info);\r
128                         }\r
129                         else if(Content is XmlSchemaSimpleTypeUnion)\r
130                         {\r
131                                 errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,info);\r
132                         }\r
133                         return errorCount;\r
134                 }\r
135                 \r
136                 [MonoTODO]\r
137                 internal int Validate(ValidationEventHandler h)\r
138                 {\r
139                         return errorCount;\r
140                 }\r
141                 \r
142                 internal void error(ValidationEventHandler handle,string message)\r
143                 {\r
144                         this.errorCount++;\r
145                         ValidationHandler.RaiseValidationError(handle,this,message);\r
146                 }\r
147         }\r
148 }\r