2002-06-10 Dwivedi, Ajay kumar <adwiv@yahoo.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchema.cs
1 // Author: Dwivedi, Ajay kumar\r
2 //            Adwiv@Yahoo.com\r
3 using System;\r
4 using System.Xml;\r
5 using System.IO;\r
6 using System.Xml.Serialization;\r
7 using System.ComponentModel;\r
8 \r
9 namespace System.Xml.Schema\r
10 {\r
11         /// <summary>\r
12         /// Summary description for XmlSchema.\r
13         /// </summary>\r
14         [XmlRoot("schema",Namespace="http://www.w3.org/2001/XMLSchema")]\r
15         public class XmlSchema : XmlSchemaObject\r
16         {\r
17                 //public constants\r
18                 public const string InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";\r
19                 public const string Namespace = "http://www.w3.org/2001/XMLSchema";\r
20 \r
21                 //private fields\r
22                 private XmlSchemaForm attributeFormDefault ;\r
23                 private XmlSchemaObjectTable attributeGroups ;\r
24                 private XmlSchemaObjectTable attributes ;\r
25                 private XmlSchemaDerivationMethod blockDefault ;\r
26                 private XmlSchemaForm elementFormDefault ;\r
27                 private XmlSchemaObjectTable elements ;\r
28                 private XmlSchemaDerivationMethod finalDefault ;\r
29                 private XmlSchemaObjectTable groups ;\r
30                 private string id ;\r
31                 private XmlSchemaObjectCollection includes ;\r
32                 private bool isCompiled ;\r
33                 private XmlSchemaObjectCollection items ;\r
34                 private XmlSchemaObjectTable notations ;\r
35                 private XmlSchemaObjectTable schemaTypes ;\r
36                 private string targetNamespace ;\r
37                 private XmlAttribute[] unhandledAttributes ;\r
38                 private string version;\r
39                 private string language;\r
40                 \r
41                 // Compiler specific things\r
42                 private XmlSchemaInfo info;\r
43                 private static string xmlname = "schema";\r
44 \r
45                 public XmlSchema()\r
46                 {\r
47                         attributeFormDefault= XmlSchemaForm.None;\r
48                         blockDefault            = XmlSchemaDerivationMethod.None;\r
49                         elementFormDefault      = XmlSchemaForm.None;\r
50                         finalDefault            = XmlSchemaDerivationMethod.None;\r
51                         includes                        = new XmlSchemaObjectCollection();\r
52                         isCompiled                      = false;\r
53                         items                           = new XmlSchemaObjectCollection();\r
54                         attributeGroups         = new XmlSchemaObjectTable();\r
55                         attributes                      = new XmlSchemaObjectTable();\r
56                         elements                        = new XmlSchemaObjectTable();\r
57                         groups                          = new XmlSchemaObjectTable();\r
58                         notations                       = new XmlSchemaObjectTable();\r
59                         schemaTypes                     = new XmlSchemaObjectTable();\r
60                 }\r
61 \r
62                 #region Properties\r
63 \r
64                 [DefaultValue(XmlSchemaForm.None)]\r
65                 [System.Xml.Serialization.XmlAttribute("attributeFormDefault")]\r
66                 public XmlSchemaForm AttributeFormDefault \r
67                 {\r
68                         get{ return attributeFormDefault; }\r
69                         set{ this.attributeFormDefault = value;}\r
70                 }\r
71 \r
72                 [XmlIgnore]\r
73                 public XmlSchemaObjectTable AttributeGroups \r
74                 {\r
75                         get{ return attributeGroups; }\r
76                 }\r
77                 \r
78                 [XmlIgnore]\r
79                 public XmlSchemaObjectTable Attributes \r
80                 {\r
81                         get{ return attributes;}\r
82                 }\r
83                 \r
84                 [DefaultValue(XmlSchemaDerivationMethod.None)]\r
85                 [System.Xml.Serialization.XmlAttribute("blockDefault")]\r
86                 public XmlSchemaDerivationMethod BlockDefault \r
87                 {\r
88                         get{ return blockDefault;}\r
89                         set{ blockDefault = value;}\r
90                 }\r
91                 \r
92                 [DefaultValue(XmlSchemaForm.None)]\r
93                 [System.Xml.Serialization.XmlAttribute("elementFormDefault")]\r
94                 public XmlSchemaForm ElementFormDefault \r
95                 {\r
96                         get{ return elementFormDefault;}\r
97                         set{ elementFormDefault = value;}\r
98                 }\r
99 \r
100                 [XmlIgnore]\r
101                 public XmlSchemaObjectTable Elements \r
102                 {\r
103                         get{ return elements;}\r
104                 }\r
105 \r
106                 [DefaultValue(XmlSchemaDerivationMethod.None)]\r
107                 [System.Xml.Serialization.XmlAttribute("finalDefault")]\r
108                 public XmlSchemaDerivationMethod FinalDefault \r
109                 {\r
110                         get{ return finalDefault;}\r
111                         set{ finalDefault = value;}\r
112                 }\r
113 \r
114                 [XmlIgnore]\r
115                 public XmlSchemaObjectTable Groups \r
116                 {\r
117                         get{ return groups;}\r
118                 }\r
119 \r
120                 [System.Xml.Serialization.XmlAttribute("id")]\r
121                 public string Id \r
122                 {\r
123                         get{ return id;}\r
124                         set{ id = value;}\r
125                 }\r
126                 \r
127                 [XmlElement("include",typeof(XmlSchemaInclude),Namespace="http://www.w3.org/2001/XMLSchema")]\r
128                 [XmlElement("import",typeof(XmlSchemaImport),Namespace="http://www.w3.org/2001/XMLSchema")]\r
129                 [XmlElement("redefine",typeof(XmlSchemaRedefine),Namespace="http://www.w3.org/2001/XMLSchema")]\r
130                 public XmlSchemaObjectCollection Includes \r
131                 {\r
132                         get{ return includes;}\r
133                 }\r
134                 \r
135                 [XmlIgnore]\r
136                 public bool IsCompiled \r
137                 {\r
138                         get{ return isCompiled;}\r
139                 }\r
140                 \r
141                 [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]\r
142                 [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]\r
143                 [XmlElement("group",typeof(XmlSchemaGroup),Namespace="http://www.w3.org/2001/XMLSchema")]\r
144                         //Only Schema's attributeGroup has type XmlSchemaAttributeGroup.\r
145                         //Others (complextype, restrictions etc) must have XmlSchemaAttributeGroupRef\r
146                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroup),Namespace="http://www.w3.org/2001/XMLSchema")]\r
147                 [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]\r
148                 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]\r
149                 [XmlElement("notation",typeof(XmlSchemaNotation),Namespace="http://www.w3.org/2001/XMLSchema")]\r
150                 [XmlElement("annotation",typeof(XmlSchemaAnnotation),Namespace="http://www.w3.org/2001/XMLSchema")]\r
151                 public XmlSchemaObjectCollection Items \r
152                 {\r
153                         get{ return items;}\r
154                 }\r
155                 \r
156                 [XmlIgnore]\r
157                 public XmlSchemaObjectTable Notations \r
158                 {\r
159                         get{ return notations;}\r
160                 }\r
161                 \r
162                 [XmlIgnore]\r
163                 public XmlSchemaObjectTable SchemaTypes \r
164                 {\r
165                         get{ return schemaTypes; }\r
166                 }\r
167                 \r
168                 [System.Xml.Serialization.XmlAttribute("targetNamespace")]\r
169                 public string TargetNamespace \r
170                 {\r
171                         get{ return targetNamespace;}\r
172                         set{ targetNamespace = value;}\r
173                 }\r
174                 \r
175                 [XmlAnyAttribute]\r
176                 public XmlAttribute[] UnhandledAttributes \r
177                 {\r
178                         get{ return unhandledAttributes;}\r
179                         set{ unhandledAttributes = value;}\r
180                 }\r
181                 \r
182                 [System.Xml.Serialization.XmlAttribute("version")]\r
183                 public string Version \r
184                 {\r
185                         get{ return version;}\r
186                         set{ version = value;}\r
187                 }\r
188 \r
189                 // New attribute defined in W3C schema element\r
190                 [System.Xml.Serialization.XmlAttribute("xml:lang")]\r
191                 public string Language\r
192                 {\r
193                         get{ return  language; }\r
194                         set{ language = value; }\r
195                 }\r
196 \r
197                 #endregion\r
198 \r
199                 // Methods\r
200                 /// <summary>\r
201                 /// This compile method does two things:\r
202                 /// 1. It compiles and fills the PSVI dataset\r
203                 /// 2. Validates the schema by calling Validate method.\r
204                 /// Every XmlSchemaObject has a Compile Method which gets called.\r
205                 /// </summary>\r
206                 /// <remarks>\r
207                 ///             1. blockDefault must be one of #all | List of (extension | restriction | substitution)\r
208                 ///             2. finalDefault must be one of (#all | List of (extension | restriction| union| list))\r
209                 ///             3. id must be of type ID\r
210                 ///             4. targetNamespace should be any uri\r
211                 ///             5. version should be a token\r
212                 ///             6. xml:lang should be a language\r
213                 ///             \r
214                 /// </remarks>\r
215                 [MonoTODO]\r
216                 public void Compile(ValidationEventHandler handler)\r
217                 {\r
218                         //1. Union and List are not allowed in block default\r
219                         if(this.blockDefault != XmlSchemaDerivationMethod.All)\r
220                         {\r
221                                 if((this.blockDefault & XmlSchemaDerivationMethod.List)!=0 )\r
222                                         error(handler, "list is not allowed in blockDefault attribute");\r
223                                 if((this.blockDefault & XmlSchemaDerivationMethod.Union)!=0 )\r
224                                         error(handler, "union is not allowed in blockDefault attribute");\r
225                         }\r
226                         //2. Substitution is not allowed in finaldefault.\r
227                         if(this.finalDefault != XmlSchemaDerivationMethod.All)\r
228                         {\r
229                                 if((this.finalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )\r
230                                         error(handler, "substitution is not allowed in finalDefault attribute");\r
231                         }\r
232                         //3. id must be of type ID\r
233                         if(this.id != null && !XmlSchemaUtil.CheckID(this.id))\r
234                                 error(handler, "id attribute is not a valid ID");\r
235 \r
236                         //4. targetNamespace should be of type anyURI\r
237                         if(!XmlSchemaUtil.CheckAnyUri(this.targetNamespace))\r
238                                 error(handler, "targetNamespace is not a valid URI");\r
239 \r
240                         //5. version should be of type TOKEN\r
241                         if(!XmlSchemaUtil.CheckToken(this.version))\r
242                                 error(handler, "version is not a valid token");\r
243 \r
244                         //6. xml:lang must be a language\r
245                         if(!XmlSchemaUtil.CheckLanguage(this.language))\r
246                                 error(handler, "xml:lang is not a valid language");\r
247 \r
248                         // Create the xmlschemainfo object which we use to pass variables like targetnamespace;\r
249                         info = new XmlSchemaInfo();\r
250                         if(this.targetNamespace != null && XmlSchemaUtil.CheckAnyUri(this.targetNamespace))\r
251                                 info.targetNS = this.TargetNamespace;\r
252                         \r
253                         if(this.ElementFormDefault != XmlSchemaForm.Qualified)\r
254                                 info.formDefault = XmlSchemaForm.Unqualified;\r
255                         else\r
256                                 info.formDefault = XmlSchemaForm.Qualified;\r
257 \r
258                         if(FinalDefault == XmlSchemaDerivationMethod.All)\r
259                                 info.finalDefault = XmlSchemaDerivationMethod.All;\r
260                         else // If finalDefault is None, info's finalDefault is set to empty\r
261                                 info.finalDefault = (FinalDefault & (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction));\r
262 \r
263                         if(BlockDefault == XmlSchemaDerivationMethod.All)\r
264                                 info.blockDefault = XmlSchemaDerivationMethod.All;\r
265                         else // If finalDefault is None, info's blockDefault is set to empty\r
266                                 info.blockDefault = (blockDefault & (XmlSchemaDerivationMethod.Extension |\r
267                                                                         XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.Substitution));\r
268 \r
269                         // Compile the content of this schema\r
270                         foreach(XmlSchemaObject obj in Includes)\r
271                         {\r
272                                 if(obj is XmlSchemaExternal)\r
273                                 {\r
274                                         //FIXME: Kuch to karo! (Do Something ;)\r
275                                 }\r
276                                 else\r
277                                 {\r
278                                         error(handler,"Object of Type "+obj.GetType().Name+" is not valid in Includes Property of Schema");\r
279                                 }\r
280                         }\r
281                         foreach(XmlSchemaObject obj in Items)\r
282                         {\r
283                                 if(obj is XmlSchemaAnnotation)\r
284                                 {\r
285                                         if(((XmlSchemaAnnotation)obj).Compile(handler,info) == 0)\r
286                                         {\r
287                                                 //FIXME: What PSVI set do we add this to?\r
288                                         }\r
289                                 }\r
290                                 else if(obj is XmlSchemaAttribute)\r
291                                 {\r
292                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
293                                         attr.parentIsSchema = true;\r
294                                         if(attr.Compile(handler,info) == 0)\r
295                                         {\r
296                                                 Attributes.Add(attr.QualifiedName, attr);\r
297                                         }\r
298                                 }\r
299                                 else if(obj is XmlSchemaAttributeGroup)\r
300                                 {\r
301                                         XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;\r
302                                         if(attrgrp.Compile(handler,info) == 0)\r
303                                         {\r
304                                                 AttributeGroups.Add(attrgrp.QualifiedName, attrgrp);\r
305                                         }\r
306                                 }\r
307                                 else if(obj is XmlSchemaComplexType)\r
308                                 {\r
309                                         XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;\r
310                                         ctype.istoplevel = true;\r
311                                         if(ctype.Compile(handler,info) == 0)\r
312                                         {\r
313                                                 schemaTypes.Add(ctype.QualifiedName, ctype);\r
314                                         }\r
315                                 }\r
316                                 else if(obj is XmlSchemaSimpleType)\r
317                                 {\r
318                                         XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;\r
319                                         stype.islocal = false; //This simple type is toplevel\r
320                                         if(stype.Compile(handler,info) == 0)\r
321                                         {\r
322                                                 SchemaTypes.Add(stype.QualifiedName, stype);\r
323                                         }\r
324                                 }\r
325                                 else if(obj is XmlSchemaElement)\r
326                                 {\r
327                                         XmlSchemaElement elem = (XmlSchemaElement) obj;\r
328                                         elem.parentIsSchema = true;\r
329                                         if(elem.Compile(handler,info) == 0)\r
330                                         {\r
331                                                 Elements.Add(elem.QualifiedName,elem);\r
332                                         }\r
333                                 }\r
334                                 else if(obj is XmlSchemaGroup)\r
335                                 {\r
336                                         XmlSchemaGroup grp = (XmlSchemaGroup) obj;\r
337                                         if(grp.Compile(handler,info) == 0)\r
338                                         {\r
339                                                 Groups.Add(grp.QualifiedName,grp);\r
340                                         }\r
341                                 }\r
342                                 else if(obj is XmlSchemaNotation)\r
343                                 {\r
344                                         XmlSchemaNotation ntn = (XmlSchemaNotation) obj;\r
345                                         if(ntn.Compile(handler,info) == 0)\r
346                                         {\r
347                                                 Notations.Add(ntn.QualifiedName, ntn);\r
348                                         }\r
349                                 }\r
350                                 else\r
351                                 {\r
352                                         ValidationHandler.RaiseValidationError(handler,this,\r
353                                                 "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema");\r
354                                 }\r
355                         }\r
356                         Validate(handler);\r
357                 }\r
358 \r
359                 [MonoTODO]\r
360                 protected void Validate(ValidationEventHandler handler)\r
361                 {\r
362 \r
363                         foreach(XmlSchemaObject obj in Includes)\r
364                         {\r
365                         }\r
366 \r
367                         //                              foreach(XmlSchemaAnnotation ann in ??????)\r
368                         //                              {\r
369                         //                                      ann.Validate(handler);\r
370                         //                              }\r
371                         foreach(XmlSchemaAttribute attr in Attributes.Values)\r
372                         {\r
373                                 attr.Validate(handler);\r
374                         }\r
375                         foreach(XmlSchemaAttributeGroup attrgrp in AttributeGroups.Values)\r
376                         {\r
377                                 attrgrp.Validate(handler);\r
378                         }\r
379                         foreach(XmlSchemaType type in SchemaTypes.Values)\r
380                         {\r
381                                 if(type is XmlSchemaComplexType)\r
382                                 {\r
383                                         ((XmlSchemaComplexType)type).Validate(handler);\r
384                                 }\r
385                                 else\r
386                                         ((XmlSchemaSimpleType)type).Validate(handler);\r
387                         }\r
388                         foreach(XmlSchemaElement elem in Elements.Values)\r
389                         {\r
390                                 elem.Validate(handler);\r
391                         }\r
392                         foreach(XmlSchemaGroup grp in Groups.Values)\r
393                         {\r
394                                 grp.Validate(handler);\r
395                         }\r
396                         foreach(XmlSchemaNotation ntn in Notations.Values)\r
397                         {\r
398                                 ntn.Validate(handler);\r
399                         }\r
400                 }\r
401 \r
402                 public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)\r
403                 {\r
404                         return Read(new XmlTextReader(reader),validationEventHandler);\r
405                 }\r
406                 public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)\r
407                 {\r
408                         return Read(new XmlTextReader(stream),validationEventHandler);\r
409                 }\r
410 \r
411                 [MonoTODO]\r
412                 public static XmlSchema Read(XmlReader rdr, ValidationEventHandler validationEventHandler)\r
413                 {\r
414                         //XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));\r
415                         //return (XmlSchema) xser.Deserialize(reader);\r
416                         XmlSchemaReader reader = new XmlSchemaReader(rdr, validationEventHandler);\r
417 \r
418                         while(reader.ReadNextElement())\r
419                         {\r
420                                 switch(reader.NodeType)\r
421                                 {\r
422                                         case XmlNodeType.Element:\r
423                                                 if(reader.LocalName == "schema")\r
424                                                 {\r
425                                                         XmlSchema schema = new XmlSchema();\r
426                                                         \r
427                                                         schema.LineNumber = reader.LineNumber;\r
428                                                         schema.LinePosition = reader.LinePosition;\r
429                                                         schema.SourceUri = reader.BaseURI;\r
430 \r
431                                                         ReadAttributes(schema, reader, validationEventHandler);\r
432                                                         //IsEmptyElement does not behave properly if reader is\r
433                                                         //positioned at an attribute.\r
434                                                         reader.MoveToElement();\r
435                                                         if(!reader.IsEmptyElement)\r
436                                                         {\r
437                                                                 ReadContent(schema, reader, validationEventHandler);\r
438                                                         }\r
439                                                         return schema;\r
440                                                 }\r
441                                                 else\r
442                                                 {\r
443                                                         //Schema can't be generated. Throw an exception\r
444                                                         throw new XmlSchemaException("The root element must be schema", null);\r
445                                                 }\r
446                                         default:\r
447                                                 error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);\r
448                                                 break;\r
449                                 }\r
450                         }\r
451                         throw new XmlSchemaException("The top level schema must have namespace "+XmlSchema.Namespace, null);\r
452                 }\r
453 \r
454                 private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)\r
455                 {\r
456                         Exception ex; \r
457                         \r
458                         reader.MoveToElement();\r
459                         while(reader.MoveToNextAttribute())\r
460                         {\r
461                                 switch(reader.Name)\r
462                                 {\r
463                                         case "attributeFormDefault" :\r
464                                                 schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);\r
465                                                 if(ex != null)\r
466                                                         error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);\r
467                                                 break;\r
468                                         case "blockDefault" :\r
469                                                 schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault");\r
470                                                 if(ex != null)\r
471                                                         warn(h, ex.Message, ex);\r
472                                                 break;\r
473                                         case "elementFormDefault":\r
474                                                 schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);\r
475                                                 if(ex != null)\r
476                                                         error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);\r
477                                                 break;\r
478                                         case "finalDefault":\r
479                                                  schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault");\r
480                                                 if(ex != null)\r
481                                                         warn(h, ex.Message , ex);\r
482                                                 break;\r
483                                         case "id":\r
484                                                 schema.id = reader.Value;\r
485                                                 break;\r
486                                         case "targetNamespace":\r
487                                                 schema.targetNamespace = reader.Value;\r
488                                                 break;\r
489                                         case "version":\r
490                                                 schema.version = reader.Value;\r
491                                                 break;\r
492                                         case "xml:lang":\r
493                                                 schema.language = reader.Value;\r
494                                                 break;\r
495                                         default:\r
496                                                 if(reader.Prefix == "xmlns")\r
497                                                         schema.Namespaces.Add(reader.LocalName, reader.Value);\r
498                                                 else if(reader.Name == "xmlns")\r
499                                                         schema.Namespaces.Add("",reader.Value);\r
500                                                 else if(reader.Prefix == "" || reader.NamespaceURI == XmlSchema.Namespace)\r
501                                                         error(h, reader.Name + " attribute is not allowed in schema element",null);\r
502 //                                      FIXME: There is no public constructor for XmlAttribute. Nor is there a way to access the XmlNode\r
503 //                                              else\r
504 //                                                      unknown.Add(new XmlAttibute());\r
505                                                 break;\r
506                                 }\r
507                         }\r
508                 }\r
509 \r
510                 private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)\r
511                 {\r
512                         reader.MoveToElement();\r
513                         if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)\r
514                                 error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);\r
515 \r
516                         //(include | import | redefine | annotation)*, \r
517                         //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*\r
518                         int level = 1;\r
519                         while(reader.ReadNextElement())\r
520                         {\r
521                                 if(reader.NodeType == XmlNodeType.EndElement)\r
522                                 {\r
523                                         if(reader.LocalName != xmlname)\r
524                                                 error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);\r
525                                         break;\r
526                                 }\r
527                                 if(level <= 1)\r
528                                 {\r
529                                         if(reader.LocalName == "include")\r
530                                         {\r
531                                                 XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);\r
532                                                 if(include != null) \r
533                                                         schema.includes.Add(include);\r
534                                                 continue;\r
535                                         }\r
536                                         if(reader.LocalName == "import")\r
537                                         {\r
538                                                 XmlSchemaImport import = XmlSchemaImport.Read(reader,h);\r
539                                                 if(import != null)\r
540                                                         schema.includes.Add(import);\r
541                                                 continue;\r
542                                         }\r
543                                         if(reader.LocalName == "redefine")\r
544                                         {\r
545                                                 XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);\r
546                                                 if(redefine != null)\r
547                                                         schema.includes.Add(redefine);\r
548                                                 continue;\r
549                                         }\r
550                                         if(reader.LocalName == "annotation")\r
551                                         {\r
552                                                 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
553                                                 if(annotation != null)\r
554                                                         schema.items.Add(annotation);\r
555                                                 continue;\r
556                                         }\r
557                                 }\r
558                                 if(level <=2)\r
559                                 {\r
560                                         level = 2;\r
561                                         if(reader.LocalName == "simpleType")\r
562                                         {\r
563                                                 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);\r
564                                                 if(stype != null)\r
565                                                         schema.items.Add(stype);\r
566                                                 continue;\r
567                                         }\r
568                                         if(reader.LocalName == "complexType")\r
569                                         {\r
570                                                 XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);\r
571                                                 if(ctype != null)\r
572                                                         schema.items.Add(ctype);\r
573                                                 continue;\r
574                                         }\r
575                                         if(reader.LocalName == "group")\r
576                                         {\r
577                                                 XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);\r
578                                                 if(group != null)\r
579                                                         schema.items.Add(group);\r
580                                                 continue;\r
581                                         }\r
582                                         if(reader.LocalName == "attributeGroup")\r
583                                         {\r
584                                                 XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);\r
585                                                 if(attributeGroup != null)\r
586                                                         schema.items.Add(attributeGroup);\r
587                                                 continue;\r
588                                         }\r
589                                         if(reader.LocalName == "element")\r
590                                         {\r
591                                                 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);\r
592                                                 if(element != null)\r
593                                                         schema.items.Add(element);\r
594                                                 continue;\r
595                                         }\r
596                                         if(reader.LocalName == "attribute")\r
597                                         {\r
598                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
599                                                 if(attr != null)\r
600                                                         schema.items.Add(attr);\r
601                                                 continue;\r
602                                         }\r
603                                         if(reader.LocalName == "notation")\r
604                                         {\r
605                                                 XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);\r
606                                                 if(notation != null)\r
607                                                         schema.items.Add(notation);\r
608                                                 continue;\r
609                                         }\r
610                                         if(reader.LocalName == "annotation")\r
611                                         {\r
612                                                 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
613                                                 if(annotation != null)\r
614                                                         schema.items.Add(annotation);\r
615                                                 continue;\r
616                                         }\r
617                                 }\r
618                                 reader.RaiseInvalidElementError();\r
619                         }\r
620                 }\r
621 \r
622                 public void Write(System.IO.Stream stream)\r
623                 {\r
624                         Write(stream,null);\r
625                 }\r
626                 public void Write(System.IO.TextWriter writer)\r
627                 {\r
628                         Write(writer,null);\r
629                 }\r
630                 public void Write(System.Xml.XmlWriter writer)\r
631                 {\r
632                         Write(writer,null);\r
633                 }\r
634                 public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)\r
635                 {\r
636                         Write(new XmlTextWriter(stream,null),namespaceManager);\r
637                 }\r
638                 public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)\r
639                 {\r
640                         XmlTextWriter xwriter = new XmlTextWriter(writer);\r
641                         xwriter.Formatting = Formatting.Indented;\r
642                         Write(xwriter,namespaceManager);\r
643                 }\r
644                 [MonoTODO]\r
645                 public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)\r
646                 {\r
647                         if(Namespaces == null)\r
648                         {\r
649                                  Namespaces = new XmlSerializerNamespaces();\r
650                         }\r
651 \r
652                         if(namespaceManager != null)\r
653                         {\r
654                                 foreach(string name in namespaceManager)\r
655                                 {\r
656                                         //xml and xmlns namespaced are added by default in namespaceManager. \r
657                                         //So we should ignore them\r
658                                         if(name!="xml" && name != "xmlns")\r
659                                                 Namespaces.Add(name,namespaceManager.LookupNamespace(name));\r
660                                 }\r
661                         }\r
662                         \r
663                         XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));\r
664                         xser.Serialize(writer,this,Namespaces);\r
665                         writer.Flush();\r
666                 }\r
667         }\r
668 }\r