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