XmlSchemaAttribute.cs 18 KB

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