2002-06-14 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 static string xmlname = "simpleType";\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.TargetNamespace);\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                                         case XmlSchemaDerivationMethod.All:\r
78                                                 this.finalResolved = XmlSchemaDerivationMethod.All;\r
79                                                 break;\r
80                                         case XmlSchemaDerivationMethod.List:\r
81                                                 this.finalResolved = XmlSchemaDerivationMethod.List;\r
82                                                 break;\r
83                                         case XmlSchemaDerivationMethod.Union:\r
84                                                 this.finalResolved = XmlSchemaDerivationMethod.Union;\r
85                                                 break;\r
86                                         case XmlSchemaDerivationMethod.Restriction:\r
87                                                 this.finalResolved = XmlSchemaDerivationMethod.Restriction;\r
88                                                 break;\r
89                                         default:\r
90                                                 error(h,"The value of final attribute is not valid for simpleType");\r
91                                                 goto case XmlSchemaDerivationMethod.None;\r
92                                                 // use assignment from finaldefault on schema.\r
93                                         case XmlSchemaDerivationMethod.None: // b.5\r
94                                                 if(info.FinalDefault == XmlSchemaDerivationMethod.All)\r
95                                                         finalResolved = XmlSchemaDerivationMethod.All;\r
96                                                 else \r
97                                                         finalResolved = info.FinalDefault & (XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.List |\r
98                                                                 XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Union );\r
99                                                 break;\r
100                                 }\r
101                         }\r
102 \r
103                         XmlSchemaUtil.CompileID(Id,this,info.IDCollection,h);\r
104 \r
105                         if(this.Content == null) //a.3,b.2\r
106                                 error(h,"Content is required in a simpletype");\r
107                         else if(Content is XmlSchemaSimpleTypeRestriction)\r
108                         {\r
109                                 errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,info);\r
110                         }\r
111                         else if(Content is XmlSchemaSimpleTypeList)\r
112                         {\r
113                                 errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,info);\r
114                         }\r
115                         else if(Content is XmlSchemaSimpleTypeUnion)\r
116                         {\r
117                                 errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,info);\r
118                         }\r
119                         return errorCount;\r
120                 }\r
121                 \r
122                 [MonoTODO]\r
123                 internal int Validate(ValidationEventHandler h)\r
124                 {\r
125                         return errorCount;\r
126                 }\r
127 \r
128                 //<simpleType \r
129                 //  final = (#all | (list | union | restriction)) \r
130                 //  id = ID \r
131                 //  name = NCName \r
132                 //  {any attributes with non-schema namespace . . .}>\r
133                 //  Content: (annotation?, (restriction | list | union))\r
134                 //</simpleType>\r
135                 internal static XmlSchemaSimpleType Read(XmlSchemaReader reader, ValidationEventHandler h)\r
136                 {\r
137                         XmlSchemaSimpleType stype = new XmlSchemaSimpleType();\r
138                         reader.MoveToElement();\r
139 \r
140                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
141                         {\r
142                                 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);\r
143                                 reader.Skip();\r
144                                 return null;\r
145                         }\r
146 \r
147                         stype.LineNumber = reader.LineNumber;\r
148                         stype.LinePosition = reader.LinePosition;\r
149                         stype.SourceUri = reader.BaseURI;\r
150 \r
151                         while(reader.MoveToNextAttribute())\r
152                         {\r
153                                 if(reader.Name == "final")\r
154                                 {\r
155                                         Exception innerex;\r
156                                         stype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out innerex, "final");\r
157                                         if(innerex != null)\r
158                                                 error(h, "some invalid values not a valid value for final", innerex);\r
159                                 }\r
160                                 else if(reader.Name == "id")\r
161                                 {\r
162                                         stype.Id = reader.Value;\r
163                                 }\r
164                                 else if(reader.Name == "name")\r
165                                 {\r
166                                         stype.Name = reader.Value;\r
167                                 }\r
168                                 else if(reader.NamespaceURI == "" || reader.NamespaceURI == XmlSchema.Namespace)\r
169                                 {\r
170                                         error(h,reader.Name + " is not a valid attribute for simpleType",null);\r
171                                 }\r
172                                 else\r
173                                 {\r
174                                         if(reader.Prefix == "xmlns")\r
175                                                 stype.Namespaces.Add(reader.LocalName, reader.Value);\r
176                                         else if(reader.Name == "xmlns")\r
177                                                 stype.Namespaces.Add("",reader.Value);\r
178                                         //TODO: Add to Unhandled attributes\r
179                                 }\r
180                         }\r
181                         \r
182                         reader.MoveToElement();\r
183                         if(reader.IsEmptyElement)\r
184                                 return stype;\r
185 \r
186                         //      Content: (annotation?, (restriction | list | union))\r
187                         int level = 1;\r
188                         while(reader.ReadNextElement())\r
189                         {\r
190                                 if(reader.NodeType == XmlNodeType.EndElement)\r
191                                 {\r
192                                         if(reader.LocalName != xmlname)\r
193                                                 error(h,"Should not happen :2: XmlSchemaSimpleType.Read, name="+reader.Name,null);\r
194                                         break;\r
195                                 }\r
196                                 if(level <= 1 && reader.LocalName == "annotation")\r
197                                 {\r
198                                         level = 2; //Only one annotation\r
199                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
200                                         if(annotation != null)\r
201                                                 stype.Annotation = annotation;\r
202                                         continue;\r
203                                 }\r
204                                 if(level <= 2)\r
205                                 {\r
206                                         if(reader.LocalName == "restriction")\r
207                                         {\r
208                                                 level = 3;\r
209                                                 XmlSchemaSimpleTypeRestriction restriction = XmlSchemaSimpleTypeRestriction.Read(reader,h);\r
210                                                 if(restriction != null)\r
211                                                         stype.content = restriction;\r
212                                                 continue;\r
213                                         }\r
214                                         if(reader.LocalName == "list")\r
215                                         {\r
216                                                 level = 3;\r
217                                                 XmlSchemaSimpleTypeList list = XmlSchemaSimpleTypeList.Read(reader,h);\r
218                                                 if(list != null)\r
219                                                         stype.content = list;\r
220                                                 continue;\r
221                                         }\r
222                                         if(reader.LocalName == "union")\r
223                                         {\r
224                                                 level = 3;\r
225                                                 XmlSchemaSimpleTypeUnion union = XmlSchemaSimpleTypeUnion.Read(reader,h);\r
226                                                 if(union != null)\r
227                                                         stype.content = union;\r
228                                                 continue;\r
229                                         }\r
230                                 }\r
231                                 reader.RaiseInvalidElementError();\r
232                         }\r
233                         return stype;\r
234                 }\r
235 \r
236 \r
237         }\r
238 }\r