1 // Author: Dwivedi, Ajay kumar
\r
6 using System.Xml.Serialization;
\r
7 using System.ComponentModel;
\r
9 namespace System.Xml.Schema
\r
12 /// Summary description for XmlSchema.
\r
14 [XmlRoot("schema",Namespace="http://www.w3.org/2001/XMLSchema")]
\r
15 public class XmlSchema : XmlSchemaObject
\r
18 public const string InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
\r
19 public const string Namespace = "http://www.w3.org/2001/XMLSchema";
\r
22 private XmlSchemaForm attributeFormDefault ;
\r
23 private XmlSchemaObjectTable attributeGroups ;
\r
24 private XmlSchemaObjectTable attributes ;
\r
25 private XmlSchemaDerivationMethod blockDefault ;
\r
26 private XmlSchemaForm elementFormDefault ;
\r
27 private XmlSchemaObjectTable elements ;
\r
28 private XmlSchemaDerivationMethod finalDefault ;
\r
29 private XmlSchemaObjectTable groups ;
\r
31 private XmlSchemaObjectCollection includes ;
\r
32 private bool isCompiled ;
\r
33 private XmlSchemaObjectCollection items ;
\r
34 private XmlSchemaObjectTable notations ;
\r
35 private XmlSchemaObjectTable schemaTypes ;
\r
36 private string targetNamespace ;
\r
37 private XmlAttribute[] unhandledAttributes ;
\r
38 private string version;
\r
39 private string language;
\r
41 // Compiler specific things
\r
42 private XmlSchemaInfo info;
\r
43 private static string xmlname = "schema";
\r
47 attributeFormDefault= XmlSchemaForm.None;
\r
48 blockDefault = XmlSchemaDerivationMethod.None;
\r
49 elementFormDefault = XmlSchemaForm.None;
\r
50 finalDefault = XmlSchemaDerivationMethod.None;
\r
51 includes = new XmlSchemaObjectCollection();
\r
53 items = new XmlSchemaObjectCollection();
\r
54 attributeGroups = new XmlSchemaObjectTable();
\r
55 attributes = new XmlSchemaObjectTable();
\r
56 elements = new XmlSchemaObjectTable();
\r
57 groups = new XmlSchemaObjectTable();
\r
58 notations = new XmlSchemaObjectTable();
\r
59 schemaTypes = new XmlSchemaObjectTable();
\r
64 [DefaultValue(XmlSchemaForm.None)]
\r
65 [System.Xml.Serialization.XmlAttribute("attributeFormDefault")]
\r
66 public XmlSchemaForm AttributeFormDefault
\r
68 get{ return attributeFormDefault; }
\r
69 set{ this.attributeFormDefault = value;}
\r
73 public XmlSchemaObjectTable AttributeGroups
\r
75 get{ return attributeGroups; }
\r
79 public XmlSchemaObjectTable Attributes
\r
81 get{ return attributes;}
\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(XmlSchemaForm.None)]
\r
93 [System.Xml.Serialization.XmlAttribute("elementFormDefault")]
\r
94 public XmlSchemaForm ElementFormDefault
\r
96 get{ return elementFormDefault;}
\r
97 set{ elementFormDefault = value;}
\r
101 public XmlSchemaObjectTable Elements
\r
103 get{ return elements;}
\r
106 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
107 [System.Xml.Serialization.XmlAttribute("finalDefault")]
\r
108 public XmlSchemaDerivationMethod FinalDefault
\r
110 get{ return finalDefault;}
\r
111 set{ finalDefault = value;}
\r
115 public XmlSchemaObjectTable Groups
\r
117 get{ return groups;}
\r
120 [System.Xml.Serialization.XmlAttribute("id")]
\r
127 [XmlElement("include",typeof(XmlSchemaInclude),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
128 [XmlElement("import",typeof(XmlSchemaImport),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
129 [XmlElement("redefine",typeof(XmlSchemaRedefine),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
130 public XmlSchemaObjectCollection Includes
\r
132 get{ return includes;}
\r
136 public bool IsCompiled
\r
138 get{ return isCompiled;}
\r
141 [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
142 [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
143 [XmlElement("group",typeof(XmlSchemaGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
144 //Only Schema's attributeGroup has type XmlSchemaAttributeGroup.
\r
145 //Others (complextype, restrictions etc) must have XmlSchemaAttributeGroupRef
\r
146 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
147 [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
148 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
149 [XmlElement("notation",typeof(XmlSchemaNotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
150 [XmlElement("annotation",typeof(XmlSchemaAnnotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
151 public XmlSchemaObjectCollection Items
\r
153 get{ return items;}
\r
157 public XmlSchemaObjectTable Notations
\r
159 get{ return notations;}
\r
163 public XmlSchemaObjectTable SchemaTypes
\r
165 get{ return schemaTypes; }
\r
168 [System.Xml.Serialization.XmlAttribute("targetNamespace")]
\r
169 public string TargetNamespace
\r
171 get{ return targetNamespace;}
\r
172 set{ targetNamespace = value;}
\r
176 public XmlAttribute[] UnhandledAttributes
\r
178 get{ return unhandledAttributes;}
\r
179 set{ unhandledAttributes = value;}
\r
182 [System.Xml.Serialization.XmlAttribute("version")]
\r
183 public string Version
\r
185 get{ return version;}
\r
186 set{ version = value;}
\r
189 // New attribute defined in W3C schema element
\r
190 [System.Xml.Serialization.XmlAttribute("xml:lang")]
\r
191 public string Language
\r
193 get{ return language; }
\r
194 set{ language = value; }
\r
201 /// This compile method does two things:
\r
202 /// 1. It compiles and fills the PSVI dataset
\r
203 /// 2. Validates the schema by calling Validate method.
\r
204 /// Every XmlSchemaObject has a Compile Method which gets called.
\r
207 /// 1. blockDefault must be one of #all | List of (extension | restriction | substitution)
\r
208 /// 2. finalDefault must be one of (#all | List of (extension | restriction| union| list))
\r
209 /// 3. id must be of type ID
\r
210 /// 4. targetNamespace should be any uri
\r
211 /// 5. version should be a normalizedString
\r
212 /// 6. xml:lang should be a language
\r
215 public void Compile(ValidationEventHandler handler)
\r
217 // Create the xmlschemainfo object which we use to pass variables like targetnamespace;
\r
218 info = new XmlSchemaInfo();
\r
220 //1. Union and List are not allowed in block default
\r
221 if(BlockDefault != XmlSchemaDerivationMethod.All)
\r
223 if((BlockDefault & XmlSchemaDerivationMethod.List)!=0 )
\r
224 error(handler, "list is not allowed in blockDefault attribute");
\r
225 if((BlockDefault & XmlSchemaDerivationMethod.Union)!=0 )
\r
226 error(handler, "union is not allowed in blockDefault attribute");
\r
229 //2. Substitution is not allowed in finaldefault.
\r
230 if(FinalDefault != XmlSchemaDerivationMethod.All)
\r
232 if((FinalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )
\r
233 error(handler, "substitution is not allowed in finalDefault attribute");
\r
236 //3. id must be of type ID
\r
237 XmlSchemaUtil.CompileID(Id, this, info.IDCollection, handler);
\r
239 //4. targetNamespace should be of type anyURI or absent
\r
240 if(TargetNamespace != null)
\r
242 if(!XmlSchemaUtil.CheckAnyUri(TargetNamespace))
\r
243 error(handler, TargetNamespace+" is not a valid value for targetNamespace attribute of schema");
\r
245 info.TargetNamespace = TargetNamespace;
\r
248 //5. version should be of type normalizedString
\r
249 if(!XmlSchemaUtil.CheckNormalizedString(Version))
\r
250 error(handler, Version + "is not a valid value for version attribute of schema");
\r
252 //6. xml:lang must be a language
\r
253 if(!XmlSchemaUtil.CheckLanguage(Language))
\r
254 error(handler, Language + " is not a valid language");
\r
256 // elementFormDefault defaults to UnQualified
\r
257 if(ElementFormDefault != XmlSchemaForm.Qualified)
\r
258 info.ElementFormDefault = XmlSchemaForm.Unqualified;
\r
260 info.ElementFormDefault = XmlSchemaForm.Qualified;
\r
262 // attributeFormDefault defaults to UnQualified
\r
263 if(AttributeFormDefault != XmlSchemaForm.Qualified)
\r
264 info.AttributeFormDefault = XmlSchemaForm.Unqualified;
\r
266 info.AttributeFormDefault = XmlSchemaForm.Qualified;
\r
268 if(FinalDefault == XmlSchemaDerivationMethod.All)
\r
269 info.FinalDefault = XmlSchemaDerivationMethod.All;
\r
270 else // If finalDefault is None, info's finalDefault is set to empty
\r
271 info.FinalDefault = (FinalDefault & (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction));
\r
273 if(BlockDefault == XmlSchemaDerivationMethod.All)
\r
274 info.BlockDefault = XmlSchemaDerivationMethod.All;
\r
275 else // If finalDefault is None, info's blockDefault is set to empty
\r
276 info.BlockDefault = (blockDefault & (XmlSchemaDerivationMethod.Extension |
\r
277 XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.Substitution));
\r
279 // Compile the content of this schema
\r
280 foreach(XmlSchemaObject obj in Includes)
\r
282 if(obj is XmlSchemaExternal)
\r
284 //FIXME: Kuch to karo! (Do Something ;)
\r
288 error(handler,"Object of Type "+obj.GetType().Name+" is not valid in Includes Property of XmlSchema");
\r
291 foreach(XmlSchemaObject obj in Items)
\r
293 if(obj is XmlSchemaAnnotation)
\r
295 int numerr = ((XmlSchemaAnnotation)obj).Compile(handler,info);
\r
296 errorCount += numerr;
\r
299 //FIXME: What PSVI set do we add this to?
\r
302 else if(obj is XmlSchemaAttribute)
\r
304 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
\r
305 attr.parentIsSchema = true;
\r
306 int numerr = attr.Compile(handler,info);
\r
307 errorCount += numerr;
\r
310 Attributes.Add(attr.QualifiedName, attr);
\r
313 else if(obj is XmlSchemaAttributeGroup)
\r
315 XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
\r
316 int numerr = attrgrp.Compile(handler,info);
\r
317 errorCount += numerr;
\r
320 AttributeGroups.Add(attrgrp.QualifiedName, attrgrp);
\r
323 else if(obj is XmlSchemaComplexType)
\r
325 XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
\r
326 ctype.istoplevel = true;
\r
327 int numerr = ctype.Compile(handler,info);
\r
328 errorCount += numerr;
\r
331 schemaTypes.Add(ctype.QualifiedName, ctype);
\r
334 else if(obj is XmlSchemaSimpleType)
\r
336 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
\r
337 stype.islocal = false; //This simple type is toplevel
\r
338 int numerr = stype.Compile(handler,info);
\r
339 errorCount += numerr;
\r
342 SchemaTypes.Add(stype.QualifiedName, stype);
\r
345 else if(obj is XmlSchemaElement)
\r
347 XmlSchemaElement elem = (XmlSchemaElement) obj;
\r
348 elem.parentIsSchema = true;
\r
349 int numerr = elem.Compile(handler,info);
\r
350 errorCount += numerr;
\r
353 Elements.Add(elem.QualifiedName,elem);
\r
356 else if(obj is XmlSchemaGroup)
\r
358 XmlSchemaGroup grp = (XmlSchemaGroup) obj;
\r
359 int numerr = grp.Compile(handler,info);
\r
360 errorCount += numerr;
\r
363 Groups.Add(grp.QualifiedName,grp);
\r
366 else if(obj is XmlSchemaNotation)
\r
368 XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
\r
369 int numerr = ntn.Compile(handler,info);
\r
370 errorCount += numerr;
\r
373 Notations.Add(ntn.QualifiedName, ntn);
\r
378 ValidationHandler.RaiseValidationError(handler,this,
\r
379 "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema");
\r
386 protected void Validate(ValidationEventHandler handler)
\r
389 foreach(XmlSchemaObject obj in Includes)
\r
394 // foreach(XmlSchemaAnnotation ann in ??????)
\r
396 // ann.Validate(handler);
\r
398 foreach(XmlSchemaAttribute attr in Attributes.Values)
\r
400 attr.Validate(handler);
\r
402 foreach(XmlSchemaAttributeGroup attrgrp in AttributeGroups.Values)
\r
404 attrgrp.Validate(handler);
\r
406 foreach(XmlSchemaType type in SchemaTypes.Values)
\r
408 if(type is XmlSchemaComplexType)
\r
410 ((XmlSchemaComplexType)type).Validate(handler);
\r
413 ((XmlSchemaSimpleType)type).Validate(handler);
\r
415 foreach(XmlSchemaElement elem in Elements.Values)
\r
417 elem.Validate(handler);
\r
419 foreach(XmlSchemaGroup grp in Groups.Values)
\r
421 grp.Validate(handler);
\r
423 foreach(XmlSchemaNotation ntn in Notations.Values)
\r
425 ntn.Validate(handler);
\r
429 public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)
\r
431 return Read(new XmlTextReader(reader),validationEventHandler);
\r
433 public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)
\r
435 return Read(new XmlTextReader(stream),validationEventHandler);
\r
438 public static XmlSchema Read(XmlReader rdr, ValidationEventHandler validationEventHandler)
\r
440 //XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
441 //return (XmlSchema) xser.Deserialize(reader);
\r
442 XmlSchemaReader reader = new XmlSchemaReader(rdr, validationEventHandler);
\r
444 while(reader.ReadNextElement())
\r
446 switch(reader.NodeType)
\r
448 case XmlNodeType.Element:
\r
449 if(reader.LocalName == "schema")
\r
451 XmlSchema schema = new XmlSchema();
\r
453 schema.LineNumber = reader.LineNumber;
\r
454 schema.LinePosition = reader.LinePosition;
\r
455 schema.SourceUri = reader.BaseURI;
\r
457 ReadAttributes(schema, reader, validationEventHandler);
\r
458 //IsEmptyElement does not behave properly if reader is
\r
459 //positioned at an attribute.
\r
460 reader.MoveToElement();
\r
461 if(!reader.IsEmptyElement)
\r
463 ReadContent(schema, reader, validationEventHandler);
\r
469 //Schema can't be generated. Throw an exception
\r
470 throw new XmlSchemaException("The root element must be schema", null);
\r
473 error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);
\r
477 throw new XmlSchemaException("The top level schema must have namespace "+XmlSchema.Namespace, null);
\r
480 private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
484 reader.MoveToElement();
\r
485 while(reader.MoveToNextAttribute())
\r
487 switch(reader.Name)
\r
489 case "attributeFormDefault" :
\r
490 schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);
\r
492 error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);
\r
494 case "blockDefault" :
\r
495 schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault");
\r
497 warn(h, ex.Message, ex);
\r
499 case "elementFormDefault":
\r
500 schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);
\r
502 error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);
\r
504 case "finalDefault":
\r
505 schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault");
\r
507 warn(h, ex.Message , ex);
\r
510 schema.id = reader.Value;
\r
512 case "targetNamespace":
\r
513 schema.targetNamespace = reader.Value;
\r
516 schema.version = reader.Value;
\r
519 schema.language = reader.Value;
\r
522 if(reader.Prefix == "xmlns")
\r
523 schema.Namespaces.Add(reader.LocalName, reader.Value);
\r
524 else if(reader.Name == "xmlns")
\r
525 schema.Namespaces.Add("",reader.Value);
\r
526 else if(reader.Prefix == "" || reader.NamespaceURI == XmlSchema.Namespace)
\r
527 error(h, reader.Name + " attribute is not allowed in schema element",null);
\r
528 // FIXME: There is no public constructor for XmlAttribute. Nor is there a way to access the XmlNode
\r
530 // unknown.Add(new XmlAttibute());
\r
536 private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
538 reader.MoveToElement();
\r
539 if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
\r
540 error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);
\r
542 //(include | import | redefine | annotation)*,
\r
543 //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
\r
545 while(reader.ReadNextElement())
\r
547 if(reader.NodeType == XmlNodeType.EndElement)
\r
549 if(reader.LocalName != xmlname)
\r
550 error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);
\r
555 if(reader.LocalName == "include")
\r
557 XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);
\r
558 if(include != null)
\r
559 schema.includes.Add(include);
\r
562 if(reader.LocalName == "import")
\r
564 XmlSchemaImport import = XmlSchemaImport.Read(reader,h);
\r
566 schema.includes.Add(import);
\r
569 if(reader.LocalName == "redefine")
\r
571 XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);
\r
572 if(redefine != null)
\r
573 schema.includes.Add(redefine);
\r
576 if(reader.LocalName == "annotation")
\r
578 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
579 if(annotation != null)
\r
580 schema.items.Add(annotation);
\r
587 if(reader.LocalName == "simpleType")
\r
589 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
\r
591 schema.items.Add(stype);
\r
594 if(reader.LocalName == "complexType")
\r
596 XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);
\r
598 schema.items.Add(ctype);
\r
601 if(reader.LocalName == "group")
\r
603 XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);
\r
605 schema.items.Add(group);
\r
608 if(reader.LocalName == "attributeGroup")
\r
610 XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);
\r
611 if(attributeGroup != null)
\r
612 schema.items.Add(attributeGroup);
\r
615 if(reader.LocalName == "element")
\r
617 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
\r
618 if(element != null)
\r
619 schema.items.Add(element);
\r
622 if(reader.LocalName == "attribute")
\r
624 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
\r
626 schema.items.Add(attr);
\r
629 if(reader.LocalName == "notation")
\r
631 XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);
\r
632 if(notation != null)
\r
633 schema.items.Add(notation);
\r
636 if(reader.LocalName == "annotation")
\r
638 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
639 if(annotation != null)
\r
640 schema.items.Add(annotation);
\r
644 reader.RaiseInvalidElementError();
\r
648 public void Write(System.IO.Stream stream)
\r
650 Write(stream,null);
\r
652 public void Write(System.IO.TextWriter writer)
\r
654 Write(writer,null);
\r
656 public void Write(System.Xml.XmlWriter writer)
\r
658 Write(writer,null);
\r
660 public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
\r
662 Write(new XmlTextWriter(stream,null),namespaceManager);
\r
664 public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
666 XmlTextWriter xwriter = new XmlTextWriter(writer);
\r
667 xwriter.Formatting = Formatting.Indented;
\r
668 Write(xwriter,namespaceManager);
\r
671 public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
673 if(Namespaces == null)
\r
675 Namespaces = new XmlSerializerNamespaces();
\r
678 if(namespaceManager != null)
\r
680 foreach(string name in namespaceManager)
\r
682 //xml and xmlns namespaced are added by default in namespaceManager.
\r
683 //So we should ignore them
\r
684 if(name!="xml" && name != "xmlns")
\r
685 Namespaces.Add(name,namespaceManager.LookupNamespace(name));
\r
689 XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
690 xser.Serialize(writer,this,Namespaces);
\r