1 // Author: Dwivedi, Ajay kumar
\r
4 using System.Collections;
\r
7 using System.Xml.Serialization;
\r
8 using System.ComponentModel;
\r
10 namespace System.Xml.Schema
\r
13 /// Summary description for XmlSchema.
\r
15 [XmlRoot("schema",Namespace="http://www.w3.org/2001/XMLSchema")]
\r
16 public class XmlSchema : XmlSchemaObject
\r
19 public const string Namespace = "http://www.w3.org/2001/XMLSchema";
\r
20 public const string InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
\r
23 private XmlSchemaForm attributeFormDefault ;
\r
24 private XmlSchemaObjectTable attributeGroups ;
\r
25 private XmlSchemaObjectTable attributes ;
\r
26 private XmlSchemaDerivationMethod blockDefault ;
\r
27 private XmlSchemaForm elementFormDefault ;
\r
28 private XmlSchemaObjectTable elements ;
\r
29 private XmlSchemaDerivationMethod finalDefault ;
\r
30 private XmlSchemaObjectTable groups ;
\r
32 private XmlSchemaObjectCollection includes ;
\r
33 private bool isCompiled ;
\r
34 private XmlSchemaObjectCollection items ;
\r
35 private XmlSchemaObjectTable notations ;
\r
36 private XmlSchemaObjectTable schemaTypes ;
\r
37 private string targetNamespace ;
\r
38 private XmlAttribute[] unhandledAttributes ;
\r
39 private string version;
\r
40 private string language;
\r
42 // Compiler specific things
\r
43 private XmlSchemaInfo info;
\r
44 private static string xmlname = "schema";
\r
48 attributeFormDefault= XmlSchemaForm.None;
\r
49 blockDefault = XmlSchemaDerivationMethod.None;
\r
50 elementFormDefault = XmlSchemaForm.None;
\r
51 finalDefault = XmlSchemaDerivationMethod.None;
\r
52 includes = new XmlSchemaObjectCollection();
\r
54 items = new XmlSchemaObjectCollection();
\r
55 attributeGroups = new XmlSchemaObjectTable();
\r
56 attributes = new XmlSchemaObjectTable();
\r
57 elements = new XmlSchemaObjectTable();
\r
58 groups = new XmlSchemaObjectTable();
\r
59 notations = new XmlSchemaObjectTable();
\r
60 schemaTypes = new XmlSchemaObjectTable();
\r
65 [DefaultValue(XmlSchemaForm.None)]
\r
66 [System.Xml.Serialization.XmlAttribute("attributeFormDefault")]
\r
67 public XmlSchemaForm AttributeFormDefault
\r
69 get{ return attributeFormDefault; }
\r
70 set{ this.attributeFormDefault = value;}
\r
73 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
74 [System.Xml.Serialization.XmlAttribute("blockDefault")]
\r
75 public XmlSchemaDerivationMethod BlockDefault
\r
77 get{ return blockDefault;}
\r
78 set{ blockDefault = value;}
\r
81 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
82 [System.Xml.Serialization.XmlAttribute("finalDefault")]
\r
83 public XmlSchemaDerivationMethod FinalDefault
\r
85 get{ return finalDefault;}
\r
86 set{ finalDefault = value;}
\r
89 [DefaultValue(XmlSchemaForm.None)]
\r
90 [System.Xml.Serialization.XmlAttribute("elementFormDefault")]
\r
91 public XmlSchemaForm ElementFormDefault
\r
93 get{ return elementFormDefault;}
\r
94 set{ elementFormDefault = value;}
\r
97 [System.Xml.Serialization.XmlAttribute("targetNamespace")]
\r
98 public string TargetNamespace
\r
100 get{ return targetNamespace;}
\r
101 set{ targetNamespace = value;}
\r
104 [System.Xml.Serialization.XmlAttribute("version")]
\r
105 public string Version
\r
107 get{ return version;}
\r
108 set{ version = value;}
\r
111 [XmlElement("include",typeof(XmlSchemaInclude),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
112 [XmlElement("import",typeof(XmlSchemaImport),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
113 [XmlElement("redefine",typeof(XmlSchemaRedefine),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
114 public XmlSchemaObjectCollection Includes
\r
116 get{ return includes;}
\r
119 [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
120 [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
121 [XmlElement("group",typeof(XmlSchemaGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
122 //Only Schema's attributeGroup has type XmlSchemaAttributeGroup.
\r
123 //Others (complextype, restrictions etc) must have XmlSchemaAttributeGroupRef
\r
124 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
125 [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
126 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
127 [XmlElement("notation",typeof(XmlSchemaNotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
128 [XmlElement("annotation",typeof(XmlSchemaAnnotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
129 public XmlSchemaObjectCollection Items
\r
131 get{ return items;}
\r
135 public bool IsCompiled
\r
137 get{ return isCompiled;}
\r
141 public XmlSchemaObjectTable Attributes
\r
143 get{ return attributes;}
\r
147 public XmlSchemaObjectTable AttributeGroups
\r
149 get{ return attributeGroups; }
\r
153 public XmlSchemaObjectTable SchemaTypes
\r
155 get{ return schemaTypes; }
\r
159 public XmlSchemaObjectTable Elements
\r
161 get{ return elements;}
\r
164 [System.Xml.Serialization.XmlAttribute("id")]
\r
172 public XmlAttribute[] UnhandledAttributes
\r
176 if(unhandledAttributeList != null)
\r
178 unhandledAttributes = (XmlAttribute[]) unhandledAttributeList.ToArray(typeof(XmlAttribute));
\r
179 unhandledAttributeList = null;
\r
181 return unhandledAttributes;
\r
185 unhandledAttributes = value;
\r
186 unhandledAttributeList = null;
\r
191 public XmlSchemaObjectTable Groups
\r
193 get{ return groups;}
\r
197 public XmlSchemaObjectTable Notations
\r
199 get{ return notations;}
\r
202 // New attribute defined in W3C schema element
\r
203 [System.Xml.Serialization.XmlAttribute("xml:lang")]
\r
204 public string Language
\r
206 get{ return language; }
\r
207 set{ language = value; }
\r
216 /// This compile method does two things:
\r
217 /// 1. It compiles and fills the PSVI dataset
\r
218 /// 2. Validates the schema by calling Validate method.
\r
219 /// Every XmlSchemaObject has a Compile Method which gets called.
\r
222 /// 1. blockDefault must be one of #all | List of (extension | restriction | substitution)
\r
223 /// 2. finalDefault must be one of (#all | List of (extension | restriction| union| list))
\r
224 /// 3. id must be of type ID
\r
225 /// 4. targetNamespace should be any uri
\r
226 /// 5. version should be a normalizedString
\r
227 /// 6. xml:lang should be a language
\r
230 public void Compile(ValidationEventHandler handler)
\r
232 // Create the xmlschemainfo object which we use to pass variables like targetnamespace;
\r
233 info = new XmlSchemaInfo();
\r
235 //1. Union and List are not allowed in block default
\r
236 if(BlockDefault != XmlSchemaDerivationMethod.All)
\r
238 if((BlockDefault & XmlSchemaDerivationMethod.List)!=0 )
\r
239 error(handler, "list is not allowed in blockDefault attribute");
\r
240 if((BlockDefault & XmlSchemaDerivationMethod.Union)!=0 )
\r
241 error(handler, "union is not allowed in blockDefault attribute");
\r
244 //2. Substitution is not allowed in finaldefault.
\r
245 if(FinalDefault != XmlSchemaDerivationMethod.All)
\r
247 if((FinalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )
\r
248 error(handler, "substitution is not allowed in finalDefault attribute");
\r
251 //3. id must be of type ID
\r
252 XmlSchemaUtil.CompileID(Id, this, info.IDCollection, handler);
\r
254 //4. targetNamespace should be of type anyURI or absent
\r
255 if(TargetNamespace != null)
\r
257 if(!XmlSchemaUtil.CheckAnyUri(TargetNamespace))
\r
258 error(handler, TargetNamespace+" is not a valid value for targetNamespace attribute of schema");
\r
260 info.TargetNamespace = TargetNamespace;
\r
263 //5. version should be of type normalizedString
\r
264 if(!XmlSchemaUtil.CheckNormalizedString(Version))
\r
265 error(handler, Version + "is not a valid value for version attribute of schema");
\r
267 //6. xml:lang must be a language
\r
268 if(!XmlSchemaUtil.CheckLanguage(Language))
\r
269 error(handler, Language + " is not a valid language");
\r
271 // elementFormDefault defaults to UnQualified
\r
272 if(ElementFormDefault != XmlSchemaForm.Qualified)
\r
273 info.ElementFormDefault = XmlSchemaForm.Unqualified;
\r
275 info.ElementFormDefault = XmlSchemaForm.Qualified;
\r
277 // attributeFormDefault defaults to UnQualified
\r
278 if(AttributeFormDefault != XmlSchemaForm.Qualified)
\r
279 info.AttributeFormDefault = XmlSchemaForm.Unqualified;
\r
281 info.AttributeFormDefault = XmlSchemaForm.Qualified;
\r
283 if(FinalDefault == XmlSchemaDerivationMethod.All)
\r
284 info.FinalDefault = XmlSchemaDerivationMethod.All;
\r
285 else // If finalDefault is None, info's finalDefault is set to empty
\r
286 info.FinalDefault = (FinalDefault & (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction));
\r
288 if(BlockDefault == XmlSchemaDerivationMethod.All)
\r
289 info.BlockDefault = XmlSchemaDerivationMethod.All;
\r
290 else // If finalDefault is None, info's blockDefault is set to empty
\r
291 info.BlockDefault = (blockDefault & (XmlSchemaDerivationMethod.Extension |
\r
292 XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.Substitution));
\r
294 // Compile the content of this schema
\r
295 foreach(XmlSchemaObject obj in Includes)
\r
297 if(obj is XmlSchemaExternal)
\r
299 //FIXME: Kuch to karo! (Do Something ;)
\r
303 error(handler,"Object of Type "+obj.GetType().Name+" is not valid in Includes Property of XmlSchema");
\r
306 foreach(XmlSchemaObject obj in Items)
\r
308 if(obj is XmlSchemaAnnotation)
\r
310 int numerr = ((XmlSchemaAnnotation)obj).Compile(handler,info);
\r
311 errorCount += numerr;
\r
314 //FIXME: What PSVI set do we add this to?
\r
317 else if(obj is XmlSchemaAttribute)
\r
319 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
\r
320 attr.parentIsSchema = true;
\r
321 int numerr = attr.Compile(handler,info);
\r
322 errorCount += numerr;
\r
325 Attributes.Add(attr.QualifiedName, attr);
\r
328 else if(obj is XmlSchemaAttributeGroup)
\r
330 XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
\r
331 int numerr = attrgrp.Compile(handler,info);
\r
332 errorCount += numerr;
\r
335 AttributeGroups.Add(attrgrp.QualifiedName, attrgrp);
\r
338 else if(obj is XmlSchemaComplexType)
\r
340 XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
\r
341 ctype.istoplevel = true;
\r
342 int numerr = ctype.Compile(handler,info);
\r
343 errorCount += numerr;
\r
346 schemaTypes.Add(ctype.QualifiedName, ctype);
\r
349 else if(obj is XmlSchemaSimpleType)
\r
351 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
\r
352 stype.islocal = false; //This simple type is toplevel
\r
353 int numerr = stype.Compile(handler,info);
\r
354 errorCount += numerr;
\r
357 SchemaTypes.Add(stype.QualifiedName, stype);
\r
360 else if(obj is XmlSchemaElement)
\r
362 XmlSchemaElement elem = (XmlSchemaElement) obj;
\r
363 elem.parentIsSchema = true;
\r
364 int numerr = elem.Compile(handler,info);
\r
365 errorCount += numerr;
\r
368 Elements.Add(elem.QualifiedName,elem);
\r
371 else if(obj is XmlSchemaGroup)
\r
373 XmlSchemaGroup grp = (XmlSchemaGroup) obj;
\r
374 int numerr = grp.Compile(handler,info);
\r
375 errorCount += numerr;
\r
378 Groups.Add(grp.QualifiedName,grp);
\r
381 else if(obj is XmlSchemaNotation)
\r
383 XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
\r
384 int numerr = ntn.Compile(handler,info);
\r
385 errorCount += numerr;
\r
388 Notations.Add(ntn.QualifiedName, ntn);
\r
393 ValidationHandler.RaiseValidationError(handler,this,
\r
394 "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema");
\r
403 private void Validate(ValidationEventHandler handler)
\r
405 foreach(XmlSchemaAttribute attr in Attributes.Values)
\r
407 attr.Validate(handler);
\r
409 foreach(XmlSchemaAttributeGroup attrgrp in AttributeGroups.Values)
\r
411 attrgrp.Validate(handler);
\r
413 foreach(XmlSchemaType type in SchemaTypes.Values)
\r
415 if(type is XmlSchemaComplexType)
\r
417 ((XmlSchemaComplexType)type).Validate(handler);
\r
420 ((XmlSchemaSimpleType)type).Validate(handler);
\r
422 foreach(XmlSchemaElement elem in Elements.Values)
\r
424 elem.Validate(handler);
\r
426 foreach(XmlSchemaGroup grp in Groups.Values)
\r
428 grp.Validate(handler);
\r
430 foreach(XmlSchemaNotation ntn in Notations.Values)
\r
432 ntn.Validate(handler);
\r
439 public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)
\r
441 return Read(new XmlTextReader(reader),validationEventHandler);
\r
443 public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)
\r
445 return Read(new XmlTextReader(stream),validationEventHandler);
\r
448 public static XmlSchema Read(XmlReader rdr, ValidationEventHandler validationEventHandler)
\r
450 //XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
451 //return (XmlSchema) xser.Deserialize(reader);
\r
452 XmlSchemaReader reader = new XmlSchemaReader(rdr, validationEventHandler);
\r
454 while(reader.ReadNextElement())
\r
456 switch(reader.NodeType)
\r
458 case XmlNodeType.Element:
\r
459 if(reader.LocalName == "schema")
\r
461 XmlSchema schema = new XmlSchema();
\r
463 schema.LineNumber = reader.LineNumber;
\r
464 schema.LinePosition = reader.LinePosition;
\r
465 schema.SourceUri = reader.BaseURI;
\r
467 ReadAttributes(schema, reader, validationEventHandler);
\r
468 //IsEmptyElement does not behave properly if reader is
\r
469 //positioned at an attribute.
\r
470 reader.MoveToElement();
\r
471 if(!reader.IsEmptyElement)
\r
473 ReadContent(schema, reader, validationEventHandler);
\r
479 //Schema can't be generated. Throw an exception
\r
480 throw new XmlSchemaException("The root element must be schema", null);
\r
483 error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);
\r
487 throw new XmlSchemaException("The top level schema must have namespace "+XmlSchema.Namespace, null);
\r
490 private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
494 reader.MoveToElement();
\r
495 while(reader.MoveToNextAttribute())
\r
497 switch(reader.Name)
\r
499 case "attributeFormDefault" :
\r
500 schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);
\r
502 error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);
\r
504 case "blockDefault" :
\r
505 schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault");
\r
507 warn(h, ex.Message, ex);
\r
509 case "elementFormDefault":
\r
510 schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);
\r
512 error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);
\r
514 case "finalDefault":
\r
515 schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault");
\r
517 warn(h, ex.Message , ex);
\r
520 schema.id = reader.Value;
\r
522 case "targetNamespace":
\r
523 schema.targetNamespace = reader.Value;
\r
526 schema.version = reader.Value;
\r
529 schema.language = reader.Value;
\r
532 if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
533 error(h, reader.Name + " attribute is not allowed in schema element",null);
\r
536 XmlSchemaUtil.ReadUnhandledAttribute(reader,schema);
\r
543 private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
545 reader.MoveToElement();
\r
546 if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
\r
547 error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);
\r
549 //(include | import | redefine | annotation)*,
\r
550 //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
\r
552 while(reader.ReadNextElement())
\r
554 if(reader.NodeType == XmlNodeType.EndElement)
\r
556 if(reader.LocalName != xmlname)
\r
557 error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);
\r
562 if(reader.LocalName == "include")
\r
564 XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);
\r
565 if(include != null)
\r
566 schema.includes.Add(include);
\r
569 if(reader.LocalName == "import")
\r
571 XmlSchemaImport import = XmlSchemaImport.Read(reader,h);
\r
573 schema.includes.Add(import);
\r
576 if(reader.LocalName == "redefine")
\r
578 XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);
\r
579 if(redefine != null)
\r
580 schema.includes.Add(redefine);
\r
583 if(reader.LocalName == "annotation")
\r
585 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
586 if(annotation != null)
\r
587 schema.items.Add(annotation);
\r
594 if(reader.LocalName == "simpleType")
\r
596 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
\r
598 schema.items.Add(stype);
\r
601 if(reader.LocalName == "complexType")
\r
603 XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);
\r
605 schema.items.Add(ctype);
\r
608 if(reader.LocalName == "group")
\r
610 XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);
\r
612 schema.items.Add(group);
\r
615 if(reader.LocalName == "attributeGroup")
\r
617 XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);
\r
618 if(attributeGroup != null)
\r
619 schema.items.Add(attributeGroup);
\r
622 if(reader.LocalName == "element")
\r
624 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
\r
625 if(element != null)
\r
626 schema.items.Add(element);
\r
629 if(reader.LocalName == "attribute")
\r
631 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
\r
633 schema.items.Add(attr);
\r
636 if(reader.LocalName == "notation")
\r
638 XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);
\r
639 if(notation != null)
\r
640 schema.items.Add(notation);
\r
643 if(reader.LocalName == "annotation")
\r
645 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
646 if(annotation != null)
\r
647 schema.items.Add(annotation);
\r
651 reader.RaiseInvalidElementError();
\r
658 public void Write(System.IO.Stream stream)
\r
660 Write(stream,null);
\r
662 public void Write(System.IO.TextWriter writer)
\r
664 Write(writer,null);
\r
666 public void Write(System.Xml.XmlWriter writer)
\r
668 Write(writer,null);
\r
670 public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
\r
672 Write(new XmlTextWriter(stream,null),namespaceManager);
\r
674 public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
676 XmlTextWriter xwriter = new XmlTextWriter(writer);
\r
677 xwriter.Formatting = Formatting.Indented;
\r
678 Write(xwriter,namespaceManager);
\r
680 public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
682 if(Namespaces == null)
\r
684 Namespaces = new XmlSerializerNamespaces();
\r
687 if(namespaceManager != null)
\r
689 foreach(string name in namespaceManager)
\r
691 //xml and xmlns namespaced are added by default in namespaceManager.
\r
692 //So we should ignore them
\r
693 if(name!="xml" && name != "xmlns")
\r
694 Namespaces.Add(name,namespaceManager.LookupNamespace(name));
\r
698 XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
699 xser.Serialize(writer,this,Namespaces);
\r