2
0

XmlSchema.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. //
  2. // System.Xml.Schema.XmlSchema.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Xml;
  11. using System.IO;
  12. using System.Xml.Serialization;
  13. using System.ComponentModel;
  14. namespace System.Xml.Schema
  15. {
  16. /// <summary>
  17. /// Summary description for XmlSchema.
  18. /// </summary>
  19. [XmlRoot("schema",Namespace=XmlSchema.Namespace)]
  20. public class XmlSchema : XmlSchemaObject
  21. {
  22. //public constants
  23. public const string Namespace = "http://www.w3.org/2001/XMLSchema";
  24. public const string InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
  25. //private fields
  26. private XmlSchemaForm attributeFormDefault ;
  27. private XmlSchemaObjectTable attributeGroups ;
  28. private XmlSchemaObjectTable attributes ;
  29. private XmlSchemaDerivationMethod blockDefault ;
  30. private XmlSchemaForm elementFormDefault ;
  31. private XmlSchemaObjectTable elements ;
  32. private XmlSchemaDerivationMethod finalDefault ;
  33. private XmlSchemaObjectTable groups ;
  34. private string id ;
  35. private XmlSchemaObjectCollection includes ;
  36. private XmlSchemaObjectCollection items ;
  37. private XmlSchemaObjectTable notations ;
  38. private XmlSchemaObjectTable schemaTypes ;
  39. private string targetNamespace ;
  40. private XmlAttribute[] unhandledAttributes ;
  41. private string version;
  42. private string language;
  43. // post schema compilation infoset
  44. private Hashtable idCollection;
  45. private Hashtable missingBaseSchemaTypeRefs;
  46. private ArrayList missingElementTypeRefs;
  47. private XmlSchemaCollection schemas;
  48. // Compiler specific things
  49. private static string xmlname = "schema";
  50. public XmlSchema()
  51. {
  52. attributeFormDefault= XmlSchemaForm.None;
  53. blockDefault = XmlSchemaDerivationMethod.None;
  54. elementFormDefault = XmlSchemaForm.None;
  55. finalDefault = XmlSchemaDerivationMethod.None;
  56. includes = new XmlSchemaObjectCollection();
  57. isCompiled = false;
  58. items = new XmlSchemaObjectCollection();
  59. attributeGroups = new XmlSchemaObjectTable();
  60. attributes = new XmlSchemaObjectTable();
  61. elements = new XmlSchemaObjectTable();
  62. groups = new XmlSchemaObjectTable();
  63. notations = new XmlSchemaObjectTable();
  64. schemaTypes = new XmlSchemaObjectTable();
  65. idCollection = new Hashtable ();
  66. missingBaseSchemaTypeRefs = new Hashtable ();
  67. missingElementTypeRefs = new ArrayList ();
  68. }
  69. #region Properties
  70. [DefaultValue(XmlSchemaForm.None)]
  71. [System.Xml.Serialization.XmlAttribute("attributeFormDefault")]
  72. public XmlSchemaForm AttributeFormDefault
  73. {
  74. get{ return attributeFormDefault; }
  75. set{ this.attributeFormDefault = value;}
  76. }
  77. [DefaultValue(XmlSchemaDerivationMethod.None)]
  78. [System.Xml.Serialization.XmlAttribute("blockDefault")]
  79. public XmlSchemaDerivationMethod BlockDefault
  80. {
  81. get{ return blockDefault;}
  82. set{ blockDefault = value;}
  83. }
  84. [DefaultValue(XmlSchemaDerivationMethod.None)]
  85. [System.Xml.Serialization.XmlAttribute("finalDefault")]
  86. public XmlSchemaDerivationMethod FinalDefault
  87. {
  88. get{ return finalDefault;}
  89. set{ finalDefault = value;}
  90. }
  91. [DefaultValue(XmlSchemaForm.None)]
  92. [System.Xml.Serialization.XmlAttribute("elementFormDefault")]
  93. public XmlSchemaForm ElementFormDefault
  94. {
  95. get{ return elementFormDefault;}
  96. set{ elementFormDefault = value;}
  97. }
  98. [System.Xml.Serialization.XmlAttribute("targetNamespace")]
  99. public string TargetNamespace
  100. {
  101. get{ return targetNamespace;}
  102. set{ targetNamespace = value;}
  103. }
  104. [System.Xml.Serialization.XmlAttribute("version")]
  105. public string Version
  106. {
  107. get{ return version;}
  108. set{ version = value;}
  109. }
  110. [XmlElement("include",typeof(XmlSchemaInclude),Namespace="http://www.w3.org/2001/XMLSchema")]
  111. [XmlElement("import",typeof(XmlSchemaImport),Namespace="http://www.w3.org/2001/XMLSchema")]
  112. [XmlElement("redefine",typeof(XmlSchemaRedefine),Namespace="http://www.w3.org/2001/XMLSchema")]
  113. public XmlSchemaObjectCollection Includes
  114. {
  115. get{ return includes;}
  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 bool IsCompiled
  133. {
  134. get{ return this.CompilationId != Guid.Empty;}
  135. }
  136. [XmlIgnore]
  137. public XmlSchemaObjectTable Attributes
  138. {
  139. get{ return attributes;}
  140. }
  141. [XmlIgnore]
  142. public XmlSchemaObjectTable AttributeGroups
  143. {
  144. get{ return attributeGroups; }
  145. }
  146. [XmlIgnore]
  147. public XmlSchemaObjectTable SchemaTypes
  148. {
  149. get{ return schemaTypes; }
  150. }
  151. [XmlIgnore]
  152. public XmlSchemaObjectTable Elements
  153. {
  154. get{ return elements;}
  155. }
  156. [System.Xml.Serialization.XmlAttribute("id")]
  157. public string Id
  158. {
  159. get{ return id;}
  160. set{ id = value;}
  161. }
  162. [XmlAnyAttribute]
  163. public XmlAttribute[] UnhandledAttributes
  164. {
  165. get
  166. {
  167. if(unhandledAttributeList != null)
  168. {
  169. unhandledAttributes = (XmlAttribute[]) unhandledAttributeList.ToArray(typeof(XmlAttribute));
  170. unhandledAttributeList = null;
  171. }
  172. return unhandledAttributes;
  173. }
  174. set
  175. {
  176. unhandledAttributes = value;
  177. unhandledAttributeList = null;
  178. }
  179. }
  180. [XmlIgnore]
  181. public XmlSchemaObjectTable Groups
  182. {
  183. get{ return groups;}
  184. }
  185. [XmlIgnore]
  186. public XmlSchemaObjectTable Notations
  187. {
  188. get{ return notations;}
  189. }
  190. // New attribute defined in W3C schema element
  191. [System.Xml.Serialization.XmlAttribute("xml:lang")]
  192. public string Language
  193. {
  194. get{ return language; }
  195. set{ language = value; }
  196. }
  197. internal Hashtable IDCollection
  198. {
  199. get { return idCollection; }
  200. }
  201. internal Hashtable MissingBaseSchemaTypeRefs
  202. {
  203. get { return missingBaseSchemaTypeRefs; }
  204. }
  205. internal ArrayList MissingElementTypeRefs
  206. {
  207. get { return missingElementTypeRefs; }
  208. }
  209. internal XmlSchemaCollection Schemas
  210. {
  211. get { return schemas; }
  212. }
  213. #endregion
  214. #region Compile
  215. // Methods
  216. /// <summary>
  217. /// This compile method does two things:
  218. /// 1. It compiles and fills the PSVI dataset
  219. /// 2. Validates the schema by calling Validate method.
  220. /// Every XmlSchemaObject has a Compile Method which gets called.
  221. /// </summary>
  222. /// <remarks>
  223. /// 1. blockDefault must be one of #all | List of (extension | restriction | substitution)
  224. /// 2. finalDefault must be one of (#all | List of (extension | restriction| union| list))
  225. /// 3. id must be of type ID
  226. /// 4. targetNamespace should be any uri
  227. /// 5. version should be a normalizedString
  228. /// 6. xml:lang should be a language
  229. /// </remarks>
  230. [MonoTODO]
  231. public void Compile(ValidationEventHandler handler)
  232. {
  233. Compile (handler, new Stack (), this);
  234. isCompiled = true;
  235. }
  236. private void Compile (ValidationEventHandler handler, Stack schemaLocationStack, XmlSchema rootSchema)
  237. {
  238. CompilationId = Guid.NewGuid ();
  239. schemas = new XmlSchemaCollection ();
  240. schemas.Add (this);
  241. attributeGroups = new XmlSchemaObjectTable ();
  242. attributes = new XmlSchemaObjectTable ();
  243. elements = new XmlSchemaObjectTable ();
  244. groups = new XmlSchemaObjectTable ();
  245. missingElementTypeRefs.Clear ();
  246. missingBaseSchemaTypeRefs.Clear ();
  247. notations = new XmlSchemaObjectTable ();
  248. schemaTypes = new XmlSchemaObjectTable ();
  249. //1. Union and List are not allowed in block default
  250. if(BlockDefault != XmlSchemaDerivationMethod.All)
  251. {
  252. if((BlockDefault & XmlSchemaDerivationMethod.List)!=0 )
  253. error(handler, "list is not allowed in blockDefault attribute");
  254. if((BlockDefault & XmlSchemaDerivationMethod.Union)!=0 )
  255. error(handler, "union is not allowed in blockDefault attribute");
  256. }
  257. //2. Substitution is not allowed in finaldefault.
  258. if(FinalDefault != XmlSchemaDerivationMethod.All)
  259. {
  260. if((FinalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )
  261. error(handler, "substitution is not allowed in finalDefault attribute");
  262. }
  263. //3. id must be of type ID
  264. XmlSchemaUtil.CompileID(Id, this, this.IDCollection, handler);
  265. //4. targetNamespace should be of type anyURI or absent
  266. if(TargetNamespace != null)
  267. {
  268. if(!XmlSchemaUtil.CheckAnyUri(TargetNamespace))
  269. error(handler, TargetNamespace+" is not a valid value for targetNamespace attribute of schema");
  270. }
  271. //5. version should be of type normalizedString
  272. if(!XmlSchemaUtil.CheckNormalizedString(Version))
  273. error(handler, Version + "is not a valid value for version attribute of schema");
  274. //6. xml:lang must be a language
  275. if(!XmlSchemaUtil.CheckLanguage(Language))
  276. error(handler, Language + " is not a valid language");
  277. // Compile the content of this schema
  278. XmlSchemaObjectCollection compilationItems = new XmlSchemaObjectCollection ();
  279. foreach (XmlSchemaObject obj in Items)
  280. compilationItems.Add (obj);
  281. foreach(XmlSchemaObject obj in Includes)
  282. {
  283. XmlSchemaExternal ext = obj as XmlSchemaExternal;
  284. if(ext != null)
  285. {
  286. if (ext.SchemaLocation == null) continue;
  287. string url = GetResolvedUri (ext.SchemaLocation);
  288. XmlSchemaInclude include = ext as XmlSchemaInclude;
  289. if (include != null) {
  290. if (schemaLocationStack.Contains (url)) {
  291. error(handler, "Nested inclusion was found: " + url);
  292. // must skip this inclusion
  293. continue;
  294. }
  295. schemaLocationStack.Push (url);
  296. XmlSchema includedSchema = XmlSchema.Read (new XmlTextReader (url), handler);
  297. includedSchema.schemas = schemas;
  298. includedSchema.Compile (handler, schemaLocationStack, rootSchema);
  299. schemaLocationStack.Pop ();
  300. foreach (XmlSchemaObject includedObj in includedSchema.Items)
  301. compilationItems.Add (includedObj);
  302. }
  303. XmlSchemaRedefine redefine = obj as XmlSchemaRedefine;
  304. if (redefine != null) {
  305. //FIXME: Kuch to karo! (Do Something ;)
  306. throw new NotImplementedException ();
  307. if (schemaLocationStack.Contains (url)) {
  308. error(handler, "Nested inclusion was found: " + url);
  309. // must skip this inclusion
  310. continue;
  311. }
  312. }
  313. XmlSchemaImport import = obj as XmlSchemaImport;
  314. if (import != null) {
  315. if (schemaLocationStack.Contains (url)) {
  316. error(handler, "Nested inclusion was found: " + url);
  317. // must skip this inclusion
  318. continue;
  319. }
  320. schemaLocationStack.Push (url);
  321. XmlSchema includedSchema = XmlSchema.Read (new XmlTextReader (url), handler);
  322. includedSchema.schemas = schemas;
  323. includedSchema.Compile (handler, schemaLocationStack, rootSchema);
  324. schemaLocationStack.Pop ();
  325. // TODO: check targetNamespace constraints
  326. rootSchema.schemas.Add (includedSchema);
  327. foreach (XmlSchemaElement element in includedSchema.Elements)
  328. elements.Add (element.QualifiedName, element);
  329. foreach (XmlSchemaAttribute attribute in includedSchema.Attributes)
  330. attributes.Add (attribute.QualifiedName, attribute);
  331. foreach (XmlSchemaAttributeGroup attrGroup in includedSchema.AttributeGroups)
  332. attributeGroups.Add (attrGroup.QualifiedName, attrGroup);
  333. foreach (XmlSchemaGroup group in includedSchema.Groups)
  334. groups.Add (group.QualifiedName, group);
  335. foreach (XmlSchemaGroup group in includedSchema.Groups)
  336. groups.Add (group.QualifiedName, group);
  337. foreach (XmlSchemaNotation notation in includedSchema.Notations)
  338. notations.Add (notation.QualifiedName, notation);
  339. foreach (XmlSchemaType schemaType in includedSchema.SchemaTypes)
  340. schemaTypes.Add (schemaType.QualifiedName, schemaType);
  341. }
  342. }
  343. else
  344. {
  345. error(handler,"Object of Type "+obj.GetType().Name+" is not valid in Includes Property of XmlSchema");
  346. }
  347. }
  348. // It is hack, but types are required before element/attributes.
  349. foreach (XmlSchemaObject obj in compilationItems)
  350. {
  351. if(obj is XmlSchemaComplexType)
  352. {
  353. XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
  354. ctype.istoplevel = true;
  355. int numerr = ctype.Compile(handler, this);
  356. errorCount += numerr;
  357. if(numerr == 0)
  358. {
  359. schemaTypes.Add(ctype.QualifiedName, ctype);
  360. }
  361. }
  362. else if(obj is XmlSchemaSimpleType)
  363. {
  364. XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
  365. stype.islocal = false; //This simple type is toplevel
  366. int numerr = stype.Compile(handler, this);
  367. errorCount += numerr;
  368. if(numerr == 0)
  369. {
  370. SchemaTypes.Add(stype.QualifiedName, stype);
  371. }
  372. }
  373. }
  374. // Add missing references.
  375. foreach (XmlSchemaComplexType cType in missingBaseSchemaTypeRefs.Keys)
  376. cType.BaseSchemaTypeInternal =
  377. SchemaTypes [missingBaseSchemaTypeRefs [cType] as XmlQualifiedName];
  378. foreach(XmlSchemaObject obj in compilationItems)
  379. {
  380. if(obj is XmlSchemaAnnotation)
  381. {
  382. int numerr = ((XmlSchemaAnnotation)obj).Compile(handler, this);
  383. errorCount += numerr;
  384. if( numerr == 0)
  385. {
  386. //FIXME: What PSVI set do we add this to?
  387. }
  388. }
  389. else if(obj is XmlSchemaAttribute)
  390. {
  391. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  392. attr.parentIsSchema = true;
  393. int numerr = attr.Compile(handler, this);
  394. errorCount += numerr;
  395. if(numerr == 0)
  396. {
  397. Attributes.Set(attr.QualifiedName, attr);
  398. }
  399. }
  400. else if(obj is XmlSchemaAttributeGroup)
  401. {
  402. XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
  403. int numerr = attrgrp.Compile(handler, this);
  404. errorCount += numerr;
  405. if(numerr == 0)
  406. {
  407. AttributeGroups.Set(attrgrp.QualifiedName, attrgrp);
  408. }
  409. }
  410. else if(obj is XmlSchemaComplexType)
  411. {
  412. // Do nothing here.
  413. }
  414. else if(obj is XmlSchemaSimpleType)
  415. {
  416. // Do nothing here.
  417. }
  418. else if(obj is XmlSchemaElement)
  419. {
  420. XmlSchemaElement elem = (XmlSchemaElement) obj;
  421. elem.parentIsSchema = true;
  422. int numerr = elem.Compile(handler, this);
  423. errorCount += numerr;
  424. if(numerr == 0)
  425. {
  426. Elements.Set(elem.QualifiedName,elem);
  427. }
  428. }
  429. else if(obj is XmlSchemaGroup)
  430. {
  431. XmlSchemaGroup grp = (XmlSchemaGroup) obj;
  432. int numerr = grp.Compile(handler, this);
  433. errorCount += numerr;
  434. if(numerr == 0)
  435. {
  436. Groups.Set(grp.QualifiedName,grp);
  437. }
  438. }
  439. else if(obj is XmlSchemaNotation)
  440. {
  441. XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
  442. int numerr = ntn.Compile(handler, this);
  443. errorCount += numerr;
  444. if(numerr == 0)
  445. {
  446. Notations.Set(ntn.QualifiedName, ntn);
  447. }
  448. }
  449. else
  450. {
  451. ValidationHandler.RaiseValidationError(handler,this,
  452. "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema");
  453. }
  454. }
  455. if (rootSchema == this)
  456. {
  457. // First, fill type information for type reference
  458. foreach (XmlSchemaElement element in missingElementTypeRefs)
  459. {
  460. if (element.SchemaTypeName != XmlQualifiedName.Empty)
  461. {
  462. XmlSchemaType type = FindSchemaType (element.SchemaTypeName);
  463. // If el is null, then it is missing sub components ... ?
  464. element.SetSchemaType (type);
  465. }
  466. }
  467. // Then, fill type information for the type references for the referencing elements
  468. foreach (XmlSchemaElement element in missingElementTypeRefs)
  469. {
  470. if (element.RefName != XmlQualifiedName.Empty)
  471. {
  472. XmlSchemaElement refElem = FindElement (element.RefName);
  473. // If el is null, then it is missing sub components ... ?
  474. if (refElem != null) element.SetSchemaType (refElem.ElementType);
  475. }
  476. }
  477. }
  478. Validate(handler);
  479. }
  480. private string GetResolvedUri (string relativeUri)
  481. {
  482. Uri baseUri = null;
  483. if (this.SourceUri != null && this.SourceUri != String.Empty)
  484. baseUri = new Uri (this.SourceUri);
  485. return new XmlUrlResolver ().ResolveUri (baseUri, relativeUri).ToString ();
  486. }
  487. #endregion
  488. [MonoTODO]
  489. private void Validate(ValidationEventHandler handler)
  490. {
  491. foreach(XmlSchemaAttribute attr in Attributes.Values)
  492. {
  493. attr.Validate(handler, this);
  494. }
  495. foreach(XmlSchemaAttributeGroup attrgrp in AttributeGroups.Values)
  496. {
  497. attrgrp.Validate(handler);
  498. }
  499. foreach(XmlSchemaType type in SchemaTypes.Values)
  500. {
  501. if(type is XmlSchemaComplexType)
  502. {
  503. ((XmlSchemaComplexType)type).Validate(handler);
  504. }
  505. else
  506. ((XmlSchemaSimpleType)type).Validate(handler, this);
  507. }
  508. foreach(XmlSchemaElement elem in Elements.Values)
  509. {
  510. elem.Validate(handler);
  511. }
  512. foreach(XmlSchemaGroup grp in Groups.Values)
  513. {
  514. grp.Validate(handler);
  515. }
  516. foreach(XmlSchemaNotation ntn in Notations.Values)
  517. {
  518. ntn.Validate(handler);
  519. }
  520. }
  521. internal XmlSchemaElement FindElement (XmlQualifiedName name)
  522. {
  523. if (name.Namespace != TargetNamespace)
  524. return null;
  525. XmlSchema target = schemas [name.Namespace];
  526. if (target == null)
  527. return null;
  528. foreach (XmlSchemaObject obj in target.Items) {
  529. XmlSchemaElement elem = obj as XmlSchemaElement;
  530. if (elem != null && elem.Name == name.Name)
  531. return elem;
  532. }
  533. return null;
  534. }
  535. internal XmlSchemaType FindSchemaType (XmlQualifiedName name)
  536. {
  537. if (name.Namespace != TargetNamespace)
  538. return null;
  539. XmlSchema target = schemas [name.Namespace];
  540. if (target == null)
  541. return null;
  542. foreach (XmlSchemaObject obj in target.Items) {
  543. XmlSchemaType type = obj as XmlSchemaType;
  544. if (type != null && type.Name == name.Name)
  545. return type;
  546. }
  547. return null;
  548. }
  549. #region Read
  550. public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)
  551. {
  552. return Read(new XmlTextReader(reader),validationEventHandler);
  553. }
  554. public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)
  555. {
  556. return Read(new XmlTextReader(stream),validationEventHandler);
  557. }
  558. [MonoTODO ("Use ValidationEventHandler")]
  559. public static XmlSchema Read(XmlReader rdr, ValidationEventHandler validationEventHandler)
  560. {
  561. string baseURI = rdr.BaseURI;
  562. XmlSerializer xser = new XmlSerializer (typeof (XmlSchema));
  563. XmlSchema schema = (XmlSchema) xser.Deserialize (rdr);
  564. schema.SourceUri = baseURI;
  565. schema.Compile (validationEventHandler);
  566. return schema;
  567. /*
  568. XmlSchemaReader reader = new XmlSchemaReader(rdr, validationEventHandler);
  569. while(reader.ReadNextElement())
  570. {
  571. switch(reader.NodeType)
  572. {
  573. case XmlNodeType.Element:
  574. if(reader.LocalName == "schema")
  575. {
  576. XmlSchema schema = new XmlSchema();
  577. schema.LineNumber = reader.LineNumber;
  578. schema.LinePosition = reader.LinePosition;
  579. schema.SourceUri = reader.BaseURI;
  580. ReadAttributes(schema, reader, validationEventHandler);
  581. //IsEmptyElement does not behave properly if reader is
  582. //positioned at an attribute.
  583. reader.MoveToElement();
  584. if(!reader.IsEmptyElement)
  585. {
  586. ReadContent(schema, reader, validationEventHandler);
  587. }
  588. return schema;
  589. }
  590. else
  591. {
  592. //Schema can't be generated. Throw an exception
  593. throw new XmlSchemaException("The root element must be schema", null);
  594. }
  595. default:
  596. error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);
  597. break;
  598. }
  599. }
  600. throw new XmlSchemaException("The top level schema must have namespace "+XmlSchema.Namespace, null);
  601. */
  602. }
  603. /*
  604. private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
  605. {
  606. Exception ex;
  607. reader.MoveToElement();
  608. while(reader.MoveToNextAttribute())
  609. {
  610. switch(reader.Name)
  611. {
  612. case "attributeFormDefault" :
  613. schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);
  614. if(ex != null)
  615. error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);
  616. break;
  617. case "blockDefault" :
  618. schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault");
  619. if(ex != null)
  620. warn(h, ex.Message, ex);
  621. break;
  622. case "elementFormDefault":
  623. schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);
  624. if(ex != null)
  625. error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);
  626. break;
  627. case "finalDefault":
  628. schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault");
  629. if(ex != null)
  630. warn(h, ex.Message , ex);
  631. break;
  632. case "id":
  633. schema.id = reader.Value;
  634. break;
  635. case "targetNamespace":
  636. schema.targetNamespace = reader.Value;
  637. break;
  638. case "version":
  639. schema.version = reader.Value;
  640. break;
  641. case "xml:lang":
  642. schema.language = reader.Value;
  643. break;
  644. default:
  645. if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  646. error(h, reader.Name + " attribute is not allowed in schema element",null);
  647. else
  648. {
  649. XmlSchemaUtil.ReadUnhandledAttribute(reader,schema);
  650. }
  651. break;
  652. }
  653. }
  654. }
  655. private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
  656. {
  657. reader.MoveToElement();
  658. if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
  659. error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);
  660. //(include | import | redefine | annotation)*,
  661. //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
  662. int level = 1;
  663. while(reader.ReadNextElement())
  664. {
  665. if(reader.NodeType == XmlNodeType.EndElement)
  666. {
  667. if(reader.LocalName != xmlname)
  668. error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);
  669. break;
  670. }
  671. if(level <= 1)
  672. {
  673. if(reader.LocalName == "include")
  674. {
  675. XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);
  676. if(include != null)
  677. schema.includes.Add(include);
  678. continue;
  679. }
  680. if(reader.LocalName == "import")
  681. {
  682. XmlSchemaImport import = XmlSchemaImport.Read(reader,h);
  683. if(import != null)
  684. schema.includes.Add(import);
  685. continue;
  686. }
  687. if(reader.LocalName == "redefine")
  688. {
  689. XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);
  690. if(redefine != null)
  691. schema.includes.Add(redefine);
  692. continue;
  693. }
  694. if(reader.LocalName == "annotation")
  695. {
  696. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  697. if(annotation != null)
  698. schema.items.Add(annotation);
  699. continue;
  700. }
  701. }
  702. if(level <=2)
  703. {
  704. level = 2;
  705. if(reader.LocalName == "simpleType")
  706. {
  707. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  708. if(stype != null)
  709. schema.items.Add(stype);
  710. continue;
  711. }
  712. if(reader.LocalName == "complexType")
  713. {
  714. XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);
  715. if(ctype != null)
  716. schema.items.Add(ctype);
  717. continue;
  718. }
  719. if(reader.LocalName == "group")
  720. {
  721. XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);
  722. if(group != null)
  723. schema.items.Add(group);
  724. continue;
  725. }
  726. if(reader.LocalName == "attributeGroup")
  727. {
  728. XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);
  729. if(attributeGroup != null)
  730. schema.items.Add(attributeGroup);
  731. continue;
  732. }
  733. if(reader.LocalName == "element")
  734. {
  735. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  736. if(element != null)
  737. schema.items.Add(element);
  738. continue;
  739. }
  740. if(reader.LocalName == "attribute")
  741. {
  742. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  743. if(attr != null)
  744. schema.items.Add(attr);
  745. continue;
  746. }
  747. if(reader.LocalName == "notation")
  748. {
  749. XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);
  750. if(notation != null)
  751. schema.items.Add(notation);
  752. continue;
  753. }
  754. if(reader.LocalName == "annotation")
  755. {
  756. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  757. if(annotation != null)
  758. schema.items.Add(annotation);
  759. continue;
  760. }
  761. }
  762. reader.RaiseInvalidElementError();
  763. }
  764. }
  765. */
  766. #endregion
  767. #region write
  768. public void Write(System.IO.Stream stream)
  769. {
  770. Write(stream,null);
  771. }
  772. public void Write(System.IO.TextWriter writer)
  773. {
  774. Write(writer,null);
  775. }
  776. public void Write(System.Xml.XmlWriter writer)
  777. {
  778. Write(writer,null);
  779. }
  780. public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
  781. {
  782. Write(new XmlTextWriter(stream,null),namespaceManager);
  783. }
  784. public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
  785. {
  786. XmlTextWriter xwriter = new XmlTextWriter(writer);
  787. xwriter.Formatting = Formatting.Indented;
  788. Write(xwriter,namespaceManager);
  789. }
  790. public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
  791. {
  792. if(Namespaces == null)
  793. {
  794. Namespaces = new XmlSerializerNamespaces();
  795. }
  796. //Add the xml schema namespace.
  797. if(Namespaces.Count == 0)
  798. {
  799. Namespaces.Add("xs", XmlSchema.Namespace);
  800. if (TargetNamespace != null && TargetNamespace != String.Empty)
  801. Namespaces.Add("tns", TargetNamespace);
  802. }
  803. if(namespaceManager != null)
  804. {
  805. foreach(string name in namespaceManager)
  806. {
  807. //xml and xmlns namespaced are added by default in namespaceManager.
  808. //So we should ignore them
  809. if(name!="xml" && name != "xmlns")
  810. Namespaces.Add(name,namespaceManager.LookupNamespace(name));
  811. }
  812. }
  813. XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
  814. xser.Serialize(writer,this,Namespaces);
  815. writer.Flush();
  816. }
  817. #endregion
  818. }
  819. }