XmlSchemaAttribute.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. private static string xmlname = "attribute";
  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. #region Properties
  39. [DefaultValue(null)]
  40. [System.Xml.Serialization.XmlAttribute("default")]
  41. public string DefaultValue
  42. {
  43. get{ return defaultValue;}
  44. set
  45. { // Default Value and fixed Value are mutually exclusive
  46. fixedValue = null;
  47. defaultValue = value;
  48. }
  49. }
  50. [DefaultValue(null)]
  51. [System.Xml.Serialization.XmlAttribute("fixed")]
  52. public string FixedValue
  53. {
  54. get{ return fixedValue;}
  55. set
  56. { // Default Value and fixed Value are mutually exclusive
  57. defaultValue = null;
  58. fixedValue = value;
  59. }
  60. }
  61. [DefaultValue(XmlSchemaForm.None)]
  62. [System.Xml.Serialization.XmlAttribute("form")]
  63. public XmlSchemaForm Form
  64. {
  65. get{ return form;}
  66. set{ form = value;}
  67. }
  68. [System.Xml.Serialization.XmlAttribute("name")]
  69. public string Name
  70. {
  71. get{ return name;}
  72. set
  73. {
  74. name = value;
  75. }
  76. }
  77. [System.Xml.Serialization.XmlAttribute("ref")]
  78. public XmlQualifiedName RefName
  79. {
  80. get{ return refName;}
  81. set
  82. {
  83. refName = value;
  84. }
  85. }
  86. [System.Xml.Serialization.XmlAttribute("type")]
  87. public XmlQualifiedName SchemaTypeName
  88. {
  89. get{ return schemaTypeName;}
  90. set{ schemaTypeName = value;}
  91. }
  92. [XmlElement("simpleType",Namespace="http://www.w3.org/2001/XMLSchema")]
  93. public XmlSchemaSimpleType SchemaType
  94. {
  95. get{ return schemaType;}
  96. set{ schemaType = value;}
  97. }
  98. [DefaultValue(XmlSchemaUse.None)]
  99. [System.Xml.Serialization.XmlAttribute("use")]
  100. public XmlSchemaUse Use
  101. {
  102. get{ return use;}
  103. set{ use = value;}
  104. }
  105. [XmlIgnore]
  106. public XmlQualifiedName QualifiedName
  107. {
  108. get{ return qualifiedName;}
  109. }
  110. [XmlIgnore]
  111. public object AttributeType
  112. { //FIXME: This is not correct. Is it?
  113. get{ return attributeType; }
  114. }
  115. #endregion
  116. /// <remarks>
  117. /// For an attribute:
  118. /// a) If the parent is schema
  119. /// 1-5 are from <xs:complexType name="topLevelAttribute"> in the Schema for Schema
  120. /// 6-8 are from "Constraints on XML Representations of Attribute Declarations"
  121. /// 9-10 are from "Attribute Declaration Schema Component"
  122. /// 11-16 are from "Constraints on Attribute Declaration Schema Components"
  123. /// 1. ref must be absent
  124. /// 2. form must be absent
  125. /// 3. use must be absent
  126. /// 4. name must be present and of type NCName
  127. /// 5. *NO CHECK REQUIRED* Only simple types and annotation are allowed as content
  128. /// 6. default and fixed must not both be present.
  129. /// 7. *NO CHECK REQUIRED* If default and use are both present... (Not possible since use is absent)
  130. /// 8. type and <simpleType> must not both be present.
  131. /// 9. Target Namespace should be schema's targetnamespace or absent
  132. /// 10. Type Definiton coressponds to <simpletype> element, or type value, or absent
  133. /// 11. *TO UNDERSTAND* Missing Sub-components
  134. /// 12. value constraint must be of the same datatype as of type
  135. /// 13. if the type definition is ID then there should be no value constraint.
  136. /// 14. name must not be xmlns
  137. /// 15. Targetnamespace must not be xsi. This implies the target namespace of schema can't be xsi if toplevel attributes are used.
  138. /// 16. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
  139. /// b) If the parent is complextype and ref is not set
  140. /// 1. name must be present and of type NCName.
  141. /// 2. type and <simpleType> must not both be present.
  142. /// 3. default and fixed must not both be present.
  143. /// 4. If default and use are both present, use must have the ·actual value· optional.
  144. /// 5. name must not be xmlns
  145. /// 6. Targetnamespace must not be xsi.
  146. /// 7. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
  147. /// 8. If form has actual value qualified or the schema's formdefault is qualified, targetnamespace
  148. /// is same as schema's target namespace, otherwise absent.
  149. /// c) if the parent is not schema and ref is set
  150. /// 1. name must not be present
  151. /// 2. all of <simpleType>, form and type must be absent.
  152. /// 3. default and fixed must not both be present.
  153. /// 4. If default and use are both present, use must have the ·actual value· optional.
  154. /// </remarks>
  155. [MonoTODO]
  156. internal int Compile(ValidationEventHandler h, XmlSchemaInfo info)
  157. {
  158. errorCount = 0;
  159. if(parentIsSchema)//a
  160. {
  161. if(RefName!= null && !RefName.IsEmpty) // a.1
  162. error(h,"ref must be absent in the top level <attribute>");
  163. if(Form != XmlSchemaForm.None) // a.2
  164. error(h,"form must be absent in the top level <attribute>");
  165. if(Use != XmlSchemaUse.None) // a.3
  166. error(h,"use must be absent in the top level <attribute>");
  167. // TODO: a.10, a.11, a.12, a.13
  168. CompileCommon(h,info, true);
  169. }
  170. else // local
  171. {
  172. //FIXME: How to Use of AttributeFormDefault????
  173. if(RefName == null || RefName.IsEmpty)
  174. {
  175. //TODO: b.8
  176. CompileCommon(h,info, true);
  177. }
  178. else
  179. {
  180. if(this.name != null)
  181. error(h,"name must be absent if ref is present");
  182. if(this.form != XmlSchemaForm.None)
  183. error(h,"form must be absent if ref is present");
  184. if(this.schemaType != null)
  185. error(h,"simpletype must be absent if ref is present");
  186. if(this.schemaTypeName != null && !this.schemaTypeName.IsEmpty)
  187. error(h,"type must be absent if ref is present");
  188. CompileCommon(h,info,false);
  189. }
  190. }
  191. return errorCount;
  192. }
  193. private void CompileCommon(ValidationEventHandler h, XmlSchemaInfo info, bool refIsNotPresent)
  194. {
  195. if(refIsNotPresent)
  196. {
  197. if(Name == null) //a.4, b.1,
  198. error(h,"Required attribute name must be present");
  199. else if(!XmlSchemaUtil.CheckNCName(Name)) // a.4.2, b1.2
  200. error(h,"attribute name must be NCName");
  201. else if(Name == "xmlns") // a.14 , b5
  202. error(h,"attribute name must not be xmlns");
  203. else
  204. qualifiedName = new XmlQualifiedName(Name, info.TargetNamespace);
  205. if(SchemaType != null)
  206. {
  207. if(SchemaTypeName != null && !SchemaTypeName.IsEmpty) // a.8
  208. error(h,"attribute can't have both a type and <simpleType> content");
  209. errorCount += SchemaType.Compile(h,info);
  210. }
  211. if(SchemaTypeName != null && !XmlSchemaUtil.CheckQName(SchemaTypeName))
  212. error(h,SchemaTypeName+" is not a valid QName");
  213. }
  214. else
  215. {
  216. if(RefName == null || RefName.IsEmpty)
  217. error(h,"Error: Should Never Happen. refname must be present");
  218. else
  219. qualifiedName = RefName;
  220. }
  221. if(info.TargetNamespace == XmlSchema.InstanceNamespace && Name != "nil" && Name != "type"
  222. && Name != "schemaLocation" && Name != "noNamespaceSchemaLocation") // a.15, a.16
  223. error(h,"targetNamespace can't be " + XmlSchema.InstanceNamespace);
  224. if(DefaultValue != null && FixedValue != null) // a.6, b.3, c.3
  225. error(h,"default and fixed must not both be present in an Attribute");
  226. if(DefaultValue != null && Use != XmlSchemaUse.None && Use != XmlSchemaUse.Optional)
  227. error(h,"if default is present, use must be optional");
  228. XmlSchemaUtil.CompileID(Id, this, info.IDCollection, h);
  229. }
  230. [MonoTODO]
  231. internal int Validate(ValidationEventHandler h)
  232. {
  233. return errorCount;
  234. }
  235. //<attribute
  236. // default = string
  237. // fixed = string
  238. // form = (qualified | unqualified)
  239. // id = ID
  240. // name = NCName
  241. // ref = QName
  242. // type = QName
  243. // use = (optional | prohibited | required) : optional
  244. // {any attributes with non-schema namespace . . .}>
  245. // Content: (annotation?, (simpleType?))
  246. //</attribute>
  247. internal static XmlSchemaAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
  248. {
  249. XmlSchemaAttribute attribute = new XmlSchemaAttribute();
  250. reader.MoveToElement();
  251. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  252. {
  253. error(h,"Should not happen :1: XmlSchemaAttribute.Read, name="+reader.Name,null);
  254. reader.SkipToEnd();
  255. return null;
  256. }
  257. attribute.LineNumber = reader.LineNumber;
  258. attribute.LinePosition = reader.LinePosition;
  259. attribute.SourceUri = reader.BaseURI;
  260. while(reader.MoveToNextAttribute())
  261. {
  262. if(reader.Name == "default")
  263. {
  264. attribute.defaultValue = reader.Value;
  265. }
  266. else if(reader.Name == "fixed")
  267. {
  268. attribute.fixedValue = reader.Value;
  269. }
  270. else if(reader.Name == "form")
  271. {
  272. Exception innerex;
  273. attribute.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
  274. if(innerex != null)
  275. error(h, reader.Value + " is not a valid value for form attribute", innerex);
  276. }
  277. else if(reader.Name == "id")
  278. {
  279. attribute.Id = reader.Value;
  280. }
  281. else if(reader.Name == "name")
  282. {
  283. attribute.name = reader.Value;
  284. }
  285. else if(reader.Name == "ref")
  286. {
  287. Exception innerex;
  288. attribute.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  289. if(innerex != null)
  290. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  291. }
  292. else if(reader.Name == "type")
  293. {
  294. Exception innerex;
  295. attribute.schemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  296. if(innerex != null)
  297. error(h, reader.Value + " is not a valid value for type attribute",innerex);
  298. }
  299. else if(reader.Name == "use")
  300. {
  301. Exception innerex;
  302. attribute.use = XmlSchemaUtil.ReadUseAttribute(reader,out innerex);
  303. if(innerex != null)
  304. error(h, reader.Value + " is not a valid value for use attribute", innerex);
  305. }
  306. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  307. {
  308. error(h,reader.Name + " is not a valid attribute for attribute",null);
  309. }
  310. else
  311. {
  312. XmlSchemaUtil.ReadUnhandledAttribute(reader,attribute);
  313. }
  314. }
  315. reader.MoveToElement();
  316. if(reader.IsEmptyElement)
  317. return attribute;
  318. // Content: (annotation?, (simpleType?))
  319. int level = 1;
  320. while(reader.ReadNextElement())
  321. {
  322. if(reader.NodeType == XmlNodeType.EndElement)
  323. {
  324. if(reader.LocalName != xmlname)
  325. error(h,"Should not happen :2: XmlSchemaAttribute.Read, name="+reader.Name,null);
  326. break;
  327. }
  328. if(level <= 1 && reader.LocalName == "annotation")
  329. {
  330. level = 2; //Only one annotation
  331. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  332. if(annotation != null)
  333. attribute.Annotation = annotation;
  334. continue;
  335. }
  336. if(level <=2 && reader.LocalName == "simpleType")
  337. {
  338. level = 3;
  339. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  340. if(stype != null)
  341. attribute.schemaType = stype;
  342. continue;
  343. }
  344. reader.RaiseInvalidElementError();
  345. }
  346. return attribute;
  347. }
  348. }
  349. }