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 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
72 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
73 [System.Xml.Serialization.XmlAttribute("blockDefault")]
\r
74 public XmlSchemaDerivationMethod BlockDefault
\r
76 get{ return blockDefault;}
\r
77 set{ blockDefault = value;}
\r
80 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
81 [System.Xml.Serialization.XmlAttribute("finalDefault")]
\r
82 public XmlSchemaDerivationMethod FinalDefault
\r
84 get{ return finalDefault;}
\r
85 set{ finalDefault = value;}
\r
88 [DefaultValue(XmlSchemaForm.None)]
\r
89 [System.Xml.Serialization.XmlAttribute("elementFormDefault")]
\r
90 public XmlSchemaForm ElementFormDefault
\r
92 get{ return elementFormDefault;}
\r
93 set{ elementFormDefault = value;}
\r
96 [System.Xml.Serialization.XmlAttribute("targetNamespace")]
\r
97 public string TargetNamespace
\r
99 get{ return targetNamespace;}
\r
100 set{ targetNamespace = value;}
\r
103 [System.Xml.Serialization.XmlAttribute("version")]
\r
104 public string Version
\r
106 get{ return version;}
\r
107 set{ version = value;}
\r
110 [XmlElement("include",typeof(XmlSchemaInclude),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
111 [XmlElement("import",typeof(XmlSchemaImport),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
112 [XmlElement("redefine",typeof(XmlSchemaRedefine),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
113 public XmlSchemaObjectCollection Includes
\r
115 get{ return includes;}
\r
118 [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
119 [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
120 [XmlElement("group",typeof(XmlSchemaGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
121 //Only Schema's attributeGroup has type XmlSchemaAttributeGroup.
\r
122 //Others (complextype, restrictions etc) must have XmlSchemaAttributeGroupRef
\r
123 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
124 [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
125 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
126 [XmlElement("notation",typeof(XmlSchemaNotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
127 [XmlElement("annotation",typeof(XmlSchemaAnnotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
128 public XmlSchemaObjectCollection Items
\r
130 get{ return items;}
\r
134 public bool IsCompiled
\r
136 get{ return isCompiled;}
\r
140 public XmlSchemaObjectTable Attributes
\r
142 get{ return attributes;}
\r
146 public XmlSchemaObjectTable AttributeGroups
\r
148 get{ return attributeGroups; }
\r
152 public XmlSchemaObjectTable SchemaTypes
\r
154 get{ return schemaTypes; }
\r
158 public XmlSchemaObjectTable Elements
\r
160 get{ return elements;}
\r
163 [System.Xml.Serialization.XmlAttribute("id")]
\r
171 public XmlAttribute[] UnhandledAttributes
\r
175 if(unhandledAttributeList != null)
\r
177 unhandledAttributes = (XmlAttribute[]) unhandledAttributeList.ToArray(typeof(XmlAttribute));
\r
178 unhandledAttributeList = null;
\r
180 return unhandledAttributes;
\r
184 unhandledAttributes = value;
\r
185 unhandledAttributeList = null;
\r
190 public XmlSchemaObjectTable Groups
\r
192 get{ return groups;}
\r
196 public XmlSchemaObjectTable Notations
\r
198 get{ return notations;}
\r
201 // New attribute defined in W3C schema element
\r
202 [System.Xml.Serialization.XmlAttribute("xml:lang")]
\r
203 public string Language
\r
205 get{ return language; }
\r
206 set{ language = value; }
\r
215 /// This compile method does two things:
\r
216 /// 1. It compiles and fills the PSVI dataset
\r
217 /// 2. Validates the schema by calling Validate method.
\r
218 /// Every XmlSchemaObject has a Compile Method which gets called.
\r
221 /// 1. blockDefault must be one of #all | List of (extension | restriction | substitution)
\r
222 /// 2. finalDefault must be one of (#all | List of (extension | restriction| union| list))
\r
223 /// 3. id must be of type ID
\r
224 /// 4. targetNamespace should be any uri
\r
225 /// 5. version should be a normalizedString
\r
226 /// 6. xml:lang should be a language
\r
229 public void Compile(ValidationEventHandler handler)
\r
231 // Create the xmlschemainfo object which we use to pass variables like targetnamespace;
\r
232 info = new XmlSchemaInfo();
\r
234 //1. Union and List are not allowed in block default
\r
235 if(BlockDefault != XmlSchemaDerivationMethod.All)
\r
237 if((BlockDefault & XmlSchemaDerivationMethod.List)!=0 )
\r
238 error(handler, "list is not allowed in blockDefault attribute");
\r
239 if((BlockDefault & XmlSchemaDerivationMethod.Union)!=0 )
\r
240 error(handler, "union is not allowed in blockDefault attribute");
\r
243 //2. Substitution is not allowed in finaldefault.
\r
244 if(FinalDefault != XmlSchemaDerivationMethod.All)
\r
246 if((FinalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )
\r
247 error(handler, "substitution is not allowed in finalDefault attribute");
\r
250 //3. id must be of type ID
\r
251 XmlSchemaUtil.CompileID(Id, this, info.IDCollection, handler);
\r
253 //4. targetNamespace should be of type anyURI or absent
\r
254 if(TargetNamespace != null)
\r
256 if(!XmlSchemaUtil.CheckAnyUri(TargetNamespace))
\r
257 error(handler, TargetNamespace+" is not a valid value for targetNamespace attribute of schema");
\r
259 info.TargetNamespace = TargetNamespace;
\r
262 //5. version should be of type normalizedString
\r
263 if(!XmlSchemaUtil.CheckNormalizedString(Version))
\r
264 error(handler, Version + "is not a valid value for version attribute of schema");
\r
266 //6. xml:lang must be a language
\r
267 if(!XmlSchemaUtil.CheckLanguage(Language))
\r
268 error(handler, Language + " is not a valid language");
\r
270 // elementFormDefault defaults to UnQualified
\r
271 if(ElementFormDefault != XmlSchemaForm.Qualified)
\r
272 info.ElementFormDefault = XmlSchemaForm.Unqualified;
\r
274 info.ElementFormDefault = XmlSchemaForm.Qualified;
\r
276 // attributeFormDefault defaults to UnQualified
\r
277 if(AttributeFormDefault != XmlSchemaForm.Qualified)
\r
278 info.AttributeFormDefault = XmlSchemaForm.Unqualified;
\r
280 info.AttributeFormDefault = XmlSchemaForm.Qualified;
\r
282 if(FinalDefault == XmlSchemaDerivationMethod.All)
\r
283 info.FinalDefault = XmlSchemaDerivationMethod.All;
\r
284 else // If finalDefault is None, info's finalDefault is set to empty
\r
285 info.FinalDefault = (FinalDefault & (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction));
\r
287 if(BlockDefault == XmlSchemaDerivationMethod.All)
\r
288 info.BlockDefault = XmlSchemaDerivationMethod.All;
\r
289 else // If finalDefault is None, info's blockDefault is set to empty
\r
290 info.BlockDefault = (blockDefault & (XmlSchemaDerivationMethod.Extension |
\r
291 XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.Substitution));
\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 foreach(XmlSchemaObject obj in Items)
\r
307 if(obj is XmlSchemaAnnotation)
\r
309 int numerr = ((XmlSchemaAnnotation)obj).Compile(handler,info);
\r
310 errorCount += numerr;
\r
313 //FIXME: What PSVI set do we add this to?
\r
316 else if(obj is XmlSchemaAttribute)
\r
318 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
\r
319 attr.parentIsSchema = true;
\r
320 int numerr = attr.Compile(handler,info);
\r
321 errorCount += numerr;
\r
324 Attributes.Add(attr.QualifiedName, attr);
\r
327 else if(obj is XmlSchemaAttributeGroup)
\r
329 XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
\r
330 int numerr = attrgrp.Compile(handler,info);
\r
331 errorCount += numerr;
\r
334 AttributeGroups.Add(attrgrp.QualifiedName, attrgrp);
\r
337 else if(obj is XmlSchemaComplexType)
\r
339 XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
\r
340 ctype.istoplevel = true;
\r
341 int numerr = ctype.Compile(handler,info);
\r
342 errorCount += numerr;
\r
345 schemaTypes.Add(ctype.QualifiedName, ctype);
\r
348 else if(obj is XmlSchemaSimpleType)
\r
350 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
\r
351 stype.islocal = false; //This simple type is toplevel
\r
352 int numerr = stype.Compile(handler,info);
\r
353 errorCount += numerr;
\r
356 SchemaTypes.Add(stype.QualifiedName, stype);
\r
359 else if(obj is XmlSchemaElement)
\r
361 XmlSchemaElement elem = (XmlSchemaElement) obj;
\r
362 elem.parentIsSchema = true;
\r
363 int numerr = elem.Compile(handler,info);
\r
364 errorCount += numerr;
\r
367 Elements.Add(elem.QualifiedName,elem);
\r
370 else if(obj is XmlSchemaGroup)
\r
372 XmlSchemaGroup grp = (XmlSchemaGroup) obj;
\r
373 int numerr = grp.Compile(handler,info);
\r
374 errorCount += numerr;
\r
377 Groups.Add(grp.QualifiedName,grp);
\r
380 else if(obj is XmlSchemaNotation)
\r
382 XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
\r
383 int numerr = ntn.Compile(handler,info);
\r
384 errorCount += numerr;
\r
387 Notations.Add(ntn.QualifiedName, ntn);
\r
392 ValidationHandler.RaiseValidationError(handler,this,
\r
393 "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema");
\r
402 private void Validate(ValidationEventHandler handler)
\r
404 info.SchemaTypes = SchemaTypes;
\r
406 foreach(XmlSchemaAttribute attr in Attributes.Values)
\r
408 attr.Validate(handler, info);
\r
410 foreach(XmlSchemaAttributeGroup attrgrp in AttributeGroups.Values)
\r
412 attrgrp.Validate(handler);
\r
414 foreach(XmlSchemaType type in SchemaTypes.Values)
\r
416 if(type is XmlSchemaComplexType)
\r
418 ((XmlSchemaComplexType)type).Validate(handler);
\r
421 ((XmlSchemaSimpleType)type).Validate(handler, info);
\r
423 foreach(XmlSchemaElement elem in Elements.Values)
\r
425 elem.Validate(handler);
\r
427 foreach(XmlSchemaGroup grp in Groups.Values)
\r
429 grp.Validate(handler);
\r
431 foreach(XmlSchemaNotation ntn in Notations.Values)
\r
433 ntn.Validate(handler);
\r
440 public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)
\r
442 return Read(new XmlTextReader(reader),validationEventHandler);
\r
444 public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)
\r
446 return Read(new XmlTextReader(stream),validationEventHandler);
\r
449 public static XmlSchema Read(XmlReader rdr, ValidationEventHandler validationEventHandler)
\r
451 //XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
452 //return (XmlSchema) xser.Deserialize(reader);
\r
453 XmlSchemaReader reader = new XmlSchemaReader(rdr, validationEventHandler);
\r
455 while(reader.ReadNextElement())
\r
457 switch(reader.NodeType)
\r
459 case XmlNodeType.Element:
\r
460 if(reader.LocalName == "schema")
\r
462 XmlSchema schema = new XmlSchema();
\r
464 schema.LineNumber = reader.LineNumber;
\r
465 schema.LinePosition = reader.LinePosition;
\r
466 schema.SourceUri = reader.BaseURI;
\r
468 ReadAttributes(schema, reader, validationEventHandler);
\r
469 //IsEmptyElement does not behave properly if reader is
\r
470 //positioned at an attribute.
\r
471 reader.MoveToElement();
\r
472 if(!reader.IsEmptyElement)
\r
474 ReadContent(schema, reader, validationEventHandler);
\r
480 //Schema can't be generated. Throw an exception
\r
481 throw new XmlSchemaException("The root element must be schema", null);
\r
484 error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);
\r
488 throw new XmlSchemaException("The top level schema must have namespace "+XmlSchema.Namespace, null);
\r
491 private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
495 reader.MoveToElement();
\r
496 while(reader.MoveToNextAttribute())
\r
498 switch(reader.Name)
\r
500 case "attributeFormDefault" :
\r
501 schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);
\r
503 error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);
\r
505 case "blockDefault" :
\r
506 schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault");
\r
508 warn(h, ex.Message, ex);
\r
510 case "elementFormDefault":
\r
511 schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);
\r
513 error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);
\r
515 case "finalDefault":
\r
516 schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault");
\r
518 warn(h, ex.Message , ex);
\r
521 schema.id = reader.Value;
\r
523 case "targetNamespace":
\r
524 schema.targetNamespace = reader.Value;
\r
527 schema.version = reader.Value;
\r
530 schema.language = reader.Value;
\r
533 if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
534 error(h, reader.Name + " attribute is not allowed in schema element",null);
\r
537 XmlSchemaUtil.ReadUnhandledAttribute(reader,schema);
\r
544 private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
546 reader.MoveToElement();
\r
547 if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
\r
548 error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);
\r
550 //(include | import | redefine | annotation)*,
\r
551 //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
\r
553 while(reader.ReadNextElement())
\r
555 if(reader.NodeType == XmlNodeType.EndElement)
\r
557 if(reader.LocalName != xmlname)
\r
558 error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);
\r
563 if(reader.LocalName == "include")
\r
565 XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);
\r
566 if(include != null)
\r
567 schema.includes.Add(include);
\r
570 if(reader.LocalName == "import")
\r
572 XmlSchemaImport import = XmlSchemaImport.Read(reader,h);
\r
574 schema.includes.Add(import);
\r
577 if(reader.LocalName == "redefine")
\r
579 XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);
\r
580 if(redefine != null)
\r
581 schema.includes.Add(redefine);
\r
584 if(reader.LocalName == "annotation")
\r
586 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
587 if(annotation != null)
\r
588 schema.items.Add(annotation);
\r
595 if(reader.LocalName == "simpleType")
\r
597 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
\r
599 schema.items.Add(stype);
\r
602 if(reader.LocalName == "complexType")
\r
604 XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);
\r
606 schema.items.Add(ctype);
\r
609 if(reader.LocalName == "group")
\r
611 XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);
\r
613 schema.items.Add(group);
\r
616 if(reader.LocalName == "attributeGroup")
\r
618 XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);
\r
619 if(attributeGroup != null)
\r
620 schema.items.Add(attributeGroup);
\r
623 if(reader.LocalName == "element")
\r
625 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
\r
626 if(element != null)
\r
627 schema.items.Add(element);
\r
630 if(reader.LocalName == "attribute")
\r
632 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
\r
634 schema.items.Add(attr);
\r
637 if(reader.LocalName == "notation")
\r
639 XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);
\r
640 if(notation != null)
\r
641 schema.items.Add(notation);
\r
644 if(reader.LocalName == "annotation")
\r
646 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
647 if(annotation != null)
\r
648 schema.items.Add(annotation);
\r
652 reader.RaiseInvalidElementError();
\r
659 public void Write(System.IO.Stream stream)
\r
661 Write(stream,null);
\r
663 public void Write(System.IO.TextWriter writer)
\r
665 Write(writer,null);
\r
667 public void Write(System.Xml.XmlWriter writer)
\r
669 Write(writer,null);
\r
671 public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
\r
673 Write(new XmlTextWriter(stream,null),namespaceManager);
\r
675 public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
677 XmlTextWriter xwriter = new XmlTextWriter(writer);
\r
678 xwriter.Formatting = Formatting.Indented;
\r
679 Write(xwriter,namespaceManager);
\r
681 public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
683 if(Namespaces == null)
\r
685 Namespaces = new XmlSerializerNamespaces();
\r
687 //Add the xml schema namespace.
\r
688 if(Namespaces.Count == 0)
\r
690 Namespaces.Add("xs", XmlSchema.Namespace);
\r
691 if (TargetNamespace != null && TargetNamespace != String.Empty)
\r
692 Namespaces.Add("tns", TargetNamespace);
\r
694 if(namespaceManager != null)
\r
696 foreach(string name in namespaceManager)
\r
698 //xml and xmlns namespaced are added by default in namespaceManager.
\r
699 //So we should ignore them
\r
700 if(name!="xml" && name != "xmlns")
\r
701 Namespaces.Add(name,namespaceManager.LookupNamespace(name));
\r
705 XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
706 xser.Serialize(writer,this,Namespaces);
\r