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 token
\r
212 /// 6. xml:lang should be a language
\r
216 public void Compile(ValidationEventHandler handler)
\r
218 //1. Union and List are not allowed in block default
\r
219 if(this.blockDefault != XmlSchemaDerivationMethod.All)
\r
221 if((this.blockDefault & XmlSchemaDerivationMethod.List)!=0 )
\r
222 error(handler, "list is not allowed in blockDefault attribute");
\r
223 if((this.blockDefault & XmlSchemaDerivationMethod.Union)!=0 )
\r
224 error(handler, "union is not allowed in blockDefault attribute");
\r
226 //2. Substitution is not allowed in finaldefault.
\r
227 if(this.finalDefault != XmlSchemaDerivationMethod.All)
\r
229 if((this.finalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )
\r
230 error(handler, "substitution is not allowed in finalDefault attribute");
\r
232 //3. id must be of type ID
\r
233 if(this.id != null && !XmlSchemaUtil.CheckID(this.id))
\r
234 error(handler, "id attribute is not a valid ID");
\r
236 //4. targetNamespace should be of type anyURI
\r
237 if(!XmlSchemaUtil.CheckAnyUri(this.targetNamespace))
\r
238 error(handler, "targetNamespace is not a valid URI");
\r
240 //5. version should be of type TOKEN
\r
241 if(!XmlSchemaUtil.CheckToken(this.version))
\r
242 error(handler, "version is not a valid token");
\r
244 //6. xml:lang must be a language
\r
245 if(!XmlSchemaUtil.CheckLanguage(this.language))
\r
246 error(handler, "xml:lang is not a valid language");
\r
248 // Create the xmlschemainfo object which we use to pass variables like targetnamespace;
\r
249 info = new XmlSchemaInfo();
\r
250 if(this.targetNamespace != null && XmlSchemaUtil.CheckAnyUri(this.targetNamespace))
\r
251 info.targetNS = this.TargetNamespace;
\r
253 if(this.ElementFormDefault != XmlSchemaForm.Qualified)
\r
254 info.formDefault = XmlSchemaForm.Unqualified;
\r
256 info.formDefault = XmlSchemaForm.Qualified;
\r
258 if(FinalDefault == XmlSchemaDerivationMethod.All)
\r
259 info.finalDefault = XmlSchemaDerivationMethod.All;
\r
260 else // If finalDefault is None, info's finalDefault is set to empty
\r
261 info.finalDefault = (FinalDefault & (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction));
\r
263 if(BlockDefault == XmlSchemaDerivationMethod.All)
\r
264 info.blockDefault = XmlSchemaDerivationMethod.All;
\r
265 else // If finalDefault is None, info's blockDefault is set to empty
\r
266 info.blockDefault = (blockDefault & (XmlSchemaDerivationMethod.Extension |
\r
267 XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.Substitution));
\r
269 // Compile the content of this schema
\r
270 foreach(XmlSchemaObject obj in Includes)
\r
272 if(obj is XmlSchemaExternal)
\r
274 //FIXME: Kuch to karo! (Do Something ;)
\r
278 error(handler,"Object of Type "+obj.GetType().Name+" is not valid in Includes Property of Schema");
\r
281 foreach(XmlSchemaObject obj in Items)
\r
283 if(obj is XmlSchemaAnnotation)
\r
285 if(((XmlSchemaAnnotation)obj).Compile(handler,info) == 0)
\r
287 //FIXME: What PSVI set do we add this to?
\r
290 else if(obj is XmlSchemaAttribute)
\r
292 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
\r
293 attr.parentIsSchema = true;
\r
294 if(attr.Compile(handler,info) == 0)
\r
296 Attributes.Add(attr.QualifiedName, attr);
\r
299 else if(obj is XmlSchemaAttributeGroup)
\r
301 XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
\r
302 if(attrgrp.Compile(handler,info) == 0)
\r
304 AttributeGroups.Add(attrgrp.QualifiedName, attrgrp);
\r
307 else if(obj is XmlSchemaComplexType)
\r
309 XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
\r
310 ctype.istoplevel = true;
\r
311 if(ctype.Compile(handler,info) == 0)
\r
313 schemaTypes.Add(ctype.QualifiedName, ctype);
\r
316 else if(obj is XmlSchemaSimpleType)
\r
318 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
\r
319 stype.islocal = false; //This simple type is toplevel
\r
320 if(stype.Compile(handler,info) == 0)
\r
322 SchemaTypes.Add(stype.QualifiedName, stype);
\r
325 else if(obj is XmlSchemaElement)
\r
327 XmlSchemaElement elem = (XmlSchemaElement) obj;
\r
328 elem.parentIsSchema = true;
\r
329 if(elem.Compile(handler,info) == 0)
\r
331 Elements.Add(elem.QualifiedName,elem);
\r
334 else if(obj is XmlSchemaGroup)
\r
336 XmlSchemaGroup grp = (XmlSchemaGroup) obj;
\r
337 if(grp.Compile(handler,info) == 0)
\r
339 Groups.Add(grp.QualifiedName,grp);
\r
342 else if(obj is XmlSchemaNotation)
\r
344 XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
\r
345 if(ntn.Compile(handler,info) == 0)
\r
347 Notations.Add(ntn.QualifiedName, ntn);
\r
352 ValidationHandler.RaiseValidationError(handler,this,
\r
353 "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema");
\r
360 protected void Validate(ValidationEventHandler handler)
\r
363 foreach(XmlSchemaObject obj in Includes)
\r
367 // foreach(XmlSchemaAnnotation ann in ??????)
\r
369 // ann.Validate(handler);
\r
371 foreach(XmlSchemaAttribute attr in Attributes.Values)
\r
373 attr.Validate(handler);
\r
375 foreach(XmlSchemaAttributeGroup attrgrp in AttributeGroups.Values)
\r
377 attrgrp.Validate(handler);
\r
379 foreach(XmlSchemaType type in SchemaTypes.Values)
\r
381 if(type is XmlSchemaComplexType)
\r
383 ((XmlSchemaComplexType)type).Validate(handler);
\r
386 ((XmlSchemaSimpleType)type).Validate(handler);
\r
388 foreach(XmlSchemaElement elem in Elements.Values)
\r
390 elem.Validate(handler);
\r
392 foreach(XmlSchemaGroup grp in Groups.Values)
\r
394 grp.Validate(handler);
\r
396 foreach(XmlSchemaNotation ntn in Notations.Values)
\r
398 ntn.Validate(handler);
\r
402 public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)
\r
404 return Read(new XmlTextReader(reader),validationEventHandler);
\r
406 public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)
\r
408 return Read(new XmlTextReader(stream),validationEventHandler);
\r
412 public static XmlSchema Read(XmlReader rdr, ValidationEventHandler validationEventHandler)
\r
414 //XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
415 //return (XmlSchema) xser.Deserialize(reader);
\r
416 XmlSchemaReader reader = new XmlSchemaReader(rdr, validationEventHandler);
\r
418 while(reader.ReadNextElement())
\r
420 switch(reader.NodeType)
\r
422 case XmlNodeType.Element:
\r
423 if(reader.LocalName == "schema")
\r
425 XmlSchema schema = new XmlSchema();
\r
427 schema.LineNumber = reader.LineNumber;
\r
428 schema.LinePosition = reader.LinePosition;
\r
429 schema.SourceUri = reader.BaseURI;
\r
431 ReadAttributes(schema, reader, validationEventHandler);
\r
432 //IsEmptyElement does not behave properly if reader is
\r
433 //positioned at an attribute.
\r
434 reader.MoveToElement();
\r
435 if(!reader.IsEmptyElement)
\r
437 ReadContent(schema, reader, validationEventHandler);
\r
443 //Schema can't be generated. Throw an exception
\r
444 throw new XmlSchemaException("The root element must be schema", null);
\r
447 error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);
\r
451 throw new XmlSchemaException("The top level schema must have namespace "+XmlSchema.Namespace, null);
\r
454 private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
458 reader.MoveToElement();
\r
459 while(reader.MoveToNextAttribute())
\r
461 switch(reader.Name)
\r
463 case "attributeFormDefault" :
\r
464 schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);
\r
466 error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);
\r
468 case "blockDefault" :
\r
469 schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault");
\r
471 warn(h, ex.Message, ex);
\r
473 case "elementFormDefault":
\r
474 schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);
\r
476 error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);
\r
478 case "finalDefault":
\r
479 schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault");
\r
481 warn(h, ex.Message , ex);
\r
484 schema.id = reader.Value;
\r
486 case "targetNamespace":
\r
487 schema.targetNamespace = reader.Value;
\r
490 schema.version = reader.Value;
\r
493 schema.language = reader.Value;
\r
496 if(reader.Prefix == "xmlns")
\r
497 schema.Namespaces.Add(reader.LocalName, reader.Value);
\r
498 else if(reader.Name == "xmlns")
\r
499 schema.Namespaces.Add("",reader.Value);
\r
500 else if(reader.Prefix == "" || reader.NamespaceURI == XmlSchema.Namespace)
\r
501 error(h, reader.Name + " attribute is not allowed in schema element",null);
\r
502 // FIXME: There is no public constructor for XmlAttribute. Nor is there a way to access the XmlNode
\r
504 // unknown.Add(new XmlAttibute());
\r
510 private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
\r
512 reader.MoveToElement();
\r
513 if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
\r
514 error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);
\r
516 //(include | import | redefine | annotation)*,
\r
517 //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
\r
519 while(reader.ReadNextElement())
\r
521 if(reader.NodeType == XmlNodeType.EndElement)
\r
523 if(reader.LocalName != xmlname)
\r
524 error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);
\r
529 if(reader.LocalName == "include")
\r
531 XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);
\r
532 if(include != null)
\r
533 schema.includes.Add(include);
\r
536 if(reader.LocalName == "import")
\r
538 XmlSchemaImport import = XmlSchemaImport.Read(reader,h);
\r
540 schema.includes.Add(import);
\r
543 if(reader.LocalName == "redefine")
\r
545 XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);
\r
546 if(redefine != null)
\r
547 schema.includes.Add(redefine);
\r
550 if(reader.LocalName == "annotation")
\r
552 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
553 if(annotation != null)
\r
554 schema.items.Add(annotation);
\r
561 if(reader.LocalName == "simpleType")
\r
563 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
\r
565 schema.items.Add(stype);
\r
568 if(reader.LocalName == "complexType")
\r
570 XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);
\r
572 schema.items.Add(ctype);
\r
575 if(reader.LocalName == "group")
\r
577 XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);
\r
579 schema.items.Add(group);
\r
582 if(reader.LocalName == "attributeGroup")
\r
584 XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);
\r
585 if(attributeGroup != null)
\r
586 schema.items.Add(attributeGroup);
\r
589 if(reader.LocalName == "element")
\r
591 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
\r
592 if(element != null)
\r
593 schema.items.Add(element);
\r
596 if(reader.LocalName == "attribute")
\r
598 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
\r
600 schema.items.Add(attr);
\r
603 if(reader.LocalName == "notation")
\r
605 XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);
\r
606 if(notation != null)
\r
607 schema.items.Add(notation);
\r
610 if(reader.LocalName == "annotation")
\r
612 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
613 if(annotation != null)
\r
614 schema.items.Add(annotation);
\r
618 reader.RaiseInvalidElementError();
\r
622 public void Write(System.IO.Stream stream)
\r
624 Write(stream,null);
\r
626 public void Write(System.IO.TextWriter writer)
\r
628 Write(writer,null);
\r
630 public void Write(System.Xml.XmlWriter writer)
\r
632 Write(writer,null);
\r
634 public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
\r
636 Write(new XmlTextWriter(stream,null),namespaceManager);
\r
638 public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
640 XmlTextWriter xwriter = new XmlTextWriter(writer);
\r
641 xwriter.Formatting = Formatting.Indented;
\r
642 Write(xwriter,namespaceManager);
\r
645 public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
647 if(Namespaces == null)
\r
649 Namespaces = new XmlSerializerNamespaces();
\r
652 if(namespaceManager != null)
\r
654 foreach(string name in namespaceManager)
\r
656 //xml and xmlns namespaced are added by default in namespaceManager.
\r
657 //So we should ignore them
\r
658 if(name!="xml" && name != "xmlns")
\r
659 Namespaces.Add(name,namespaceManager.LookupNamespace(name));
\r
663 XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
664 xser.Serialize(writer,this,Namespaces);
\r