XmlSchemaAttribute.cs 6.7 KB

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