XmlSchemaElement.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. using System.ComponentModel;
  7. namespace System.Xml.Schema
  8. {
  9. /// <summary>
  10. /// Summary description for XmlSchemaElement.
  11. /// </summary>
  12. public class XmlSchemaElement : XmlSchemaParticle
  13. {
  14. private XmlSchemaDerivationMethod block;
  15. private XmlSchemaDerivationMethod blockResolved;
  16. private XmlSchemaObjectCollection constraints;
  17. private string defaultValue;
  18. private object elementType;
  19. private XmlSchemaDerivationMethod final;
  20. private XmlSchemaDerivationMethod finalResolved;
  21. private string fixedValue;
  22. private XmlSchemaForm form;
  23. private bool isAbstract;
  24. private bool isNillable;
  25. private string name;
  26. private XmlQualifiedName qName;
  27. private XmlQualifiedName refName;
  28. private XmlSchemaType schemaType;
  29. private XmlQualifiedName schemaTypeName;
  30. private XmlQualifiedName substitutionGroup;
  31. internal bool parentIsSchema = false;
  32. private int errorCount;
  33. private string targetNamespace;
  34. public XmlSchemaElement()
  35. {
  36. block = XmlSchemaDerivationMethod.None;
  37. final = XmlSchemaDerivationMethod.None;
  38. constraints = new XmlSchemaObjectCollection();
  39. qName = XmlQualifiedName.Empty;
  40. refName = XmlQualifiedName.Empty;
  41. schemaTypeName = XmlQualifiedName.Empty;
  42. substitutionGroup = XmlQualifiedName.Empty;
  43. substitutionGroup = XmlQualifiedName.Empty;
  44. }
  45. #region Attributes
  46. [DefaultValue(false)]
  47. [System.Xml.Serialization.XmlAttribute("abstract")]
  48. public bool IsAbstract
  49. {
  50. get{ return isAbstract; }
  51. set{ isAbstract = value; }
  52. }
  53. [DefaultValue(XmlSchemaDerivationMethod.None)]
  54. [System.Xml.Serialization.XmlAttribute("block")]
  55. public XmlSchemaDerivationMethod Block
  56. {
  57. get{ return block; }
  58. set{ block = value; }
  59. }
  60. [DefaultValue(null)]
  61. [System.Xml.Serialization.XmlAttribute("default")]
  62. public string DefaultValue
  63. {
  64. get{ return defaultValue; }
  65. set{ defaultValue = value; }
  66. }
  67. [DefaultValue(XmlSchemaDerivationMethod.None)]
  68. [System.Xml.Serialization.XmlAttribute("final")]
  69. public XmlSchemaDerivationMethod Final
  70. {
  71. get{ return final; }
  72. set{ final = value; }
  73. }
  74. [DefaultValue(null)]
  75. [System.Xml.Serialization.XmlAttribute("fixed")]
  76. public string FixedValue
  77. {
  78. get{ return fixedValue; }
  79. set{ fixedValue = value; }
  80. }
  81. [DefaultValue(XmlSchemaForm.None)]
  82. [System.Xml.Serialization.XmlAttribute("form")]
  83. public XmlSchemaForm Form
  84. {
  85. get{ return form; }
  86. set{ form = value; }
  87. }
  88. [DefaultValue(false)]
  89. [System.Xml.Serialization.XmlAttribute("nillable")]
  90. public bool IsNillable
  91. {
  92. get{ return isNillable; }
  93. set{ isNillable = value; }
  94. }
  95. [DefaultValue(null)]
  96. [System.Xml.Serialization.XmlAttribute("name")]
  97. public string Name
  98. {
  99. get{ return name; }
  100. set{ name = value; }
  101. }
  102. [System.Xml.Serialization.XmlAttribute("ref")]
  103. public XmlQualifiedName RefName
  104. {
  105. get{ return refName; }
  106. set{ refName = value;}
  107. }
  108. [System.Xml.Serialization.XmlAttribute("type")]
  109. public XmlQualifiedName SchemaTypeName
  110. {
  111. get{ return schemaTypeName; }
  112. set{ schemaTypeName = value; }
  113. }
  114. [System.Xml.Serialization.XmlAttribute("substitutionGroup")]
  115. public XmlQualifiedName SubstitutionGroup
  116. {
  117. get{ return substitutionGroup; }
  118. set{ substitutionGroup = value; }
  119. }
  120. #endregion
  121. #region Elements
  122. [XmlElement("unique",typeof(XmlSchemaUnique),Namespace="http://www.w3.org/2001/XMLSchema")]
  123. [XmlElement("key",typeof(XmlSchemaKey),Namespace="http://www.w3.org/2001/XMLSchema")]
  124. [XmlElement("keyref",typeof(XmlSchemaKeyref),Namespace="http://www.w3.org/2001/XMLSchema")]
  125. public XmlSchemaObjectCollection Constraints
  126. {
  127. get{ return constraints; }
  128. }
  129. [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]
  130. [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
  131. public XmlSchemaType SchemaType
  132. {
  133. get{ return schemaType; }
  134. set{ schemaType = value; }
  135. }
  136. #endregion
  137. #region XmlIgnore
  138. [XmlIgnore]
  139. public XmlSchemaDerivationMethod BlockResolved
  140. {
  141. get{ return blockResolved; }
  142. }
  143. [XmlIgnore]
  144. public object ElementType
  145. {
  146. get{ return elementType; }
  147. }
  148. [XmlIgnore]
  149. public XmlSchemaDerivationMethod FinalResolved
  150. {
  151. get{ return finalResolved; }
  152. }
  153. [XmlIgnore]
  154. public XmlQualifiedName QualifiedName
  155. {
  156. get{ return qName; }
  157. }
  158. #endregion
  159. /// <remarks>
  160. /// a) If Element has parent as schema:
  161. /// 1. name must be present and of type NCName.
  162. /// 2. ref must be absent
  163. /// 3. form must be absent
  164. /// 4. minOccurs must be absent
  165. /// 5. maxOccurs must be absent
  166. /// b) If Element has parent is not schema and ref is absent
  167. /// 1. name must be present and of type NCName.
  168. /// 2. if form equals qualified or form is absent and schema's formdefault is qualifed,
  169. /// targetNamespace is schema's targetnamespace else empty.
  170. /// 3. type and either <simpleType> or <complexType> are mutually exclusive
  171. /// 4. default and fixed must not both be present.
  172. /// 5. substitutiongroup must be absent
  173. /// 6. final must be absent
  174. /// 7. abstract must be absent
  175. /// c) if the parent is not schema and ref is set
  176. /// 1. name must not be present
  177. /// 2. all of <simpleType>,<complexType>, <key>, <keyref>, <unique>, nillable,
  178. /// default, fixed, form, block and type, must be absent.
  179. /// 3. substitutiongroup is prohibited
  180. /// 4. final is prohibited
  181. /// 5. abstract is prohibited
  182. /// 6. default and fixed must not both be present.(Actually both are absent)
  183. /// </remarks>
  184. [MonoTODO]
  185. internal int Compile(ValidationEventHandler h, XmlSchemaInfo info)
  186. {
  187. if(this.defaultValue != null && this.fixedValue != null)
  188. error(h,"both default and fixed can't be present");
  189. if(parentIsSchema)
  190. {
  191. if(this.refName != null && !RefName.IsEmpty)
  192. error(h,"ref must be absent");
  193. if(this.name == null) //b1
  194. error(h,"Required attribute name must be present");
  195. else if(!XmlSchemaUtil.CheckNCName(this.name)) // b1.2
  196. error(h,"attribute name must be NCName");
  197. else
  198. this.qName = new XmlQualifiedName(this.name, info.targetNS);
  199. if(form != XmlSchemaForm.None)
  200. error(h,"form must be absent");
  201. if(MinOccursString != null)
  202. error(h,"minOccurs must be absent");
  203. if(MaxOccursString != null)
  204. error(h,"maxOccurs must be absent");
  205. if(final == XmlSchemaDerivationMethod.All)
  206. finalResolved = XmlSchemaDerivationMethod.All;
  207. else if(final == XmlSchemaDerivationMethod.None)
  208. finalResolved = info.blockDefault & (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction);
  209. else
  210. finalResolved = final & (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction);
  211. if(block == XmlSchemaDerivationMethod.All)
  212. blockResolved = XmlSchemaDerivationMethod.All;
  213. else if(block == XmlSchemaDerivationMethod.None)
  214. blockResolved = info.blockDefault & (XmlSchemaDerivationMethod.Extension |
  215. XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.Substitution);
  216. else
  217. blockResolved = block & (XmlSchemaDerivationMethod.Extension |
  218. XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.Substitution);
  219. if(schemaType != null && schemaTypeName != null && !schemaTypeName.IsEmpty)
  220. {
  221. error(h,"both schemaType and content can't be present");
  222. }
  223. else
  224. {
  225. if(schemaType != null)
  226. {
  227. if(schemaType is XmlSchemaSimpleType)
  228. errorCount += ((XmlSchemaSimpleType)schemaType).Compile(h,info);
  229. else if(schemaType is XmlSchemaComplexType)
  230. errorCount += ((XmlSchemaComplexType)schemaType).Compile(h,info);
  231. else
  232. error(h,"only simpletype or complextype is allowed");
  233. }
  234. else
  235. {
  236. if(schemaTypeName == null || schemaTypeName.IsEmpty)
  237. error(h,"one of schemaType or schemaTypeName must be present");
  238. }
  239. }
  240. foreach(XmlSchemaObject obj in constraints)
  241. {
  242. if(obj is XmlSchemaUnique)
  243. ((XmlSchemaUnique)obj).Compile(h,info);
  244. else if(obj is XmlSchemaKey)
  245. ((XmlSchemaKey)obj).Compile(h,info);
  246. else if(obj is XmlSchemaKeyref)
  247. ((XmlSchemaKeyref)obj).Compile(h,info);
  248. }
  249. }
  250. else
  251. {
  252. if(substitutionGroup != null && !substitutionGroup.IsEmpty)
  253. error(h,"substitutionGroup must be absent");
  254. if(final != XmlSchemaDerivationMethod.None)
  255. error(h,"final must be absent");
  256. if(isAbstract)
  257. error(h,"abstract must be absent");
  258. if(refName == null || RefName.IsEmpty)
  259. {
  260. if(form == XmlSchemaForm.Qualified || (form == XmlSchemaForm.None && info.formDefault == XmlSchemaForm.Qualified))
  261. this.targetNamespace = info.targetNS;
  262. else
  263. this.targetNamespace = string.Empty;
  264. if(this.name == null) //b1
  265. error(h,"Required attribute name must be present");
  266. else if(!XmlSchemaUtil.CheckNCName(this.name)) // b1.2
  267. error(h,"attribute name must be NCName");
  268. else
  269. this.qName = new XmlQualifiedName(this.name, this.targetNamespace);
  270. if(block == XmlSchemaDerivationMethod.All)
  271. blockResolved = XmlSchemaDerivationMethod.All;
  272. else if(block == XmlSchemaDerivationMethod.None)
  273. blockResolved = info.blockDefault & (XmlSchemaDerivationMethod.Extension |
  274. XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.Substitution);
  275. else
  276. blockResolved = block & (XmlSchemaDerivationMethod.Extension |
  277. XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.Substitution);
  278. if(schemaType != null && schemaTypeName != null && !schemaTypeName.IsEmpty)
  279. {
  280. error(h,"both schemaType and content can't be present");
  281. }
  282. else
  283. {
  284. if(schemaType != null)
  285. {
  286. if(schemaType is XmlSchemaSimpleType)
  287. errorCount += ((XmlSchemaSimpleType)schemaType).Compile(h,info);
  288. else if(schemaType is XmlSchemaComplexType)
  289. errorCount += ((XmlSchemaComplexType)schemaType).Compile(h,info);
  290. else
  291. error(h,"only simpletype or complextype is allowed");
  292. }
  293. else
  294. {
  295. if(schemaTypeName == null || schemaTypeName.IsEmpty)
  296. error(h,"one of schemaType or schemaTypeName must be present");
  297. }
  298. }
  299. foreach(XmlSchemaObject obj in constraints)
  300. {
  301. if(obj is XmlSchemaUnique)
  302. ((XmlSchemaUnique)obj).Compile(h,info);
  303. else if(obj is XmlSchemaKey)
  304. ((XmlSchemaKey)obj).Compile(h,info);
  305. else if(obj is XmlSchemaKeyref)
  306. ((XmlSchemaKeyref)obj).Compile(h,info);
  307. }
  308. }
  309. else
  310. {
  311. if(name != null)
  312. error(h,"name must not be present when ref is present");
  313. if(Constraints.Count != 0)
  314. error(h,"key, keyref and unique must be absent");
  315. if(isNillable)
  316. error(h,"nillable must be absent");
  317. if(defaultValue != null)
  318. error(h,"default must be absent");
  319. if(fixedValue != null)
  320. error(h,"fixed must be null");
  321. if(form != XmlSchemaForm.None)
  322. error(h,"form must be absent");
  323. if(block != XmlSchemaDerivationMethod.None)
  324. error(h,"block must be absent");
  325. if(schemaTypeName != null && !schemaTypeName.IsEmpty)
  326. error(h,"type must be absent");
  327. if(SchemaType != null)
  328. error(h,"simpleType or complexType must be absent");
  329. }
  330. }
  331. if(this.Id != null && !XmlSchemaUtil.CheckID(Id))
  332. error(h, "id must be a valid ID");
  333. return errorCount;
  334. }
  335. [MonoTODO]
  336. internal int Validate(ValidationEventHandler h)
  337. {
  338. return errorCount;
  339. }
  340. internal void error(ValidationEventHandler handle,string message)
  341. {
  342. errorCount++;
  343. ValidationHandler.RaiseValidationError(handle,this,message);
  344. }
  345. }
  346. }