2 // System.Xml.Schema.XmlSchema.cs
\r
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
\r
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
\r
9 using System.Collections;
\r
12 using System.Xml.Serialization;
\r
13 using System.ComponentModel;
\r
15 namespace System.Xml.Schema
\r
18 /// Summary description for XmlSchema.
\r
20 [XmlRoot("schema",Namespace=XmlSchema.Namespace)]
\r
21 public class XmlSchema : XmlSchemaObject
\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
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
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
46 // other post schema compilation infoset
\r
47 private Hashtable idCollection;
\r
48 private XmlSchemaObjectTable namedIdentities;
\r
49 private XmlSchemaCollection schemas;
\r
51 private XmlNameTable nameTable;
\r
53 internal bool missedSubComponents;
\r
55 // Only compilation-time use
\r
56 private XmlSchemaObjectCollection compilationItems;
\r
57 private Hashtable handledUris;
\r
59 // Compiler specific things
\r
60 const string xmlname = "schema";
\r
64 attributeFormDefault= XmlSchemaForm.None;
\r
65 blockDefault = XmlSchemaDerivationMethod.None;
\r
66 elementFormDefault = XmlSchemaForm.None;
\r
67 finalDefault = XmlSchemaDerivationMethod.None;
\r
68 includes = new XmlSchemaObjectCollection();
\r
70 items = new XmlSchemaObjectCollection();
\r
71 attributeGroups = new XmlSchemaObjectTable();
\r
72 attributes = new XmlSchemaObjectTable();
\r
73 elements = new XmlSchemaObjectTable();
\r
74 groups = new XmlSchemaObjectTable();
\r
75 notations = new XmlSchemaObjectTable();
\r
76 schemaTypes = new XmlSchemaObjectTable();
\r
77 idCollection = new Hashtable ();
\r
78 namedIdentities = new XmlSchemaObjectTable();
\r
83 [DefaultValue(XmlSchemaForm.None)]
\r
84 [System.Xml.Serialization.XmlAttribute("attributeFormDefault")]
\r
85 public XmlSchemaForm AttributeFormDefault
\r
87 get{ return attributeFormDefault; }
\r
88 set{ this.attributeFormDefault = value;}
\r
91 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
92 [System.Xml.Serialization.XmlAttribute("blockDefault")]
\r
93 public XmlSchemaDerivationMethod BlockDefault
\r
95 get{ return blockDefault;}
\r
96 set{ blockDefault = value;}
\r
99 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
100 [System.Xml.Serialization.XmlAttribute("finalDefault")]
\r
101 public XmlSchemaDerivationMethod FinalDefault
\r
103 get{ return finalDefault;}
\r
104 set{ finalDefault = value;}
\r
107 [DefaultValue(XmlSchemaForm.None)]
\r
108 [System.Xml.Serialization.XmlAttribute("elementFormDefault")]
\r
109 public XmlSchemaForm ElementFormDefault
\r
111 get{ return elementFormDefault;}
\r
112 set{ elementFormDefault = value;}
\r
115 [System.Xml.Serialization.XmlAttribute("targetNamespace")]
\r
116 public string TargetNamespace
\r
118 get{ return targetNamespace;}
\r
119 set{ targetNamespace = value;}
\r
122 [System.Xml.Serialization.XmlAttribute("version")]
\r
123 public string Version
\r
125 get{ return version;}
\r
126 set{ version = value;}
\r
129 [XmlElement("include",typeof(XmlSchemaInclude),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
130 [XmlElement("import",typeof(XmlSchemaImport),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
131 [XmlElement("redefine",typeof(XmlSchemaRedefine),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
132 public XmlSchemaObjectCollection Includes
\r
134 get{ return includes;}
\r
137 [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
138 [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
139 [XmlElement("group",typeof(XmlSchemaGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
140 //Only Schema's attributeGroup has type XmlSchemaAttributeGroup.
\r
141 //Others (complextype, restrictions etc) must have XmlSchemaAttributeGroupRef
\r
142 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
143 [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
144 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
145 [XmlElement("notation",typeof(XmlSchemaNotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
146 [XmlElement("annotation",typeof(XmlSchemaAnnotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
147 public XmlSchemaObjectCollection Items
\r
149 get{ return items;}
\r
153 public bool IsCompiled
\r
155 get{ return this.CompilationId != Guid.Empty;}
\r
159 public XmlSchemaObjectTable Attributes
\r
161 get{ return attributes;}
\r
165 public XmlSchemaObjectTable AttributeGroups
\r
167 get{ return attributeGroups; }
\r
171 public XmlSchemaObjectTable SchemaTypes
\r
173 get{ return schemaTypes; }
\r
177 public XmlSchemaObjectTable Elements
\r
179 get{ return elements;}
\r
182 [System.Xml.Serialization.XmlAttribute("id")]
\r
190 public XmlAttribute[] UnhandledAttributes
\r
194 if(unhandledAttributeList != null)
\r
196 unhandledAttributes = (XmlAttribute[]) unhandledAttributeList.ToArray(typeof(XmlAttribute));
\r
197 unhandledAttributeList = null;
\r
199 return unhandledAttributes;
\r
203 unhandledAttributes = value;
\r
204 unhandledAttributeList = null;
\r
209 public XmlSchemaObjectTable Groups
\r
211 get{ return groups;}
\r
215 public XmlSchemaObjectTable Notations
\r
217 get{ return notations;}
\r
220 internal Hashtable IDCollection
\r
222 get { return idCollection; }
\r
225 internal XmlSchemaObjectTable NamedIdentities
\r
227 get { return namedIdentities; }
\r
230 internal XmlSchemaCollection Schemas
\r
232 get { return schemas; }
\r
240 /// This compile method does two things:
\r
241 /// 1. It compiles and fills the PSVI dataset
\r
242 /// 2. Validates the schema by calling Validate method.
\r
243 /// Every XmlSchemaObject has a Compile Method which gets called.
\r
246 /// 1. blockDefault must be one of #all | List of (extension | restriction | substitution)
\r
247 /// 2. finalDefault must be one of (#all | List of (extension | restriction| union| list))
\r
248 /// 3. id must be of type ID
\r
249 /// 4. targetNamespace should be any uri
\r
250 /// 5. version should be a normalizedString
\r
251 /// 6. xml:lang should be a language
\r
253 public void Compile (ValidationEventHandler handler)
\r
255 Compile (handler, new XmlUrlResolver ());
\r
259 internal void Compile (ValidationEventHandler handler, XmlResolver resolver)
\r
261 public void Compile (ValidationEventHandler handler, XmlResolver resolver)
\r
264 Compile (handler, new Stack (), this, null, resolver);
\r
268 internal void Compile (ValidationEventHandler handler, XmlSchemaCollection col, XmlResolver resolver)
\r
270 Compile (handler, new Stack (), this, col, resolver);
\r
273 private void Compile (ValidationEventHandler handler, Stack schemaLocationStack, XmlSchema rootSchema, XmlSchemaCollection col, XmlResolver resolver)
\r
275 if (rootSchema != this) {
\r
276 CompilationId = rootSchema.CompilationId;
\r
277 schemas = rootSchema.schemas;
\r
281 if (schemas == null) {
\r
282 schemas = new XmlSchemaCollection ();
\r
283 schemas.SchemaSet.CompilationId = Guid.NewGuid ();
\r
285 CompilationId = schemas.SchemaSet.CompilationId;
\r
286 this.idCollection.Clear ();
\r
288 schemas.Add (this);
\r
290 attributeGroups.Clear ();
\r
291 attributes.Clear ();
\r
294 notations.Clear ();
\r
295 schemaTypes.Clear ();
\r
297 //1. Union and List are not allowed in block default
\r
298 if(BlockDefault != XmlSchemaDerivationMethod.All)
\r
300 if((BlockDefault & XmlSchemaDerivationMethod.List)!=0 )
\r
301 error(handler, "list is not allowed in blockDefault attribute");
\r
302 if((BlockDefault & XmlSchemaDerivationMethod.Union)!=0 )
\r
303 error(handler, "union is not allowed in blockDefault attribute");
\r
306 //2. Substitution is not allowed in finaldefault.
\r
307 if(FinalDefault != XmlSchemaDerivationMethod.All)
\r
309 if((FinalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )
\r
310 error(handler, "substitution is not allowed in finalDefault attribute");
\r
313 //3. id must be of type ID
\r
314 XmlSchemaUtil.CompileID(Id, this, this.IDCollection, handler);
\r
316 //4. targetNamespace should be of type anyURI or absent
\r
317 if(TargetNamespace != null)
\r
319 if(!XmlSchemaUtil.CheckAnyUri(TargetNamespace))
\r
320 error(handler, TargetNamespace+" is not a valid value for targetNamespace attribute of schema");
\r
323 //5. version should be of type normalizedString
\r
324 if(!XmlSchemaUtil.CheckNormalizedString(Version))
\r
325 error(handler, Version + "is not a valid value for version attribute of schema");
\r
327 //6. xml:lang must be a language
\r
328 // if(!XmlSchemaUtil.CheckLanguage(Language))
\r
329 // error(handler, Language + " is not a valid language");
\r
331 // Compile the content of this schema
\r
333 compilationItems = new XmlSchemaObjectCollection ();
\r
334 for (int i = 0; i < Items.Count; i++)
\r
335 compilationItems.Add (Items [i]);
\r
336 if (this == rootSchema)
\r
337 handledUris = new Hashtable ();
\r
339 // First, we run into inclusion schemas to collect
\r
340 // compilation target items into compiledItems.
\r
341 for (int i = 0; i < Includes.Count; i++) {
\r
342 XmlSchemaExternal ext = Includes [i] as XmlSchemaExternal;
\r
344 if (ext.SchemaLocation == null)
\r
346 Stream stream = null;
\r
348 if (resolver != null) {
\r
349 url = GetResolvedUri (resolver, ext.SchemaLocation);
\r
350 if (schemaLocationStack.Contains (url)) {
\r
351 error(handler, "Nested inclusion was found: " + url);
\r
352 // must skip this inclusion
\r
355 if (rootSchema.handledUris.Contains (url))
\r
356 // This schema is already handled, so simply skip (otherwise, duplicate definition errrors occur.
\r
358 rootSchema.handledUris.Add (url, url);
\r
360 stream = resolver.GetEntity (new Uri (url), null, typeof (Stream)) as Stream;
\r
361 } catch (Exception) {
\r
362 // LAMESPEC: This is not good way to handle errors, but since we cannot know what kind of XmlResolver will come, so there are no mean to avoid this ugly catch.
\r
363 warn (handler, "Could not resolve schema location URI: " + url);
\r
368 // Process redefinition children in advance.
\r
369 XmlSchemaRedefine redefine = Includes [i] as XmlSchemaRedefine;
\r
370 if (redefine != null) {
\r
371 for (int j = 0; j < redefine.Items.Count; j++) {
\r
372 XmlSchemaObject redefinedObj = redefine.Items [j];
\r
373 redefinedObj.isRedefinedComponent = true;
\r
374 redefinedObj.isRedefineChild = true;
\r
375 if (redefinedObj is XmlSchemaType ||
\r
376 redefinedObj is XmlSchemaGroup ||
\r
377 redefinedObj is XmlSchemaAttributeGroup)
\r
378 compilationItems.Add (redefinedObj);
\r
380 error (handler, "Redefinition is only allowed to simpleType, complexType, group and attributeGroup.");
\r
384 XmlSchema includedSchema = null;
\r
385 if (stream == null) {
\r
386 // It is missing schema components.
\r
387 missedSubComponents = true;
\r
390 schemaLocationStack.Push (url);
\r
391 includedSchema = XmlSchema.Read (new XmlTextReader (url, stream, nameTable), handler);
\r
392 includedSchema.schemas = schemas;
\r
395 // Set - actual - target namespace for the included schema * before compilation*.
\r
396 XmlSchemaImport import = ext as XmlSchemaImport;
\r
397 if (import != null) {
\r
398 if (TargetNamespace == includedSchema.TargetNamespace) {
\r
399 error (handler, "Target namespace must be different from that of included schema.");
\r
401 } else if (includedSchema.TargetNamespace != import.Namespace) {
\r
402 error (handler, "Attribute namespace and its importing schema's target namespace must be the same.");
\r
406 if (TargetNamespace == null &&
\r
407 includedSchema.TargetNamespace != null) {
\r
408 error (handler, "Target namespace is required to include a schema which has its own target namespace");
\r
411 else if (TargetNamespace != null &&
\r
412 includedSchema.TargetNamespace == null)
\r
413 includedSchema.TargetNamespace = TargetNamespace;
\r
416 // Compile included schema.
\r
417 includedSchema.idCollection = this.IDCollection;
\r
418 includedSchema.Compile (handler, schemaLocationStack, rootSchema, col, resolver);
\r
419 schemaLocationStack.Pop ();
\r
421 if (import != null)
\r
422 rootSchema.schemas.Add (includedSchema);
\r
424 // Note that we use compiled items. Items
\r
425 // may not exist in Items, since included
\r
426 // schema also includes another schemas.
\r
427 foreach (DictionaryEntry entry in includedSchema.Attributes)
\r
428 compilationItems.Add ((XmlSchemaObject) entry.Value);
\r
429 foreach (DictionaryEntry entry in includedSchema.Elements)
\r
430 compilationItems.Add ((XmlSchemaObject) entry.Value);
\r
431 foreach (DictionaryEntry entry in includedSchema.SchemaTypes)
\r
432 compilationItems.Add ((XmlSchemaObject) entry.Value);
\r
433 foreach (DictionaryEntry entry in includedSchema.AttributeGroups)
\r
434 compilationItems.Add ((XmlSchemaObject) entry.Value);
\r
435 foreach (DictionaryEntry entry in includedSchema.Groups)
\r
436 compilationItems.Add ((XmlSchemaObject) entry.Value);
\r
437 foreach (DictionaryEntry entry in includedSchema.Notations)
\r
438 compilationItems.Add ((XmlSchemaObject) entry.Value);
\r
442 error(handler,"Object of Type "+Includes [i].GetType().Name+" is not valid in Includes Property of XmlSchema");
\r
446 // Compilation phase.
\r
447 // At least each Compile() must gives unique (qualified) name for each component.
\r
448 // It also checks self-resolvable properties correct.
\r
449 // Post compilation schema information contribution is not required here.
\r
450 // It should be done by Validate().
\r
451 for (int i = 0; i < compilationItems.Count; i++) {
\r
452 XmlSchemaObject obj = compilationItems [i];
\r
453 if(obj is XmlSchemaAnnotation)
\r
455 int numerr = ((XmlSchemaAnnotation)obj).Compile(handler, this);
\r
456 errorCount += numerr;
\r
459 //FIXME: What PSVI set do we add this to?
\r
462 else if(obj is XmlSchemaAttribute)
\r
464 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
\r
465 attr.ParentIsSchema = true;
\r
466 int numerr = attr.Compile(handler, this);
\r
467 errorCount += numerr;
\r
470 if (!attr.IsComplied (this.CompilationId))
\r
471 schemas.SchemaSet.GlobalAttributes.Add (attr.QualifiedName, attr);
\r
472 XmlSchemaUtil.AddToTable (Attributes, attr, attr.QualifiedName, handler);
\r
475 else if(obj is XmlSchemaAttributeGroup)
\r
477 XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
\r
478 int numerr = attrgrp.Compile(handler, this);
\r
479 errorCount += numerr;
\r
482 XmlSchemaUtil.AddToTable (AttributeGroups, attrgrp, attrgrp.QualifiedName, handler);
\r
485 else if(obj is XmlSchemaComplexType)
\r
487 XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
\r
488 ctype.ParentIsSchema = true;
\r
489 int numerr = ctype.Compile(handler, this);
\r
490 errorCount += numerr;
\r
493 if (!ctype.IsComplied (this.CompilationId))
\r
494 schemas.SchemaSet.GlobalTypes.Add (ctype.QualifiedName, ctype);
\r
495 XmlSchemaUtil.AddToTable (schemaTypes, ctype, ctype.QualifiedName, handler);
\r
498 else if(obj is XmlSchemaSimpleType)
\r
500 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
\r
501 stype.islocal = false; //This simple type is toplevel
\r
502 int numerr = stype.Compile(handler, this);
\r
503 errorCount += numerr;
\r
506 if (!stype.IsComplied (this.CompilationId))
\r
507 schemas.SchemaSet.GlobalTypes.Add (stype.QualifiedName, stype);
\r
508 XmlSchemaUtil.AddToTable (SchemaTypes, stype, stype.QualifiedName, handler);
\r
511 else if(obj is XmlSchemaElement)
\r
513 XmlSchemaElement elem = (XmlSchemaElement) obj;
\r
514 elem.parentIsSchema = true;
\r
515 int numerr = elem.Compile(handler, this);
\r
516 errorCount += numerr;
\r
519 if (!elem.IsComplied (this.CompilationId))
\r
520 schemas.SchemaSet.GlobalElements.Add (elem.QualifiedName, elem);
\r
521 XmlSchemaUtil.AddToTable (Elements, elem, elem.QualifiedName, handler);
\r
524 else if(obj is XmlSchemaGroup)
\r
526 XmlSchemaGroup grp = (XmlSchemaGroup) obj;
\r
527 int numerr = grp.Compile(handler, this);
\r
528 errorCount += numerr;
\r
531 XmlSchemaUtil.AddToTable (Groups, grp, grp.QualifiedName, handler);
\r
534 else if(obj is XmlSchemaNotation)
\r
536 XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
\r
537 int numerr = ntn.Compile(handler, this);
\r
538 errorCount += numerr;
\r
541 XmlSchemaUtil.AddToTable (Notations, ntn, ntn.QualifiedName, handler);
\r
546 ValidationHandler.RaiseValidationEvent (
\r
548 "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema",
\r
549 null, this, null, XmlSeverityType.Error);
\r
553 if (rootSchema == this)
\r
557 private string GetResolvedUri (XmlResolver resolver, string relativeUri)
\r
559 Uri baseUri = null;
\r
560 if (this.SourceUri != null && this.SourceUri != String.Empty)
\r
561 baseUri = new Uri (this.SourceUri);
\r
562 return resolver.ResolveUri (baseUri, relativeUri).ToString ();
\r
565 internal bool IsNamespaceAbsent (string ns)
\r
567 return this.schemas [ns] == null;
\r
572 private void Validate(ValidationEventHandler handler)
\r
574 ValidationId = CompilationId;
\r
576 // Firstly Element needs to be filled their substitution group info
\r
577 foreach(XmlSchemaElement elem in Elements.Values)
\r
578 elem.FillSubstitutionElementInfo ();
\r
581 foreach(XmlSchemaAttribute attr in Attributes.Values)
\r
583 errorCount += attr.Validate(handler, this);
\r
585 foreach(XmlSchemaAttributeGroup attrgrp in AttributeGroups.Values)
\r
587 errorCount += attrgrp.Validate(handler, this);
\r
589 foreach(XmlSchemaType type in SchemaTypes.Values)
\r
591 errorCount += type.Validate(handler, this);
\r
593 foreach(XmlSchemaElement elem in Elements.Values)
\r
595 errorCount += elem.Validate(handler, this);
\r
597 foreach(XmlSchemaGroup grp in Groups.Values)
\r
599 errorCount += grp.Validate(handler, this);
\r
601 foreach(XmlSchemaNotation ntn in Notations.Values)
\r
603 errorCount += ntn.Validate(handler, this);
\r
609 // We cannot use xml deserialization, since it does not provide line info, qname context, and so on.
\r
610 public static XmlSchema Read (TextReader reader, ValidationEventHandler validationEventHandler)
\r
612 return Read (new XmlTextReader (reader),validationEventHandler);
\r
614 public static XmlSchema Read (Stream stream, ValidationEventHandler validationEventHandler)
\r
616 return Read (new XmlTextReader (stream),validationEventHandler);
\r
619 public static XmlSchema Read (XmlReader rdr, ValidationEventHandler validationEventHandler)
\r
621 XmlSchemaReader reader = new XmlSchemaReader (rdr, validationEventHandler);
\r
623 if (reader.ReadState == ReadState.Initial)
\r
624 reader.ReadNextElement ();
\r
626 int startDepth = reader.Depth;
\r
630 switch(reader.NodeType)
\r
632 case XmlNodeType.Element:
\r
633 if(reader.LocalName == "schema")
\r
635 XmlSchema schema = new XmlSchema ();
\r
636 schema.nameTable = rdr.NameTable;
\r
638 schema.LineNumber = reader.LineNumber;
\r
639 schema.LinePosition = reader.LinePosition;
\r
640 schema.SourceUri = reader.BaseURI;
\r
642 ReadAttributes(schema, reader, validationEventHandler);
\r
643 //IsEmptyElement does not behave properly if reader is
\r
644 //positioned at an attribute.
\r
645 reader.MoveToElement();
\r
646 if(!reader.IsEmptyElement)
\r
648 ReadContent(schema, reader, validationEventHandler);
\r
653 if (rdr.NodeType == XmlNodeType.EndElement)
\r
658 //Schema can't be generated. Throw an exception
\r
659 error (validationEventHandler, "The root element must be schema", null);
\r
662 error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);
\r
665 } while(reader.Depth > startDepth && reader.ReadNextElement());
\r
667 // This is thrown regardless of ValidationEventHandler existence.
\r
668 throw new XmlSchemaException ("The top level schema must have namespace " + XmlSchema.Namespace, null);
\r
671 private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
675 reader.MoveToElement();
\r
676 while(reader.MoveToNextAttribute())
\r
678 switch(reader.Name)
\r
680 case "attributeFormDefault" :
\r
681 schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);
\r
683 error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);
\r
685 case "blockDefault" :
\r
686 schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault",
\r
687 XmlSchemaUtil.ElementBlockAllowed);
\r
689 error (h, ex.Message, ex);
\r
691 case "elementFormDefault":
\r
692 schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);
\r
694 error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);
\r
696 case "finalDefault":
\r
697 schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault",
\r
698 XmlSchemaUtil.FinalAllowed);
\r
700 error (h, ex.Message , ex);
\r
703 schema.id = reader.Value;
\r
705 case "targetNamespace":
\r
706 schema.targetNamespace = reader.Value;
\r
709 schema.version = reader.Value;
\r
712 schema.language = reader.Value;
\r
715 if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
716 error(h, reader.Name + " attribute is not allowed in schema element",null);
\r
719 XmlSchemaUtil.ReadUnhandledAttribute(reader,schema);
\r
726 private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
728 reader.MoveToElement();
\r
729 if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
\r
730 error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);
\r
732 //(include | import | redefine | annotation)*,
\r
733 //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
\r
735 while(reader.ReadNextElement())
\r
737 if(reader.NodeType == XmlNodeType.EndElement)
\r
739 if(reader.LocalName != xmlname)
\r
740 error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);
\r
745 if(reader.LocalName == "include")
\r
747 XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);
\r
748 if(include != null)
\r
749 schema.includes.Add(include);
\r
752 if(reader.LocalName == "import")
\r
754 XmlSchemaImport import = XmlSchemaImport.Read(reader,h);
\r
756 schema.includes.Add(import);
\r
759 if(reader.LocalName == "redefine")
\r
761 XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);
\r
762 if(redefine != null)
\r
763 schema.includes.Add(redefine);
\r
766 if(reader.LocalName == "annotation")
\r
768 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
769 if(annotation != null)
\r
770 schema.items.Add(annotation);
\r
777 if(reader.LocalName == "simpleType")
\r
779 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
\r
781 schema.items.Add(stype);
\r
784 if(reader.LocalName == "complexType")
\r
786 XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);
\r
788 schema.items.Add(ctype);
\r
791 if(reader.LocalName == "group")
\r
793 XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);
\r
795 schema.items.Add(group);
\r
798 if(reader.LocalName == "attributeGroup")
\r
800 XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);
\r
801 if(attributeGroup != null)
\r
802 schema.items.Add(attributeGroup);
\r
805 if(reader.LocalName == "element")
\r
807 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
\r
808 if(element != null)
\r
809 schema.items.Add(element);
\r
812 if(reader.LocalName == "attribute")
\r
814 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
\r
816 schema.items.Add(attr);
\r
819 if(reader.LocalName == "notation")
\r
821 XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);
\r
822 if(notation != null)
\r
823 schema.items.Add(notation);
\r
826 if(reader.LocalName == "annotation")
\r
828 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
829 if(annotation != null)
\r
830 schema.items.Add(annotation);
\r
834 reader.RaiseInvalidElementError();
\r
841 public void Write(System.IO.Stream stream)
\r
843 Write(stream,null);
\r
845 public void Write(System.IO.TextWriter writer)
\r
847 Write(writer,null);
\r
849 public void Write(System.Xml.XmlWriter writer)
\r
851 Write(writer,null);
\r
853 public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
\r
855 Write(new XmlTextWriter(stream,null),namespaceManager);
\r
857 public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
859 XmlTextWriter xwriter = new XmlTextWriter(writer);
\r
860 xwriter.Formatting = Formatting.Indented;
\r
861 Write(xwriter,namespaceManager);
\r
864 public void Write (System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
866 XmlSerializerNamespaces nss = new XmlSerializerNamespaces ();
\r
868 if (namespaceManager != null) {
\r
870 nss = new XmlSerializerNamespaces ();
\r
871 foreach (string name in namespaceManager) {
\r
872 //xml and xmlns namespaces are added by default in namespaceManager.
\r
873 //So we should ignore them
\r
874 if (name !="xml" && name != "xmlns")
\r
875 nss.Add (name, namespaceManager.LookupNamespace (name));
\r
879 if (Namespaces != null && Namespaces.Count > 0) {
\r
880 nss.Add (String.Empty, XmlSchema.Namespace);
\r
881 foreach (XmlQualifiedName qn in Namespaces.ToArray ()) {
\r
882 nss.Add (qn.Name, qn.Namespace);
\r
886 if (nss.Count == 0) {
\r
887 // Add the xml schema namespace. (It is done
\r
888 // only when no entry exists in Namespaces).
\r
889 nss.Add ("xs", XmlSchema.Namespace);
\r
890 if (TargetNamespace != null)
\r
891 nss.Add ("tns", TargetNamespace);
\r
894 XmlSerializer xser = new XmlSerializer (typeof (XmlSchema));
\r
895 xser.Serialize (writer, this, nss);
\r