XmlSchema.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Xml;
  5. using System.IO;
  6. using System.Xml.Serialization;
  7. using System.ComponentModel;
  8. namespace System.Xml.Schema
  9. {
  10. /// <summary>
  11. /// Summary description for XmlSchema.
  12. /// </summary>
  13. [XmlRoot("schema",Namespace="http://www.w3.org/2001/XMLSchema")]
  14. public class XmlSchema : XmlSchemaObject
  15. {
  16. //public constants
  17. [XmlIgnore]
  18. public const string InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
  19. [XmlIgnore]
  20. public const string Namespace = "http://www.w3.org/2001/XMLSchema";
  21. //private fields
  22. private XmlSchemaForm attributeFormDefault ;
  23. private XmlSchemaObjectTable attributeGroups ;
  24. private XmlSchemaObjectTable attributes ;
  25. private XmlSchemaDerivationMethod blockDefault ;
  26. private XmlSchemaForm elementFormDefault ;
  27. private XmlSchemaObjectTable elements ;
  28. private XmlSchemaDerivationMethod finalDefault ;
  29. private XmlSchemaObjectTable groups ;
  30. private string id ;
  31. private XmlSchemaObjectCollection includes ;
  32. private bool isCompiled ;
  33. private XmlSchemaObjectCollection items ;
  34. private XmlSchemaObjectTable notations ;
  35. private XmlSchemaObjectTable schemaTypes ;
  36. private string targetNamespace ;
  37. private XmlAttribute[] unhandledAttributes ;
  38. private string version;
  39. private string language;
  40. public XmlSchema()
  41. {
  42. attributeFormDefault= XmlSchemaForm.None;
  43. blockDefault = XmlSchemaDerivationMethod.None;
  44. elementFormDefault = XmlSchemaForm.None;
  45. finalDefault = XmlSchemaDerivationMethod.None;
  46. includes = new XmlSchemaObjectCollection();
  47. isCompiled = false;
  48. items = new XmlSchemaObjectCollection();
  49. }
  50. #region Properties
  51. [DefaultValue(XmlSchemaForm.None)]
  52. [System.Xml.Serialization.XmlAttribute("attributeFormDefault")]
  53. public XmlSchemaForm AttributeFormDefault
  54. {
  55. get{ return attributeFormDefault; }
  56. set{ this.attributeFormDefault = value;}
  57. }
  58. [XmlIgnore]
  59. public XmlSchemaObjectTable AttributeGroups
  60. {
  61. get{ return attributeGroups; }
  62. }
  63. [XmlIgnore]
  64. public XmlSchemaObjectTable Attributes
  65. {
  66. get{ return attributes;}
  67. }
  68. [DefaultValue(XmlSchemaDerivationMethod.None)]
  69. [System.Xml.Serialization.XmlAttribute("blockDefault")]
  70. public XmlSchemaDerivationMethod BlockDefault
  71. {
  72. get{ return blockDefault;}
  73. set{ blockDefault = value;}
  74. }
  75. [DefaultValue(XmlSchemaForm.None)]
  76. [System.Xml.Serialization.XmlAttribute("elementFormDefault")]
  77. public XmlSchemaForm ElementFormDefault
  78. {
  79. get{ return elementFormDefault;}
  80. set{ elementFormDefault = value;}
  81. }
  82. [XmlIgnore]
  83. public XmlSchemaObjectTable Elements
  84. {
  85. get{ return elements;}
  86. }
  87. [DefaultValue(XmlSchemaDerivationMethod.None)]
  88. [System.Xml.Serialization.XmlAttribute("finalDefault")]
  89. public XmlSchemaDerivationMethod FinalDefault
  90. {
  91. get{ return finalDefault;}
  92. set{ finalDefault = value;}
  93. }
  94. [XmlIgnore]
  95. public XmlSchemaObjectTable Groups
  96. {
  97. get{ return groups;}
  98. }
  99. [System.Xml.Serialization.XmlAttribute("id")]
  100. public string Id
  101. {
  102. get{ return id;}
  103. set{ id = value;}
  104. }
  105. [XmlElement("include",typeof(XmlSchemaInclude),Namespace="http://www.w3.org/2001/XMLSchema")]
  106. [XmlElement("import",typeof(XmlSchemaImport),Namespace="http://www.w3.org/2001/XMLSchema")]
  107. [XmlElement("redefine",typeof(XmlSchemaRedefine),Namespace="http://www.w3.org/2001/XMLSchema")]
  108. public XmlSchemaObjectCollection Includes
  109. {
  110. get{ return includes;}
  111. }
  112. [XmlIgnore]
  113. public bool IsCompiled
  114. {
  115. get{ return isCompiled;}
  116. }
  117. [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]
  118. [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
  119. [XmlElement("group",typeof(XmlSchemaGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
  120. //Only Schema's attributeGroup has type XmlSchemaAttributeGroup.
  121. //Others (complextype, restrictions etc) must have XmlSchemaAttributeGroupRef
  122. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
  123. [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
  124. [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]
  125. [XmlElement("notation",typeof(XmlSchemaNotation),Namespace="http://www.w3.org/2001/XMLSchema")]
  126. [XmlElement("annotation",typeof(XmlSchemaAnnotation),Namespace="http://www.w3.org/2001/XMLSchema")]
  127. public XmlSchemaObjectCollection Items
  128. {
  129. get{ return items;}
  130. }
  131. [XmlIgnore]
  132. public XmlSchemaObjectTable Notations
  133. {
  134. get{ return notations;}
  135. }
  136. [XmlIgnore]
  137. public XmlSchemaObjectTable SchemaTypes
  138. {
  139. get{ return schemaTypes;}
  140. }
  141. [System.Xml.Serialization.XmlAttribute("targetNamespace")]
  142. public string TargetNamespace
  143. {
  144. get{ return targetNamespace;}
  145. set{ targetNamespace = value;}
  146. }
  147. [XmlAnyAttribute]
  148. public XmlAttribute[] UnhandledAttributes
  149. {
  150. get{ return unhandledAttributes;}
  151. set{ unhandledAttributes = value;}
  152. }
  153. [System.Xml.Serialization.XmlAttribute("version")]
  154. public string Version
  155. {
  156. get{ return version;}
  157. set{ version = value;}
  158. }
  159. // New attribute defined in W3C schema element
  160. [System.Xml.Serialization.XmlAttribute("xml:lang")]
  161. public string Language
  162. {
  163. get{ return language; }
  164. set{ language = value; }
  165. }
  166. #endregion
  167. // Methods
  168. [MonoTODO]
  169. public void Compile(ValidationEventHandler validationEventHandler)
  170. {
  171. attributeGroups = null;
  172. }
  173. public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)
  174. {
  175. return Read(new XmlTextReader(reader),validationEventHandler);
  176. }
  177. public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)
  178. {
  179. return Read(new XmlTextReader(stream),validationEventHandler);
  180. }
  181. //<ToBeRemoved>
  182. private static void Serializer_UnknownAttribute(object sender, XmlAttributeEventArgs e)
  183. {
  184. Console.WriteLine("Unknown Attribute");
  185. Console.WriteLine("\t" + e.Attr.Name + " " + e.Attr.InnerXml);
  186. Console.WriteLine("\t LineNumber: " + e.LineNumber);
  187. Console.WriteLine("\t LinePosition: " + e.LinePosition);
  188. }
  189. private static void Serializer_UnknownElement(object sender, XmlElementEventArgs e)
  190. {
  191. Console.WriteLine("Unknown Element");
  192. Console.WriteLine("\t" + e.Element.Name + " " + e.Element.InnerXml);
  193. Console.WriteLine("\t LineNumber: " + e.LineNumber);
  194. Console.WriteLine("\t LinePosition: " + e.LinePosition);
  195. }
  196. private static void Serializer_UnknownNode(object sender, XmlNodeEventArgs e)
  197. {
  198. Console.WriteLine("Unknown Node");
  199. Console.WriteLine("\t" + e.Name + " " + e.Text);
  200. Console.WriteLine("\t LineNumber: " + e.LineNumber);
  201. Console.WriteLine("\t LinePosition: " + e.LinePosition);
  202. }
  203. private static void Serializer_UnknownAttribute(object sender, UnreferencedObjectEventArgs e)
  204. {
  205. Console.WriteLine("Unknown");
  206. Console.WriteLine("\t" + e.UnreferencedId);
  207. Console.WriteLine("\t" + e.UnreferencedObject);
  208. }
  209. //</ToBeRemoved>
  210. [MonoTODO]
  211. public static XmlSchema Read(XmlReader reader, ValidationEventHandler validationEventHandler)
  212. {
  213. XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
  214. //<ToBeRemoved>
  215. xser.UnknownAttribute += new XmlAttributeEventHandler(Serializer_UnknownAttribute);
  216. xser.UnknownElement += new XmlElementEventHandler(Serializer_UnknownElement);
  217. xser.UnknownNode += new XmlNodeEventHandler(Serializer_UnknownNode);
  218. xser.UnreferencedObject += new UnreferencedObjectEventHandler(Serializer_UnknownAttribute);
  219. //</ToBeRemoved>
  220. return (XmlSchema) xser.Deserialize(reader);
  221. }
  222. public void Write(System.IO.Stream stream)
  223. {
  224. Write(stream,null);
  225. }
  226. public void Write(System.IO.TextWriter writer)
  227. {
  228. Write(writer,null);
  229. }
  230. public void Write(System.Xml.XmlWriter writer)
  231. {
  232. Write(writer,null);
  233. }
  234. public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
  235. {
  236. Write(new XmlTextWriter(stream,null),namespaceManager);
  237. }
  238. public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
  239. {
  240. XmlTextWriter xwriter = new XmlTextWriter(writer);
  241. // This is why the Write was not writing schema with line breaks
  242. xwriter.Formatting = Formatting.Indented;
  243. Write(xwriter,namespaceManager);
  244. }
  245. [MonoTODO]
  246. public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
  247. {
  248. XmlSerializerNamespaces xns;
  249. if(Namespaces != null)
  250. {
  251. xns = new XmlSerializerNamespaces(this.Namespaces);
  252. }
  253. else
  254. {
  255. xns = new XmlSerializerNamespaces();
  256. }
  257. if(namespaceManager != null)
  258. {
  259. foreach(string name in namespaceManager)
  260. {
  261. //xml and xmlns namespaced are added by default in namespaceManager.
  262. //So we should ignore them
  263. if(name!="xml" && name != "xmlns")
  264. xns.Add(name,namespaceManager.LookupNamespace(name));
  265. }
  266. }
  267. this.Namespaces = xns;
  268. XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
  269. xser.Serialize(writer,this,xns);
  270. writer.Flush();
  271. }
  272. }
  273. }