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