XmlSchemaAttribute.cs 19 KB

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