5782ca77efc7cde1a4564988d717a1f2e2313f4e
[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=XmlSchema.Namespace)]\r
24                 [XmlElement("list",typeof(XmlSchemaSimpleTypeList),Namespace=XmlSchema.Namespace)]\r
25                 [XmlElement("union",typeof(XmlSchemaSimpleTypeUnion),Namespace=XmlSchema.Namespace)]\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, XmlSchema schema)\r
53                 {\r
54                         // If this is already compiled this time, simply skip.\r
55                         if (this.IsComplied (schema.CompilationId))\r
56                                 return 0;\r
57 \r
58                         errorCount = 0;\r
59 \r
60                         if(this.islocal) // a\r
61                         {\r
62                                 if(this.Name != null) // a.1\r
63                                         error(h,"Name is prohibited in a local simpletype");\r
64                                 if(this.Final != XmlSchemaDerivationMethod.None) //a.2\r
65                                         error(h,"Final is prohibited in a local simpletype");\r
66                         }\r
67                         else //b\r
68                         {\r
69                                 if(this.Name == null) //b.1\r
70                                         error(h,"Name is required in top level simpletype");\r
71                                 else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2\r
72                                         error(h,"name attribute of a simpleType must be NCName");\r
73                                 else\r
74                                         this.qName = new XmlQualifiedName(this.Name,schema.TargetNamespace);\r
75                                 \r
76                                 //NOTE: Although the FinalResolved can be Empty, it is not a valid value for Final\r
77                                 //DEVIATION: If an error occurs, the finaldefault is always consulted. This deviates\r
78                                 //                       from the way MS implementation works.\r
79                                 switch(this.Final) //b.3, b.4\r
80                                 {\r
81                                         case XmlSchemaDerivationMethod.All:\r
82                                                 this.finalResolved = XmlSchemaDerivationMethod.All;\r
83                                                 break;\r
84                                         case XmlSchemaDerivationMethod.List:\r
85                                                 this.finalResolved = XmlSchemaDerivationMethod.List;\r
86                                                 break;\r
87                                         case XmlSchemaDerivationMethod.Union:\r
88                                                 this.finalResolved = XmlSchemaDerivationMethod.Union;\r
89                                                 break;\r
90                                         case XmlSchemaDerivationMethod.Restriction:\r
91                                                 this.finalResolved = XmlSchemaDerivationMethod.Restriction;\r
92                                                 break;\r
93                                         default:\r
94                                                 error(h,"The value of final attribute is not valid for simpleType");\r
95                                                 goto case XmlSchemaDerivationMethod.None;\r
96                                                 // use assignment from finaldefault on schema.\r
97                                         case XmlSchemaDerivationMethod.None: // b.5\r
98                                                 XmlSchemaDerivationMethod flags = \r
99                                                         (XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.List |\r
100                                                         XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Union );\r
101                                                 if(schema.FinalDefault == XmlSchemaDerivationMethod.All)\r
102                                                         finalResolved = XmlSchemaDerivationMethod.All;\r
103                                                 else if(schema.FinalDefault == XmlSchemaDerivationMethod.None)\r
104                                                         finalResolved = flags;\r
105                                                 else \r
106                                                         finalResolved = schema.FinalDefault & flags;\r
107                                                 break;\r
108                                 }\r
109                         }\r
110 \r
111                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
112 \r
113                         if(this.Content == null) //a.3,b.2\r
114                                 error(h,"Content is required in a simpletype");\r
115                         else if(Content is XmlSchemaSimpleTypeRestriction)\r
116                         {\r
117                                 errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,schema);\r
118                         }\r
119                         else if(Content is XmlSchemaSimpleTypeList)\r
120                         {\r
121                                 errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,schema);\r
122                         }\r
123                         else if(Content is XmlSchemaSimpleTypeUnion)\r
124                         {\r
125                                 errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,schema);\r
126                         }\r
127                         this.CompilationId = schema.CompilationId;\r
128                         return errorCount;\r
129                 }\r
130                 \r
131                 [MonoTODO]\r
132                 internal int Validate(ValidationEventHandler h, XmlSchema schema)\r
133                 {\r
134                         if(isCompiled)\r
135                                 return errorCount;\r
136 \r
137                         return errorCount;\r
138                 }\r
139 \r
140                 //<simpleType \r
141                 //  final = (#all | (list | union | restriction)) \r
142                 //  id = ID \r
143                 //  name = NCName \r
144                 //  {any attributes with non-schema namespace . . .}>\r
145                 //  Content: (annotation?, (restriction | list | union))\r
146                 //</simpleType>\r
147                 internal static XmlSchemaSimpleType Read(XmlSchemaReader reader, ValidationEventHandler h)\r
148                 {\r
149                         XmlSchemaSimpleType stype = new XmlSchemaSimpleType();\r
150                         reader.MoveToElement();\r
151 \r
152                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
153                         {\r
154                                 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);\r
155                                 reader.Skip();\r
156                                 return null;\r
157                         }\r
158 \r
159                         stype.LineNumber = reader.LineNumber;\r
160                         stype.LinePosition = reader.LinePosition;\r
161                         stype.SourceUri = reader.BaseURI;\r
162 \r
163                         while(reader.MoveToNextAttribute())\r
164                         {\r
165                                 if(reader.Name == "final")\r
166                                 {\r
167                                         Exception innerex;\r
168                                         stype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out innerex, "final");\r
169                                         if(innerex != null)\r
170                                                 error(h, "some invalid values not a valid value for final", innerex);\r
171                                 }\r
172                                 else if(reader.Name == "id")\r
173                                 {\r
174                                         stype.Id = reader.Value;\r
175                                 }\r
176                                 else if(reader.Name == "name")\r
177                                 {\r
178                                         stype.Name = reader.Value;\r
179                                 }\r
180                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
181                                 {\r
182                                         error(h,reader.Name + " is not a valid attribute for simpleType",null);\r
183                                 }\r
184                                 else\r
185                                 {\r
186                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,stype);\r
187                                 }\r
188                         }\r
189                         \r
190                         reader.MoveToElement();\r
191                         if(reader.IsEmptyElement)\r
192                                 return stype;\r
193 \r
194                         //      Content: (annotation?, (restriction | list | union))\r
195                         int level = 1;\r
196                         while(reader.ReadNextElement())\r
197                         {\r
198                                 if(reader.NodeType == XmlNodeType.EndElement)\r
199                                 {\r
200                                         if(reader.LocalName != xmlname)\r
201                                                 error(h,"Should not happen :2: XmlSchemaSimpleType.Read, name="+reader.Name,null);\r
202                                         break;\r
203                                 }\r
204                                 if(level <= 1 && reader.LocalName == "annotation")\r
205                                 {\r
206                                         level = 2; //Only one annotation\r
207                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
208                                         if(annotation != null)\r
209                                                 stype.Annotation = annotation;\r
210                                         continue;\r
211                                 }\r
212                                 if(level <= 2)\r
213                                 {\r
214                                         if(reader.LocalName == "restriction")\r
215                                         {\r
216                                                 level = 3;\r
217                                                 XmlSchemaSimpleTypeRestriction restriction = XmlSchemaSimpleTypeRestriction.Read(reader,h);\r
218                                                 if(restriction != null)\r
219                                                         stype.content = restriction;\r
220                                                 continue;\r
221                                         }\r
222                                         if(reader.LocalName == "list")\r
223                                         {\r
224                                                 level = 3;\r
225                                                 XmlSchemaSimpleTypeList list = XmlSchemaSimpleTypeList.Read(reader,h);\r
226                                                 if(list != null)\r
227                                                         stype.content = list;\r
228                                                 continue;\r
229                                         }\r
230                                         if(reader.LocalName == "union")\r
231                                         {\r
232                                                 level = 3;\r
233                                                 XmlSchemaSimpleTypeUnion union = XmlSchemaSimpleTypeUnion.Read(reader,h);\r
234                                                 if(union != null)\r
235                                                         stype.content = union;\r
236                                                 continue;\r
237                                         }\r
238                                 }\r
239                                 reader.RaiseInvalidElementError();\r
240                         }\r
241                         return stype;\r
242                 }\r
243 \r
244 \r
245         }\r
246 }\r