2 // System.Xml.Query.XQueryConvert
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2004 Novell Inc.
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Globalization;
37 using System.Xml.Schema;
38 using System.Xml.XPath;
42 public class XQueryConvert
44 private XQueryConvert ()
49 public static bool ShouldCheckValueFacets (XmlSchemaType schemaTypeDest)
51 throw new NotImplementedException ();
54 public static XmlTypeCode GetFallbackType (XmlTypeCode type)
57 case XmlTypeCode.AnyAtomicType:
58 return XmlTypeCode.Item;
59 case XmlTypeCode.UntypedAtomic:
60 return XmlTypeCode.String;
61 case XmlTypeCode.Notation:
62 return XmlTypeCode.QName;
63 case XmlTypeCode.NormalizedString:
64 case XmlTypeCode.Token:
65 case XmlTypeCode.Language:
66 case XmlTypeCode.NmToken:
67 case XmlTypeCode.Name:
68 case XmlTypeCode.NCName:
70 case XmlTypeCode.Idref:
71 case XmlTypeCode.Entity:
72 return XmlTypeCode.String;
73 case XmlTypeCode.NonPositiveInteger:
74 return XmlTypeCode.Decimal;
75 case XmlTypeCode.NegativeInteger:
76 return XmlTypeCode.NonPositiveInteger;
77 case XmlTypeCode.Long:
78 return XmlTypeCode.Integer;
79 case XmlTypeCode.Short:
80 return XmlTypeCode.Int;
81 case XmlTypeCode.Byte:
82 return XmlTypeCode.Int;
83 case XmlTypeCode.NonNegativeInteger:
84 return XmlTypeCode.Decimal;
85 case XmlTypeCode.UnsignedLong:
86 return XmlTypeCode.NonNegativeInteger;
87 case XmlTypeCode.UnsignedInt:
88 return XmlTypeCode.Integer;
89 case XmlTypeCode.UnsignedShort:
90 return XmlTypeCode.Int;
91 case XmlTypeCode.UnsignedByte:
92 return XmlTypeCode.UnsignedShort;
93 case XmlTypeCode.PositiveInteger:
94 return XmlTypeCode.NonNegativeInteger;
96 return XmlTypeCode.None;
101 // See XQuery & XPath 2.0 functions & operators section 17.
102 public static bool CanConvert (XPathItem item, XmlSchemaType schemaTypeDest)
105 throw new ArgumentNullException ("item");
106 if (schemaTypeDest == null)
107 throw new ArgumentNullException ("schemaTypeDest");
108 XmlTypeCode src = item.XmlType.TypeCode;
109 XmlTypeCode dst = schemaTypeDest.TypeCode;
111 // Notation cannot be converted from other than Notation
112 if (src == XmlTypeCode.Notation && dst != XmlTypeCode.Notation)
115 // untypedAtomic and string are convertable unless source type is QName.
117 case XmlTypeCode.UntypedAtomic:
118 case XmlTypeCode.String:
119 return src != XmlTypeCode.QName;
123 case XmlTypeCode.None:
124 case XmlTypeCode.Item:
125 case XmlTypeCode.Node:
126 case XmlTypeCode.Document:
127 case XmlTypeCode.Element:
128 case XmlTypeCode.Attribute:
129 case XmlTypeCode.Namespace:
130 case XmlTypeCode.ProcessingInstruction:
131 case XmlTypeCode.Comment:
132 case XmlTypeCode.Text:
133 throw new NotImplementedException (); // FIXME: check what happens
135 case XmlTypeCode.AnyAtomicType:
136 throw new NotImplementedException (); // FIXME: check what happens
137 case XmlTypeCode.UntypedAtomic:
138 case XmlTypeCode.String:
140 throw new NotImplementedException (); // FIXME: check what happens
142 case XmlTypeCode.Boolean:
143 case XmlTypeCode.Decimal:
145 case XmlTypeCode.Float:
146 case XmlTypeCode.Double:
147 case XmlTypeCode.Decimal:
148 case XmlTypeCode.Boolean:
153 case XmlTypeCode.Float:
154 case XmlTypeCode.Double:
155 if (dst == XmlTypeCode.Decimal)
157 throw new NotImplementedException (); // FIXME: check what happens
158 goto case XmlTypeCode.Decimal;
160 case XmlTypeCode.Duration:
162 case XmlTypeCode.Duration:
163 case XmlTypeCode.YearMonthDuration:
164 case XmlTypeCode.DayTimeDuration:
169 case XmlTypeCode.DateTime:
171 case XmlTypeCode.DateTime:
172 case XmlTypeCode.Time:
173 case XmlTypeCode.Date:
174 case XmlTypeCode.GYearMonth:
175 case XmlTypeCode.GYear:
176 case XmlTypeCode.GMonthDay:
177 case XmlTypeCode.GDay:
178 case XmlTypeCode.GMonth:
183 case XmlTypeCode.Time:
185 case XmlTypeCode.Time:
186 case XmlTypeCode.Date:
191 case XmlTypeCode.Date:
192 if (dst == XmlTypeCode.Time)
194 goto case XmlTypeCode.DateTime;
196 case XmlTypeCode.GYearMonth:
197 case XmlTypeCode.GYear:
198 case XmlTypeCode.GMonthDay:
199 case XmlTypeCode.GDay:
200 case XmlTypeCode.GMonth:
203 case XmlTypeCode.HexBinary:
204 case XmlTypeCode.Base64Binary:
208 case XmlTypeCode.HexBinary:
209 case XmlTypeCode.Base64Binary:
214 case XmlTypeCode.AnyUri:
215 case XmlTypeCode.QName:
216 case XmlTypeCode.Notation:
219 case XmlTypeCode.NormalizedString:
220 case XmlTypeCode.Token:
221 case XmlTypeCode.Language:
222 case XmlTypeCode.NmToken:
223 case XmlTypeCode.Name:
224 case XmlTypeCode.NCName:
226 case XmlTypeCode.Idref:
227 case XmlTypeCode.Entity:
228 case XmlTypeCode.Integer:
229 case XmlTypeCode.NonPositiveInteger:
230 case XmlTypeCode.NegativeInteger:
231 case XmlTypeCode.Long:
232 case XmlTypeCode.Int:
233 case XmlTypeCode.Short:
234 case XmlTypeCode.Byte:
235 case XmlTypeCode.NonNegativeInteger:
236 case XmlTypeCode.UnsignedLong:
237 case XmlTypeCode.UnsignedInt:
238 case XmlTypeCode.UnsignedShort:
239 case XmlTypeCode.UnsignedByte:
240 case XmlTypeCode.PositiveInteger:
241 throw new NotImplementedException ();
244 case XmlTypeCode.YearMonthDuration:
245 if (dst == XmlTypeCode.DayTimeDuration)
247 goto case XmlTypeCode.Duration;
248 case XmlTypeCode.DayTimeDuration:
249 if (dst == XmlTypeCode.YearMonthDuration)
251 goto case XmlTypeCode.Duration;
254 throw new NotImplementedException ();
257 // Individual conversion
259 public static string AnyUriToString (string value)
264 public static byte [] Base64BinaryToHexBinary (byte [] value)
266 return XmlConvert.FromBinHexString (Convert.ToBase64String (value));
269 public static string Base64BinaryToString (byte [] value)
271 return Convert.ToBase64String (value);
274 public static decimal BooleanToDecimal (bool value)
276 return Convert.ToDecimal (value);
279 public static double BooleanToDouble (bool value)
281 return Convert.ToDouble (value);
284 public static float BooleanToFloat (bool value)
286 return Convert.ToSingle (value);
289 public static int BooleanToInt (bool value)
291 return Convert.ToInt32 (value);
294 public static long BooleanToInteger (bool value)
296 return Convert.ToInt64 (value);
299 public static string BooleanToString (bool value)
301 // It looks not returning "True"
302 return value ? "true" : "false";
306 public static DateTime DateTimeToDate (DateTime value)
312 public static DateTime DateTimeToGDay (DateTime value)
314 return new DateTime (0, 0, value.Day);
318 public static DateTime DateTimeToGMonth (DateTime value)
320 return new DateTime (0, value.Month, 0);
324 public static DateTime DateTimeToGMonthDay (DateTime value)
326 return new DateTime (0, value.Month, value.Day);
330 public static DateTime DateTimeToGYear (DateTime value)
332 return new DateTime (value.Year, 0, 0);
336 public static DateTime DateTimeToGYearMonth (DateTime value)
338 return new DateTime (value.Year, value.Month, 0);
342 public static DateTime DateTimeToTime (DateTime value)
344 return new DateTime (value.TimeOfDay.Ticks);
348 public static DateTime DateToDateTime (DateTime value)
354 public static DateTime DateToGDay (DateTime value)
356 return new DateTime (0, 0, value.Day);
360 public static DateTime DateToGMonth (DateTime value)
362 return new DateTime (0, value.Month, 0);
366 public static DateTime DateToGMonthDay (DateTime value)
368 return new DateTime (0, value.Month, value.Day);
372 public static DateTime DateToGYear (DateTime value)
374 return new DateTime (value.Year, 0, 0);
378 public static DateTime DateToGYearMonth (DateTime value)
380 return new DateTime (value.Year, value.Month, 0);
384 public static string DateToString (DateTime value)
386 return XmlConvert.ToString (value);
390 public static string DateTimeToString (DateTime value)
392 return XmlConvert.ToString (value);
396 public static string DayTimeDurationToDuration (TimeSpan value)
398 return XmlConvert.ToString (value);
402 public static string DayTimeDurationToString (TimeSpan value)
404 return DayTimeDurationToDuration (value);
408 public static bool DecimalToBoolean (decimal value)
414 public static double DecimalToDouble (decimal value)
416 return (double) value;
420 public static float DecimalToFloat (decimal value)
422 return (float) value;
426 public static int DecimalToInt (decimal value)
432 public static long DecimalToInteger (decimal value)
437 [MonoTODO] // what if value was negative?
438 public static decimal DecimalToNonNegativeInteger (decimal value)
440 throw new NotImplementedException ();
443 [MonoTODO] // what if value was positive?
444 public static decimal DecimalToNonPositiveInteger (decimal value)
446 throw new NotImplementedException ();
450 public static string DecimalToString (decimal value)
452 return XmlConvert.ToString (value);
456 public static bool DoubleToBoolean (double value)
462 public static decimal DoubleToDecimal (double value)
464 return (decimal) value;
468 public static float DoubleToFloat (double value)
470 return (float) value;
474 public static int DoubleToInt (double value)
480 public static long DoubleToInteger (double value)
485 [MonoTODO] // what if value was negative?
486 public static decimal DoubleToNonNegativeInteger (double value)
488 throw new NotImplementedException ();
491 [MonoTODO] // what if value was positive?
492 public static decimal DoubleToNonPositiveInteger (double value)
494 throw new NotImplementedException ();
498 public static string DoubleToString (double value)
500 return XmlConvert.ToString (value);
504 public static TimeSpan DurationToDayTimeDuration (string value)
506 return XmlConvert.ToTimeSpan (value);
510 public static string DurationToString (string value)
512 return XmlConvert.ToString (XmlConvert.ToTimeSpan (value));
516 public static TimeSpan DurationToYearMonthDuration (string value)
518 return XmlConvert.ToTimeSpan (value);
523 public static bool FloatToBoolean (float value)
529 public static decimal FloatToDecimal (float value)
531 return (decimal) value;
535 public static double FloatToDouble (float value)
537 return (double) value;
541 public static int FloatToInt (float value)
547 public static long FloatToInteger (float value)
552 [MonoTODO] // what if value was negative?
553 public static decimal FloatToNonNegativeInteger (float value)
555 throw new NotImplementedException ();
558 [MonoTODO] // what if value was positive?
559 public static decimal FloatToNonPositiveInteger (float value)
561 throw new NotImplementedException ();
565 public static string FloatToString (float value)
567 return XmlConvert.ToString (value);
571 public static string GDayToString (DateTime value)
573 return XmlConvert.ToString (TimeSpan.FromDays (value.Day));
577 public static string GMonthDayToString (DateTime value)
579 return XmlConvert.ToString (new TimeSpan (value.Day, value.Hour, value.Minute, value.Second));
583 public static string GMonthToString (DateTime value)
585 return XmlConvert.ToString (new TimeSpan (0, value.Month, 0));
589 public static string GYearMonthToString (DateTime value)
591 return XmlConvert.ToString (new TimeSpan (value.Year, value.Month, 0));
595 public static string GYearToString (DateTime value)
597 return XmlConvert.ToString (new TimeSpan (new DateTime (value.Year, 0, 0).Ticks));
600 public static string HexBinaryToString (byte [] data)
602 return XmlConvert.ToBinHexString (data);
606 public static byte [] HexBinaryToBase64Binary (byte [] data)
608 return data;//XmlConvert.ToBinHexString (data);
613 public static bool IntegerToBoolean (long value)
619 public static decimal IntegerToDecimal (long value)
621 return (decimal) value;
625 public static double IntegerToDouble (long value)
627 return (double) value;
631 public static float IntegerToFloat (long value)
633 return (float) value;
637 public static int IntegerToInt (long value)
643 public static string IntegerToString (long value)
645 return XmlConvert.ToString (value);
649 public static bool IntToBoolean (int value)
655 public static decimal IntToDecimal (int value)
657 return (decimal) value;
661 public static double IntToDouble (int value)
663 return (double) value;
667 public static float IntToFloat (int value)
669 return (float) value;
673 public static long IntToInteger (int value)
679 public static string IntToString (int value)
681 return XmlConvert.ToString (value);
685 public static string NonNegativeIntegerToString (decimal value)
687 return XmlConvert.ToString (value);
691 public static string NonPositiveIntegerToString (decimal value)
693 return XmlConvert.ToString (value);
697 public static DateTime TimeToDateTime (DateTime value)
703 public static string TimeToString (DateTime value)
705 return XmlConvert.ToString (value, "HH:mm:ssZ");
709 public static string YearMonthDurationToDuration (TimeSpan value)
711 return XmlConvert.ToString (value);
715 public static string YearMonthDurationToString (TimeSpan value)
717 return YearMonthDurationToDuration (value);
721 public static string StringToAnyUri (string value)
727 public static byte [] StringToBase64Binary (string value)
729 return Convert.FromBase64String (value);
733 public static bool StringToBoolean (string value)
735 return XmlConvert.ToBoolean (value);
739 public static DateTime StringToDate (string value)
741 return XmlConvert.ToDateTime (value);
745 public static DateTime StringToDateTime (string value)
747 return XmlConvert.ToDateTime (value);
751 public static TimeSpan StringToDayTimeDuration (string value)
753 return XmlConvert.ToTimeSpan (value);
757 public static decimal StringToDecimal (string value)
759 return XmlConvert.ToDecimal (value);
763 public static double StringToDouble (string value)
765 return XmlConvert.ToDouble (value);
769 public static string StringToDuration (string value)
771 return XmlConvert.ToString (XmlConvert.ToTimeSpan (value));
775 public static float StringToFloat (string value)
777 return XmlConvert.ToSingle (value);
781 public static DateTime StringToGDay (string value)
783 return XmlConvert.ToDateTime (value);
787 public static DateTime StringToGMonth (string value)
789 return XmlConvert.ToDateTime (value);
793 public static DateTime StringToGMonthDay (string value)
795 return XmlConvert.ToDateTime (value);
799 public static DateTime StringToGYear (string value)
801 return XmlConvert.ToDateTime (value);
805 public static DateTime StringToGYearMonth (string value)
807 return XmlConvert.ToDateTime (value);
811 public static byte [] StringToHexBinary (string value)
813 return XmlConvert.FromBinHexString (value);
817 public static int StringToInt (string value)
819 return XmlConvert.ToInt32 (value);
823 public static long StringToInteger (string value)
825 return XmlConvert.ToInt64 (value);
829 public static decimal StringToNonNegativeInteger (string value)
831 return XmlConvert.ToDecimal (value);
835 public static decimal StringToNonPositiveInteger (string value)
837 return XmlConvert.ToDecimal (value);
841 public static DateTime StringToTime (string value)
843 return XmlConvert.ToDateTime (value);
847 public static long StringToUnsignedInt (string value)
849 return XmlConvert.ToInt32 (value);
853 public static decimal StringToUnsignedLong (string value)
855 return XmlConvert.ToInt32 (value);
859 public static int StringToUnsignedShort (string value)
861 return XmlConvert.ToInt32 (value);
865 public static TimeSpan StringToYearMonthDuration (string value)
867 return XmlConvert.ToTimeSpan (value);
871 public static string ItemToAnyUri (XPathItem value)
877 public static byte [] ItemToBase64Binary (XPathItem value)
879 return Convert.FromBase64String (value.Value);
883 public static bool ItemToBoolean (XPathItem value)
885 return XmlConvert.ToBoolean (value.Value);
889 public static DateTime ItemToDate (XPathItem value)
891 return XmlConvert.ToDateTime (value.Value);
895 public static DateTime ItemToDateTime (XPathItem value)
897 return XmlConvert.ToDateTime (value.Value);
901 public static TimeSpan ItemToDayTimeDuration (XPathItem value)
903 return XmlConvert.ToTimeSpan (value.Value);
907 public static decimal ItemToDecimal (XPathItem value)
909 return XmlConvert.ToDecimal (value.Value);
913 public static double ItemToDouble (XPathItem value)
915 return XmlConvert.ToDouble (value.Value);
919 public static string ItemToDuration (XPathItem value)
921 return XmlConvert.ToString (XmlConvert.ToTimeSpan (value.Value));
925 public static float ItemToFloat (XPathItem value)
927 return XmlConvert.ToSingle (value.Value);
931 public static DateTime ItemToGDay (XPathItem value)
933 return XmlConvert.ToDateTime (value.Value);
937 public static DateTime ItemToGMonth (XPathItem value)
939 return XmlConvert.ToDateTime (value.Value);
943 public static DateTime ItemToGMonthDay (XPathItem value)
945 return XmlConvert.ToDateTime (value.Value);
949 public static DateTime ItemToGYear (XPathItem value)
951 return XmlConvert.ToDateTime (value.Value);
955 public static DateTime ItemToGYearMonth (XPathItem value)
957 return XmlConvert.ToDateTime (value.Value);
961 public static byte [] ItemToHexBinary (XPathItem value)
963 return XmlConvert.FromBinHexString (value.Value);
967 public static int ItemToInt (XPathItem value)
969 return XmlConvert.ToInt32 (value.Value);
973 public static long ItemToInteger (XPathItem value)
975 return XmlConvert.ToInt64 (value.Value);
979 public static XPathItem ItemToItem (XPathItem value, XmlSchemaType schemaTypeDest)
981 return new XPathAtomicValue (value.Value, schemaTypeDest);
985 public static decimal ItemToNonNegativeInteger (XPathItem value)
987 return XmlConvert.ToDecimal (value.Value);
991 public static decimal ItemToNonPositiveInteger (XPathItem value)
993 return XmlConvert.ToDecimal (value.Value);
997 public static XmlQualifiedName ItemToQName (XPathItem value)
999 return (XmlQualifiedName) value.TypedValue;
1003 public static string ItemToString (XPathItem value)
1005 if (value.ValueType == typeof (DateTime))
1006 return XmlConvert.ToString ((DateTime) value.TypedValue);
1007 if (value.TypedValue is XmlQualifiedName)
1008 throw new ArgumentException ("Invalid cast from schema QName type to string type.");
1013 public static DateTime ItemToTime (XPathItem value)
1015 return XmlConvert.ToDateTime (value.Value);
1019 public static long ItemToUnsignedInt (XPathItem value)
1021 return XmlConvert.ToInt32 (value.Value);
1025 public static decimal ItemToUnsignedLong (XPathItem value)
1027 return XmlConvert.ToInt32 (value.Value);
1031 public static int ItemToUnsignedShort (XPathItem value)
1033 return XmlConvert.ToInt32 (value.Value);
1037 public static TimeSpan ItemToYearMonthDuration (XPathItem value)
1039 return XmlConvert.ToTimeSpan (value.Value);