XmlSchemaAttribute.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. //
  2. // System.Xml.Schema.XmlSchemaAttribute.cs
  3. //
  4. // Authors:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Enomoto, Atsushi [email protected]
  7. //
  8. using System;
  9. using System.Xml;
  10. using System.ComponentModel;
  11. using System.Xml.Serialization;
  12. namespace System.Xml.Schema
  13. {
  14. /// <summary>
  15. /// Summary description for XmlSchemaAttribute.
  16. /// </summary>
  17. public class XmlSchemaAttribute : XmlSchemaAnnotated
  18. {
  19. private object attributeType;
  20. private string defaultValue;
  21. private string fixedValue;
  22. private string validatedDefaultValue;
  23. private string validatedFixedValue;
  24. private XmlSchemaForm form;
  25. private string name;
  26. private string targetNamespace;
  27. private XmlQualifiedName qualifiedName;
  28. private XmlQualifiedName refName;
  29. private XmlSchemaSimpleType schemaType;
  30. private XmlQualifiedName schemaTypeName;
  31. private XmlSchemaUse use;
  32. //Compilation fields
  33. internal bool ParentIsSchema = false;
  34. private XmlSchemaAttribute referencedAttribute;
  35. private static string xmlname = "attribute";
  36. public XmlSchemaAttribute()
  37. {
  38. //FIXME: Docs says the default is optional.
  39. //Whereas the MS implementation has default None.
  40. form = XmlSchemaForm.None;
  41. use = XmlSchemaUse.None;
  42. schemaTypeName = XmlQualifiedName.Empty;
  43. qualifiedName = XmlQualifiedName.Empty;
  44. refName = XmlQualifiedName.Empty;
  45. }
  46. // Properties
  47. #region Properties
  48. [DefaultValue(null)]
  49. [System.Xml.Serialization.XmlAttribute("default")]
  50. public string DefaultValue
  51. {
  52. get{ return defaultValue;}
  53. set
  54. { // Default Value and fixed Value are mutually exclusive
  55. fixedValue = null;
  56. defaultValue = value;
  57. }
  58. }
  59. [DefaultValue(null)]
  60. [System.Xml.Serialization.XmlAttribute("fixed")]
  61. public string FixedValue
  62. {
  63. get{ return fixedValue;}
  64. set
  65. { // Default Value and fixed Value are mutually exclusive
  66. defaultValue = null;
  67. fixedValue = value;
  68. }
  69. }
  70. [DefaultValue(XmlSchemaForm.None)]
  71. [System.Xml.Serialization.XmlAttribute("form")]
  72. public XmlSchemaForm Form
  73. {
  74. get{ return form;}
  75. set{ form = value;}
  76. }
  77. [System.Xml.Serialization.XmlAttribute("name")]
  78. public string Name
  79. {
  80. get{ return name;}
  81. set
  82. {
  83. name = value;
  84. }
  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. [System.Xml.Serialization.XmlAttribute("type")]
  96. public XmlQualifiedName SchemaTypeName
  97. {
  98. get{ return schemaTypeName;}
  99. set{ schemaTypeName = value;}
  100. }
  101. [XmlElement("simpleType",Namespace=XmlSchema.Namespace)]
  102. public XmlSchemaSimpleType SchemaType
  103. {
  104. get{ return schemaType;}
  105. set{ schemaType = 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. [XmlIgnore]
  115. public XmlQualifiedName QualifiedName
  116. {
  117. get{ return qualifiedName;}
  118. }
  119. [XmlIgnore]
  120. public object AttributeType
  121. {
  122. get{
  123. if (referencedAttribute != null)
  124. return referencedAttribute.AttributeType;
  125. else
  126. return attributeType;
  127. }
  128. }
  129. // Post compilation default value (normalized)
  130. internal string ValidatedDefaultValue
  131. {
  132. // DefaultValue can be overriden in case of ref.
  133. get { return validatedDefaultValue; }
  134. }
  135. // Post compilation fixed value (normalized)
  136. internal string ValidatedFixedValue
  137. {
  138. // FixedValue can be overriden in case of ref.
  139. get { return validatedFixedValue; }
  140. }
  141. #endregion
  142. /// <remarks>
  143. /// For an attribute:
  144. /// a) If the parent is schema
  145. /// 1-5 are from <xs:complexType name="topLevelAttribute"> in the Schema for Schema
  146. /// 6-8 are from "Constraints on XML Representations of Attribute Declarations"
  147. /// 9-10 are from "Attribute Declaration Schema Component"
  148. /// 11-16 are from "Constraints on Attribute Declaration Schema Components"
  149. /// 1. ref must be absent
  150. /// 2. form must be absent
  151. /// 3. use must be absent
  152. /// 4. name must be present and of type NCName
  153. /// 5. *NO CHECK REQUIRED* Only simple types and annotation are allowed as content
  154. /// 6. default and fixed must not both be present.
  155. /// 7. *NO CHECK REQUIRED* If default and use are both present... (Not possible since use is absent)
  156. /// 8. type and <simpleType> must not both be present.
  157. /// 9. Target Namespace should be schema's targetnamespace or absent
  158. /// 10. Type Definiton coressponds to <simpletype> element, or type value, or absent
  159. /// 11. *TO UNDERSTAND* Missing Sub-components
  160. /// 12. value constraint must be of the same datatype as of type
  161. /// 13. if the type definition is ID then there should be no value constraint.
  162. /// 14. name must not be xmlns
  163. /// 15. Targetnamespace must not be xsi. This implies the target namespace of schema can't be xsi if toplevel attributes are used.
  164. /// 16. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
  165. /// b) If the parent is complextype and ref is not set
  166. /// 1. name must be present and of type NCName.
  167. /// 2. type and <simpleType> must not both be present.
  168. /// 3. default and fixed must not both be present.
  169. /// 4. If default and use are both present, use must have the ·actual value· optional.
  170. /// 5. name must not be xmlns
  171. /// 6. Targetnamespace must not be xsi.
  172. /// 7. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
  173. /// 8. If form has actual value qualified or the schema's formdefault is qualified, targetnamespace
  174. /// is same as schema's target namespace, otherwise absent.
  175. /// c) if the parent is not schema and ref is set
  176. /// 1. name must not be present
  177. /// 2. all of <simpleType>, form and type must be absent.
  178. /// 3. default and fixed must not both be present.
  179. /// 4. If default and use are both present, use must have the ·actual value· optional.
  180. /// </remarks>
  181. [MonoTODO]
  182. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  183. {
  184. // If this is already compiled this time, simply skip.
  185. if (this.IsComplied (schema.CompilationId))
  186. return 0;
  187. errorCount = 0;
  188. if(ParentIsSchema || isRedefineChild)//a
  189. {
  190. if(RefName!= null && !RefName.IsEmpty) // a.1
  191. error(h,"ref must be absent in the top level <attribute>");
  192. if(Form != XmlSchemaForm.None) // a.2
  193. error(h,"form must be absent in the top level <attribute>");
  194. if(Use != XmlSchemaUse.None) // a.3
  195. error(h,"use must be absent in the top level <attribute>");
  196. targetNamespace = schema.TargetNamespace;
  197. // TODO: a.10, a.11, a.12, a.13
  198. CompileCommon(h, schema, true);
  199. }
  200. else // local
  201. {
  202. //FIXME: How to Use of AttributeFormDefault????
  203. if(RefName == null || RefName.IsEmpty)
  204. {
  205. if(form == XmlSchemaForm.Qualified || (form == XmlSchemaForm.None && schema.AttributeFormDefault == XmlSchemaForm.Qualified))
  206. this.targetNamespace = schema.TargetNamespace;
  207. else
  208. this.targetNamespace = "";
  209. //TODO: b.8
  210. CompileCommon(h, schema, true);
  211. }
  212. else
  213. {
  214. if(this.name != null)
  215. error(h,"name must be absent if ref is present");
  216. if(this.form != XmlSchemaForm.None)
  217. error(h,"form must be absent if ref is present");
  218. if(this.schemaType != null)
  219. error(h,"simpletype must be absent if ref is present");
  220. if(this.schemaTypeName != null && !this.schemaTypeName.IsEmpty)
  221. error(h,"type must be absent if ref is present");
  222. CompileCommon(h, schema, false);
  223. }
  224. }
  225. this.CompilationId = schema.CompilationId;
  226. return errorCount;
  227. }
  228. private void CompileCommon(ValidationEventHandler h, XmlSchema schema, bool refIsNotPresent)
  229. {
  230. if(refIsNotPresent)
  231. {
  232. if(Name == null) //a.4, b.1,
  233. error(h,"Required attribute name must be present");
  234. else if(!XmlSchemaUtil.CheckNCName(Name)) // a.4.2, b1.2
  235. error(h,"attribute name must be NCName");
  236. else if(Name == "xmlns") // a.14 , b5
  237. error(h,"attribute name must not be xmlns");
  238. else
  239. qualifiedName = new XmlQualifiedName(Name, targetNamespace);
  240. if(SchemaType != null)
  241. {
  242. if(SchemaTypeName != null && !SchemaTypeName.IsEmpty) // a.8
  243. error(h,"attribute can't have both a type and <simpleType> content");
  244. errorCount += SchemaType.Compile(h, schema);
  245. }
  246. if(SchemaTypeName != null && !XmlSchemaUtil.CheckQName(SchemaTypeName))
  247. error(h,SchemaTypeName+" is not a valid QName");
  248. }
  249. else
  250. {
  251. if(RefName == null || RefName.IsEmpty)
  252. throw new NotImplementedException ("Error: Should Never Happen. refname must be present");
  253. else
  254. qualifiedName = RefName;
  255. }
  256. if(schema.TargetNamespace == XmlSchema.InstanceNamespace && Name != "nil" && Name != "type"
  257. && Name != "schemaLocation" && Name != "noNamespaceSchemaLocation") // a.15, a.16
  258. error(h,"targetNamespace can't be " + XmlSchema.InstanceNamespace);
  259. if(DefaultValue != null && FixedValue != null) // a.6, b.3, c.3
  260. error(h,"default and fixed must not both be present in an Attribute");
  261. if(DefaultValue != null && Use != XmlSchemaUse.None && Use != XmlSchemaUse.Optional)
  262. error(h,"if default is present, use must be optional");
  263. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  264. }
  265. /// <summary>
  266. /// Schema Component:
  267. /// QName, SimpleType, Scope, Default|Fixed, annotation
  268. /// </summary>
  269. [MonoTODO]
  270. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  271. {
  272. if(IsValidated (schema.ValidationId))
  273. return errorCount;
  274. // -- Attribute Declaration Schema Component --
  275. // {name}, {target namespace} -> QualifiedName. Already Compile()d.
  276. // {type definition} -> attributeType. From SchemaType or SchemaTypeName.
  277. // {scope} -> ParentIsSchema | isRedefineChild.
  278. // {value constraint} -> ValidatedFixedValue, ValidatedDefaultValue.
  279. // {annotation}
  280. // -- Attribute Use Schema Component --
  281. // {required}
  282. // {attribute declaration}
  283. // {value constraint}
  284. // First, fill type information for type reference
  285. if (SchemaTypeName != null && SchemaTypeName != XmlQualifiedName.Empty)
  286. {
  287. // If type is null, then it is missing sub components .
  288. XmlSchemaType type = schema.SchemaTypes [SchemaTypeName] as XmlSchemaType;
  289. if (type is XmlSchemaComplexType)
  290. error(h,"An attribute can't have complexType Content");
  291. else if (type != null) { // simple type
  292. errorCount += type.Validate (h, schema);
  293. attributeType = type;
  294. }
  295. else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
  296. attributeType = XmlSchemaComplexType.AnyType;
  297. else if (SchemaTypeName.Namespace == XmlSchema.Namespace) {
  298. attributeType = XmlSchemaDatatype.FromName (SchemaTypeName);
  299. if (attributeType == null)
  300. error (h, "Invalid xml schema namespace datatype was specified.");
  301. }
  302. // otherwise, it might be missing sub components.
  303. else if (!schema.IsNamespaceAbsent (SchemaTypeName.Namespace))
  304. error (h, "Referenced schema type " + SchemaTypeName + " was not found in the corresponding schema.");
  305. }
  306. // Then, fill type information for the type references for the referencing attributes
  307. if (RefName != null && RefName != XmlQualifiedName.Empty)
  308. {
  309. referencedAttribute = schema.Attributes [RefName] as XmlSchemaAttribute;
  310. // If el is null, then it is missing sub components .
  311. if (referencedAttribute != null)
  312. errorCount += referencedAttribute.Validate (h, schema);
  313. // otherwise, it might be missing sub components.
  314. else if (!schema.IsNamespaceAbsent (RefName.Namespace))
  315. error (h, "Referenced attribute " + RefName + " was not found in the corresponding schema.");
  316. }
  317. if (attributeType == null)
  318. attributeType = XmlSchemaSimpleType.AnySimpleType;
  319. // Validate {value constraints}
  320. if (defaultValue != null || fixedValue != null) {
  321. XmlSchemaDatatype datatype = attributeType as XmlSchemaDatatype;
  322. if (datatype == null)
  323. datatype = ((XmlSchemaSimpleType) attributeType).Datatype;
  324. if (datatype.TokenizedType == XmlTokenizedType.QName)
  325. error (h, "By the defection of the W3C XML Schema specification, it is impossible to supply QName default or fixed values.");
  326. else {
  327. try {
  328. if (defaultValue != null) {
  329. validatedDefaultValue = datatype.Normalize (defaultValue);
  330. datatype.ParseValue (validatedDefaultValue, null, null);
  331. }
  332. } catch (Exception ex) {
  333. // FIXME: This is not a good way to handle exception.
  334. error (h, "The Attribute's default value is invalid with its type definition.", ex);
  335. }
  336. try {
  337. if (fixedValue != null) {
  338. validatedFixedValue = datatype.Normalize (fixedValue);
  339. datatype.ParseValue (validatedFixedValue, null, null);
  340. }
  341. } catch (Exception ex) {
  342. // FIXME: This is not a good way to handle exception.
  343. error (h, "The Attribute's fixed value is invalid with its type definition.", ex);
  344. }
  345. }
  346. }
  347. ValidationId = schema.ValidationId;
  348. return errorCount;
  349. }
  350. //<attribute
  351. // default = string
  352. // fixed = string
  353. // form = (qualified | unqualified)
  354. // id = ID
  355. // name = NCName
  356. // ref = QName
  357. // type = QName
  358. // use = (optional | prohibited | required) : optional
  359. // {any attributes with non-schema namespace . . .}>
  360. // Content: (annotation?, (simpleType?))
  361. //</attribute>
  362. internal static XmlSchemaAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
  363. {
  364. XmlSchemaAttribute attribute = new XmlSchemaAttribute();
  365. reader.MoveToElement();
  366. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  367. {
  368. error(h,"Should not happen :1: XmlSchemaAttribute.Read, name="+reader.Name,null);
  369. reader.SkipToEnd();
  370. return null;
  371. }
  372. attribute.LineNumber = reader.LineNumber;
  373. attribute.LinePosition = reader.LinePosition;
  374. attribute.SourceUri = reader.BaseURI;
  375. while(reader.MoveToNextAttribute())
  376. {
  377. if(reader.Name == "default")
  378. {
  379. attribute.defaultValue = reader.Value;
  380. }
  381. else if(reader.Name == "fixed")
  382. {
  383. attribute.fixedValue = reader.Value;
  384. }
  385. else if(reader.Name == "form")
  386. {
  387. Exception innerex;
  388. attribute.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
  389. if(innerex != null)
  390. error(h, reader.Value + " is not a valid value for form attribute", innerex);
  391. }
  392. else if(reader.Name == "id")
  393. {
  394. attribute.Id = reader.Value;
  395. }
  396. else if(reader.Name == "name")
  397. {
  398. attribute.name = reader.Value;
  399. }
  400. else if(reader.Name == "ref")
  401. {
  402. Exception innerex;
  403. attribute.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  404. if(innerex != null)
  405. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  406. }
  407. else if(reader.Name == "type")
  408. {
  409. Exception innerex;
  410. attribute.schemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  411. if(innerex != null)
  412. error(h, reader.Value + " is not a valid value for type attribute",innerex);
  413. }
  414. else if(reader.Name == "use")
  415. {
  416. Exception innerex;
  417. attribute.use = XmlSchemaUtil.ReadUseAttribute(reader,out innerex);
  418. if(innerex != null)
  419. error(h, reader.Value + " is not a valid value for use attribute", innerex);
  420. }
  421. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  422. {
  423. error(h,reader.Name + " is not a valid attribute for attribute",null);
  424. }
  425. else
  426. {
  427. XmlSchemaUtil.ReadUnhandledAttribute(reader,attribute);
  428. }
  429. }
  430. reader.MoveToElement();
  431. if(reader.IsEmptyElement)
  432. return attribute;
  433. // Content: (annotation?, (simpleType?))
  434. int level = 1;
  435. while(reader.ReadNextElement())
  436. {
  437. if(reader.NodeType == XmlNodeType.EndElement)
  438. {
  439. if(reader.LocalName != xmlname)
  440. error(h,"Should not happen :2: XmlSchemaAttribute.Read, name="+reader.Name,null);
  441. break;
  442. }
  443. if(level <= 1 && reader.LocalName == "annotation")
  444. {
  445. level = 2; //Only one annotation
  446. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  447. if(annotation != null)
  448. attribute.Annotation = annotation;
  449. continue;
  450. }
  451. if(level <=2 && reader.LocalName == "simpleType")
  452. {
  453. level = 3;
  454. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  455. if(stype != null)
  456. attribute.schemaType = stype;
  457. continue;
  458. }
  459. reader.RaiseInvalidElementError();
  460. }
  461. return attribute;
  462. }
  463. }
  464. }