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 // post schema compilation infoset
\r
47 private Hashtable idCollection;
\r
49 // Compiler specific things
\r
50 private static string xmlname = "schema";
\r
54 attributeFormDefault= XmlSchemaForm.None;
\r
55 blockDefault = XmlSchemaDerivationMethod.None;
\r
56 elementFormDefault = XmlSchemaForm.None;
\r
57 finalDefault = XmlSchemaDerivationMethod.None;
\r
58 includes = new XmlSchemaObjectCollection();
\r
60 items = new XmlSchemaObjectCollection();
\r
61 attributeGroups = new XmlSchemaObjectTable();
\r
62 attributes = new XmlSchemaObjectTable();
\r
63 elements = new XmlSchemaObjectTable();
\r
64 groups = new XmlSchemaObjectTable();
\r
65 notations = new XmlSchemaObjectTable();
\r
66 schemaTypes = new XmlSchemaObjectTable();
\r
67 idCollection = new Hashtable ();
\r
73 [DefaultValue(XmlSchemaForm.None)]
\r
74 [System.Xml.Serialization.XmlAttribute("attributeFormDefault")]
\r
75 public XmlSchemaForm AttributeFormDefault
\r
77 get{ return attributeFormDefault; }
\r
78 set{ this.attributeFormDefault = value;}
\r
81 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
82 [System.Xml.Serialization.XmlAttribute("blockDefault")]
\r
83 public XmlSchemaDerivationMethod BlockDefault
\r
85 get{ return blockDefault;}
\r
86 set{ blockDefault = value;}
\r
89 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
90 [System.Xml.Serialization.XmlAttribute("finalDefault")]
\r
91 public XmlSchemaDerivationMethod FinalDefault
\r
93 get{ return finalDefault;}
\r
94 set{ finalDefault = value;}
\r
97 [DefaultValue(XmlSchemaForm.None)]
\r
98 [System.Xml.Serialization.XmlAttribute("elementFormDefault")]
\r
99 public XmlSchemaForm ElementFormDefault
\r
101 get{ return elementFormDefault;}
\r
102 set{ elementFormDefault = value;}
\r
105 [System.Xml.Serialization.XmlAttribute("targetNamespace")]
\r
106 public string TargetNamespace
\r
108 get{ return targetNamespace;}
\r
109 set{ targetNamespace = value;}
\r
112 [System.Xml.Serialization.XmlAttribute("version")]
\r
113 public string Version
\r
115 get{ return version;}
\r
116 set{ version = value;}
\r
119 [XmlElement("include",typeof(XmlSchemaInclude),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
120 [XmlElement("import",typeof(XmlSchemaImport),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
121 [XmlElement("redefine",typeof(XmlSchemaRedefine),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
122 public XmlSchemaObjectCollection Includes
\r
124 get{ return includes;}
\r
127 [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
128 [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
129 [XmlElement("group",typeof(XmlSchemaGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
130 //Only Schema's attributeGroup has type XmlSchemaAttributeGroup.
\r
131 //Others (complextype, restrictions etc) must have XmlSchemaAttributeGroupRef
\r
132 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
133 [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
134 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
135 [XmlElement("notation",typeof(XmlSchemaNotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
136 [XmlElement("annotation",typeof(XmlSchemaAnnotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
137 public XmlSchemaObjectCollection Items
\r
139 get{ return items;}
\r
143 public bool IsCompiled
\r
145 get{ return isCompiled;}
\r
149 public XmlSchemaObjectTable Attributes
\r
151 get{ return attributes;}
\r
155 public XmlSchemaObjectTable AttributeGroups
\r
157 get{ return attributeGroups; }
\r
161 public XmlSchemaObjectTable SchemaTypes
\r
163 get{ return schemaTypes; }
\r
167 public XmlSchemaObjectTable Elements
\r
169 get{ return elements;}
\r
172 [System.Xml.Serialization.XmlAttribute("id")]
\r
180 public XmlAttribute[] UnhandledAttributes
\r
184 if(unhandledAttributeList != null)
\r
186 unhandledAttributes = (XmlAttribute[]) unhandledAttributeList.ToArray(typeof(XmlAttribute));
\r
187 unhandledAttributeList = null;
\r
189 return unhandledAttributes;
\r
193 unhandledAttributes = value;
\r
194 unhandledAttributeList = null;
\r
199 public XmlSchemaObjectTable Groups
\r
201 get{ return groups;}
\r
205 public XmlSchemaObjectTable Notations
\r
207 get{ return notations;}
\r
210 // New attribute defined in W3C schema element
\r
211 [System.Xml.Serialization.XmlAttribute("xml:lang")]
\r
212 public string Language
\r
214 get{ return language; }
\r
215 set{ language = value; }
\r
218 internal Hashtable IDCollection
\r
220 get { return idCollection; }
\r
224 internal XmlSchemaForm ElementFormDefaultCompiled
\r
226 get { return elementFormDefaultCompiled; }
\r
229 internal XmlSchemaForm AttributeFormDefaultCompiled
\r
231 get { return attributeFormDefaultCompiled; }
\r
234 internal XmlSchemaDerivationMethod BlockDefaultCompiled
\r
236 get { return blockDefaultCompiled; }
\r
239 internal XmlSchemaDerivationMethod FinalDefaultCompiled
\r
241 get { return finalDefaultCompiled; }
\r
251 /// This compile method does two things:
\r
252 /// 1. It compiles and fills the PSVI dataset
\r
253 /// 2. Validates the schema by calling Validate method.
\r
254 /// Every XmlSchemaObject has a Compile Method which gets called.
\r
257 /// 1. blockDefault must be one of #all | List of (extension | restriction | substitution)
\r
258 /// 2. finalDefault must be one of (#all | List of (extension | restriction| union| list))
\r
259 /// 3. id must be of type ID
\r
260 /// 4. targetNamespace should be any uri
\r
261 /// 5. version should be a normalizedString
\r
262 /// 6. xml:lang should be a language
\r
265 public void Compile(ValidationEventHandler handler)
\r
267 CompilationId = Guid.NewGuid ();
\r
269 //1. Union and List are not allowed in block default
\r
270 if(BlockDefault != XmlSchemaDerivationMethod.All)
\r
272 if((BlockDefault & XmlSchemaDerivationMethod.List)!=0 )
\r
273 error(handler, "list is not allowed in blockDefault attribute");
\r
274 if((BlockDefault & XmlSchemaDerivationMethod.Union)!=0 )
\r
275 error(handler, "union is not allowed in blockDefault attribute");
\r
278 //2. Substitution is not allowed in finaldefault.
\r
279 if(FinalDefault != XmlSchemaDerivationMethod.All)
\r
281 if((FinalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )
\r
282 error(handler, "substitution is not allowed in finalDefault attribute");
\r
285 //3. id must be of type ID
\r
286 XmlSchemaUtil.CompileID(Id, this, this.IDCollection, handler);
\r
288 //4. targetNamespace should be of type anyURI or absent
\r
289 if(TargetNamespace != null)
\r
291 if(!XmlSchemaUtil.CheckAnyUri(TargetNamespace))
\r
292 error(handler, TargetNamespace+" is not a valid value for targetNamespace attribute of schema");
\r
295 //5. version should be of type normalizedString
\r
296 if(!XmlSchemaUtil.CheckNormalizedString(Version))
\r
297 error(handler, Version + "is not a valid value for version attribute of schema");
\r
299 //6. xml:lang must be a language
\r
300 if(!XmlSchemaUtil.CheckLanguage(Language))
\r
301 error(handler, Language + " is not a valid language");
\r
303 // Compile the content of this schema
\r
304 foreach(XmlSchemaObject obj in Includes)
\r
306 if(obj is XmlSchemaExternal)
\r
308 //FIXME: Kuch to karo! (Do Something ;)
\r
312 error(handler,"Object of Type "+obj.GetType().Name+" is not valid in Includes Property of XmlSchema");
\r
315 // It is hack, but types are required before element/attributes.
\r
316 foreach (XmlSchemaObject obj in Items)
\r
318 if(obj is XmlSchemaComplexType)
\r
320 XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
\r
321 ctype.istoplevel = true;
\r
322 int numerr = ctype.Compile(handler, this);
\r
323 errorCount += numerr;
\r
326 schemaTypes.Add(ctype.QualifiedName, ctype);
\r
329 else if(obj is XmlSchemaSimpleType)
\r
331 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
\r
332 stype.islocal = false; //This simple type is toplevel
\r
333 int numerr = stype.Compile(handler, this);
\r
334 errorCount += numerr;
\r
337 SchemaTypes.Add(stype.QualifiedName, stype);
\r
342 foreach(XmlSchemaObject obj in Items)
\r
344 if(obj is XmlSchemaAnnotation)
\r
346 int numerr = ((XmlSchemaAnnotation)obj).Compile(handler, this);
\r
347 errorCount += numerr;
\r
350 //FIXME: What PSVI set do we add this to?
\r
353 else if(obj is XmlSchemaAttribute)
\r
355 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
\r
356 attr.parentIsSchema = true;
\r
357 int numerr = attr.Compile(handler, this);
\r
358 errorCount += numerr;
\r
361 Attributes.Add(attr.QualifiedName, attr);
\r
364 else if(obj is XmlSchemaAttributeGroup)
\r
366 XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
\r
367 int numerr = attrgrp.Compile(handler, this);
\r
368 errorCount += numerr;
\r
371 AttributeGroups.Add(attrgrp.QualifiedName, attrgrp);
\r
374 else if(obj is XmlSchemaComplexType)
\r
376 // Do nothing here.
\r
378 else if(obj is XmlSchemaSimpleType)
\r
380 // Do nothing here.
\r
382 else if(obj is XmlSchemaElement)
\r
384 XmlSchemaElement elem = (XmlSchemaElement) obj;
\r
385 elem.parentIsSchema = true;
\r
386 int numerr = elem.Compile(handler, this);
\r
387 errorCount += numerr;
\r
390 Elements.Add(elem.QualifiedName,elem);
\r
393 else if(obj is XmlSchemaGroup)
\r
395 XmlSchemaGroup grp = (XmlSchemaGroup) obj;
\r
396 int numerr = grp.Compile(handler, this);
\r
397 errorCount += numerr;
\r
400 Groups.Add(grp.QualifiedName,grp);
\r
403 else if(obj is XmlSchemaNotation)
\r
405 XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
\r
406 int numerr = ntn.Compile(handler, this);
\r
407 errorCount += numerr;
\r
410 Notations.Add(ntn.QualifiedName, ntn);
\r
415 ValidationHandler.RaiseValidationError(handler,this,
\r
416 "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema");
\r
425 private void Validate(ValidationEventHandler handler)
\r
427 foreach(XmlSchemaAttribute attr in Attributes.Values)
\r
429 attr.Validate(handler, this);
\r
431 foreach(XmlSchemaAttributeGroup attrgrp in AttributeGroups.Values)
\r
433 attrgrp.Validate(handler);
\r
435 foreach(XmlSchemaType type in SchemaTypes.Values)
\r
437 if(type is XmlSchemaComplexType)
\r
439 ((XmlSchemaComplexType)type).Validate(handler);
\r
442 ((XmlSchemaSimpleType)type).Validate(handler, this);
\r
444 foreach(XmlSchemaElement elem in Elements.Values)
\r
446 elem.Validate(handler);
\r
448 foreach(XmlSchemaGroup grp in Groups.Values)
\r
450 grp.Validate(handler);
\r
452 foreach(XmlSchemaNotation ntn in Notations.Values)
\r
454 ntn.Validate(handler);
\r
461 public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)
\r
463 return Read(new XmlTextReader(reader),validationEventHandler);
\r
465 public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)
\r
467 return Read(new XmlTextReader(stream),validationEventHandler);
\r
470 public static XmlSchema Read(XmlReader rdr, ValidationEventHandler validationEventHandler)
\r
472 //XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
473 //return (XmlSchema) xser.Deserialize(reader);
\r
474 XmlSchemaReader reader = new XmlSchemaReader(rdr, validationEventHandler);
\r
476 while(reader.ReadNextElement())
\r
478 switch(reader.NodeType)
\r
480 case XmlNodeType.Element:
\r
481 if(reader.LocalName == "schema")
\r
483 XmlSchema schema = new XmlSchema();
\r
485 schema.LineNumber = reader.LineNumber;
\r
486 schema.LinePosition = reader.LinePosition;
\r
487 schema.SourceUri = reader.BaseURI;
\r
489 ReadAttributes(schema, reader, validationEventHandler);
\r
490 //IsEmptyElement does not behave properly if reader is
\r
491 //positioned at an attribute.
\r
492 reader.MoveToElement();
\r
493 if(!reader.IsEmptyElement)
\r
495 ReadContent(schema, reader, validationEventHandler);
\r
501 //Schema can't be generated. Throw an exception
\r
502 throw new XmlSchemaException("The root element must be schema", null);
\r
505 error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);
\r
509 throw new XmlSchemaException("The top level schema must have namespace "+XmlSchema.Namespace, null);
\r
512 private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
516 reader.MoveToElement();
\r
517 while(reader.MoveToNextAttribute())
\r
519 switch(reader.Name)
\r
521 case "attributeFormDefault" :
\r
522 schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);
\r
524 error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);
\r
526 case "blockDefault" :
\r
527 schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault");
\r
529 warn(h, ex.Message, ex);
\r
531 case "elementFormDefault":
\r
532 schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);
\r
534 error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);
\r
536 case "finalDefault":
\r
537 schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault");
\r
539 warn(h, ex.Message , ex);
\r
542 schema.id = reader.Value;
\r
544 case "targetNamespace":
\r
545 schema.targetNamespace = reader.Value;
\r
548 schema.version = reader.Value;
\r
551 schema.language = reader.Value;
\r
554 if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
555 error(h, reader.Name + " attribute is not allowed in schema element",null);
\r
558 XmlSchemaUtil.ReadUnhandledAttribute(reader,schema);
\r
565 private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
567 reader.MoveToElement();
\r
568 if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
\r
569 error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);
\r
571 //(include | import | redefine | annotation)*,
\r
572 //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
\r
574 while(reader.ReadNextElement())
\r
576 if(reader.NodeType == XmlNodeType.EndElement)
\r
578 if(reader.LocalName != xmlname)
\r
579 error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);
\r
584 if(reader.LocalName == "include")
\r
586 XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);
\r
587 if(include != null)
\r
588 schema.includes.Add(include);
\r
591 if(reader.LocalName == "import")
\r
593 XmlSchemaImport import = XmlSchemaImport.Read(reader,h);
\r
595 schema.includes.Add(import);
\r
598 if(reader.LocalName == "redefine")
\r
600 XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);
\r
601 if(redefine != null)
\r
602 schema.includes.Add(redefine);
\r
605 if(reader.LocalName == "annotation")
\r
607 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
608 if(annotation != null)
\r
609 schema.items.Add(annotation);
\r
616 if(reader.LocalName == "simpleType")
\r
618 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
\r
620 schema.items.Add(stype);
\r
623 if(reader.LocalName == "complexType")
\r
625 XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);
\r
627 schema.items.Add(ctype);
\r
630 if(reader.LocalName == "group")
\r
632 XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);
\r
634 schema.items.Add(group);
\r
637 if(reader.LocalName == "attributeGroup")
\r
639 XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);
\r
640 if(attributeGroup != null)
\r
641 schema.items.Add(attributeGroup);
\r
644 if(reader.LocalName == "element")
\r
646 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
\r
647 if(element != null)
\r
648 schema.items.Add(element);
\r
651 if(reader.LocalName == "attribute")
\r
653 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
\r
655 schema.items.Add(attr);
\r
658 if(reader.LocalName == "notation")
\r
660 XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);
\r
661 if(notation != null)
\r
662 schema.items.Add(notation);
\r
665 if(reader.LocalName == "annotation")
\r
667 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
668 if(annotation != null)
\r
669 schema.items.Add(annotation);
\r
673 reader.RaiseInvalidElementError();
\r
680 public void Write(System.IO.Stream stream)
\r
682 Write(stream,null);
\r
684 public void Write(System.IO.TextWriter writer)
\r
686 Write(writer,null);
\r
688 public void Write(System.Xml.XmlWriter writer)
\r
690 Write(writer,null);
\r
692 public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
\r
694 Write(new XmlTextWriter(stream,null),namespaceManager);
\r
696 public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
698 XmlTextWriter xwriter = new XmlTextWriter(writer);
\r
699 xwriter.Formatting = Formatting.Indented;
\r
700 Write(xwriter,namespaceManager);
\r
702 public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
704 if(Namespaces == null)
\r
706 Namespaces = new XmlSerializerNamespaces();
\r
708 //Add the xml schema namespace.
\r
709 if(Namespaces.Count == 0)
\r
711 Namespaces.Add("xs", XmlSchema.Namespace);
\r
712 if (TargetNamespace != null && TargetNamespace != String.Empty)
\r
713 Namespaces.Add("tns", TargetNamespace);
\r
715 if(namespaceManager != null)
\r
717 foreach(string name in namespaceManager)
\r
719 //xml and xmlns namespaced are added by default in namespaceManager.
\r
720 //So we should ignore them
\r
721 if(name!="xml" && name != "xmlns")
\r
722 Namespaces.Add(name,namespaceManager.LookupNamespace(name));
\r
726 XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
727 xser.Serialize(writer,this,Namespaces);
\r