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

XmlSchemaSimpleTypeRestriction.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: 0c6d4f07dcbfd6aa7727ccc53670abf8ca86617c (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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
// Author: Dwivedi, Ajay kumar
//            Adwiv@Yahoo.com

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

#if NET_2_0
using NSResolver = System.Xml.IXmlNamespaceResolver;
#else
using NSResolver = System.Xml.XmlNamespaceManager;
#endif

namespace System.Xml.Schema
{
	/// <summary>
	/// Summary description for XmlSchemaSimpleTypeRestriction.
	/// </summary>
	public class XmlSchemaSimpleTypeRestriction : XmlSchemaSimpleTypeContent
	{
		private XmlSchemaSimpleType baseType;
		private XmlQualifiedName baseTypeName;
		private XmlSchemaObjectCollection facets;
		const string xmlname = "restriction";
		private string [] enumarationFacetValues;
		private string [] patternFacetValues;
		private Regex [] rexPatterns;
		private decimal lengthFacet;
		private decimal maxLengthFacet;
		private decimal minLengthFacet;
		private decimal fractionDigitsFacet;
		private decimal totalDigitsFacet;
		private object maxInclusiveFacet ;
		private object maxExclusiveFacet ;
		private object minInclusiveFacet ;
		private object minExclusiveFacet ;
		private XmlSchemaFacet.Facet fixedFacets = XmlSchemaFacet.Facet.None; 
		private static NumberStyles lengthStyle = NumberStyles.Integer;


		public XmlSchemaSimpleTypeRestriction()
		{
			baseTypeName = XmlQualifiedName.Empty;
			facets = new XmlSchemaObjectCollection();
		}

		[System.Xml.Serialization.XmlAttribute("base")]
		public XmlQualifiedName BaseTypeName 
		{
			get{ return  baseTypeName; } 
			set{ baseTypeName = value; }
		}

		[XmlElement("simpleType", Type=typeof (XmlSchemaSimpleType))]
		public XmlSchemaSimpleType BaseType 
		{
			get{ return  baseType; } 
			set{ baseType = value; }
		}

		[XmlElement("minExclusive",typeof(XmlSchemaMinExclusiveFacet))]
		[XmlElement("minInclusive",typeof(XmlSchemaMinInclusiveFacet))] 
		[XmlElement("maxExclusive",typeof(XmlSchemaMaxExclusiveFacet))]
		[XmlElement("maxInclusive",typeof(XmlSchemaMaxInclusiveFacet))]
		[XmlElement("totalDigits",typeof(XmlSchemaTotalDigitsFacet))]
		[XmlElement("fractionDigits",typeof(XmlSchemaFractionDigitsFacet))]
		[XmlElement("length",typeof(XmlSchemaLengthFacet))]
		[XmlElement("minLength",typeof(XmlSchemaMinLengthFacet))]
		[XmlElement("maxLength",typeof(XmlSchemaMaxLengthFacet))]
		[XmlElement("enumeration",typeof(XmlSchemaEnumerationFacet))]
		[XmlElement("whiteSpace",typeof(XmlSchemaWhiteSpaceFacet))]
		[XmlElement("pattern",typeof(XmlSchemaPatternFacet))]
		public XmlSchemaObjectCollection Facets 
		{
			get{ return facets; }
		}

		internal override void SetParent (XmlSchemaObject parent)
		{
			base.SetParent (parent);
			if (BaseType != null)
				BaseType.SetParent (this);
			foreach (XmlSchemaObject obj in Facets)
				obj.SetParent (this);
		}

		/// <remarks>
		/// 1. One of base or simpletype must be present but not both
		/// 2. id must be a valid ID
		/// 3. base must be a valid QName *NO CHECK REQUIRED*
		/// </remarks>
		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
		{
			// If this is already compiled this time, simply skip.
			if (CompilationId == schema.CompilationId)
				return 0;

			errorCount = 0;

			if(this.baseType != null && !this.BaseTypeName.IsEmpty)
				error(h, "both base and simpletype can't be set");
			if(this.baseType == null && this.BaseTypeName.IsEmpty)
				error(h, "one of basetype or simpletype must be present");
			if(this.baseType != null)
			{
				errorCount += this.baseType.Compile(h,schema);
			}
			if(!XmlSchemaUtil.CheckQName(BaseTypeName))
				error(h,"BaseTypeName must be a XmlQualifiedName");

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

			for (int i = 0; i < this.Facets.Count; i++)
				if (! (Facets [i] is XmlSchemaFacet))
					error (h, "Only XmlSchemaFacet objects are allowed for Facets property");
			this.CompilationId = schema.CompilationId;
			return errorCount;
		}
		
		/** Checks if this facet is valid on this restriction. Does not check that it has
			* not been fixed in the baseType. That is done elsewhere.
			*/
		
		private static readonly XmlSchemaFacet.Facet listFacets =
						 XmlSchemaFacet.Facet.length | XmlSchemaFacet.Facet.minLength |
						 XmlSchemaFacet.Facet.maxLength | XmlSchemaFacet.Facet.pattern | 
						 XmlSchemaFacet.Facet.enumeration | XmlSchemaFacet.Facet.whiteSpace; 
 
		
		private bool IsAllowedFacet(XmlSchemaFacet xsf) {
		/* Must be called after this.ValidateActualType, as it uses actualBaseSchemaType */

			XsdAnySimpleType ast = ActualBaseSchemaType as XsdAnySimpleType;
			if (ast != null) {
				// Based directly on an xsd type 
				return ast.AllowsFacet(xsf);
			}
			else {
			 XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType)ActualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
			 if (st != null) {
				 XmlSchemaSimpleTypeRestriction str = st as XmlSchemaSimpleTypeRestriction;
				 if (str != null && str != this) {
					 return str.IsAllowedFacet(xsf);
				 }
				 XmlSchemaSimpleTypeList stl = st as XmlSchemaSimpleTypeList;
				 if (stl != null) {
					 return ((xsf.ThisFacet & listFacets) != 0);
				 }

				 XmlSchemaSimpleTypeUnion stu = st as XmlSchemaSimpleTypeUnion;
				 if (stu != null) {
					 return (xsf is XmlSchemaPatternFacet ||
									 xsf is XmlSchemaEnumerationFacet);
				 }
				 
			 }
			 else {
				 // TODO: Should this be either a XmlSchemaSimpleType or XmlSchemaDatatype ?
				 // If so report error
			 }
			}
			// Not sure it could ever get here
			return false;
		}
		
		
	
		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
		{
			
			if (IsValidated (schema.ValidationId))
				return errorCount;

			this.ValidateActualType (h, schema);

			
			lengthFacet = maxLengthFacet = minLengthFacet = fractionDigitsFacet = totalDigitsFacet = -1;
			
			XmlSchemaSimpleTypeRestriction baseSTR = null; 

			if (ActualBaseSchemaType is XmlSchemaSimpleType) {
				XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType)ActualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
				baseSTR = st as XmlSchemaSimpleTypeRestriction;
			}
			
				
			if (baseSTR != null) {
				fixedFacets = baseSTR.fixedFacets;
				lengthFacet = baseSTR.lengthFacet;
				maxLengthFacet = baseSTR.maxLengthFacet;
				minLengthFacet = baseSTR.minLengthFacet;
				fractionDigitsFacet = baseSTR.fractionDigitsFacet;
				totalDigitsFacet = baseSTR.totalDigitsFacet;
				maxInclusiveFacet = baseSTR.maxInclusiveFacet;
				maxExclusiveFacet = baseSTR.maxExclusiveFacet;
				minInclusiveFacet = baseSTR.minInclusiveFacet;
				minExclusiveFacet = baseSTR.minExclusiveFacet;
			}
			
			enumarationFacetValues = patternFacetValues = null;
			rexPatterns = null;
			
			XmlSchemaFacet.Facet facetsDefined = XmlSchemaFacet.Facet.None; 

			ArrayList enums = null;
			ArrayList patterns = null;
			for (int i = 0; i < facets.Count; i++) {

				XmlSchemaFacet facet = facets[i] as XmlSchemaFacet;
				if (facet != null) {
					if (!IsAllowedFacet(facet)) {
						facet.error(h, facet.ThisFacet +" is not a valid facet for this type");
						continue;
					}
				}
				else {
					// Other than XmlSchemaFacet; already reported at Compile()
					continue;
				}

				
				XmlSchemaEnumerationFacet ef = facets [i] as XmlSchemaEnumerationFacet;
				if (ef != null) {
					if (enums == null)
						enums = new ArrayList ();
					enums.Add (ef.Value);
					continue;
				}
				XmlSchemaPatternFacet pf = facets [i] as XmlSchemaPatternFacet;
				if (pf != null) {
					if (patterns == null)
						patterns = new ArrayList ();
					patterns.Add (pf.Value);
					continue;
				}
				
				// Put this test here, as pattern and enumeration 
				// can occur multiple times.
				if ( (facetsDefined & facet.ThisFacet) !=0) {
					facet.error (h, "This is a duplicate '" + facet.ThisFacet + "' facet.");
					continue;
				}
				else {
					facetsDefined |= facet.ThisFacet; 
				}

				
				

				
				if (facet is XmlSchemaLengthFacet) {
					checkLengthFacet((XmlSchemaLengthFacet)facet, facetsDefined, h);
				}
				else if (facet is XmlSchemaMaxLengthFacet) {
					checkMaxLengthFacet((XmlSchemaMaxLengthFacet)facet, facetsDefined, h);
				}
				else if (facet is XmlSchemaMinLengthFacet) {
					checkMinLengthFacet((XmlSchemaMinLengthFacet)facet, facetsDefined, h);
				}
				
				else if (facet is XmlSchemaMinInclusiveFacet) {
					checkMinMaxFacet((XmlSchemaMinInclusiveFacet)facet, ref minInclusiveFacet, h);
				}
				else if (facet is XmlSchemaMaxInclusiveFacet) {
					checkMinMaxFacet((XmlSchemaMaxInclusiveFacet)facet, ref maxInclusiveFacet, h);
				}
				else if (facet is XmlSchemaMinExclusiveFacet) {
					checkMinMaxFacet((XmlSchemaMinExclusiveFacet)facet, ref minExclusiveFacet, h);
				}
				else if (facet is XmlSchemaMaxExclusiveFacet) {
					checkMinMaxFacet((XmlSchemaMaxExclusiveFacet)facet, ref maxExclusiveFacet, h);
				}
				else if (facet is XmlSchemaFractionDigitsFacet) {
					checkFractionDigitsFacet((XmlSchemaFractionDigitsFacet)facet, h);
				}
				else if (facet is XmlSchemaTotalDigitsFacet) {
					checkTotalDigitsFacet((XmlSchemaTotalDigitsFacet)facet, h);
				}

				if (facet.IsFixed) {
					fixedFacets |= facet.ThisFacet;
				}
				
			 
			}
			if (enums != null)
				this.enumarationFacetValues = enums.ToArray (typeof (string)) as string [];
			if (patterns != null) {
				this.patternFacetValues = patterns.ToArray (typeof (string)) as string [];
				this.rexPatterns = new Regex [patterns.Count];
				for (int i = 0; i < patternFacetValues.Length; i++) {
					try {
						string src = patternFacetValues [i];
						StringBuilder sb = null;
						int start = 0;
						for (int c = 0; c < src.Length; c++) {
							if (src [c] == '\\' && src.Length > i + 1) {
								string subst = null;
								switch (src [c + 1]) {
								case 'i':
									subst = "[\\p{L}_]";
									break;
								case 'I':
									subst = "[^\\p{L}_]";
									break;
								case 'c':
									subst = "[\\p{L}\\p{N}_\\.\\-:]";
									break;
								case 'C':
									subst = "[^\\p{L}\\p{N}_\\.\\-:]";
									break;
								}
								if (subst != null) {
									if (sb == null)
										sb = new StringBuilder ();
									sb.Append (src, start, c - start);
									sb.Append (subst);
									start = c + 2;
								}
							}
						}
						if (sb != null) {
							sb.Append (src, start, src.Length - start);
							src = sb.ToString ();
						}
//						src = src.Replace ("\\i", "[\\p{L}_]").Replace ("\\I", "[^\\p{L}_]").Replace ("\\c", "[\\p{L}\\p{N}_\\.\\-:]").Replace ("\\C", "[^\\p{L}\\p{N}_\\.\\-:]");
						Regex rex = new Regex ("^" + src + "$");
						rexPatterns [i] = rex;
					} catch (Exception ex) {
						error (h, "Invalid regular expression pattern was specified.", ex);
					}
				}
				
				
			
			}

			ValidationId = schema.ValidationId;
		 /* 
				Console.WriteLine("Facets:\n defined\t{10}\n fixed\t{0}\n length\t{1}\n maxLen\t{2}\n minLen\t{3}\n " +
													"frac\t{4}\n tot\t{5}\n maxI\t{6}\n maxE\t{7}\n minI\t{8}\n minE\t{9}\n", 
						fixedFacets , 
						lengthFacet, 
						maxLengthFacet ,
						minLengthFacet ,
						fractionDigitsFacet ,
						totalDigitsFacet ,
						maxInclusiveFacet ,
						maxExclusiveFacet ,
						minInclusiveFacet ,
						minExclusiveFacet , 
						facetsDefined);
*/
			return errorCount;
		}


		internal void ValidateActualType (ValidationEventHandler h, XmlSchema schema)
		{
			GetActualType (h, schema, true);
		}

		internal object GetActualType (ValidationEventHandler h, XmlSchema schema, bool validate)
		{
			object actualBaseSchemaType = null;

			XmlSchemaSimpleType type = baseType;
			if (type == null)
				type = schema.FindSchemaType (baseTypeName) as XmlSchemaSimpleType;
			if (type != null) {
				if (validate)
					errorCount += type.Validate (h, schema);
				actualBaseSchemaType = type;
			} else if (baseTypeName == XmlSchemaComplexType.AnyTypeName) {
				actualBaseSchemaType = XmlSchemaSimpleType.AnySimpleType;
			} else if (baseTypeName.Namespace == XmlSchema.Namespace ||
				baseTypeName.Namespace == XmlSchema.XdtNamespace) {
				actualBaseSchemaType = XmlSchemaDatatype.FromName (baseTypeName);
				if (actualBaseSchemaType == null)
					if (validate)
						error (h, "Invalid schema type name was specified: " + baseTypeName);
			}
			// otherwise, it might be missing sub components.
			else if (!schema.IsNamespaceAbsent (baseTypeName.Namespace))
				if (validate)
					error (h, "Referenced base schema type " + baseTypeName + " was not found in the corresponding schema.");

			return actualBaseSchemaType;
		}

		
		private void checkTotalDigitsFacet (XmlSchemaTotalDigitsFacet totf, 
																				ValidationEventHandler h) {
			if (totf != null) {
			/* totalDigits is the maximum number of digits in values of datatypes
			 * derived from decimal. The value of totalDigits must be a
			 * positiveInteger. */
				try {
					decimal newTotalDigits = decimal.Parse (totf.Value.Trim (), lengthStyle, CultureInfo.InvariantCulture);
					if (newTotalDigits <= 0) 
						totf.error(h, String.Format(CultureInfo.InvariantCulture, "The value '{0}' is an invalid totalDigits value", newTotalDigits));
					// Valid restriction
					if ((totalDigitsFacet > 0) && (newTotalDigits > totalDigitsFacet)) {
						totf.error(h, String.Format(CultureInfo.InvariantCulture, "The value '{0}' is not a valid restriction of the base totalDigits facet '{1}'", newTotalDigits, totalDigitsFacet));
					}
					totalDigitsFacet = newTotalDigits;
				}
				catch (FormatException ) {
					totf.error(h, String.Format("The value '{0}' is an invalid totalDigits facet specification", totf.Value.Trim () ));
				}
			}
		}

		
		private void checkFractionDigitsFacet (XmlSchemaFractionDigitsFacet fracf, 
																					 ValidationEventHandler h) {

			if (fracf != null) {
				try {
					decimal newFractionDigits = decimal.Parse (fracf.Value.Trim (), lengthStyle, CultureInfo.InvariantCulture);
					if (newFractionDigits< 0) 
						fracf.error(h, String.Format(CultureInfo.InvariantCulture, "The value '{0}' is an invalid fractionDigits value", newFractionDigits));
					
					if ((fractionDigitsFacet >= 0) && (newFractionDigits > fractionDigitsFacet)) {
						fracf.error(h, String.Format(CultureInfo.InvariantCulture, "The value '{0}' is not a valid restriction of the base fractionDigits facet '{1}'", newFractionDigits, fractionDigitsFacet));
					}
					fractionDigitsFacet = newFractionDigits;
				}
				catch (FormatException ) {
					fracf.error(h, String.Format("The value '{0}' is an invalid fractionDigits facet specification", fracf.Value.Trim () ));
				}
			}

		}
 
		
		private void checkMinMaxFacet(XmlSchemaFacet facet, 
																		ref object baseFacet,
																		ValidationEventHandler h) { 
// Is it a valid instance of the base type.
		 object newValue = ValidateValueWithDatatype(facet.Value);
		 if (newValue != null) {
// Is the base fixed - if so is it the same
			 if (((fixedFacets & facet.ThisFacet) != 0)  && (baseFacet != null)){
				 XsdAnySimpleType dt = getDatatype();
				 if (dt.Compare (newValue, baseFacet) != XsdOrdering.Equal) {
					 facet.error (h, 
							 String.Format(CultureInfo.InvariantCulture, "{0} is not the same as fixed parent {1} facet.", 
										 facet.Value, facet.ThisFacet));
				 }
			 }
			 baseFacet = newValue;
		 }
		 else {
			 facet.error(h, 
					 String.Format("The value '{0}' is not valid against the base type.", facet.Value));
		 }
		}
		
		

		private void checkLengthFacet(XmlSchemaLengthFacet lf,	
																	XmlSchemaFacet.Facet facetsDefined, 
																	ValidationEventHandler h) {
				if (lf != null) {
					try {
					if ((facetsDefined & (XmlSchemaFacet.Facet.minLength | XmlSchemaFacet.Facet.maxLength)) != 0)  
							lf.error(h, "It is an error for both length and minLength or maxLength to be present.");
						else {
							lengthFacet = decimal.Parse (lf.Value.Trim (), lengthStyle, CultureInfo.InvariantCulture);
						/* TODO: Check that it is between inherited max/min lengths */
							if (lengthFacet < 0) 
								lf.error(h, "The value '" + lengthFacet + "' is an invalid length");
						}
				} catch (FormatException) { // FIXME: better catch ;-(
						lf.error (h, "The value '" + lf.Value + "' is an invalid length facet specification");
					}
				}
		}

		private void checkMaxLengthFacet(XmlSchemaMaxLengthFacet maxlf, 
																		 XmlSchemaFacet.Facet facetsDefined,
																		 ValidationEventHandler h) {
				if (maxlf != null) {
					try {
					if ((facetsDefined & XmlSchemaFacet.Facet.length) != 0) 
							maxlf.error(h, "It is an error for both length and minLength or maxLength to be present.");
						else {
						decimal newMaxLengthFacet = decimal.Parse (maxlf.Value.Trim (), lengthStyle, CultureInfo.InvariantCulture);
						
						if (((fixedFacets & XmlSchemaFacet.Facet.maxLength)!=0) && (newMaxLengthFacet != maxLengthFacet)) 
							maxlf.error(h, String.Format(CultureInfo.InvariantCulture, "The value '{0}' is not the same as the fixed value '{1}' on the base type", maxlf.Value.Trim (), maxLengthFacet));
						if ((maxLengthFacet >0) && (newMaxLengthFacet > maxLengthFacet)) 
							maxlf.error(h, String.Format(CultureInfo.InvariantCulture, "The value '{0}' is not a valid restriction of the value '{1}' on the base maxLength facet", maxlf.Value.Trim (), maxLengthFacet));
						else
							maxLengthFacet = newMaxLengthFacet;
							if (maxLengthFacet < 0) 
								maxlf.error(h, "The value '" + maxLengthFacet + "' is an invalid maxLength");
							if (minLengthFacet >=0 && minLengthFacet > maxLengthFacet)
								maxlf.error(h, "minLength is greater than maxLength.");
						}

				} catch (FormatException) { 
						maxlf.error (h, "The value '" + maxlf.Value+ "' is an invalid maxLength facet specification");
					}
				}
		}

		private void checkMinLengthFacet(XmlSchemaMinLengthFacet minlf, 
																		 XmlSchemaFacet.Facet facetsDefined,
																		 ValidationEventHandler h) {
				if (minlf != null) {
					try {
						if (lengthFacet >=0) 
							minlf.error(h, "It is an error for both length and minLength or maxLength to be present.");
						else {
						decimal newMinLengthFacet = decimal.Parse (minlf.Value.Trim (), lengthStyle, CultureInfo.InvariantCulture);
						
						if (((fixedFacets & XmlSchemaFacet.Facet.minLength)!=0) && (newMinLengthFacet != minLengthFacet)) 
							minlf.error(h, String.Format(CultureInfo.InvariantCulture, "The value '{0}' is not the same as the fixed value '{1}' on the base type", minlf.Value.Trim (), minLengthFacet));
						if (newMinLengthFacet < minLengthFacet) 
							minlf.error(h, String.Format(CultureInfo.InvariantCulture, "The value '{0}' is not a valid restriction of the value '{1}' on the base minLength facet", minlf.Value.Trim (), minLengthFacet));
						else
							minLengthFacet = newMinLengthFacet;
							if (minLengthFacet < 0) 
								minlf.error(h, "The value '" + minLengthFacet + "' is an invalid minLength");
							if (maxLengthFacet >=0 && minLengthFacet > maxLengthFacet)
								minlf.error(h, "minLength is greater than maxLength.");
						}
				} catch (FormatException) {
						minlf.error (h, "The value '" + minlf.Value + "' is an invalid minLength facet specification");
					}
				}
			}


		private XsdAnySimpleType getDatatype() {
			XsdAnySimpleType ast = ActualBaseSchemaType as XsdAnySimpleType;
			if (ast != null) {
				// Based directly on an xsd type 
				return ast;
			}
			XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType)ActualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
			
			if (st is XmlSchemaSimpleTypeRestriction) {
				return ((XmlSchemaSimpleTypeRestriction)st).getDatatype();
			}
			else if ((st is XmlSchemaSimpleTypeList) ||
							 (st is XmlSchemaSimpleTypeUnion)) {
				return null;
			}
			return null;
		}

		
		private object ValidateValueWithDatatype(string value) {
			XsdAnySimpleType dt = getDatatype();
			object ret = null;
			//		Console.WriteLine("DT: " + dt);
			if (dt != null) {
					try {
					/* I think we can parse null here, as the types 
					 * that use the nametable and nsmgr are ones that 
					 * we don't need to parse here.
					 */ 
					ret = dt.ParseValue (value, null, null);
				//	Console.WriteLine("Ret: " + ret);
					// If we are based on something with facets, check that we are valid
					if (ActualBaseSchemaType is XmlSchemaSimpleType) {
						XmlSchemaSimpleTypeContent st = ((XmlSchemaSimpleType) ActualBaseSchemaType).Content as XmlSchemaSimpleTypeContent;
						if (st is XmlSchemaSimpleTypeRestriction) {
							if (((XmlSchemaSimpleTypeRestriction)st).ValidateValueWithFacets(value, null, null)) {
								return ret;
							} else {
								return null;
					}
				}
			}

				} catch (Exception) {
					return null;
				}
			}
			return ret;
		}

		internal bool ValidateValueWithFacets (string value, XmlNameTable nt, NSResolver nsmgr)
		{
			/*
			 * FIXME: Shouldn't this be recursing more? What if this is a 
			 * restriction of a restriction of a list type?
			 */
			XmlSchemaSimpleType baseST = this.ActualBaseSchemaType as XmlSchemaSimpleType;
			XmlSchemaSimpleTypeList listType = baseST != null ? baseST.Content as XmlSchemaSimpleTypeList : null;

			// numeric
			if (listType != null)
				return ValidateListValueWithFacets (value, nt, nsmgr);
			else
				return ValidateNonListValueWithFacets (value, nt, nsmgr);
		}

		private bool ValidateListValueWithFacets (string value, XmlNameTable nt, NSResolver nsmgr)
		{
			try {
				return ValidateListValueWithFacetsCore (value, nt, nsmgr);
			} catch (Exception) { // this is for datatype ParseValue()
				return false;
			}
		}

		private bool ValidateListValueWithFacetsCore (string value, XmlNameTable nt, NSResolver nsmgr)
		{
			string [] list = ((XsdAnySimpleType) XmlSchemaDatatype.FromName ("anySimpleType", XmlSchema.Namespace)).ParseListValue (value, nt);

			// pattern
			if (this.patternFacetValues != null) {
				for (int l = 0; l < list.Length; l++) {
					for (int i = 0; i < this.patternFacetValues.Length; i++)
						if (rexPatterns [i] != null && !rexPatterns [i].IsMatch (list [l]))
							return false;
				}
			}

			bool enumMatched = false;
#if true
			// enumeration - lexical space comparison
			//
			// I'm not sure if allowing literally-equivalent values
			// without parsing (because it will cause trouble when
			// you try to get TypedValue anyways), but at least it
			// avoids extraneous validation errors ...
			if (this.enumarationFacetValues != null) {
				for (int l = 0; l < list.Length; l++) {
					for (int i = 0; i < this.enumarationFacetValues.Length; i++) {
						if (list [l] == this.enumarationFacetValues [i]) {
							enumMatched = true;
							break;
						}
					}
				}
			}
#endif
			// enumeration - value space comparison
			if (!enumMatched && this.enumarationFacetValues != null) {
				for (int l = 0; l < list.Length; l++) {

					XsdAnySimpleType dt = getDatatype ();
					if (dt == null)
						dt = (XsdAnySimpleType) XmlSchemaDatatype.FromName ("anySimpleType", XmlSchema.Namespace);
					object v = dt.ParseValue (list [l], nt, nsmgr);

					for (int i = 0; i < this.enumarationFacetValues.Length; i++) {
						if (XmlSchemaUtil.AreSchemaDatatypeEqual (dt, v, dt, dt.ParseValue (this.enumarationFacetValues [i], nt, nsmgr))) {
							enumMatched = true;
							break;
						}
					}
					if (!enumMatched)
						return false;
				}
			}

			// numeric
			// : length
			if (lengthFacet >= 0 && list.Length != lengthFacet)
					return false;
			// : maxLength
			if (maxLengthFacet >= 0 && list.Length > maxLengthFacet)
					return false;
			// : minLength
			if (minLengthFacet >= 0 && list.Length < minLengthFacet)
					return false;
			
			return true;
		}

		private bool ValidateNonListValueWithFacets (string value, XmlNameTable nt, NSResolver nsmgr)
		{
			try {
				return ValidateNonListValueWithFacetsCore (value, nt, nsmgr);
			} catch (Exception) { // this is for datatype ParseValue()
				return false;
			}
		}

		private bool ValidateNonListValueWithFacetsCore (string value, XmlNameTable nt, NSResolver nsmgr)
		{
			// pattern
			// Patterns are the only facets that need to be checked on this
			// type and its base types. We should probably separate them, then
			// do base-type pattern validation.
			if (this.patternFacetValues != null) {
				bool matched = false;
				for (int i = 0; i < this.patternFacetValues.Length; i++)
					if (rexPatterns [i] != null && rexPatterns [i].IsMatch (value)) {
						matched = true;
				                break;
					}
				if (!matched)
					return false;
			}

			XsdAnySimpleType dt = getDatatype ();

			bool enumMatched = false;

#if true
			// enumeration - lexical space comparison
			//
			// I'm not sure if allowing literally-equivalent values
			// without parsing (because it will cause trouble when
			// you try to get TypedValue anyways), but at least it
			// avoids extraneous validation errors ...
			if (this.enumarationFacetValues != null) {
				for (int i = 0; i < this.enumarationFacetValues.Length; i++) {
					if (value == this.enumarationFacetValues [i]) {
						enumMatched = true;
						break;
					}
				}
			}
#endif
			// enumeration - value space comparison
			if (!enumMatched && this.enumarationFacetValues != null) {
				XsdAnySimpleType edt = dt;
				if (edt == null)
					edt = (XsdAnySimpleType) XmlSchemaDatatype.FromName ("anySimpleType", XmlSchema.Namespace);
				object v = edt.ParseValue (value, nt, nsmgr);
				for (int i = 0; i < this.enumarationFacetValues.Length; i++) {
					if (XmlSchemaUtil.AreSchemaDatatypeEqual (edt, v, edt, edt.ParseValue (this.enumarationFacetValues [i], nt, nsmgr))) {
						enumMatched = true;
						break;
					}
				}
				if (!enumMatched)
					return false;
			}
			
			// Need to skip length tests for 
			// types derived from QName or NOTATION
			// see errata: E2-36 Clarification
			
			if (! ( (dt is XsdQName) || (dt is XsdNotation))) {  
        // Length potentially slower now, so only calculate if needed			
				if (! ((lengthFacet == -1) && (maxLengthFacet == -1) && (minLengthFacet == -1))) {
											
					// numeric
					// : length
				
					int length = dt.Length(value);
					
					if (lengthFacet >= 0 && length != lengthFacet)
							return false;
					// : maxLength
					if (maxLengthFacet >= 0 && length > maxLengthFacet)
							return false;
					// : minLength
					if (minLengthFacet >= 0 && length < minLengthFacet)
							return false;

				}
		  }
				
			if ((totalDigitsFacet >=0) || (fractionDigitsFacet >=0)) {
				String newValue = value.Trim(new Char [] { '+', '-', '0', '.' });
				int fractionDigits = 0;
				int totalDigits = newValue.Length;
				int point = newValue.IndexOf(".");
				if (point != -1) {
					totalDigits -= 1;
					fractionDigits = newValue.Length - point -1; 
				} 
				if ((totalDigitsFacet >=0) && (totalDigits > totalDigitsFacet)) 
					return false;
				if ((fractionDigitsFacet >=0) && (fractionDigits > fractionDigitsFacet)) 
					return false;
			}
			
			if ((maxInclusiveFacet != null) ||
					(maxExclusiveFacet != null) ||
					(minInclusiveFacet != null) ||
					(minExclusiveFacet != null)) { 
				if (dt != null) {
					object parsed;
					try {
						parsed = dt.ParseValue (value, nt, null);
					} catch (OverflowException ) {
						/* This appears to be what .NET does */
						return false ;
					} catch (FormatException ) {
						/* This appears to be what .NET does */
						return false ;
					}
					
					if (maxInclusiveFacet != null) {
						XsdOrdering result = dt.Compare (parsed, maxInclusiveFacet);
						if ((result != XsdOrdering.LessThan) &&
								(result != XsdOrdering.Equal)) 
							return false;
					}
					if (maxExclusiveFacet != null) {
					
						XsdOrdering result = dt.Compare (parsed, maxExclusiveFacet);
						if (result != XsdOrdering.LessThan) 
							return false;
					}
					if (minInclusiveFacet != null) {
						XsdOrdering result = dt.Compare (parsed, minInclusiveFacet);
						if ((result != XsdOrdering.GreaterThan) &&
								(result != XsdOrdering.Equal)) 
							return false;
					}
					if (minExclusiveFacet != null) {
						XsdOrdering result = dt.Compare (parsed, minExclusiveFacet);
						if (result != XsdOrdering.GreaterThan) 
							return false;
					}

				}
			}

			// all passed
			return true;
		}

		//<restriction 
		//  base = QName 
		//  id = ID 
		//  {any attributes with non-schema namespace . . .}>
		//  Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*))
		//</restriction>
		internal static XmlSchemaSimpleTypeRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
		{
			XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
			reader.MoveToElement();

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

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

			while(reader.MoveToNextAttribute())
			{
				if(reader.Name == "id")
				{
					restriction.Id = reader.Value;
				}
				else if(reader.Name == "base")
				{
					Exception innerex;
					restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
					if(innerex != null)
						error(h, reader.Value + " is not a valid value for base attribute",innerex);
				}
				else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
				{
					error(h,reader.Name + " is not a valid attribute for restriction",null);
				}
				else
				{
					XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);
				}
			}
			
			reader.MoveToElement();
			if(reader.IsEmptyElement)
				return restriction;

			//  Content: annotation?, simpleType?, (minExclusive |. .. | pattern)*
			int level = 1;
			while(reader.ReadNextElement())
			{
				if(reader.NodeType == XmlNodeType.EndElement)
				{
					if(reader.LocalName != xmlname)
						error(h,"Should not happen :2: XmlSchemaSimpleTypeRestriction.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)
						restriction.Annotation = annotation;
					continue;
				}
				if(level <= 2 && reader.LocalName == "simpleType")
				{
					level = 3;
					XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
					if(stype != null)
						restriction.baseType = stype;
					continue;
				}
				if(level <= 3)
				{
					if(reader.LocalName == "minExclusive")
					{
						level = 3;
						XmlSchemaMinExclusiveFacet minex = XmlSchemaMinExclusiveFacet.Read(reader,h);
						if(minex != null)
							restriction.facets.Add(minex);
						continue;
					}
					else if(reader.LocalName == "minInclusive")
					{
						level = 3;
						XmlSchemaMinInclusiveFacet mini = XmlSchemaMinInclusiveFacet.Read(reader,h);
						if(mini != null)
							restriction.facets.Add(mini);
						continue;
					}
					else if(reader.LocalName == "maxExclusive")
					{
						level = 3;
						XmlSchemaMaxExclusiveFacet maxex = XmlSchemaMaxExclusiveFacet.Read(reader,h);
						if(maxex != null)
							restriction.facets.Add(maxex);
						continue;
					}
					else if(reader.LocalName == "maxInclusive")
					{
						level = 3;
						XmlSchemaMaxInclusiveFacet maxi = XmlSchemaMaxInclusiveFacet.Read(reader,h);
						if(maxi != null)
							restriction.facets.Add(maxi);
						continue;
					}
					else if(reader.LocalName == "totalDigits")
					{
						level = 3;
						XmlSchemaTotalDigitsFacet total = XmlSchemaTotalDigitsFacet.Read(reader,h);
						if(total != null)
							restriction.facets.Add(total);
						continue;
					}
					else if(reader.LocalName == "fractionDigits")
					{
						level = 3;
						XmlSchemaFractionDigitsFacet fraction = XmlSchemaFractionDigitsFacet.Read(reader,h);
						if(fraction != null)
							restriction.facets.Add(fraction);
						continue;
					}
					else if(reader.LocalName == "length")
					{
						level = 3;
						XmlSchemaLengthFacet length = XmlSchemaLengthFacet.Read(reader,h);
						if(length != null)
							restriction.facets.Add(length);
						continue;
					}
					else if(reader.LocalName == "minLength")
					{
						level = 3;
						XmlSchemaMinLengthFacet minlen = XmlSchemaMinLengthFacet.Read(reader,h);
						if(minlen != null)
							restriction.facets.Add(minlen);
						continue;
					}
					else if(reader.LocalName == "maxLength")
					{
						level = 3;
						XmlSchemaMaxLengthFacet maxlen = XmlSchemaMaxLengthFacet.Read(reader,h);
						if(maxlen != null)
							restriction.facets.Add(maxlen);
						continue;
					}
					else if(reader.LocalName == "enumeration")
					{
						level = 3;
						XmlSchemaEnumerationFacet enumeration = XmlSchemaEnumerationFacet.Read(reader,h);
						if(enumeration != null)
							restriction.facets.Add(enumeration);
						continue;
					}
					else if(reader.LocalName == "whiteSpace")
					{
						level = 3;
						XmlSchemaWhiteSpaceFacet ws = XmlSchemaWhiteSpaceFacet.Read(reader,h);
						if(ws != null)
							restriction.facets.Add(ws);
						continue;
					}
					else if(reader.LocalName == "pattern")
					{
						level = 3;
						XmlSchemaPatternFacet pattern = XmlSchemaPatternFacet.Read(reader,h);
						if(pattern != null)
							restriction.facets.Add(pattern);
						continue;
					}
				}
				reader.RaiseInvalidElementError();
			}
			return restriction;
		}
	}
}