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
46 attributeFormDefault= XmlSchemaForm.None;
\r
47 blockDefault = XmlSchemaDerivationMethod.None;
\r
48 elementFormDefault = XmlSchemaForm.None;
\r
49 finalDefault = XmlSchemaDerivationMethod.None;
\r
50 includes = new XmlSchemaObjectCollection();
\r
52 items = new XmlSchemaObjectCollection();
\r
53 attributeGroups = new XmlSchemaObjectTable();
\r
54 attributes = new XmlSchemaObjectTable();
\r
55 elements = new XmlSchemaObjectTable();
\r
56 groups = new XmlSchemaObjectTable();
\r
57 notations = new XmlSchemaObjectTable();
\r
58 schemaTypes = new XmlSchemaObjectTable();
\r
63 [DefaultValue(XmlSchemaForm.None)]
\r
64 [System.Xml.Serialization.XmlAttribute("attributeFormDefault")]
\r
65 public XmlSchemaForm AttributeFormDefault
\r
67 get{ return attributeFormDefault; }
\r
68 set{ this.attributeFormDefault = value;}
\r
72 public XmlSchemaObjectTable AttributeGroups
\r
74 get{ return attributeGroups; }
\r
78 public XmlSchemaObjectTable Attributes
\r
80 get{ return attributes;}
\r
83 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
84 [System.Xml.Serialization.XmlAttribute("blockDefault")]
\r
85 public XmlSchemaDerivationMethod BlockDefault
\r
87 get{ return blockDefault;}
\r
88 set{ blockDefault = value;}
\r
91 [DefaultValue(XmlSchemaForm.None)]
\r
92 [System.Xml.Serialization.XmlAttribute("elementFormDefault")]
\r
93 public XmlSchemaForm ElementFormDefault
\r
95 get{ return elementFormDefault;}
\r
96 set{ elementFormDefault = value;}
\r
100 public XmlSchemaObjectTable Elements
\r
102 get{ return elements;}
\r
105 [DefaultValue(XmlSchemaDerivationMethod.None)]
\r
106 [System.Xml.Serialization.XmlAttribute("finalDefault")]
\r
107 public XmlSchemaDerivationMethod FinalDefault
\r
109 get{ return finalDefault;}
\r
110 set{ finalDefault = value;}
\r
114 public XmlSchemaObjectTable Groups
\r
116 get{ return groups;}
\r
119 [System.Xml.Serialization.XmlAttribute("id")]
\r
126 [XmlElement("include",typeof(XmlSchemaInclude),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
127 [XmlElement("import",typeof(XmlSchemaImport),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
128 [XmlElement("redefine",typeof(XmlSchemaRedefine),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
129 public XmlSchemaObjectCollection Includes
\r
131 get{ return includes;}
\r
135 public bool IsCompiled
\r
137 get{ return isCompiled;}
\r
140 [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
141 [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
142 [XmlElement("group",typeof(XmlSchemaGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
143 //Only Schema's attributeGroup has type XmlSchemaAttributeGroup.
\r
144 //Others (complextype, restrictions etc) must have XmlSchemaAttributeGroupRef
\r
145 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
146 [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
147 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
148 [XmlElement("notation",typeof(XmlSchemaNotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
149 [XmlElement("annotation",typeof(XmlSchemaAnnotation),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
150 public XmlSchemaObjectCollection Items
\r
152 get{ return items;}
\r
156 public XmlSchemaObjectTable Notations
\r
158 get{ return notations;}
\r
162 public XmlSchemaObjectTable SchemaTypes
\r
164 get{ return schemaTypes;}
\r
167 [System.Xml.Serialization.XmlAttribute("targetNamespace")]
\r
168 public string TargetNamespace
\r
170 get{ return targetNamespace;}
\r
171 set{ targetNamespace = value;}
\r
175 public XmlAttribute[] UnhandledAttributes
\r
177 get{ return unhandledAttributes;}
\r
178 set{ unhandledAttributes = value;}
\r
181 [System.Xml.Serialization.XmlAttribute("version")]
\r
182 public string Version
\r
184 get{ return version;}
\r
185 set{ version = value;}
\r
188 // New attribute defined in W3C schema element
\r
189 [System.Xml.Serialization.XmlAttribute("xml:lang")]
\r
190 public string Language
\r
192 get{ return language; }
\r
193 set{ language = value; }
\r
200 /// This compile method does two things:
\r
201 /// 1. It compiles and fills the PSVI dataset
\r
202 /// 2. Validates the schema by calling Validate method.
\r
203 /// Every XmlSchemaObject has a Compile Method which gets called.
\r
206 public void Compile(ValidationEventHandler handler)
\r
208 // Create the xmlschemainfo object which we use to pass variables like targetnamespace;
\r
209 info = new XmlSchemaInfo();
\r
210 info.targetNS = this.TargetNamespace;
\r
211 info.finalDefault = this.FinalDefault;
\r
213 foreach(XmlSchemaObject obj in Includes)
\r
215 if(obj is XmlSchemaExternal)
\r
217 //FIXME: Kuch to karo (Do Something ;)
\r
221 ValidationHandler.RaiseValidationError(handler,this,
\r
222 "Object of Type "+obj.GetType().Name+" is not valid in Includes Property of Schema");
\r
225 foreach(XmlSchemaObject obj in Items)
\r
227 if(obj is XmlSchemaAnnotation)
\r
229 if(((XmlSchemaAnnotation)obj).Compile(handler,info))
\r
231 //FIXME: What PSVI set do we add this to?
\r
234 else if(obj is XmlSchemaAttribute)
\r
236 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
\r
237 if(attr.Compile(handler,info))
\r
239 Attributes.Add(attr.QualifiedName, attr);
\r
242 else if(obj is XmlSchemaAttributeGroup)
\r
244 XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
\r
245 if(attrgrp.Compile(handler,info))
\r
247 AttributeGroups.Add(XmlQualifiedName.Empty, attrgrp);
\r
250 else if(obj is XmlSchemaComplexType)
\r
252 XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
\r
253 if(ctype.Compile(handler,info))
\r
255 schemaTypes.Add(ctype.QualifiedName, ctype);
\r
258 else if(obj is XmlSchemaSimpleType)
\r
260 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
\r
261 if(stype.Compile(handler,info))
\r
263 SchemaTypes.Add(stype.QualifiedName, stype);
\r
266 else if(obj is XmlSchemaElement)
\r
268 XmlSchemaElement elem = (XmlSchemaElement) obj;
\r
269 if(elem.Compile(handler,info))
\r
271 Elements.Add(elem.QualifiedName,elem);
\r
274 else if(obj is XmlSchemaGroup)
\r
276 XmlSchemaGroup grp = (XmlSchemaGroup) obj;
\r
277 if(grp.Compile(handler,info))
\r
279 Groups.Add(XmlQualifiedName.Empty,grp);
\r
282 else if(obj is XmlSchemaNotation)
\r
284 XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
\r
285 if(ntn.Compile(handler,info))
\r
287 Notations.Add(XmlQualifiedName.Empty, ntn);
\r
292 ValidationHandler.RaiseValidationError(handler,this,
\r
293 "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema");
\r
300 protected void Validate(ValidationEventHandler handler)
\r
303 foreach(XmlSchemaObject obj in Includes)
\r
306 foreach(XmlSchemaObject obj in Items)
\r
308 // foreach(XmlSchemaAnnotation ann in ??????)
\r
310 // ann.Validate(handler);
\r
312 foreach(XmlSchemaAttribute attr in Attributes)
\r
314 attr.parentIsSchema = true;
\r
315 attr.Validate(handler);
\r
317 foreach(XmlSchemaAttributeGroup attrgrp in AttributeGroups)
\r
319 attrgrp.Validate(handler);
\r
321 foreach(XmlSchemaType type in SchemaTypes)
\r
323 if(type is XmlSchemaComplexType)
\r
325 ((XmlSchemaComplexType)type).Validate(handler);
\r
328 ((XmlSchemaSimpleType)type).Validate(handler);
\r
330 foreach(XmlSchemaElement elem in Elements)
\r
332 elem.Validate(handler);
\r
334 foreach(XmlSchemaGroup grp in Groups)
\r
336 grp.Validate(handler);
\r
338 foreach(XmlSchemaNotation ntn in Notations)
\r
340 ntn.Validate(handler);
\r
345 public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)
\r
347 return Read(new XmlTextReader(reader),validationEventHandler);
\r
349 public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)
\r
351 return Read(new XmlTextReader(stream),validationEventHandler);
\r
355 //FIXME: Use the validationeventhandler. Probably needs the parser
\r
356 public static XmlSchema Read(XmlReader reader, ValidationEventHandler validationEventHandler)
\r
358 XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
359 return (XmlSchema) xser.Deserialize(reader);
\r
361 public void Write(System.IO.Stream stream)
\r
363 Write(stream,null);
\r
365 public void Write(System.IO.TextWriter writer)
\r
367 Write(writer,null);
\r
369 public void Write(System.Xml.XmlWriter writer)
\r
371 Write(writer,null);
\r
373 public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
\r
375 Write(new XmlTextWriter(stream,null),namespaceManager);
\r
377 public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
379 XmlTextWriter xwriter = new XmlTextWriter(writer);
\r
380 xwriter.Formatting = Formatting.Indented;
\r
381 Write(xwriter,namespaceManager);
\r
384 public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
\r
386 XmlSerializerNamespaces xns;
\r
388 if(Namespaces != null)
\r
390 xns = new XmlSerializerNamespaces(this.Namespaces);
\r
394 xns = new XmlSerializerNamespaces();
\r
397 if(namespaceManager != null)
\r
399 foreach(string name in namespaceManager)
\r
401 //xml and xmlns namespaced are added by default in namespaceManager.
\r
402 //So we should ignore them
\r
403 if(name!="xml" && name != "xmlns")
\r
404 xns.Add(name,namespaceManager.LookupNamespace(name));
\r
408 this.Namespaces = xns;
\r
410 XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
\r
411 xser.Serialize(writer,this,xns);
\r