XmlSchemaAttribute.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. /// <summary>
  231. /// Schema Component:
  232. /// QName, SimpleType, Scope, Default|Fixed, annotation
  233. /// </summary>
  234. [MonoTODO]
  235. internal int Validate(ValidationEventHandler h, XmlSchemaInfo info)
  236. {
  237. if(isCompiled)
  238. return errorCount;
  239. //If Parent is schema:
  240. if(parentIsSchema)
  241. {
  242. if(SchemaType != null)
  243. {
  244. errorCount += SchemaType.Validate(h, info);
  245. attributeType = SchemaType;
  246. }
  247. else if(SchemaTypeName != null && !SchemaTypeName.IsEmpty)
  248. {
  249. //First Try to get a Inbuilt DataType
  250. XmlSchemaDatatype dtype = XmlSchemaDatatype.GetType(SchemaTypeName);
  251. if(dtype != null)
  252. {
  253. attributeType = dtype;
  254. }
  255. else
  256. {
  257. XmlSchemaObject obj = info.SchemaTypes[SchemaTypeName];
  258. if(obj is XmlSchemaSimpleType)
  259. {
  260. XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
  261. errorCount += stype.Validate(h, info);
  262. attributeType = stype;
  263. }
  264. else if(attributeType == null)
  265. error(h,"The type '"+ SchemaTypeName +"' is not defined in the schema");
  266. else if(attributeType is XmlSchemaComplexType)
  267. error(h,"An attribute can't have complexType Content");
  268. else
  269. error(h, "Should Never Happen. Illegal content in SchemaTypes");
  270. }
  271. }
  272. else
  273. {
  274. error(h,"Should Never Happen. Attribute SimpleType Not set. Should have been caught in the Compile() phase");
  275. }
  276. }
  277. else
  278. {
  279. //TODO: Local Attribute Validation
  280. }
  281. isCompiled = true;
  282. return errorCount;
  283. }
  284. //<attribute
  285. // default = string
  286. // fixed = string
  287. // form = (qualified | unqualified)
  288. // id = ID
  289. // name = NCName
  290. // ref = QName
  291. // type = QName
  292. // use = (optional | prohibited | required) : optional
  293. // {any attributes with non-schema namespace . . .}>
  294. // Content: (annotation?, (simpleType?))
  295. //</attribute>
  296. internal static XmlSchemaAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
  297. {
  298. XmlSchemaAttribute attribute = new XmlSchemaAttribute();
  299. reader.MoveToElement();
  300. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  301. {
  302. error(h,"Should not happen :1: XmlSchemaAttribute.Read, name="+reader.Name,null);
  303. reader.SkipToEnd();
  304. return null;
  305. }
  306. attribute.LineNumber = reader.LineNumber;
  307. attribute.LinePosition = reader.LinePosition;
  308. attribute.SourceUri = reader.BaseURI;
  309. while(reader.MoveToNextAttribute())
  310. {
  311. if(reader.Name == "default")
  312. {
  313. attribute.defaultValue = reader.Value;
  314. }
  315. else if(reader.Name == "fixed")
  316. {
  317. attribute.fixedValue = reader.Value;
  318. }
  319. else if(reader.Name == "form")
  320. {
  321. Exception innerex;
  322. attribute.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
  323. if(innerex != null)
  324. error(h, reader.Value + " is not a valid value for form attribute", innerex);
  325. }
  326. else if(reader.Name == "id")
  327. {
  328. attribute.Id = reader.Value;
  329. }
  330. else if(reader.Name == "name")
  331. {
  332. attribute.name = reader.Value;
  333. }
  334. else if(reader.Name == "ref")
  335. {
  336. Exception innerex;
  337. attribute.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  338. if(innerex != null)
  339. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  340. }
  341. else if(reader.Name == "type")
  342. {
  343. Exception innerex;
  344. attribute.schemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  345. if(innerex != null)
  346. error(h, reader.Value + " is not a valid value for type attribute",innerex);
  347. }
  348. else if(reader.Name == "use")
  349. {
  350. Exception innerex;
  351. attribute.use = XmlSchemaUtil.ReadUseAttribute(reader,out innerex);
  352. if(innerex != null)
  353. error(h, reader.Value + " is not a valid value for use attribute", innerex);
  354. }
  355. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  356. {
  357. error(h,reader.Name + " is not a valid attribute for attribute",null);
  358. }
  359. else
  360. {
  361. XmlSchemaUtil.ReadUnhandledAttribute(reader,attribute);
  362. }
  363. }
  364. reader.MoveToElement();
  365. if(reader.IsEmptyElement)
  366. return attribute;
  367. // Content: (annotation?, (simpleType?))
  368. int level = 1;
  369. while(reader.ReadNextElement())
  370. {
  371. if(reader.NodeType == XmlNodeType.EndElement)
  372. {
  373. if(reader.LocalName != xmlname)
  374. error(h,"Should not happen :2: XmlSchemaAttribute.Read, name="+reader.Name,null);
  375. break;
  376. }
  377. if(level <= 1 && reader.LocalName == "annotation")
  378. {
  379. level = 2; //Only one annotation
  380. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  381. if(annotation != null)
  382. attribute.Annotation = annotation;
  383. continue;
  384. }
  385. if(level <=2 && reader.LocalName == "simpleType")
  386. {
  387. level = 3;
  388. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  389. if(stype != null)
  390. attribute.schemaType = stype;
  391. continue;
  392. }
  393. reader.RaiseInvalidElementError();
  394. }
  395. return attribute;
  396. }
  397. }
  398. }