XmlSchemaAttribute.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Xml;
  5. using System.ComponentModel;
  6. using System.Xml.Serialization;
  7. namespace System.Xml.Schema
  8. {
  9. /// <summary>
  10. /// Summary description for XmlSchemaAttribute.
  11. /// </summary>
  12. public class XmlSchemaAttribute : XmlSchemaAnnotated
  13. {
  14. private object attributeType;
  15. private string defaultValue;
  16. private string fixedValue;
  17. private XmlSchemaForm form;
  18. private string name;
  19. private XmlQualifiedName qualifiedName;
  20. private XmlQualifiedName refName;
  21. private XmlSchemaSimpleType schemaType;
  22. private XmlQualifiedName schemaTypeName;
  23. private XmlSchemaUse use;
  24. //Compilation fields
  25. internal bool parentIsSchema = false;
  26. internal int errorCount;
  27. public XmlSchemaAttribute()
  28. {
  29. //FIXME: Docs says the default is optional.
  30. //Whereas the MS implementation has default None.
  31. form = XmlSchemaForm.None;
  32. use = XmlSchemaUse.None;
  33. schemaTypeName = XmlQualifiedName.Empty;
  34. qualifiedName = XmlQualifiedName.Empty;
  35. refName = XmlQualifiedName.Empty;
  36. }
  37. // Properties
  38. [XmlIgnore]
  39. public object AttributeType
  40. { //FIXME: This is not correct. Is it?
  41. get{ return attributeType; }
  42. }
  43. [DefaultValue(null)]
  44. [System.Xml.Serialization.XmlAttribute("default")]
  45. public string DefaultValue
  46. {
  47. get{ return defaultValue;}
  48. set
  49. { // Default Value and fixed Value are mutually exclusive
  50. fixedValue = null;
  51. defaultValue = value;
  52. }
  53. }
  54. [DefaultValue(null)]
  55. [System.Xml.Serialization.XmlAttribute("fixed")]
  56. public string FixedValue
  57. {
  58. get{ return fixedValue;}
  59. set
  60. { // Default Value and fixed Value are mutually exclusive
  61. defaultValue = null;
  62. fixedValue = value;
  63. }
  64. }
  65. [DefaultValue(XmlSchemaForm.None)]
  66. [System.Xml.Serialization.XmlAttribute("form")]
  67. public XmlSchemaForm Form
  68. {
  69. get{ return form;}
  70. set{ form = value;}
  71. }
  72. [System.Xml.Serialization.XmlAttribute("name")]
  73. public string Name
  74. {
  75. get{ return name;}
  76. set
  77. {
  78. name = value;
  79. }
  80. }
  81. [XmlIgnore]
  82. public XmlQualifiedName QualifiedName
  83. {
  84. get{ return qualifiedName;}
  85. }
  86. [System.Xml.Serialization.XmlAttribute("ref")]
  87. public XmlQualifiedName RefName
  88. {
  89. get{ return refName;}
  90. set
  91. {
  92. refName = value;
  93. }
  94. }
  95. [XmlElement("simpleType",Namespace="http://www.w3.org/2001/XMLSchema")]
  96. public XmlSchemaSimpleType SchemaType
  97. {
  98. get{ return schemaType;}
  99. set{ schemaType = value;}
  100. }
  101. [System.Xml.Serialization.XmlAttribute("type")]
  102. public XmlQualifiedName SchemaTypeName
  103. {
  104. get{ return schemaTypeName;}
  105. set{ schemaTypeName = value;}
  106. }
  107. [DefaultValue(XmlSchemaUse.None)]
  108. [System.Xml.Serialization.XmlAttribute("use")]
  109. public XmlSchemaUse Use
  110. {
  111. get{ return use;}
  112. set{ use = value;}
  113. }
  114. /// <remarks>
  115. /// For an attribute:
  116. /// a) If the parent is schema
  117. /// 1-5 are from <xs:complexType name="topLevelAttribute"> in the Schema for Schema
  118. /// 6-8 are from "Constraints on XML Representations of Attribute Declarations"
  119. /// 9-10 are from "Attribute Declaration Schema Component"
  120. /// 11-16 are from "Constraints on Attribute Declaration Schema Components"
  121. /// 1. ref must be absent
  122. /// 2. form must be absent
  123. /// 3. use must be absent
  124. /// 4. name must be present and of type NCName
  125. /// 5. *NO CHECK REQUIRED* Only simple types and annotation are allowed as content
  126. /// 6. default and fixed must not both be present.
  127. /// 7. *NO CHECK REQUIRED* If default and use are both present... (Not possible since use is absent)
  128. /// 8. type and <simpleType> must not both be present.
  129. /// 9. Target Namespace should be schema's targetnamespace or absent
  130. /// 10. Type Definiton coressponds to <simpletype> element, or type value, or absent
  131. /// 11. *TO UNDERSTAND* Missing Sub-components
  132. /// 12. value constraint must be of the same datatype as of type
  133. /// 13. if the type definition is ID then there should be no value constraint.
  134. /// 14. name must not be xmlns
  135. /// 15. Targetnamespace must not be xsi. This implies the target namespace of schema can't be xsi if toplevel attributes are used.
  136. /// 16. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
  137. /// b) If the parent is complextype and ref is not set
  138. /// 1. name must be present and of type NCName.
  139. /// 2. type and <simpleType> must not both be present.
  140. /// 3. default and fixed must not both be present.
  141. /// 4. If default and use are both present, use must have the ·actual value· optional.
  142. /// 5. name must not be xmlns
  143. /// 6. Targetnamespace must not be xsi.
  144. /// 7. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
  145. /// 8. If form has actual value qualified or the schema's formdefault is qualified, targetnamespace
  146. /// is same as schema's target namespace, otherwise absent.
  147. /// c) if the parent is not schema and ref is set
  148. /// 1. name must not be present
  149. /// 2. all of <simpleType>, form and type must be absent.
  150. /// 3. default and fixed must not both be present.
  151. /// 4. If default and use are both present, use must have the ·actual value· optional.
  152. /// </remarks>
  153. [MonoTODO]
  154. //FIXME: Should we set a property to null if an error occurs? Or should we stop the validation?
  155. internal int Compile(ValidationEventHandler h, XmlSchemaInfo info)
  156. {
  157. errorCount = 0;
  158. if(parentIsSchema)//a
  159. {
  160. if(this.refName!= null && !this.refName.IsEmpty) // a.1
  161. error(h,"ref must be absent in the top level <attribute>");
  162. if(this.form != XmlSchemaForm.None) // a.2
  163. error(h,"form must be absent in the top level <attribute>");
  164. if(this.use != XmlSchemaUse.None) // a.3
  165. error(h,"use must be absent in the top level <attribute>");
  166. // TODO: a.10, a.11, a.12, a.13
  167. CompileCommon(h,info, true);
  168. }
  169. else // local
  170. {
  171. if(this.refName == null || this.refName.IsEmpty)
  172. {
  173. //TODO: b.8
  174. CompileCommon(h,info, true);
  175. }
  176. else
  177. {
  178. if(this.name != null)
  179. error(h,"name must be absent if ref is present");
  180. if(this.form != XmlSchemaForm.None)
  181. error(h,"form must be absent if ref is present");
  182. if(this.schemaType != null)
  183. error(h,"simpletype must be absent if ref is present");
  184. if(this.schemaTypeName != null && !this.schemaTypeName.IsEmpty)
  185. error(h,"type must be absent if ref is present");
  186. CompileCommon(h,info,false);
  187. }
  188. }
  189. return errorCount;
  190. }
  191. private void CompileCommon(ValidationEventHandler h, XmlSchemaInfo info, bool refIsNotPresent)
  192. {
  193. if(refIsNotPresent)
  194. {
  195. if(this.name == null) //a.4, b.1,
  196. error(h,"Required attribute name must be present");
  197. else if(!XmlSchemaUtil.CheckNCName(this.name)) // a.4.2, b1.2
  198. error(h,"attribute name must be NCName");
  199. else if(this.name == "xmlns") // a.14 , b5
  200. error(h,"attribute name can't be xmlns");
  201. else
  202. this.qualifiedName = new XmlQualifiedName(this.name, info.targetNS);
  203. if(this.schemaType != null)
  204. {
  205. if(this.schemaTypeName != null && !this.SchemaTypeName.IsEmpty) // a.8
  206. error(h,"attribute can't have both a type and <simpleType> content");
  207. else
  208. {
  209. errorCount += this.schemaType.Compile(h,info);
  210. }
  211. }
  212. }
  213. else
  214. {
  215. // redundant since we call function after check
  216. //if(this.refName == null || this.refName.IsEmpty)
  217. // error(h,"refname must be present");
  218. this.qualifiedName = this.refName;
  219. }
  220. if(info.targetNS == XmlSchema.InstanceNamespace && this.name != "nil" && this.name != "type"
  221. && this.name != "schemaLocation" && this.name != "noNamespaceSchemaLocation") // a.15, a.16
  222. error(h,"targetNamespace can't be " + XmlSchema.InstanceNamespace);
  223. if(this.defaultValue != null && this.fixedValue != null) // a.6, b.3, c.3
  224. error(h,"default and fixed must not both be present in an Attribute");
  225. if(this.defaultValue != null && this.use != XmlSchemaUse.None && this.use != XmlSchemaUse.Optional)
  226. error(h,"if default is present, use must be optional");
  227. if(this.Id != null && !XmlSchemaUtil.CheckID(Id))
  228. error(h,"id attribute must be a valid ID");
  229. }
  230. [MonoTODO]
  231. internal int Validate(ValidationEventHandler h)
  232. {
  233. return errorCount;
  234. }
  235. internal void error(ValidationEventHandler handle,string message)
  236. {
  237. this.errorCount++;
  238. ValidationHandler.RaiseValidationError(handle,this,message);
  239. }
  240. }
  241. }