XmlSchemaAttribute.cs 19 KB

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