2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System.XML / System.Xml.Schema / XmlValueConverter.cs
blobb3577dd036bb47ab33c7669ffa0318e5b788f591
1 //
2 // XmlValueConverter.cs
3 //
4 // Author:
5 // Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C)2004 Novell Inc,
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #if NET_2_0
33 using System;
35 namespace System.Xml.Schema
37 public abstract class XmlValueConverter
39 [MonoTODO]
40 protected XmlValueConverter ()
44 public abstract object ChangeType (bool value, Type type);
46 public abstract object ChangeType (DateTime value, Type type);
48 public abstract object ChangeType (decimal value, Type type);
50 public abstract object ChangeType (double value, Type type);
52 public abstract object ChangeType (int value, Type type);
54 public abstract object ChangeType (long value, Type type);
56 public abstract object ChangeType (object value, Type type);
58 public abstract object ChangeType (float value, Type type);
60 public abstract object ChangeType (string value, Type type);
62 public abstract object ChangeType (object value, Type type, IXmlNamespaceResolver nsResolver);
64 public abstract object ChangeType (string value, Type type, IXmlNamespaceResolver nsResolver);
66 public abstract bool ToBoolean (bool value);
68 public abstract bool ToBoolean (DateTime value);
70 public abstract bool ToBoolean (decimal value);
72 public abstract bool ToBoolean (double value);
74 public abstract bool ToBoolean (int value);
76 public abstract bool ToBoolean (long value);
78 public abstract bool ToBoolean (object value);
80 public abstract bool ToBoolean (float value);
82 public abstract bool ToBoolean (string value);
84 public abstract DateTime ToDateTime (bool value);
86 public abstract DateTime ToDateTime (DateTime value);
88 public abstract DateTime ToDateTime (decimal value);
90 public abstract DateTime ToDateTime (double value);
92 public abstract DateTime ToDateTime (int value);
94 public abstract DateTime ToDateTime (long value);
96 public abstract DateTime ToDateTime (object value);
98 public abstract DateTime ToDateTime (float value);
100 public abstract DateTime ToDateTime (string value);
102 public abstract decimal ToDecimal (bool value);
104 public abstract decimal ToDecimal (DateTime value);
106 public abstract decimal ToDecimal (decimal value);
108 public abstract decimal ToDecimal (double value);
110 public abstract decimal ToDecimal (int value);
112 public abstract decimal ToDecimal (long value);
114 public abstract decimal ToDecimal (object value);
116 public abstract decimal ToDecimal (float value);
118 public abstract decimal ToDecimal (string value);
120 public abstract double ToDouble (bool value);
122 public abstract double ToDouble (DateTime value);
124 public abstract double ToDouble (decimal value);
126 public abstract double ToDouble (double value);
128 public abstract double ToDouble (int value);
130 public abstract double ToDouble (long value);
132 public abstract double ToDouble (object value);
134 public abstract double ToDouble (float value);
136 public abstract double ToDouble (string value);
138 public abstract int ToInt32 (bool value);
140 public abstract int ToInt32 (DateTime value);
142 public abstract int ToInt32 (decimal value);
144 public abstract int ToInt32 (double value);
146 public abstract int ToInt32 (int value);
148 public abstract int ToInt32 (long value);
150 public abstract int ToInt32 (object value);
152 public abstract int ToInt32 (float value);
154 public abstract int ToInt32 (string value);
156 public abstract long ToInt64 (bool value);
158 public abstract long ToInt64 (DateTime value);
160 public abstract long ToInt64 (decimal value);
162 public abstract long ToInt64 (double value);
164 public abstract long ToInt64 (int value);
166 public abstract long ToInt64 (long value);
168 public abstract long ToInt64 (object value);
170 public abstract long ToInt64 (float value);
172 public abstract long ToInt64 (string value);
174 public abstract float ToSingle (bool value);
176 public abstract float ToSingle (DateTime value);
178 public abstract float ToSingle (decimal value);
180 public abstract float ToSingle (double value);
182 public abstract float ToSingle (int value);
184 public abstract float ToSingle (long value);
186 public abstract float ToSingle (object value);
188 public abstract float ToSingle (float value);
190 public abstract float ToSingle (string value);
192 public abstract string ToString (bool value);
194 public abstract string ToString (DateTime value);
196 public abstract string ToString (decimal value);
198 public abstract string ToString (double value);
200 public abstract string ToString (int value);
202 public abstract string ToString (long value);
204 public abstract string ToString (object value);
206 public abstract string ToString (object value, IXmlNamespaceResolver nsResolver);
208 public abstract string ToString (float value);
210 public abstract string ToString (string value);
212 public abstract string ToString (string value, IXmlNamespaceResolver nsResolver);
215 internal class XsdNonPermissiveConverter : XmlValueConverter
217 readonly XmlTypeCode typeCode;
219 public XsdNonPermissiveConverter (XmlTypeCode typeCode)
221 this.typeCode = typeCode;
224 public XmlTypeCode Code {
225 get { return typeCode; }
228 public override object ChangeType (bool value, Type type)
230 return ChangeType ((object) value, type);
233 public override object ChangeType (DateTime value, Type type)
235 return ChangeType ((object) value, type);
238 public override object ChangeType (decimal value, Type type)
240 return ChangeType ((object) value, type);
243 public override object ChangeType (double value, Type type)
245 return ChangeType ((object) value, type);
248 public override object ChangeType (int value, Type type)
250 return ChangeType ((object) value, type);
253 public override object ChangeType (long value, Type type)
255 return ChangeType ((object) value, type);
258 public override object ChangeType (float value, Type type)
260 return ChangeType ((object) value, type);
263 public override object ChangeType (string value, Type type)
265 return ChangeType ((object) value, type);
268 public override object ChangeType (object value, Type type)
270 return ChangeType (value, type, null);
273 [MonoTODO]
274 public override object ChangeType (object value, Type type, IXmlNamespaceResolver nsResolver)
276 if (value == null)
277 throw new ArgumentNullException ("value");
278 if (type == null)
279 throw new ArgumentNullException ("type");
280 switch (Type.GetTypeCode (value.GetType ())) {
281 case TypeCode.Boolean:
282 bool bvalue = (bool) value;
283 switch (Type.GetTypeCode (type)) {
284 case TypeCode.Boolean:
285 return ToBoolean (bvalue);
286 case TypeCode.DateTime:
287 return ToDateTime (bvalue);
288 case TypeCode.Decimal:
289 return ToDecimal (bvalue);
290 case TypeCode.Double:
291 return ToDouble (bvalue);
292 case TypeCode.Int32:
293 return ToInt32 (bvalue);
294 case TypeCode.Int64:
295 return ToInt64 (bvalue);
296 case TypeCode.Single:
297 return ToSingle (bvalue);
298 case TypeCode.String:
299 return ToString (bvalue);
301 break;
302 // case TypeCode.Byte:
303 // case TypeCode.Char:
304 case TypeCode.DateTime:
305 DateTime dtvalue = (DateTime) value;
306 switch (Type.GetTypeCode (type)) {
307 case TypeCode.Boolean:
308 return ToBoolean (dtvalue);
309 case TypeCode.DateTime:
310 return ToDateTime (dtvalue);
311 case TypeCode.Decimal:
312 return ToDecimal (dtvalue);
313 case TypeCode.Double:
314 return ToDouble (dtvalue);
315 case TypeCode.Int32:
316 return ToInt32 (dtvalue);
317 case TypeCode.Int64:
318 return ToInt64 (dtvalue);
319 case TypeCode.Single:
320 return ToSingle (dtvalue);
321 case TypeCode.String:
322 return ToString (dtvalue);
324 break;
325 // case TypeCode.DBNull:
326 case TypeCode.Decimal:
327 decimal decvalue = (decimal) value;
328 switch (Type.GetTypeCode (type)) {
329 case TypeCode.Boolean:
330 return ToBoolean (decvalue);
331 case TypeCode.DateTime:
332 return ToDateTime (decvalue);
333 case TypeCode.Decimal:
334 return ToDecimal (decvalue);
335 case TypeCode.Double:
336 return ToDouble (decvalue);
337 case TypeCode.Int32:
338 return ToInt32 (decvalue);
339 case TypeCode.Int64:
340 return ToInt64 (decvalue);
341 case TypeCode.Single:
342 return ToSingle (decvalue);
343 case TypeCode.String:
344 return ToString (decvalue);
346 break;
347 case TypeCode.Double:
348 double dblvalue = (double) value;
349 switch (Type.GetTypeCode (type)) {
350 case TypeCode.Boolean:
351 return ToBoolean (dblvalue);
352 case TypeCode.DateTime:
353 return ToDateTime (dblvalue);
354 case TypeCode.Decimal:
355 return ToDecimal (dblvalue);
356 case TypeCode.Double:
357 return ToDouble (dblvalue);
358 case TypeCode.Int32:
359 return ToInt32 (dblvalue);
360 case TypeCode.Int64:
361 return ToInt64 (dblvalue);
362 case TypeCode.Single:
363 return ToSingle (dblvalue);
364 case TypeCode.String:
365 return ToString (dblvalue);
367 break;
368 // case TypeCode.Empty:
369 // case TypeCode.Int16:
370 case TypeCode.Int32:
371 int ivalue = (int) value;
372 switch (Type.GetTypeCode (type)) {
373 case TypeCode.Boolean:
374 return ToBoolean (ivalue);
375 case TypeCode.DateTime:
376 return ToDateTime (ivalue);
377 case TypeCode.Decimal:
378 return ToDecimal (ivalue);
379 case TypeCode.Double:
380 return ToDouble (ivalue);
381 case TypeCode.Int32:
382 return ToInt32 (ivalue);
383 case TypeCode.Int64:
384 return ToInt64 (ivalue);
385 case TypeCode.Single:
386 return ToSingle (ivalue);
387 case TypeCode.String:
388 return ToString (ivalue);
390 break;
391 case TypeCode.Int64:
392 long lvalue = (long) value;
393 switch (Type.GetTypeCode (type)) {
394 case TypeCode.Boolean:
395 return ToBoolean (lvalue);
396 case TypeCode.DateTime:
397 return ToDateTime (lvalue);
398 case TypeCode.Decimal:
399 return ToDecimal (lvalue);
400 case TypeCode.Double:
401 return ToDouble (lvalue);
402 case TypeCode.Int32:
403 return ToInt32 (lvalue);
404 case TypeCode.Int64:
405 return ToInt64 (lvalue);
406 case TypeCode.Single:
407 return ToSingle (lvalue);
408 case TypeCode.String:
409 return ToString (lvalue);
411 break;
412 // case TypeCode.Object:
413 // case TypeCode.SByte:
414 case TypeCode.Single:
415 float fvalue = (float) value;
416 switch (Type.GetTypeCode (type)) {
417 case TypeCode.Boolean:
418 return ToBoolean (fvalue);
419 case TypeCode.DateTime:
420 return ToDateTime (fvalue);
421 case TypeCode.Decimal:
422 return ToDecimal (fvalue);
423 case TypeCode.Double:
424 return ToDouble (fvalue);
425 case TypeCode.Int32:
426 return ToInt32 (fvalue);
427 case TypeCode.Int64:
428 return ToInt64 (fvalue);
429 case TypeCode.Single:
430 return ToSingle (fvalue);
431 case TypeCode.String:
432 return ToString (fvalue);
434 break;
435 case TypeCode.String:
436 string svalue = (string) value;
437 switch (Type.GetTypeCode (type)) {
438 case TypeCode.Boolean:
439 return ToBoolean (svalue);
440 case TypeCode.DateTime:
441 return ToDateTime (svalue);
442 case TypeCode.Decimal:
443 return ToDecimal (svalue);
444 case TypeCode.Double:
445 return ToDouble (svalue);
446 case TypeCode.Int32:
447 return ToInt32 (svalue);
448 case TypeCode.Int64:
449 return ToInt64 (svalue);
450 case TypeCode.Single:
451 return ToSingle (svalue);
452 case TypeCode.String:
453 return ToString (svalue);
455 break;
456 // case TypeCode.UInt16:
457 // case TypeCode.UInt32:
458 // case TypeCode.UInt64:
459 default:
460 if (type == typeof (TimeSpan))
461 // xs:duration, xdt:yearMonthDuration,
462 // xdt:dayTimeDuration. FIXME: yMD to
463 // dTD and dTD to yMD are not allowed.
464 return ToTimeSpan (value);
465 if (value.GetType () == typeof (byte [])) {
466 // xs:base64 by default
467 if (type == typeof (string))
468 return XQueryConvert.Base64BinaryToString ((byte []) value);
469 else if (type == typeof (byte []))
470 return value;
472 if (value.GetType () == type) {
473 if (type == typeof (XmlQualifiedName)) {
474 // xs:QName and xs:NOTATION
475 throw new NotImplementedException ();
478 break;
480 throw Error (value.GetType (), type);
483 public override object ChangeType (string value, Type type, IXmlNamespaceResolver nsResolver)
485 return ChangeType ((object) value, type, nsResolver);
488 public TimeSpan ToTimeSpan (bool value)
490 throw Error (typeof (bool), typeof (TimeSpan));
493 public TimeSpan ToTimeSpan (DateTime value)
495 throw Error (typeof (bool), typeof (TimeSpan));
498 public TimeSpan ToTimeSpan (decimal value)
500 throw Error (typeof (bool), typeof (TimeSpan));
503 public TimeSpan ToTimeSpan (double value)
505 throw Error (typeof (bool), typeof (TimeSpan));
508 public TimeSpan ToTimeSpan (int value)
510 throw Error (typeof (bool), typeof (TimeSpan));
513 public TimeSpan ToTimeSpan (long value)
515 throw Error (typeof (bool), typeof (TimeSpan));
518 public virtual TimeSpan ToTimeSpan (object value)
520 // Allow on overriden converter for xs:duration,
521 // xdt:dayTimeDuration and xdt:yearMonthDuration.
522 throw Error (typeof (bool), typeof (TimeSpan));
525 public TimeSpan ToTimeSpan (float value)
527 throw Error (typeof (bool), typeof (TimeSpan));
530 public TimeSpan ToTimeSpan (string value)
532 throw Error (typeof (bool), typeof (TimeSpan));
535 protected InvalidCastException Error (Type valueType, Type destType)
537 return new InvalidCastException (String.Format ("The conversion from {0} value to {1} type via {2} type is not allowed.", valueType, destType, typeCode));
540 public override bool ToBoolean (bool value)
542 throw Error (typeof (bool), typeof (bool));
545 public override bool ToBoolean (DateTime value)
547 throw Error (typeof (DateTime), typeof (bool));
550 public override bool ToBoolean (decimal value)
552 throw Error (typeof (decimal), typeof (bool));
555 public override bool ToBoolean (double value)
557 throw Error (typeof (double), typeof (bool));
560 public override bool ToBoolean (int value)
562 throw Error (typeof (int), typeof (bool));
565 public override bool ToBoolean (long value)
567 throw Error (typeof (long), typeof (bool));
570 public override bool ToBoolean (object value)
572 if (value == null)
573 throw new ArgumentNullException ("value");
574 throw Error (value.GetType (), typeof (bool));
577 public override bool ToBoolean (float value)
579 throw Error (typeof (float), typeof (bool));
582 public override bool ToBoolean (string value)
584 throw Error (typeof (string), typeof (bool));
587 public override DateTime ToDateTime (bool value)
589 throw Error (typeof (bool), typeof (DateTime));
592 public override DateTime ToDateTime (DateTime value)
594 throw Error (typeof (DateTime), typeof (DateTime));
597 public override DateTime ToDateTime (decimal value)
599 throw Error (typeof (decimal), typeof (DateTime));
602 public override DateTime ToDateTime (double value)
604 throw Error (typeof (double), typeof (DateTime));
607 public override DateTime ToDateTime (int value)
609 throw Error (typeof (int), typeof (DateTime));
612 public override DateTime ToDateTime (long value)
614 throw Error (typeof (long), typeof (DateTime));
617 public override DateTime ToDateTime (object value)
619 if (value == null)
620 throw new ArgumentNullException ("value");
621 throw Error (value.GetType (), typeof (DateTime));
624 public override DateTime ToDateTime (float value)
626 throw Error (typeof (float), typeof (DateTime));
629 public override DateTime ToDateTime (string value)
631 throw Error (typeof (string), typeof (DateTime));
634 public override decimal ToDecimal (bool value)
636 throw Error (typeof (bool), typeof (decimal));
639 public override decimal ToDecimal (DateTime value)
641 throw Error (typeof (DateTime), typeof (decimal));
644 public override decimal ToDecimal (decimal value)
646 throw Error (typeof (decimal), typeof (decimal));
649 public override decimal ToDecimal (double value)
651 throw Error (typeof (double), typeof (decimal));
654 public override decimal ToDecimal (int value)
656 throw Error (typeof (int), typeof (decimal));
659 public override decimal ToDecimal (long value)
661 throw Error (typeof (long), typeof (decimal));
664 public override decimal ToDecimal (object value)
666 if (value == null)
667 throw new ArgumentNullException ("value");
668 throw Error (value.GetType (), typeof (decimal));
671 public override decimal ToDecimal (float value)
673 throw Error (typeof (float), typeof (decimal));
676 public override decimal ToDecimal (string value)
678 throw Error (typeof (string), typeof (decimal));
681 public override double ToDouble (bool value)
683 throw Error (typeof (bool), typeof (double));
686 public override double ToDouble (DateTime value)
688 throw Error (typeof (DateTime), typeof (double));
691 public override double ToDouble (decimal value)
693 throw Error (typeof (decimal), typeof (double));
696 public override double ToDouble (double value)
698 throw Error (typeof (double), typeof (double));
701 public override double ToDouble (int value)
703 throw Error (typeof (int), typeof (double));
706 public override double ToDouble (long value)
708 throw Error (typeof (long), typeof (double));
711 public override double ToDouble (object value)
713 if (value == null)
714 throw new ArgumentNullException ("value");
715 throw Error (value.GetType (), typeof (double));
718 public override double ToDouble (float value)
720 throw Error (typeof (float), typeof (double));
723 public override double ToDouble (string value)
725 throw Error (typeof (string), typeof (double));
728 public override float ToSingle (bool value)
730 throw Error (typeof (bool), typeof (float));
733 public override float ToSingle (DateTime value)
735 throw Error (typeof (DateTime), typeof (float));
738 public override float ToSingle (decimal value)
740 throw Error (typeof (decimal), typeof (float));
743 public override float ToSingle (double value)
745 throw Error (typeof (double), typeof (float));
748 public override float ToSingle (int value)
750 throw Error (typeof (int), typeof (float));
753 public override float ToSingle (long value)
755 throw Error (typeof (long), typeof (float));
758 public override float ToSingle (object value)
760 if (value == null)
761 throw new ArgumentNullException ("value");
762 throw Error (value.GetType (), typeof (float));
765 public override float ToSingle (float value)
767 throw Error (typeof (float), typeof (float));
770 public override float ToSingle (string value)
772 throw Error (typeof (string), typeof (float));
775 public override int ToInt32 (bool value)
777 throw Error (typeof (bool), typeof (int));
780 public override int ToInt32 (DateTime value)
782 throw Error (typeof (DateTime), typeof (int));
785 public override int ToInt32 (decimal value)
787 throw Error (typeof (decimal), typeof (int));
790 public override int ToInt32 (double value)
792 throw Error (typeof (double), typeof (int));
795 public override int ToInt32 (int value)
797 throw Error (typeof (int), typeof (int));
800 public override int ToInt32 (long value)
802 throw Error (typeof (long), typeof (int));
805 public override int ToInt32 (object value)
807 if (value == null)
808 throw new ArgumentNullException ("value");
809 throw Error (value.GetType (), typeof (int));
812 public override int ToInt32 (float value)
814 throw Error (typeof (float), typeof (int));
817 public override int ToInt32 (string value)
819 throw Error (typeof (string), typeof (int));
822 public override long ToInt64 (bool value)
824 throw Error (typeof (bool), typeof (long));
827 public override long ToInt64 (DateTime value)
829 throw Error (typeof (DateTime), typeof (long));
832 public override long ToInt64 (decimal value)
834 throw Error (typeof (decimal), typeof (long));
837 public override long ToInt64 (double value)
839 throw Error (typeof (double), typeof (long));
842 public override long ToInt64 (int value)
844 throw Error (typeof (int), typeof (long));
847 public override long ToInt64 (long value)
849 throw Error (typeof (long), typeof (long));
852 public override long ToInt64 (object value)
854 if (value == null)
855 throw new ArgumentNullException ("value");
856 throw Error (value.GetType (), typeof (long));
859 public override long ToInt64 (float value)
861 throw Error (typeof (float), typeof (long));
864 public override long ToInt64 (string value)
866 throw Error (typeof (string), typeof (long));
869 public override string ToString (bool value)
871 throw Error (typeof (bool), typeof (string));
874 public override string ToString (DateTime value)
876 throw Error (typeof (DateTime), typeof (string));
879 public override string ToString (decimal value)
881 throw Error (typeof (decimal), typeof (string));
884 public override string ToString (double value)
886 throw Error (typeof (double), typeof (string));
889 public override string ToString (int value)
891 throw Error (typeof (int), typeof (string));
894 public override string ToString (long value)
896 throw Error (typeof (long), typeof (string));
899 public override string ToString (object value)
901 return ToString (value, null);
904 public override string ToString (object value, IXmlNamespaceResolver nsResolver)
906 if (value == null)
907 throw new ArgumentNullException ("value");
908 throw Error (value.GetType (), typeof (string));
911 public override string ToString (float value)
913 throw Error (typeof (float), typeof (string));
916 public override string ToString (string value)
918 return ToString (value, null);
921 public override string ToString (string value, IXmlNamespaceResolver nsResolver)
923 throw Error (typeof (string), typeof (string));
927 internal class XsdLaxConverter : XsdNonPermissiveConverter
929 public XsdLaxConverter (XmlTypeCode code)
930 : base (code)
934 public override string ToString (bool value)
936 return XmlConvert.ToString (value);
939 public override string ToString (decimal value)
941 return XmlConvert.ToString (value);
944 public override string ToString (double value)
946 return XmlConvert.ToString (value);
949 public override string ToString (int value)
951 return XmlConvert.ToString (value);
954 public override string ToString (long value)
956 return XmlConvert.ToString (value);
959 public override string ToString (object value)
961 return ChangeType (value, typeof (string)) as string;
964 public override string ToString (float value)
966 return XmlConvert.ToString (value);
969 public override bool ToBoolean (bool value)
971 return value;
974 public override decimal ToDecimal (decimal value)
976 return value;
979 public override decimal ToDecimal (double value)
981 return (decimal) value;
984 public override decimal ToDecimal (int value)
986 return (decimal) value;
989 public override decimal ToDecimal (long value)
991 return (decimal) value;
994 [MonoTODO]
995 public override decimal ToDecimal (object value)
997 return (decimal) ChangeType (value, typeof (decimal));
1000 public override decimal ToDecimal (float value)
1002 return (decimal) value;
1005 public override double ToDouble (decimal value)
1007 return (double) value;
1010 public override double ToDouble (double value)
1012 return (double) value;
1015 public override double ToDouble (int value)
1017 return (double) value;
1020 public override double ToDouble (long value)
1022 return (double) value;
1025 [MonoTODO]
1026 public override double ToDouble (object value)
1028 return (double) ChangeType (value, typeof (double));
1031 public override double ToDouble (float value)
1033 return (double) value;
1036 public override float ToSingle (decimal value)
1038 return (float) value;
1041 public override float ToSingle (double value)
1043 return (float) value;
1046 public override float ToSingle (int value)
1048 return (float) value;
1051 public override float ToSingle (long value)
1053 return (float) value;
1056 [MonoTODO]
1057 public override float ToSingle (object value)
1059 return (float) ChangeType (value, typeof (float));
1062 public override float ToSingle (float value)
1064 return (float) value;
1067 public override int ToInt32 (int value)
1069 return value;
1072 public override int ToInt32 (long value)
1074 return XQueryConvert.IntegerToInt (value);
1077 [MonoTODO]
1078 public override int ToInt32 (object value)
1080 return (int) ChangeType (value, typeof (int));
1083 public override long ToInt64 (int value)
1085 return value;
1088 public override long ToInt64 (long value)
1090 return value;
1093 [MonoTODO]
1094 public override long ToInt64 (object value)
1096 return (long) ChangeType (value, typeof (long));
1100 internal class XsdAnyTypeConverter : XsdNumericConverter
1102 public XsdAnyTypeConverter (XmlTypeCode code)
1103 : base (code)
1107 #region boolean
1108 public override bool ToBoolean (decimal value)
1110 return value != 0;
1113 public override bool ToBoolean (double value)
1115 return value != 0;
1118 public override bool ToBoolean (int value)
1120 return value != 0;
1123 public override bool ToBoolean (long value)
1125 return value != 0;
1128 [MonoTODO]
1129 public override bool ToBoolean (object value)
1131 return (bool) ChangeType (value, typeof (bool));
1134 public override bool ToBoolean (float value)
1136 return value != 0;
1139 public override decimal ToDecimal (bool value)
1141 return value ? 1 : 0;
1144 public override double ToDouble (bool value)
1146 return value ? 1 : 0;
1149 public override float ToSingle (bool value)
1151 return value ? 1 : 0;
1154 public override int ToInt32 (bool value)
1156 return value ? 1 : 0;
1159 public override long ToInt64 (bool value)
1161 return value ? 1 : 0;
1163 #endregion
1165 #region string
1167 public override DateTime ToDateTime (DateTime value)
1169 return value;
1172 public override string ToString (DateTime value)
1174 return XmlConvert.ToString (value);
1177 public override string ToString (string value)
1179 return value;
1181 #endregion
1184 internal class XsdStringConverter : XsdLaxConverter
1186 public XsdStringConverter (XmlTypeCode code)
1187 : base (code)
1191 public override DateTime ToDateTime (DateTime value)
1193 return value;
1196 public override string ToString (DateTime value)
1198 return XmlConvert.ToString (value);
1201 public override string ToString (string value)
1203 return value;
1207 internal class XsdNumericConverter : XsdLaxConverter
1209 public XsdNumericConverter (XmlTypeCode code)
1210 : base (code)
1214 #region boolean
1215 public override bool ToBoolean (decimal value)
1217 return value != 0;
1220 public override bool ToBoolean (double value)
1222 return value != 0;
1225 public override bool ToBoolean (int value)
1227 return value != 0;
1230 public override bool ToBoolean (long value)
1232 return value != 0;
1235 [MonoTODO]
1236 public override bool ToBoolean (object value)
1238 return (bool) ChangeType (value, typeof (bool));
1241 public override bool ToBoolean (float value)
1243 return value != 0;
1246 public override decimal ToDecimal (bool value)
1248 return value ? 1 : 0;
1251 public override double ToDouble (bool value)
1253 return value ? 1 : 0;
1256 public override float ToSingle (bool value)
1258 return value ? 1 : 0;
1261 public override int ToInt32 (bool value)
1263 return value ? 1 : 0;
1266 public override long ToInt64 (bool value)
1268 return value ? 1 : 0;
1270 #endregion
1272 #region numeric with point to without point
1273 public override int ToInt32 (decimal value)
1275 return XQueryConvert.DecimalToInt (value);
1278 public override int ToInt32 (double value)
1280 return XQueryConvert.DoubleToInt (value);
1283 public override int ToInt32 (float value)
1285 return XQueryConvert.FloatToInt (value);
1288 public override long ToInt64 (decimal value)
1290 return XQueryConvert.DecimalToInteger (value);
1293 public override long ToInt64 (double value)
1295 return XQueryConvert.DoubleToInteger (value);
1298 public override long ToInt64 (float value)
1300 return XQueryConvert.FloatToInteger (value);
1302 #endregion
1305 internal class XsdDateTimeConverter : XsdNonPermissiveConverter
1307 public XsdDateTimeConverter (XmlTypeCode code)
1308 : base (code)
1312 public override string ToString (DateTime value)
1314 return XmlConvert.ToString (value);
1317 public override DateTime ToDateTime (DateTime value)
1319 return value;
1323 internal class XsdBooleanConverter : XsdNumericConverter
1325 public XsdBooleanConverter (XmlTypeCode code)
1326 : base (code)
1331 internal class XsdMiscBaseConverter : XsdNonPermissiveConverter
1333 public XsdMiscBaseConverter (XmlTypeCode code)
1334 : base (code)
1338 public override string ToString (string value)
1340 return value;
1343 public override object ChangeType (object value, Type type, IXmlNamespaceResolver nsResolver)
1345 if (Code == XmlTypeCode.HexBinary) {
1346 if (value == null)
1347 throw new ArgumentNullException ("value");
1348 if (type == null)
1349 throw new ArgumentNullException ("type");
1350 if (value.GetType () == typeof (byte [])) {
1351 if (type == typeof (string))
1352 return XQueryConvert.HexBinaryToString ((byte []) value);
1353 else if (type == typeof (byte []))
1354 return value;
1357 return base.ChangeType (value, type, nsResolver);
1362 #endif