XmlSchemaAttribute.cs 19 KB

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