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