XmlSchemaAttribute.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. const string xmlname = "attribute";
  37. public XmlSchemaAttribute()
  38. {
  39. //LAMESPEC: 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. CompileCommon (h, schema, true);
  202. }
  203. else // local
  204. {
  205. // Q:How to Use of AttributeFormDefault????
  206. // A:Global attribute cannot be defined locally
  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. CompileCommon(h, schema, true);
  214. }
  215. else
  216. {
  217. if(this.name != null)
  218. error(h,"name must be absent if ref is present");
  219. if(this.form != XmlSchemaForm.None)
  220. error(h,"form must be absent if ref is present");
  221. if(this.schemaType != null)
  222. error(h,"simpletype must be absent if ref is present");
  223. if(this.schemaTypeName != null && !this.schemaTypeName.IsEmpty)
  224. error(h,"type must be absent if ref is present");
  225. CompileCommon(h, schema, false);
  226. }
  227. }
  228. this.CompilationId = schema.CompilationId;
  229. return errorCount;
  230. }
  231. private void CompileCommon(ValidationEventHandler h, XmlSchema schema, bool refIsNotPresent)
  232. {
  233. if(refIsNotPresent)
  234. {
  235. if(Name == null) //a.4, b.1,
  236. error(h,"Required attribute name must be present");
  237. else if(!XmlSchemaUtil.CheckNCName(Name)) // a.4.2, b1.2
  238. error(h,"attribute name must be NCName");
  239. else if(Name == "xmlns") // a.14 , b5
  240. error(h,"attribute name must not be xmlns");
  241. else
  242. qualifiedName = new XmlQualifiedName(Name, targetNamespace);
  243. if(SchemaType != null)
  244. {
  245. if(SchemaTypeName != null && !SchemaTypeName.IsEmpty) // a.8
  246. error(h,"attribute can't have both a type and <simpleType> content");
  247. errorCount += SchemaType.Compile(h, schema);
  248. }
  249. if(SchemaTypeName != null && !XmlSchemaUtil.CheckQName(SchemaTypeName))
  250. error(h,SchemaTypeName+" is not a valid QName");
  251. }
  252. else
  253. {
  254. if(RefName == null || RefName.IsEmpty)
  255. throw new InvalidOperationException ("Error: Should Never Happen. refname must be present");
  256. else
  257. qualifiedName = RefName;
  258. }
  259. if(schema.TargetNamespace == XmlSchema.InstanceNamespace && Name != "nil" && Name != "type"
  260. && Name != "schemaLocation" && Name != "noNamespaceSchemaLocation") // a.15, a.16
  261. error(h,"targetNamespace can't be " + XmlSchema.InstanceNamespace);
  262. if(DefaultValue != null && FixedValue != null) // a.6, b.3, c.3
  263. error(h,"default and fixed must not both be present in an Attribute");
  264. if(DefaultValue != null && Use != XmlSchemaUse.None && Use != XmlSchemaUse.Optional)
  265. error(h,"if default is present, use must be optional");
  266. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  267. }
  268. /// <summary>
  269. /// Schema Component:
  270. /// QName, SimpleType, Scope, Default|Fixed, annotation
  271. /// </summary>
  272. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  273. {
  274. if(IsValidated (schema.ValidationId))
  275. return errorCount;
  276. // -- Attribute Declaration Schema Component --
  277. // {name}, {target namespace} -> QualifiedName. Already Compile()d.
  278. // {type definition} -> attributeType. From SchemaType or SchemaTypeName.
  279. // {scope} -> ParentIsSchema | isRedefineChild.
  280. // {value constraint} -> ValidatedFixedValue, ValidatedDefaultValue.
  281. // {annotation}
  282. // -- Attribute Use Schema Component --
  283. // {required}
  284. // {attribute declaration}
  285. // {value constraint}
  286. // First, fill type information for type reference
  287. if (SchemaType != null) {
  288. SchemaType.Validate (h, schema);
  289. attributeType = SchemaType;
  290. }
  291. else if (SchemaTypeName != null && SchemaTypeName != XmlQualifiedName.Empty)
  292. {
  293. // If type is null, then it is missing sub components .
  294. XmlSchemaType type = schema.SchemaTypes [SchemaTypeName] as XmlSchemaType;
  295. if (type is XmlSchemaComplexType)
  296. error(h,"An attribute can't have complexType Content");
  297. else if (type != null) { // simple type
  298. errorCount += type.Validate (h, schema);
  299. attributeType = type;
  300. }
  301. else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
  302. attributeType = XmlSchemaComplexType.AnyType;
  303. else if (XmlSchemaUtil.IsBuiltInDatatypeName (SchemaTypeName)) {
  304. attributeType = XmlSchemaDatatype.FromName (SchemaTypeName);
  305. if (attributeType == null)
  306. error (h, "Invalid xml schema namespace datatype was specified.");
  307. }
  308. // otherwise, it might be missing sub components.
  309. else if (!schema.IsNamespaceAbsent (SchemaTypeName.Namespace))
  310. error (h, "Referenced schema type " + SchemaTypeName + " was not found in the corresponding schema.");
  311. }
  312. // Then, fill type information for the type references for the referencing attributes
  313. if (RefName != null && RefName != XmlQualifiedName.Empty)
  314. {
  315. referencedAttribute = schema.Attributes [RefName] as XmlSchemaAttribute;
  316. // If el is null, then it is missing sub components .
  317. if (referencedAttribute != null)
  318. errorCount += referencedAttribute.Validate (h, schema);
  319. // otherwise, it might be missing sub components.
  320. else if (!schema.IsNamespaceAbsent (RefName.Namespace))
  321. error (h, "Referenced attribute " + RefName + " was not found in the corresponding schema.");
  322. }
  323. if (attributeType == null)
  324. attributeType = XmlSchemaSimpleType.AnySimpleType;
  325. // Validate {value constraints}
  326. if (defaultValue != null || fixedValue != null) {
  327. XmlSchemaDatatype datatype = attributeType as XmlSchemaDatatype;
  328. if (datatype == null)
  329. datatype = ((XmlSchemaSimpleType) attributeType).Datatype;
  330. if (datatype.TokenizedType == XmlTokenizedType.QName)
  331. error (h, "By the defection of the W3C XML Schema specification, it is impossible to supply QName default or fixed values.");
  332. else {
  333. try {
  334. if (defaultValue != null) {
  335. validatedDefaultValue = datatype.Normalize (defaultValue);
  336. datatype.ParseValue (validatedDefaultValue, null, null);
  337. }
  338. } catch (Exception ex) {
  339. // FIXME: This is not a good way to handle exception.
  340. error (h, "The Attribute's default value is invalid with its type definition.", ex);
  341. }
  342. try {
  343. if (fixedValue != null) {
  344. validatedFixedValue = datatype.Normalize (fixedValue);
  345. datatype.ParseValue (validatedFixedValue, null, null);
  346. }
  347. } catch (Exception ex) {
  348. // FIXME: This is not a good way to handle exception.
  349. error (h, "The Attribute's fixed value is invalid with its type definition.", ex);
  350. }
  351. }
  352. }
  353. if (Use == XmlSchemaUse.None)
  354. validatedUse = XmlSchemaUse.Optional;
  355. else
  356. validatedUse = Use;
  357. ValidationId = schema.ValidationId;
  358. return errorCount;
  359. }
  360. //<attribute
  361. // default = string
  362. // fixed = string
  363. // form = (qualified | unqualified)
  364. // id = ID
  365. // name = NCName
  366. // ref = QName
  367. // type = QName
  368. // use = (optional | prohibited | required) : optional
  369. // {any attributes with non-schema namespace . . .}>
  370. // Content: (annotation?, (simpleType?))
  371. //</attribute>
  372. internal static XmlSchemaAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
  373. {
  374. XmlSchemaAttribute attribute = new XmlSchemaAttribute();
  375. reader.MoveToElement();
  376. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  377. {
  378. error(h,"Should not happen :1: XmlSchemaAttribute.Read, name="+reader.Name,null);
  379. reader.SkipToEnd();
  380. return null;
  381. }
  382. attribute.LineNumber = reader.LineNumber;
  383. attribute.LinePosition = reader.LinePosition;
  384. attribute.SourceUri = reader.BaseURI;
  385. while(reader.MoveToNextAttribute())
  386. {
  387. if(reader.Name == "default")
  388. {
  389. attribute.defaultValue = reader.Value;
  390. }
  391. else if(reader.Name == "fixed")
  392. {
  393. attribute.fixedValue = reader.Value;
  394. }
  395. else if(reader.Name == "form")
  396. {
  397. Exception innerex;
  398. attribute.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
  399. if(innerex != null)
  400. error(h, reader.Value + " is not a valid value for form attribute", innerex);
  401. }
  402. else if(reader.Name == "id")
  403. {
  404. attribute.Id = reader.Value;
  405. }
  406. else if(reader.Name == "name")
  407. {
  408. attribute.name = reader.Value;
  409. }
  410. else if(reader.Name == "ref")
  411. {
  412. Exception innerex;
  413. attribute.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  414. if(innerex != null)
  415. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  416. }
  417. else if(reader.Name == "type")
  418. {
  419. Exception innerex;
  420. attribute.schemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  421. if(innerex != null)
  422. error(h, reader.Value + " is not a valid value for type attribute",innerex);
  423. }
  424. else if(reader.Name == "use")
  425. {
  426. Exception innerex;
  427. attribute.use = XmlSchemaUtil.ReadUseAttribute(reader,out innerex);
  428. if(innerex != null)
  429. error(h, reader.Value + " is not a valid value for use attribute", innerex);
  430. }
  431. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  432. {
  433. error(h,reader.Name + " is not a valid attribute for attribute",null);
  434. }
  435. else
  436. {
  437. XmlSchemaUtil.ReadUnhandledAttribute(reader,attribute);
  438. }
  439. }
  440. reader.MoveToElement();
  441. if(reader.IsEmptyElement)
  442. return attribute;
  443. // Content: (annotation?, (simpleType?))
  444. int level = 1;
  445. while(reader.ReadNextElement())
  446. {
  447. if(reader.NodeType == XmlNodeType.EndElement)
  448. {
  449. if(reader.LocalName != xmlname)
  450. error(h,"Should not happen :2: XmlSchemaAttribute.Read, name="+reader.Name,null);
  451. break;
  452. }
  453. if(level <= 1 && reader.LocalName == "annotation")
  454. {
  455. level = 2; //Only one annotation
  456. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  457. if(annotation != null)
  458. attribute.Annotation = annotation;
  459. continue;
  460. }
  461. if(level <=2 && reader.LocalName == "simpleType")
  462. {
  463. level = 3;
  464. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  465. if(stype != null)
  466. attribute.schemaType = stype;
  467. continue;
  468. }
  469. reader.RaiseInvalidElementError();
  470. }
  471. return attribute;
  472. }
  473. }
  474. }