XmlSchemaAttribute.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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=XmlSchema.Namespace)]
  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, XmlSchema schema)
  157. {
  158. // If this is already compiled this time, simply skip.
  159. if (this.IsComplied (schema.CompilationId))
  160. return 0;
  161. errorCount = 0;
  162. if(parentIsSchema)//a
  163. {
  164. if(RefName!= null && !RefName.IsEmpty) // a.1
  165. error(h,"ref must be absent in the top level <attribute>");
  166. if(Form != XmlSchemaForm.None) // a.2
  167. error(h,"form must be absent in the top level <attribute>");
  168. if(Use != XmlSchemaUse.None) // a.3
  169. error(h,"use must be absent in the top level <attribute>");
  170. // TODO: a.10, a.11, a.12, a.13
  171. CompileCommon(h, schema, true);
  172. }
  173. else // local
  174. {
  175. //FIXME: How to Use of AttributeFormDefault????
  176. if(RefName == null || RefName.IsEmpty)
  177. {
  178. //TODO: b.8
  179. CompileCommon(h, schema, true);
  180. }
  181. else
  182. {
  183. if(this.name != null)
  184. error(h,"name must be absent if ref is present");
  185. if(this.form != XmlSchemaForm.None)
  186. error(h,"form must be absent if ref is present");
  187. if(this.schemaType != null)
  188. error(h,"simpletype must be absent if ref is present");
  189. if(this.schemaTypeName != null && !this.schemaTypeName.IsEmpty)
  190. error(h,"type must be absent if ref is present");
  191. CompileCommon(h, schema, false);
  192. }
  193. }
  194. this.CompilationId = schema.CompilationId;
  195. return errorCount;
  196. }
  197. private void CompileCommon(ValidationEventHandler h, XmlSchema schema, bool refIsNotPresent)
  198. {
  199. if(refIsNotPresent)
  200. {
  201. if(Name == null) //a.4, b.1,
  202. error(h,"Required attribute name must be present");
  203. else if(!XmlSchemaUtil.CheckNCName(Name)) // a.4.2, b1.2
  204. error(h,"attribute name must be NCName");
  205. else if(Name == "xmlns") // a.14 , b5
  206. error(h,"attribute name must not be xmlns");
  207. else
  208. qualifiedName = new XmlQualifiedName(Name, schema.TargetNamespace);
  209. if(SchemaType != null)
  210. {
  211. if(SchemaTypeName != null && !SchemaTypeName.IsEmpty) // a.8
  212. error(h,"attribute can't have both a type and <simpleType> content");
  213. errorCount += SchemaType.Compile(h, schema);
  214. }
  215. if(SchemaTypeName != null && !XmlSchemaUtil.CheckQName(SchemaTypeName))
  216. error(h,SchemaTypeName+" is not a valid QName");
  217. }
  218. else
  219. {
  220. if(RefName == null || RefName.IsEmpty)
  221. error(h,"Error: Should Never Happen. refname must be present");
  222. else
  223. qualifiedName = RefName;
  224. }
  225. if(schema.TargetNamespace == XmlSchema.InstanceNamespace && Name != "nil" && Name != "type"
  226. && Name != "schemaLocation" && Name != "noNamespaceSchemaLocation") // a.15, a.16
  227. error(h,"targetNamespace can't be " + XmlSchema.InstanceNamespace);
  228. if(DefaultValue != null && FixedValue != null) // a.6, b.3, c.3
  229. error(h,"default and fixed must not both be present in an Attribute");
  230. if(DefaultValue != null && Use != XmlSchemaUse.None && Use != XmlSchemaUse.Optional)
  231. error(h,"if default is present, use must be optional");
  232. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  233. }
  234. /// <summary>
  235. /// Schema Component:
  236. /// QName, SimpleType, Scope, Default|Fixed, annotation
  237. /// </summary>
  238. [MonoTODO]
  239. internal int Validate(ValidationEventHandler h, XmlSchema schema)
  240. {
  241. if(isCompiled)
  242. return errorCount;
  243. //If Parent is schema:
  244. if(parentIsSchema)
  245. {
  246. if(SchemaType != null)
  247. {
  248. errorCount += SchemaType.Validate(h, schema);
  249. attributeType = SchemaType;
  250. }
  251. else if(SchemaTypeName != null && !SchemaTypeName.IsEmpty)
  252. {
  253. //First Try to get a Inbuilt DataType
  254. XmlSchemaDatatype dtype = XmlSchemaDatatype.FromName(SchemaTypeName);
  255. if(dtype != null)
  256. {
  257. attributeType = dtype;
  258. }
  259. else
  260. {
  261. XmlSchemaObject obj = schema.SchemaTypes[SchemaTypeName];
  262. if(obj is XmlSchemaSimpleType)
  263. {
  264. XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
  265. errorCount += stype.Validate(h, schema);
  266. attributeType = stype;
  267. }
  268. else if(attributeType == null)
  269. error(h,"The type '"+ SchemaTypeName +"' is not defined in the schema");
  270. else if(attributeType is XmlSchemaComplexType)
  271. error(h,"An attribute can't have complexType Content");
  272. else
  273. error(h, "Should Never Happen. Illegal content in SchemaTypes");
  274. }
  275. }
  276. else
  277. {
  278. error(h,"Should Never Happen. Attribute SimpleType Not set. Should have been caught in the Compile() phase");
  279. }
  280. }
  281. else
  282. {
  283. //TODO: Local Attribute Validation
  284. }
  285. isCompiled = true;
  286. return errorCount;
  287. }
  288. //<attribute
  289. // default = string
  290. // fixed = string
  291. // form = (qualified | unqualified)
  292. // id = ID
  293. // name = NCName
  294. // ref = QName
  295. // type = QName
  296. // use = (optional | prohibited | required) : optional
  297. // {any attributes with non-schema namespace . . .}>
  298. // Content: (annotation?, (simpleType?))
  299. //</attribute>
  300. internal static XmlSchemaAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
  301. {
  302. XmlSchemaAttribute attribute = new XmlSchemaAttribute();
  303. reader.MoveToElement();
  304. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  305. {
  306. error(h,"Should not happen :1: XmlSchemaAttribute.Read, name="+reader.Name,null);
  307. reader.SkipToEnd();
  308. return null;
  309. }
  310. attribute.LineNumber = reader.LineNumber;
  311. attribute.LinePosition = reader.LinePosition;
  312. attribute.SourceUri = reader.BaseURI;
  313. while(reader.MoveToNextAttribute())
  314. {
  315. if(reader.Name == "default")
  316. {
  317. attribute.defaultValue = reader.Value;
  318. }
  319. else if(reader.Name == "fixed")
  320. {
  321. attribute.fixedValue = reader.Value;
  322. }
  323. else if(reader.Name == "form")
  324. {
  325. Exception innerex;
  326. attribute.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
  327. if(innerex != null)
  328. error(h, reader.Value + " is not a valid value for form attribute", innerex);
  329. }
  330. else if(reader.Name == "id")
  331. {
  332. attribute.Id = reader.Value;
  333. }
  334. else if(reader.Name == "name")
  335. {
  336. attribute.name = reader.Value;
  337. }
  338. else if(reader.Name == "ref")
  339. {
  340. Exception innerex;
  341. attribute.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  342. if(innerex != null)
  343. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  344. }
  345. else if(reader.Name == "type")
  346. {
  347. Exception innerex;
  348. attribute.schemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  349. if(innerex != null)
  350. error(h, reader.Value + " is not a valid value for type attribute",innerex);
  351. }
  352. else if(reader.Name == "use")
  353. {
  354. Exception innerex;
  355. attribute.use = XmlSchemaUtil.ReadUseAttribute(reader,out innerex);
  356. if(innerex != null)
  357. error(h, reader.Value + " is not a valid value for use attribute", innerex);
  358. }
  359. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  360. {
  361. error(h,reader.Name + " is not a valid attribute for attribute",null);
  362. }
  363. else
  364. {
  365. XmlSchemaUtil.ReadUnhandledAttribute(reader,attribute);
  366. }
  367. }
  368. reader.MoveToElement();
  369. if(reader.IsEmptyElement)
  370. return attribute;
  371. // Content: (annotation?, (simpleType?))
  372. int level = 1;
  373. while(reader.ReadNextElement())
  374. {
  375. if(reader.NodeType == XmlNodeType.EndElement)
  376. {
  377. if(reader.LocalName != xmlname)
  378. error(h,"Should not happen :2: XmlSchemaAttribute.Read, name="+reader.Name,null);
  379. break;
  380. }
  381. if(level <= 1 && reader.LocalName == "annotation")
  382. {
  383. level = 2; //Only one annotation
  384. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  385. if(annotation != null)
  386. attribute.Annotation = annotation;
  387. continue;
  388. }
  389. if(level <=2 && reader.LocalName == "simpleType")
  390. {
  391. level = 3;
  392. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  393. if(stype != null)
  394. attribute.schemaType = stype;
  395. continue;
  396. }
  397. reader.RaiseInvalidElementError();
  398. }
  399. return attribute;
  400. }
  401. }
  402. }