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
48 private Hashtable missingBaseSchemaTypeRefs;
\r
49 private Hashtable missingElementTypeRefs;
\r
51 // Compiler specific things
\r
52 private static string xmlname = "schema";
\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
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
76 [DefaultValue(XmlSchemaForm.None)]
\r
77 [System.Xml.Serialization.XmlAttribute("attributeFormDefault")]
\r
78 public XmlSchemaForm AttributeFormDefault
\r
80 get{ return attributeFormDefault; }
\r
81 set{ this.attributeFormDefault = value;}
\r
84 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
85 [System.Xml.Serialization.XmlAttribute("blockDefault")]
\r
86 public XmlSchemaDerivationMethod BlockDefault
\r
88 get{ return blockDefault;}
\r
89 set{ blockDefault = value;}
\r
92 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
93 [System.Xml.Serialization.XmlAttribute("finalDefault")]
\r
94 public XmlSchemaDerivationMethod FinalDefault
\r
96 get{ return finalDefault;}
\r
97 set{ finalDefault = value;}
\r
100 [DefaultValue(XmlSchemaForm.None)]
\r
101 [System.Xml.Serialization.XmlAttribute("elementFormDefault")]
\r
102 public XmlSchemaForm ElementFormDefault
\r
104 get{ return elementFormDefault;}
\r
105 set{ elementFormDefault = value;}
\r
108 [System.Xml.Serialization.XmlAttribute("targetNamespace")]
\r
109 public string TargetNamespace
\r
111 get{ return targetNamespace;}
\r
112 set{ targetNamespace = value;}
\r
115 [System.Xml.Serialization.XmlAttribute("version")]
\r
116 public string Version
\r
118 get{ return version;}
\r
119 set{ version = value;}
\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
127 get{ return includes;}
\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
142 get{ return items;}
\r
146 public bool IsCompiled
\r
148 get{ return isCompiled;}
\r
152 public XmlSchemaObjectTable Attributes
\r
154 get{ return attributes;}
\r
158 public XmlSchemaObjectTable AttributeGroups
\r
160 get{ return attributeGroups; }
\r
164 public XmlSchemaObjectTable SchemaTypes
\r
166 get{ return schemaTypes; }
\r
170 public XmlSchemaObjectTable Elements
\r
172 get{ return elements;}
\r
175 [System.Xml.Serialization.XmlAttribute("id")]
\r
183 public XmlAttribute[] UnhandledAttributes
\r
187 if(unhandledAttributeList != null)
\r
189 unhandledAttributes = (XmlAttribute[]) unhandledAttributeList.ToArray(typeof(XmlAttribute));
\r
190 unhandledAttributeList = null;
\r
192 return unhandledAttributes;
\r
196 unhandledAttributes = value;
\r
197 unhandledAttributeList = null;
\r
202 public XmlSchemaObjectTable Groups
\r
204 get{ return groups;}
\r
208 public XmlSchemaObjectTable Notations
\r
210 get{ return notations;}
\r
213 // New attribute defined in W3C schema element
\r
214 [System.Xml.Serialization.XmlAttribute("xml:lang")]
\r
215 public string Language
\r
217 get{ return language; }
\r
218 set{ language = value; }
\r
221 internal Hashtable IDCollection
\r
223 get { return idCollection; }
\r
226 internal Hashtable MissingBaseSchemaTypeRefs
\r
228 get { return missingBaseSchemaTypeRefs; }
\r
231 internal Hashtable MissingElementTypeRefs
\r
233 get { return missingElementTypeRefs; }
\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
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
255 public void Compile(ValidationEventHandler handler)
\r
257 CompilationId = Guid.NewGuid ();
\r
259 //1. Union and List are not allowed in block default
\r
260 if(BlockDefault != XmlSchemaDerivationMethod.All)
\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
268 //2. Substitution is not allowed in finaldefault.
\r
269 if(FinalDefault != XmlSchemaDerivationMethod.All)
\r
271 if((FinalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )
\r
272 error(handler, "substitution is not allowed in finalDefault attribute");
\r
275 //3. id must be of type ID
\r
276 XmlSchemaUtil.CompileID(Id, this, this.IDCollection, handler);
\r
278 //4. targetNamespace should be of type anyURI or absent
\r
279 if(TargetNamespace != null)
\r
281 if(!XmlSchemaUtil.CheckAnyUri(TargetNamespace))
\r
282 error(handler, TargetNamespace+" is not a valid value for targetNamespace attribute of schema");
\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
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
293 // Compile the content of this schema
\r
294 foreach(XmlSchemaObject obj in Includes)
\r
296 if(obj is XmlSchemaExternal)
\r
298 //FIXME: Kuch to karo! (Do Something ;)
\r
302 error(handler,"Object of Type "+obj.GetType().Name+" is not valid in Includes Property of XmlSchema");
\r
305 // It is hack, but types are required before element/attributes.
\r
306 foreach (XmlSchemaObject obj in Items)
\r
308 if(obj is XmlSchemaComplexType)
\r
310 XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
\r
311 ctype.istoplevel = true;
\r
312 int numerr = ctype.Compile(handler, this);
\r
313 errorCount += numerr;
\r
316 schemaTypes.Add(ctype.QualifiedName, ctype);
\r
319 else if(obj is XmlSchemaSimpleType)
\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
327 SchemaTypes.Add(stype.QualifiedName, stype);
\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
336 foreach(XmlSchemaObject obj in Items)
\r
338 if(obj is XmlSchemaAnnotation)
\r
340 int numerr = ((XmlSchemaAnnotation)obj).Compile(handler, this);
\r
341 errorCount += numerr;
\r
344 //FIXME: What PSVI set do we add this to?
\r
347 else if(obj is XmlSchemaAttribute)
\r
349 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
\r
350 attr.parentIsSchema = true;
\r
351 int numerr = attr.Compile(handler, this);
\r
352 errorCount += numerr;
\r
355 Attributes.Add(attr.QualifiedName, attr);
\r
358 else if(obj is XmlSchemaAttributeGroup)
\r
360 XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
\r
361 int numerr = attrgrp.Compile(handler, this);
\r
362 errorCount += numerr;
\r
365 AttributeGroups.Add(attrgrp.QualifiedName, attrgrp);
\r
368 else if(obj is XmlSchemaComplexType)
\r
370 // Do nothing here.
\r
372 else if(obj is XmlSchemaSimpleType)
\r
374 // Do nothing here.
\r
376 else if(obj is XmlSchemaElement)
\r
378 XmlSchemaElement elem = (XmlSchemaElement) obj;
\r
379 elem.parentIsSchema = true;
\r
380 int numerr = elem.Compile(handler, this);
\r
381 errorCount += numerr;
\r
384 Elements.Add(elem.QualifiedName,elem);
\r
387 else if(obj is XmlSchemaGroup)
\r
389 XmlSchemaGroup grp = (XmlSchemaGroup) obj;
\r
390 int numerr = grp.Compile(handler, this);
\r
391 errorCount += numerr;
\r
394 Groups.Add(grp.QualifiedName,grp);
\r
397 else if(obj is XmlSchemaNotation)
\r
399 XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
\r
400 int numerr = ntn.Compile(handler, this);
\r
401 errorCount += numerr;
\r
404 Notations.Add(ntn.QualifiedName, ntn);
\r
409 ValidationHandler.RaiseValidationError(handler,this,
\r
410 "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema");
\r
413 foreach (XmlSchemaElement element in missingElementTypeRefs.Keys)
\r
415 element.SetReferedElementInfo (
\r
416 FindElement (missingElementTypeRefs [element] as XmlQualifiedName));
\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
458 internal XmlSchemaElement FindElement (XmlQualifiedName name)
\r
460 if (name.Namespace != TargetNamespace)
\r
463 foreach (XmlSchemaObject obj in Items) {
\r
464 XmlSchemaElement elem = obj as XmlSchemaElement;
\r
465 if (obj != null && elem.Name == name.Name)
\r
473 public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)
\r
475 return Read(new XmlTextReader(reader),validationEventHandler);
\r
477 public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)
\r
479 return Read(new XmlTextReader(stream),validationEventHandler);
\r
482 [MonoTODO ("Use ValidationEventHandler")]
\r
483 public static XmlSchema Read(XmlReader rdr, ValidationEventHandler validationEventHandler)
\r
485 XmlSerializer xser = new XmlSerializer (typeof (XmlSchema));
\r
486 return (XmlSchema) xser.Deserialize (rdr);
\r
488 XmlSchemaReader reader = new XmlSchemaReader(rdr, validationEventHandler);
\r
490 while(reader.ReadNextElement())
\r
492 switch(reader.NodeType)
\r
494 case XmlNodeType.Element:
\r
495 if(reader.LocalName == "schema")
\r
497 XmlSchema schema = new XmlSchema();
\r
499 schema.LineNumber = reader.LineNumber;
\r
500 schema.LinePosition = reader.LinePosition;
\r
501 schema.SourceUri = reader.BaseURI;
\r
503 ReadAttributes(schema, reader, validationEventHandler);
\r
504 //IsEmptyElement does not behave properly if reader is
\r
505 //positioned at an attribute.
\r
506 reader.MoveToElement();
\r
507 if(!reader.IsEmptyElement)
\r
509 ReadContent(schema, reader, validationEventHandler);
\r
515 //Schema can't be generated. Throw an exception
\r
516 throw new XmlSchemaException("The root element must be schema", null);
\r
519 error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);
\r
523 throw new XmlSchemaException("The top level schema must have namespace "+XmlSchema.Namespace, null);
\r
527 private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
531 reader.MoveToElement();
\r
532 while(reader.MoveToNextAttribute())
\r
534 switch(reader.Name)
\r
536 case "attributeFormDefault" :
\r
537 schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);
\r
539 error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);
\r
541 case "blockDefault" :
\r
542 schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault");
\r
544 warn(h, ex.Message, ex);
\r
546 case "elementFormDefault":
\r
547 schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);
\r
549 error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);
\r
551 case "finalDefault":
\r
552 schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault");
\r
554 warn(h, ex.Message , ex);
\r
557 schema.id = reader.Value;
\r
559 case "targetNamespace":
\r
560 schema.targetNamespace = reader.Value;
\r
563 schema.version = reader.Value;
\r
566 schema.language = reader.Value;
\r
569 if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
570 error(h, reader.Name + " attribute is not allowed in schema element",null);
\r
573 XmlSchemaUtil.ReadUnhandledAttribute(reader,schema);
\r
580 private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
582 reader.MoveToElement();
\r
583 if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
\r
584 error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);
\r
586 //(include | import | redefine | annotation)*,
\r
587 //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
\r
589 while(reader.ReadNextElement())
\r
591 if(reader.NodeType == XmlNodeType.EndElement)
\r
593 if(reader.LocalName != xmlname)
\r
594 error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);
\r
599 if(reader.LocalName == "include")
\r
601 XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);
\r
602 if(include != null)
\r
603 schema.includes.Add(include);
\r
606 if(reader.LocalName == "import")
\r
608 XmlSchemaImport import = XmlSchemaImport.Read(reader,h);
\r
610 schema.includes.Add(import);
\r
613 if(reader.LocalName == "redefine")
\r
615 XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);
\r
616 if(redefine != null)
\r
617 schema.includes.Add(redefine);
\r
620 if(reader.LocalName == "annotation")
\r
622 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
623 if(annotation != null)
\r
624 schema.items.Add(annotation);
\r
631 if(reader.LocalName == "simpleType")
\r
633 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
\r
635 schema.items.Add(stype);
\r
638 if(reader.LocalName == "complexType")
\r
640 XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);
\r
642 schema.items.Add(ctype);
\r
645 if(reader.LocalName == "group")
\r
647 XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);
\r
649 schema.items.Add(group);
\r
652 if(reader.LocalName == "attributeGroup")
\r
654 XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);
\r
655 if(attributeGroup != null)
\r
656 schema.items.Add(attributeGroup);
\r
659 if(reader.LocalName == "element")
\r
661 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
\r
662 if(element != null)
\r
663 schema.items.Add(element);
\r
666 if(reader.LocalName == "attribute")
\r
668 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
\r
670 schema.items.Add(attr);
\r
673 if(reader.LocalName == "notation")
\r
675 XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);
\r
676 if(notation != null)
\r
677 schema.items.Add(notation);
\r
680 if(reader.LocalName == "annotation")
\r
682 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
683 if(annotation != null)
\r
684 schema.items.Add(annotation);
\r
688 reader.RaiseInvalidElementError();
\r
695 public void Write(System.IO.Stream stream)
\r
697 Write(stream,null);
\r
699 public void Write(System.IO.TextWriter writer)
\r
701 Write(writer,null);
\r
703 public void Write(System.Xml.XmlWriter writer)
\r
705 Write(writer,null);
\r
707 public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
\r
709 Write(new XmlTextWriter(stream,null),namespaceManager);
\r
711 public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
713 XmlTextWriter xwriter = new XmlTextWriter(writer);
\r
714 xwriter.Formatting = Formatting.Indented;
\r
715 Write(xwriter,namespaceManager);
\r
717 public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
719 if(Namespaces == null)
\r
721 Namespaces = new XmlSerializerNamespaces();
\r
723 //Add the xml schema namespace.
\r
724 if(Namespaces.Count == 0)
\r
726 Namespaces.Add("xs", XmlSchema.Namespace);
\r
727 if (TargetNamespace != null && TargetNamespace != String.Empty)
\r
728 Namespaces.Add("tns", TargetNamespace);
\r
730 if(namespaceManager != null)
\r
732 foreach(string name in namespaceManager)
\r
734 //xml and xmlns namespaced are added by default in namespaceManager.
\r
735 //So we should ignore them
\r
736 if(name!="xml" && name != "xmlns")
\r
737 Namespaces.Add(name,namespaceManager.LookupNamespace(name));
\r
741 XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
742 xser.Serialize(writer,this,Namespaces);
\r