Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XmlSchemaAttribute.cs « System.Xml.Schema « System.XML « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b4b5082058944ad214c181675c60c4bd7a5c138d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//
// System.Xml.Schema.XmlSchemaAttribute.cs
//
// Authors:
//	Dwivedi, Ajay kumar  Adwiv@Yahoo.com
//	Enomoto, Atsushi     ginga@kit.hi-ho.ne.jp
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Xml;
using System.ComponentModel;
using System.Xml.Serialization;

namespace System.Xml.Schema
{
	/// <summary>
	/// Summary description for XmlSchemaAttribute.
	/// </summary>
	public class XmlSchemaAttribute : XmlSchemaAnnotated
	{
		private object attributeType;
#if NET_2_0
		private XmlSchemaSimpleType attributeSchemaType;
#endif
		private string defaultValue;
		private string fixedValue;
		private string validatedDefaultValue;
		private string validatedFixedValue;
		private object validatedFixedTypedValue;
		private XmlSchemaForm form;
		private string name;
		private string targetNamespace;
		private XmlQualifiedName qualifiedName;
		private XmlQualifiedName refName;
		private XmlSchemaSimpleType schemaType;
		private XmlQualifiedName schemaTypeName;
		private XmlSchemaUse use;
		private XmlSchemaUse validatedUse;
		//Compilation fields
		internal bool ParentIsSchema = false;
		private XmlSchemaAttribute referencedAttribute;
		const string xmlname = "attribute";

		public XmlSchemaAttribute()
		{
			//LAMESPEC: Docs says the default is optional.
			//Whereas the MS implementation has default None.
			form	= XmlSchemaForm.None;
			use		= XmlSchemaUse.None;
			schemaTypeName	= XmlQualifiedName.Empty;
			qualifiedName	= XmlQualifiedName.Empty;
			refName			= XmlQualifiedName.Empty;
		}

		// Properties
		#region Properties

		[DefaultValue(null)]
		[System.Xml.Serialization.XmlAttribute("default")]
		public string DefaultValue 
		{
			get{ return defaultValue;}
			set
			{ // Default Value and fixed Value are mutually exclusive
				fixedValue = null;
				defaultValue = value;
			}
		}

		[DefaultValue(null)]
		[System.Xml.Serialization.XmlAttribute("fixed")]
		public string FixedValue 
		{
			get{ return fixedValue;}
			set
			{ // Default Value and fixed Value are mutually exclusive
				defaultValue = null;
				fixedValue = value;
			}
		}

		[DefaultValue(XmlSchemaForm.None)]
		[System.Xml.Serialization.XmlAttribute("form")]
		public XmlSchemaForm Form 
		{
			get{ return form;}
			set{ form = value;}
		}

		[System.Xml.Serialization.XmlAttribute("name")]
		public string Name 
		{
			get{ return name;}
			set
			{
				name  = value;
			}
		}

		[System.Xml.Serialization.XmlAttribute("ref")]
		public XmlQualifiedName RefName 
		{
			get{ return refName;}
			set
			{
				refName = value; 
			}
		}
		
		[System.Xml.Serialization.XmlAttribute("type")]
		public XmlQualifiedName SchemaTypeName 
		{
			get{ return schemaTypeName;}
			set{ schemaTypeName = value;}
		}

		[XmlElement("simpleType")]
		public XmlSchemaSimpleType SchemaType 
		{
			get{ return schemaType;}
			set{ schemaType = value;}
		}

		[DefaultValue(XmlSchemaUse.None)]
		[System.Xml.Serialization.XmlAttribute("use")]
		public XmlSchemaUse Use 
		{
			get{ return use;}
			set{ use = value;}
		}

		[XmlIgnore]
		public XmlQualifiedName QualifiedName 
		{
			get{ return qualifiedName;}
		}

		[XmlIgnore]
#if NET_2_0
		[Obsolete]
#endif
		public object AttributeType 
		{
			get{
				if (referencedAttribute != null)
					return referencedAttribute.AttributeType;
				else
					return attributeType;
			}
		}

#if NET_2_0
		[XmlIgnore]
		public XmlSchemaSimpleType AttributeSchemaType
		{
			get {
				if (referencedAttribute != null)
					return referencedAttribute.AttributeSchemaType;
				else
					return attributeSchemaType;
			}
		}
#endif

		// Post compilation default value (normalized)
		internal string ValidatedDefaultValue
		{
			// DefaultValue can be overriden in case of ref.
			get { return validatedDefaultValue; }
		}

		// Post compilation fixed value (normalized)
		internal string ValidatedFixedValue 
		{
			// FixedValue can be overriden in case of ref.
			get { return validatedFixedValue; }
		}

		internal object ValidatedFixedTypedValue
		{
			get { return validatedFixedTypedValue; }
		}

		internal XmlSchemaUse ValidatedUse
		{
			get { return validatedUse; }
		}

		#endregion

		/// <remarks>
		/// For an attribute:
		///  a) If the parent is schema 
		///		1-5		are from <xs:complexType name="topLevelAttribute"> in the Schema for Schema
		///		6-8		are from  "Constraints on XML Representations of Attribute Declarations"
		///		9-10	are from "Attribute Declaration Schema Component"
		///		11-16	are from "Constraints on Attribute Declaration Schema Components"
		///		1. ref	must be absent
		///		2. form must be absent
		///		3. use	must be absent
		///		4. name must be present and of type NCName
		///		5. *NO CHECK REQUIRED* Only simple types and annotation are allowed as content
		///		6. default and fixed must not both be present. 
		///		7. *NO CHECK REQUIRED* If default and use are both present... (Not possible since use is absent)
		///		8. type and <simpleType> must not both be present.
		///		9. Target Namespace should be schema's targetnamespace or absent
		///		10. Type Definiton coressponds to <simpletype> element, or type value, or absent
		///		11. *TO UNDERSTAND* Missing Sub-components
		///		12. value constraint must be of the same datatype as of type
		///		13. if the type definition is ID then there should be no value constraint.
		///		14. name must not be xmlns
		///		15. Targetnamespace must not be xsi. This implies the target namespace of schema can't be xsi if toplevel attributes are used.
		///		16. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
		///	b) If the parent is complextype and ref is not set
		///		1. name must be present and of type NCName.
		///		2. type and <simpleType> must not both be present.
		///		3. default and fixed must not both be present. 
		///     4. If default and use are both present, use must have the ·actual value· optional.
		///		5. name must not be xmlns
		///		6. Targetnamespace must not be xsi.
		///		7. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
		///		8. If form has actual value qualified or the schema's formdefault is qualified, targetnamespace
		///		   is same as schema's target namespace, otherwise absent.
		///	c) if the parent is not schema and ref is set
		///		1. name must not be present
		///		2. all of <simpleType>, form and type must be absent. 
		///		3. default and fixed must not both be present. 
		///     4. If default and use are both present, use must have the ·actual value· optional.
		/// </remarks>
		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
		{
			// If this is already compiled this time, simply skip.
			if (this.IsComplied (schema.CompilationId))
				return 0;

#if NET_2_0
			if (schemaType != null)
				schemaType.Parent = this;
#endif

			errorCount = 0;
			
			if(ParentIsSchema || isRedefineChild)//a
			{
				if(RefName!= null && !RefName.IsEmpty) // a.1
					error(h,"ref must be absent in the top level <attribute>");
				
				if(Form != XmlSchemaForm.None)	// a.2
					error(h,"form must be absent in the top level <attribute>");
				
				if(Use != XmlSchemaUse.None)		// a.3
					error(h,"use must be absent in the top level <attribute>");

				targetNamespace = schema.TargetNamespace;

				CompileCommon (h, schema, true);
			}
			else // local
			{
				// Q:How to Use of AttributeFormDefault????
				// A:Global attribute cannot be defined locally
				if(RefName == null || RefName.IsEmpty)
				{
					if(form == XmlSchemaForm.Qualified || (form == XmlSchemaForm.None && schema.AttributeFormDefault == XmlSchemaForm.Qualified))
						this.targetNamespace = schema.TargetNamespace;
					else
						this.targetNamespace = "";

					CompileCommon(h, schema, true);
				}
				else
				{
					if(this.name != null)
						error(h,"name must be absent if ref is present");
					if(this.form != XmlSchemaForm.None)
						error(h,"form must be absent if ref is present");
					if(this.schemaType != null)
						error(h,"simpletype must be absent if ref is present");
					if(this.schemaTypeName != null && !this.schemaTypeName.IsEmpty)
						error(h,"type must be absent if ref is present");

					CompileCommon(h, schema, false);
				}
			}

			this.CompilationId = schema.CompilationId;
			return errorCount;
		}
		
		private void CompileCommon(ValidationEventHandler h, XmlSchema schema, bool refIsNotPresent)
		{
			if(refIsNotPresent)
			{
				if(Name == null)	//a.4, b.1, 
					error(h,"Required attribute name must be present");
				else if(!XmlSchemaUtil.CheckNCName(Name)) // a.4.2, b1.2
					error(h,"attribute name must be NCName");
				else if(Name == "xmlns") // a.14 , b5
					error(h,"attribute name must not be xmlns");
				else
					qualifiedName = new XmlQualifiedName(Name, targetNamespace);

				if(SchemaType != null)
				{
					if(SchemaTypeName != null && !SchemaTypeName.IsEmpty) // a.8
						error(h,"attribute can't have both a type and <simpleType> content");

					errorCount += SchemaType.Compile(h, schema); 
				}

				if(SchemaTypeName != null && !XmlSchemaUtil.CheckQName(SchemaTypeName))
					error(h,SchemaTypeName+" is not a valid QName");
			}
			else
			{
				if(RefName == null || RefName.IsEmpty) 
					throw new InvalidOperationException ("Error: Should Never Happen. refname must be present");
				else
					qualifiedName = RefName;
			}

			if(schema.TargetNamespace == XmlSchema.InstanceNamespace && Name != "nil" && Name != "type" 
				&& Name != "schemaLocation" && Name != "noNamespaceSchemaLocation") // a.15, a.16
				error(h,"targetNamespace can't be " + XmlSchema.InstanceNamespace);

			if(DefaultValue != null && FixedValue != null) // a.6, b.3, c.3
				error(h,"default and fixed must not both be present in an Attribute");

			if(DefaultValue != null && Use != XmlSchemaUse.None && Use != XmlSchemaUse.Optional)
				error(h,"if default is present, use must be optional");

			XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
		}

		/// <summary>
		/// Schema Component: 
		///			QName, SimpleType, Scope, Default|Fixed, annotation
		/// </summary>
		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
		{
			if(IsValidated (schema.ValidationId))
				return errorCount;

			// -- Attribute Declaration Schema Component --
			// {name}, {target namespace} -> QualifiedName. Already Compile()d.
			// {type definition} -> attributeType. From SchemaType or SchemaTypeName.
			// {scope} -> ParentIsSchema | isRedefineChild.
			// {value constraint} -> ValidatedFixedValue, ValidatedDefaultValue.
			// {annotation}
			// -- Attribute Use Schema Component --
			// {required}
			// {attribute declaration}
			// {value constraint}

			// First, fill type information for type reference
			if (SchemaType != null) {
				SchemaType.Validate (h, schema);
				attributeType = SchemaType;
			}
			else if (SchemaTypeName != null && SchemaTypeName != XmlQualifiedName.Empty)
			{
				// If type is null, then it is missing sub components .
				XmlSchemaType type = schema.SchemaTypes [SchemaTypeName] as XmlSchemaType;
				if (type is XmlSchemaComplexType)
					error(h,"An attribute can't have complexType Content");
				else if (type != null) {	// simple type
					errorCount += type.Validate (h, schema);
					attributeType = type;
				}
				else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
					attributeType = XmlSchemaComplexType.AnyType;
				else if (XmlSchemaUtil.IsBuiltInDatatypeName (SchemaTypeName)) {
					attributeType = XmlSchemaDatatype.FromName (SchemaTypeName);
					if (attributeType == null)
						error (h, "Invalid xml schema namespace datatype was specified.");
				}
				// otherwise, it might be missing sub components.
				else if (!schema.IsNamespaceAbsent (SchemaTypeName.Namespace))
					error (h, "Referenced schema type " + SchemaTypeName + " was not found in the corresponding schema.");
			}

			// Then, fill type information for the type references for the referencing attributes
			if (RefName != null && RefName != XmlQualifiedName.Empty)
			{
				referencedAttribute = schema.Attributes [RefName] as XmlSchemaAttribute;
				// If el is null, then it is missing sub components .
				if (referencedAttribute != null)
					errorCount += referencedAttribute.Validate (h, schema);
				// otherwise, it might be missing sub components.
				else if (!schema.IsNamespaceAbsent (RefName.Namespace))
					error (h, "Referenced attribute " + RefName + " was not found in the corresponding schema.");
			}

			if (attributeType == null)
				attributeType = XmlSchemaSimpleType.AnySimpleType;

			// Validate {value constraints}
			if (defaultValue != null || fixedValue != null) {
				XmlSchemaDatatype datatype = attributeType as XmlSchemaDatatype;
				if (datatype == null)
					datatype = ((XmlSchemaSimpleType) attributeType).Datatype;
				if (datatype.TokenizedType == XmlTokenizedType.QName)
					error (h, "By the defection of the W3C XML Schema specification, it is impossible to supply QName default or fixed values.");
				else {
					try {
						if (defaultValue != null) {
							validatedDefaultValue = datatype.Normalize (defaultValue);
							datatype.ParseValue (validatedDefaultValue, null, null);
						}
					} catch (Exception ex) {
						// FIXME: This is not a good way to handle exception.
						error (h, "The Attribute's default value is invalid with its type definition.", ex);
					}
					try {
						if (fixedValue != null) {
							validatedFixedValue = datatype.Normalize (fixedValue);
							validatedFixedTypedValue = datatype.ParseValue (validatedFixedValue, null, null);
						}
					} catch (Exception ex) {
						// FIXME: This is not a good way to handle exception.
						error (h, "The Attribute's fixed value is invalid with its type definition.", ex);
					}
				}
			}
			if (Use == XmlSchemaUse.None)
				validatedUse = XmlSchemaUse.Optional;
			else
				validatedUse = Use;

#if NET_2_0
			if (attributeType != null) {
				attributeSchemaType = attributeType as XmlSchemaSimpleType;
				if (attributeType == XmlSchemaSimpleType.AnySimpleType)
					attributeSchemaType = XmlSchemaSimpleType.XsAnySimpleType;
				if (attributeSchemaType == null)
					attributeSchemaType = XmlSchemaType.GetBuiltInSimpleType (SchemaTypeName);
			}
#endif

			ValidationId = schema.ValidationId;
			return errorCount;
		}

		//<attribute
		//  default = string
		//  fixed = string
		//  form = (qualified | unqualified)
		//  id = ID
		//  name = NCName
		//  ref = QName
		//  type = QName
		//  use = (optional | prohibited | required) : optional
		//  {any attributes with non-schema namespace . . .}>
		//  Content: (annotation?, (simpleType?))
		//</attribute>
		internal static XmlSchemaAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
		{
			XmlSchemaAttribute attribute = new XmlSchemaAttribute();
			reader.MoveToElement();

			if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
			{
				error(h,"Should not happen :1: XmlSchemaAttribute.Read, name="+reader.Name,null);
				reader.SkipToEnd();
				return null;
			}

			attribute.LineNumber = reader.LineNumber;
			attribute.LinePosition = reader.LinePosition;
			attribute.SourceUri = reader.BaseURI;

			while(reader.MoveToNextAttribute())
			{
				if(reader.Name == "default")
				{
					attribute.defaultValue = reader.Value;
				}
				else if(reader.Name == "fixed")
				{
					attribute.fixedValue = reader.Value;
				}
				else if(reader.Name == "form")
				{
					Exception innerex;
					attribute.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
					if(innerex != null)
						error(h, reader.Value + " is not a valid value for form attribute", innerex);
				}
				else if(reader.Name == "id")
				{
					attribute.Id = reader.Value;
				}
				else if(reader.Name == "name")
				{
					attribute.name = reader.Value;
				}
				else if(reader.Name == "ref")
				{
					Exception innerex;
					attribute.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
					if(innerex != null)
						error(h, reader.Value + " is not a valid value for ref attribute",innerex);
				}
				else if(reader.Name == "type")
				{
					Exception innerex;
					attribute.schemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
					if(innerex != null)
						error(h, reader.Value + " is not a valid value for type attribute",innerex);
				}
				else if(reader.Name == "use")
				{
					Exception innerex;
					attribute.use = XmlSchemaUtil.ReadUseAttribute(reader,out innerex);
					if(innerex != null)
						error(h, reader.Value + " is not a valid value for use attribute", innerex);
				}
				else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
				{
					error(h,reader.Name + " is not a valid attribute for attribute",null);
				}
				else
				{
					XmlSchemaUtil.ReadUnhandledAttribute(reader,attribute);
				}
			}
			
			reader.MoveToElement();
			if(reader.IsEmptyElement)
				return attribute;

			//  Content: (annotation?, (simpleType?))
			int level = 1;
			while(reader.ReadNextElement())
			{
				if(reader.NodeType == XmlNodeType.EndElement)
				{
					if(reader.LocalName != xmlname)
						error(h,"Should not happen :2: XmlSchemaAttribute.Read, name="+reader.Name,null);
					break;
				}
				if(level <= 1 && reader.LocalName == "annotation")
				{
					level = 2; //Only one annotation
					XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
					if(annotation != null)
						attribute.Annotation = annotation;
					continue;
				}
				if(level <=2 && reader.LocalName == "simpleType")
				{
					level = 3;
					XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
					if(stype != null)
						attribute.schemaType = stype;
					continue;
				}
				reader.RaiseInvalidElementError();
			}
			return attribute;
		}
		
	}
}