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

XmlSchemaUtil.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: 4b392d2bb9354135e01620d804d2c4c25b3733ed (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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700

//
// 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.Collections;
using System.Text;
using Mono.Xml;
using Mono.Xml.Schema;
using System.Xml.Serialization;

namespace System.Xml.Schema
{
	/// <summary>
	///  All Methods in this class should use XmlConvert. Some Methods are not present in the
	///  MS Implementation. We should provide them.
	/// </summary>
	internal class XmlSchemaUtil
	{
		static XmlSchemaUtil ()
		{
			FinalAllowed = XmlSchemaDerivationMethod.Restriction | 
				XmlSchemaDerivationMethod.Extension;
			ComplexTypeBlockAllowed = FinalAllowed;
			ElementBlockAllowed = XmlSchemaDerivationMethod.Substitution | 
				FinalAllowed;
		}

		internal static XmlSchemaDerivationMethod FinalAllowed;
		internal static XmlSchemaDerivationMethod ElementBlockAllowed;
		internal static XmlSchemaDerivationMethod ComplexTypeBlockAllowed;
		internal static readonly bool StrictMsCompliant = Environment.GetEnvironmentVariable ("MONO_STRICT_MS_COMPLIANT") == "yes";


		public static void AddToTable (XmlSchemaObjectTable table, XmlSchemaObject obj,
			XmlQualifiedName qname, ValidationEventHandler h)
		{
			if (table.Contains (qname)) {
				// FIXME: This logic unexpectedly allows 
				// one redefining item and two or more redefining items.
				// FIXME: redefining item is not simple replacement,
				// but much more complex stuff.
				if (obj.isRedefineChild) {	// take precedence.
					if (obj.redefinedObject != null)
						obj.error (h, String.Format ("Named item {0} was already contained in the schema object table.", qname));
					else
						obj.redefinedObject = table [qname];
					table.Set (qname, obj);
				}
				else if (table [qname].isRedefineChild) {
					if (table [qname].redefinedObject != null)
						obj.error (h, String.Format ("Named item {0} was already contained in the schema object table.", qname));
					else
						table [qname].redefinedObject = obj;
					return;	// never add to the table.
				}
				else if (StrictMsCompliant) {
					table.Set (qname, obj);
				}
				else
					obj.error (h, String.Format ("Named item {0} was already contained in the schema object table. {1}",
					                             qname, "Consider setting MONO_STRICT_MS_COMPLIANT to 'yes' to mimic MS implementation."));
			}
			else
				table.Set (qname, obj);
		}

		public static void CompileID (string id,  XmlSchemaObject xso, Hashtable idCollection, ValidationEventHandler h)
		{
			//check if the string conforms to http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/datatypes.html#ID
			// 1. ID must be a NCName
			// 2. ID must be unique in the schema
			if(id == null)
				return;
			if(!CheckNCName(id)) 
				xso.error(h,id+" is not a valid id attribute");
			else if(idCollection.ContainsKey(id))
				xso.error(h,"Duplicate id attribute "+id);
			else
				idCollection.Add(id,xso);
		}

		public static bool CheckAnyUri (string uri)
		{
			if (uri.StartsWith ("##"))
				return false;
			return true;
		}

		public static bool CheckNormalizedString (string token)
		{
			return true;
		}

		public static bool CheckNCName (string name)
		{
			//check if the string conforms to http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/datatypes.html#NCName
			return XmlChar.IsNCName (name);
		}

		public static bool CheckQName (XmlQualifiedName qname)
		{
			// What is this doing?
			return true;
		}

		public static XmlParserContext GetParserContext (XmlReader reader)
		{
			IHasXmlParserContext xctx = reader as IHasXmlParserContext;
			if (xctx != null)
				return xctx.ParserContext;

			return null;
		}

		public static bool IsBuiltInDatatypeName (XmlQualifiedName qname)
		{
			if (qname.Namespace == XmlSchema.XdtNamespace) {
				switch (qname.Name) {
				case "anyAtomicType":
				case "untypedAtomic":
				case "dayTimeDuration":
				case "yearMonthDuration":
					return true;
				default:
					return false;
				}
			}
			if (qname.Namespace != XmlSchema.Namespace)
				return false;
			switch (qname.Name) {
			case "anySimpleType":
			case "duration": case "dateTime": case "time":
			case "date": case "gYearMonth": case "gYear":
			case "gMonthDay": case "gDay": case "gMonth":
			case "boolean":
			case "base64Binary": case "hexBinary":
			case "float": case "double":
			case "anyURI":
			case "QName":
			case "NOTATION":
			case "string": case "normalizedString": case "token":
			case "language": case "Name": case "NCName":
			case "ID": case "IDREF": case "IDREFS":
			case "ENTITY": case "ENTITIES":
			case "NMTOKEN": case "NMTOKENS":
			case "decimal": case "integer":
			case "nonPositiveInteger": case "negativeInteger":
			case "nonNegativeInteger":
			case "unsignedLong": case "unsignedInt":
			case "unsignedShort": case "unsignedByte":
			case "positiveInteger":
			case "long": case "int": case "short": case "byte":
				return true;
			}
			return false;
		}

		public static bool AreSchemaDatatypeEqual (XmlSchemaSimpleType st1, object v1, XmlSchemaSimpleType st2, object v2)
		{
			if (st1.Datatype is XsdAnySimpleType)
				return AreSchemaDatatypeEqual (st1.Datatype as XsdAnySimpleType, v1, st2.Datatype as XsdAnySimpleType, v2);
			// otherwise the types are lists of strings.
			string [] a1 = v1 as string [];
			string [] a2 = v2 as string [];
			if (st1 != st2 || a1 == null || a2 == null || a1.Length != a2.Length)
				return false;
			for (int i = 0; i < a1.Length; i++)
				if (a1 [i] != a2 [i])
					return false;
			return true;
		}

		public static bool AreSchemaDatatypeEqual (XsdAnySimpleType st1, object v1,
			XsdAnySimpleType st2, object v2)
		{
			if (v1 == null || v2 == null)
				return false;

			if (st1 == null)
				st1 = XmlSchemaSimpleType.AnySimpleType;
			if (st2 == null)
				st2 = XmlSchemaSimpleType.AnySimpleType;

			Type t = st2.GetType ();
			if (st1 is XsdFloat) {
				return st2 is XsdFloat && Convert.ToSingle (v1) == Convert.ToSingle (v2);
			} else if (st1 is XsdDouble) {
				return st2 is XsdDouble && Convert.ToDouble (v1) == Convert.ToDouble (v2);
			} else if (st1 is XsdDecimal) {
				if (!(st2 is XsdDecimal) || Convert.ToDecimal (v1) != Convert.ToDecimal (v2))
					return false;
				if (st1 is XsdNonPositiveInteger)
					return st2 is XsdNonPositiveInteger || t == typeof (XsdDecimal) || t == typeof (XsdInteger);
				else if (st1 is XsdPositiveInteger)
					return st2 is XsdPositiveInteger || t == typeof (XsdDecimal) || 
						t == typeof (XsdInteger) || t == typeof (XsdNonNegativeInteger);
				else if (st1 is XsdUnsignedLong)
					return st2 is XsdUnsignedLong || t == typeof (XsdDecimal) || 
						t == typeof (XsdInteger) || t == typeof (XsdNonNegativeInteger);
				else if (st1 is XsdNonNegativeInteger)
					return st2 is XsdNonNegativeInteger || t == typeof (XsdDecimal) || t == typeof (XsdInteger);
				else if (st1 is XsdLong)
					return st2 is XsdLong || t == typeof (XsdDecimal) || t == typeof (XsdInteger);
				return true;
			}
			else if (!v1.Equals (v2))
				return false;
			if (st1 is XsdString) {
				if (!(st2 is XsdString))
					return false;
				if (st1 is XsdNMToken && (st2 is XsdLanguage || st2 is XsdName))
					return false;
				if (st2 is XsdNMToken && (st1 is XsdLanguage || st1 is XsdName))
					return false;
				if (st1 is XsdName && (st2 is XsdLanguage || st2 is XsdNMToken))
					return false;
				if (st2 is XsdName && (st1 is XsdLanguage || st1 is XsdNMToken))
					return false;
				if (st1 is XsdID && st2 is XsdIDRef)
					return false;
				if (st1 is XsdIDRef && st2 is XsdID)
					return false;
			}
			else if (st1 != st2)
				return false;
			return true;
		}

		public static bool IsValidQName(string qname)
		{
			foreach(string part in qname.Split(new char[]{':'},2))
			{
				if(!CheckNCName(part))
					return false;
			}
			return true;
		}

		//FIXME: First remove all the multiple instances of whitespace and then return the strings.
		//The current method returns empty strings if there are two or more consecutive whitespaces.
		public static string[] SplitList(string list)
		{
			if(list == null || list == string.Empty)
				return new string [0];

			ArrayList al = null;
			int start = 0;
			bool wait = true;
			for (int i = 0; i < list.Length; i++) {
				switch (list [i]) {
				case ' ':
				case '\r':
				case '\n':
				case '\t':
					if (!wait) {
						if (al == null)
							al = new ArrayList ();
						al.Add (list.Substring (start, i - start));
					}
					wait = true;
					break;
				default:
					if (wait) {
						wait = false;
						start = i;
					}
					break;
				}
			}

			if (!wait && start == 0)
				return new string [] {list};

			if (!wait && start < list.Length)
				al.Add (start == 0 ? list : list.Substring (start));
			return al.ToArray (typeof (string)) as string [];
		}

		public static void ReadUnhandledAttribute(XmlReader reader, XmlSchemaObject xso)
		{
			if(reader.Prefix == "xmlns")
				xso.Namespaces.Add(reader.LocalName, reader.Value);
			else if(reader.Name == "xmlns")
				xso.Namespaces.Add("",reader.Value);
			else
			{
				if(xso.unhandledAttributeList == null)
					xso.unhandledAttributeList = new System.Collections.ArrayList();
				XmlAttribute attr = new XmlDocument().CreateAttribute(reader.LocalName,reader.NamespaceURI);
				attr.Value = reader.Value;
				ParseWsdlArrayType (reader, attr);
				xso.unhandledAttributeList.Add(attr);
			}
		}
		
		static void ParseWsdlArrayType (XmlReader reader, XmlAttribute attr)
		{
			if (attr.NamespaceURI == XmlSerializer.WsdlNamespace && attr.LocalName == "arrayType")
			{
				string ns = "", type, dimensions;
				TypeTranslator.ParseArrayType (attr.Value, out type, out ns, out dimensions);
				if (ns != "") ns = reader.LookupNamespace (ns) + ":";
				attr.Value = ns + type + dimensions;
			}
		}

		public static bool ReadBoolAttribute(XmlReader reader, out Exception innerExcpetion)
		{
			innerExcpetion = null;
			try
			{
				bool val = XmlConvert.ToBoolean(reader.Value);
				return val;
			}
			catch(Exception ex)
			{
				innerExcpetion = ex;
				return false;
			}
		}
		public static decimal ReadDecimalAttribute(XmlReader reader,  out Exception innerExcpetion)
		{
			innerExcpetion = null;
			try
			{
				decimal val = XmlConvert.ToDecimal(reader.Value);
				return val;
			}
			catch(Exception ex)
			{
				innerExcpetion = ex;
				return decimal.Zero;
			}
		}

		// Is some value is read, return it.
		// If no values return empty.
		// If exception, return none
		public static XmlSchemaDerivationMethod ReadDerivationAttribute(XmlReader reader, out Exception innerExcpetion, string name, XmlSchemaDerivationMethod allowed)
		{
			innerExcpetion = null;
			try
			{
				string list = reader.Value;
				string warn = "";
				XmlSchemaDerivationMethod val = 0;
				
				if(list.IndexOf("#all") != -1 && list.Trim() != "#all")
				{
					innerExcpetion = new Exception(list+" is not a valid value for "+ name +". #all if present must be the only value");
					return XmlSchemaDerivationMethod.All;
				}
				foreach(string xsdm in XmlSchemaUtil.SplitList(list))
				{
					switch(xsdm)
					{
						case "":
							val = AddFlag (val, XmlSchemaDerivationMethod.Empty, allowed); break;
						case "#all":
							val = AddFlag (val,XmlSchemaDerivationMethod.All, allowed); break;
						case "substitution":
							val = AddFlag (val,XmlSchemaDerivationMethod.Substitution, allowed); break;
						case "extension":
							val = AddFlag (val,XmlSchemaDerivationMethod.Extension, allowed); break;
						case "restriction":
							val = AddFlag (val,XmlSchemaDerivationMethod.Restriction, allowed); break;
						case "list":
							val = AddFlag (val,XmlSchemaDerivationMethod.List, allowed); break;
						case "union":
							val = AddFlag (val,XmlSchemaDerivationMethod.Union, allowed); break;
						default:
							warn += xsdm + " "; break;
					}
				}
				if(warn != "")
						innerExcpetion = new Exception(warn + "is/are not valid values for " + name);
				return val;
			}
			catch(Exception ex)
			{
				innerExcpetion = ex;
				return XmlSchemaDerivationMethod.None;
			}
		}

		private static XmlSchemaDerivationMethod AddFlag (XmlSchemaDerivationMethod dst,
			XmlSchemaDerivationMethod add, XmlSchemaDerivationMethod allowed)
		{
			if ((add & allowed) == 0 && allowed != XmlSchemaDerivationMethod.All)
				throw new ArgumentException (add + " is not allowed in this attribute.");
			if ((dst & add) != 0)
				throw new ArgumentException (add + " is already specified in this attribute.");
			return dst | add;
		}

		public static XmlSchemaForm ReadFormAttribute(XmlReader reader, out Exception innerExcpetion)
		{
			innerExcpetion = null;
			XmlSchemaForm val = XmlSchemaForm.None;
			switch(reader.Value)
			{
				case "qualified":
					val = XmlSchemaForm.Qualified; break;
				case "unqualified":
					val = XmlSchemaForm.Unqualified; break;
				default:
					innerExcpetion = new Exception("only qualified or unqulified is a valid value"); break;
			}
			return val;
		}

		public static XmlSchemaContentProcessing ReadProcessingAttribute(XmlReader reader, out Exception innerExcpetion)
		{
			innerExcpetion = null;
			XmlSchemaContentProcessing val = XmlSchemaContentProcessing.None;
			switch(reader.Value)
			{
				case "lax":
					val = XmlSchemaContentProcessing.Lax; break;
				case "strict":
					val = XmlSchemaContentProcessing.Strict; break;
				case "skip":
					val = XmlSchemaContentProcessing.Skip; break;
				default:
					innerExcpetion = new Exception("only lax , strict or skip are valid values for processContents");
					break;
			}
			return val;
		}

		public static XmlSchemaUse ReadUseAttribute(XmlReader reader, out Exception innerExcpetion)
		{
			innerExcpetion = null;
			XmlSchemaUse val = XmlSchemaUse.None;
			switch(reader.Value)
			{
				case "optional":
					val = XmlSchemaUse.Optional; break;
				case "prohibited":
					val = XmlSchemaUse.Prohibited; break;
				case "required":
					val = XmlSchemaUse.Required; break;
				default:
					innerExcpetion = new Exception("only optional , prohibited or required are valid values for use");
					break;
			}
			return val;
		}
		public static XmlQualifiedName ReadQNameAttribute(XmlReader reader, out Exception innerEx)
		{
			return ToQName(reader, reader.Value, out innerEx);
		}

		//While Creating a XmlQualifedName, we should check:
		// 1. If a prefix is present, its namespace should be resolvable.
		// 2. If a prefix is not present, and if the defaultNamespace is set, 
		public static XmlQualifiedName ToQName(XmlReader reader, string qnamestr, out Exception innerEx)
		{

			string ns;
			string name;
			XmlQualifiedName qname;
			innerEx = null;
			
			if(!IsValidQName(qnamestr))
			{
				innerEx = new Exception(qnamestr + " is an invalid QName. Either name or namespace is not a NCName");
				return XmlQualifiedName.Empty;
			}

			string[] values = qnamestr.Split(new char[]{':'},2);

			if(values.Length == 2)
			{
				ns = reader.LookupNamespace(values[0]);
				if(ns == null)
				{
					innerEx = new Exception("Namespace Prefix '"+values[0]+"could not be resolved");
					return XmlQualifiedName.Empty;
				}
				name = values[1];
			}
			else
			{
				//Default Namespace
				ns = reader.LookupNamespace("");
				name = values[0];
			}

			qname = new XmlQualifiedName(name,ns);
			return qname;
		}

		public static int ValidateAttributesResolved (
			XmlSchemaObjectTable attributesResolved,
			ValidationEventHandler h,
			XmlSchema schema,
			XmlSchemaObjectCollection attributes,
			XmlSchemaAnyAttribute anyAttribute,
			ref XmlSchemaAnyAttribute anyAttributeUse,
			XmlSchemaAttributeGroup redefined,
			bool skipEquivalent)
		{
			int errorCount = 0;
			if (anyAttribute != null && anyAttributeUse == null)
				anyAttributeUse = anyAttribute;

			ArrayList newAttrNames = new ArrayList ();

			foreach (XmlSchemaObject xsobj in attributes) {
				XmlSchemaAttributeGroupRef grpRef = xsobj as XmlSchemaAttributeGroupRef;
				if (grpRef != null) {
					// Resolve attributeGroup redefinition.
					XmlSchemaAttributeGroup grp = null;
					if (redefined != null && grpRef.RefName == redefined.QualifiedName)
						grp = redefined;
					else
						grp = schema.FindAttributeGroup (grpRef.RefName);
					// otherwise, it might be missing sub components.
					if (grp == null) {
						if (!schema.missedSubComponents)// && schema.Schemas [grpRef.RefName.Namespace] != null)
							grpRef.error (h, "Referenced attribute group " + grpRef.RefName + " was not found in the corresponding schema.");
						continue;
					}
					if (grp.AttributeGroupRecursionCheck) {
						grp.error (h, "Attribute group recursion was found: " + grpRef.RefName);
						continue;
					}
					try {
						grp.AttributeGroupRecursionCheck = true;
						errorCount += grp.Validate (h, schema);
					} finally {
						grp.AttributeGroupRecursionCheck = false;
					}
					if (grp.AnyAttributeUse != null) {
						if (anyAttribute == null)
							anyAttributeUse = grp.AnyAttributeUse;
					}
					foreach (DictionaryEntry entry in grp.AttributeUses) {
						XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;

						if (StrictMsCompliant && attr.Use == XmlSchemaUse.Prohibited)
							continue;

						if (attr.RefName != null && attr.RefName != XmlQualifiedName.Empty && (!skipEquivalent || !AreAttributesEqual (attr, attributesResolved [attr.RefName] as XmlSchemaAttribute)))
							AddToTable (attributesResolved, attr, attr.RefName, h);
						else if (!skipEquivalent || !AreAttributesEqual (attr, attributesResolved [attr.QualifiedName] as XmlSchemaAttribute))
							AddToTable (attributesResolved, attr, attr.QualifiedName, h);
					}
				} else {
					XmlSchemaAttribute attr = xsobj as XmlSchemaAttribute;
					if (attr != null) {
						errorCount += attr.Validate (h, schema);

						if (newAttrNames.Contains (attr.QualifiedName))
							attr.error (h, String.Format ("Duplicate attributes was found for '{0}'", attr.QualifiedName));
						newAttrNames.Add (attr.QualifiedName);


						if (StrictMsCompliant && attr.Use == XmlSchemaUse.Prohibited)
							continue;

						if (attr.RefName != null && attr.RefName != XmlQualifiedName.Empty && (!skipEquivalent || !AreAttributesEqual (attr, attributesResolved [attr.RefName] as XmlSchemaAttribute)))
							AddToTable (attributesResolved, attr, attr.RefName, h);
						else if (!skipEquivalent || !AreAttributesEqual (attr, attributesResolved [attr.QualifiedName] as XmlSchemaAttribute))
							AddToTable (attributesResolved, attr, attr.QualifiedName, h);
					} else {
						if (anyAttribute == null) {
							anyAttributeUse = (XmlSchemaAnyAttribute) xsobj;
							anyAttribute.Validate (h, schema);
						}
					}
				}
			}
			return errorCount;
		}

		internal static bool AreAttributesEqual (XmlSchemaAttribute one,
			XmlSchemaAttribute another)
		{
			if (one == null || another == null)
				return false;
			return one.AttributeType == another.AttributeType &&
				one.Form == another.Form &&
				one.ValidatedUse == another.ValidatedUse &&
				one.ValidatedDefaultValue == another.ValidatedDefaultValue &&
				one.ValidatedFixedValue == another.ValidatedFixedValue;
		}

#if NET_2_0
		public static object ReadTypedValue (XmlReader reader,
			object type, IXmlNamespaceResolver nsResolver,
			StringBuilder tmpBuilder)
#else
		public static object ReadTypedValue (XmlReader reader,
			object type, XmlNamespaceManager nsResolver,
			StringBuilder tmpBuilder)
#endif
		{
			if (tmpBuilder == null)
				tmpBuilder = new StringBuilder ();
			XmlSchemaDatatype dt = type as XmlSchemaDatatype;
			XmlSchemaSimpleType st = type as XmlSchemaSimpleType;
			if (st != null)
				dt = st.Datatype;
			if (dt == null)
				return null;

			switch (reader.NodeType) {
			case XmlNodeType.Element:
				if (reader.IsEmptyElement)
					return null;

				tmpBuilder.Length = 0;
				bool loop = true;
				do {
					reader.Read ();
					switch (reader.NodeType) {
					case XmlNodeType.SignificantWhitespace:
					case XmlNodeType.Text:
					case XmlNodeType.CDATA:
						tmpBuilder.Append (reader.Value);
						break;
					case XmlNodeType.Comment:
						break;
					default:
						loop = false;
						break;
					}
				} while (loop && !reader.EOF && reader.ReadState == ReadState.Interactive);
				return dt.ParseValue (tmpBuilder.ToString (), reader.NameTable, nsResolver);
			case XmlNodeType.Attribute:
				return dt.ParseValue (reader.Value, reader.NameTable, nsResolver);
			}
			return null;
		}

		public static XmlSchemaObject FindAttributeDeclaration (
			string ns,
			XmlSchemaSet schemas,
			XmlSchemaComplexType cType,
			XmlQualifiedName qname)
		{
			XmlSchemaObject result = cType.AttributeUses [qname];
			if (result != null)
				return result;
			if (cType.AttributeWildcard == null)
				return null;

			if (!AttributeWildcardItemValid (cType.AttributeWildcard, qname, ns))
				return null;

			if (cType.AttributeWildcard.ResolvedProcessContents == XmlSchemaContentProcessing.Skip)
				return cType.AttributeWildcard;
			XmlSchemaAttribute attr = schemas.GlobalAttributes [qname] as XmlSchemaAttribute;
			if (attr != null)
				return attr;
			if (cType.AttributeWildcard.ResolvedProcessContents == XmlSchemaContentProcessing.Lax)
				return cType.AttributeWildcard;
			else
				return null;
		}

		// Spec 3.10.4 Item Valid (Wildcard)
		private static bool AttributeWildcardItemValid (XmlSchemaAnyAttribute anyAttr, XmlQualifiedName qname, string ns)
		{
			if (anyAttr.HasValueAny)
				return true;
			if (anyAttr.HasValueOther && (anyAttr.TargetNamespace == "" || ns != anyAttr.TargetNamespace))
				return true;
			if (anyAttr.HasValueTargetNamespace && ns == anyAttr.TargetNamespace)
				return true;
			if (anyAttr.HasValueLocal && ns == "")
				return true;
			for (int i = 0; i < anyAttr.ResolvedNamespaces.Count; i++)
				if (anyAttr.ResolvedNamespaces [i] == ns)
					return true;
			return false;
		}
	}
}