XmlSchemaAttribute.cs 17 KB

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