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
DateTimeToGYear (DateTime
value)
326 return new DateTime (value.Year
, 0, 0);
330 public static DateTime
DateTimeToGYearMonth (DateTime
value)
332 return new DateTime (value.Year
, value.Month
, 0);
336 public static DateTime
DateToDateTime (DateTime
value)
342 public static DateTime
DateToGDay (DateTime
value)
344 return new DateTime (0, 0, value.Day
);
348 public static DateTime
DateToGMonth (DateTime
value)
350 return new DateTime (0, value.Month
, 0);
354 public static DateTime
DateToGYear (DateTime
value)
356 return new DateTime (value.Year
, 0, 0);
360 public static DateTime
DateToGYearMonth (DateTime
value)
362 return new DateTime (value.Year
, value.Month
, 0);
366 public static string DateToString (DateTime
value)
368 return XmlConvert
.ToString (value);
372 public static string DateTimeToString (DateTime
value)
374 return XmlConvert
.ToString (value);
378 public static string DayTimeDurationToDuration (TimeSpan
value)
380 return XmlConvert
.ToString (value);
384 public static string DayTimeDurationToString (TimeSpan
value)
386 return DayTimeDurationToDuration (value);
390 public static bool DecimalToBoolean (decimal value)
396 public static double DecimalToDouble (decimal value)
398 return (double) value;
402 public static float DecimalToFloat (decimal value)
404 return (float) value;
408 public static int DecimalToInt (decimal value)
414 public static long DecimalToInteger (decimal value)
419 [MonoTODO
] // what if value was negative?
420 public static decimal DecimalToNonNegativeInteger (decimal value)
422 throw new NotImplementedException ();
425 [MonoTODO
] // what if value was positive?
426 public static decimal DecimalToNonPositiveInteger (decimal value)
428 throw new NotImplementedException ();
432 public static string DecimalToString (decimal value)
434 return XmlConvert
.ToString (value);
438 public static bool DoubleToBoolean (double value)
444 public static decimal DoubleToDecimal (double value)
446 return (decimal) value;
450 public static float DoubleToFloat (double value)
452 return (float) value;
456 public static int DoubleToInt (double value)
462 public static long DoubleToInteger (double value)
467 [MonoTODO
] // what if value was negative?
468 public static decimal DoubleToNonNegativeInteger (double value)
470 throw new NotImplementedException ();
473 [MonoTODO
] // what if value was positive?
474 public static decimal DoubleToNonPositiveInteger (double value)
476 throw new NotImplementedException ();
480 public static string DoubleToString (double value)
482 return XmlConvert
.ToString (value);
486 public static TimeSpan
DurationToDayTimeDuration (string value)
488 return XmlConvert
.ToTimeSpan (value);
492 public static string DurationToString (string value)
494 return XmlConvert
.ToString (XmlConvert
.ToTimeSpan (value));
498 public static TimeSpan
DurationToYearMonthDuration (string value)
500 return XmlConvert
.ToTimeSpan (value);
505 public static bool FloatToBoolean (float value)
511 public static decimal FloatToDecimal (float value)
513 return (decimal) value;
517 public static double FloatToDouble (float value)
519 return (double) value;
523 public static int FloatToInt (float value)
529 public static long FloatToInteger (float value)
534 [MonoTODO
] // what if value was negative?
535 public static decimal FloatToNonNegativeInteger (float value)
537 throw new NotImplementedException ();
540 [MonoTODO
] // what if value was positive?
541 public static decimal FloatToNonPositiveInteger (float value)
543 throw new NotImplementedException ();
547 public static string FloatToString (float value)
549 return XmlConvert
.ToString (value);
553 public static string GDayToString (DateTime
value)
555 return XmlConvert
.ToString (TimeSpan
.FromDays (value.Day
));
559 public static string GMonthDayToString (DateTime
value)
561 return XmlConvert
.ToString (new TimeSpan (value.Day
, value.Hour
, value.Minute
, value.Second
));
565 public static string GMonthToString (DateTime
value)
567 return XmlConvert
.ToString (new TimeSpan (0, value.Month
, 0));
571 public static string GYearMonthToString (DateTime
value)
573 return XmlConvert
.ToString (new TimeSpan (value.Year
, value.Month
, 0));
577 public static string GYearToString (DateTime
value)
579 return XmlConvert
.ToString (new TimeSpan (new DateTime (value.Year
, 0, 0).Ticks
));
582 public static string HexBinaryToString (byte [] data
)
584 return XmlConvert
.ToBinHexString (data
);
588 public static byte [] HexBinaryToBase64Binary (byte [] data
)
590 return data
;//XmlConvert.ToBinHexString (data);
595 public static bool IntegerToBoolean (long value)
601 public static decimal IntegerToDecimal (long value)
603 return (decimal) value;
607 public static double IntegerToDouble (long value)
609 return (double) value;
613 public static float IntegerToFloat (long value)
615 return (float) value;
619 public static int IntegerToInt (long value)
625 public static string IntegerToString (long value)
627 return XmlConvert
.ToString (value);
631 public static bool IntToBoolean (int value)
637 public static decimal IntToDecimal (int value)
639 return (decimal) value;
643 public static double IntToDouble (int value)
645 return (double) value;
649 public static float IntToFloat (int value)
651 return (float) value;
655 public static long IntToInteger (int value)
661 public static string IntToString (int value)
663 return XmlConvert
.ToString (value);
667 public static string NonNegativeIntegerToString (decimal value)
669 return XmlConvert
.ToString (value);
673 public static string NonPositiveIntegerToString (decimal value)
675 return XmlConvert
.ToString (value);
679 public static DateTime
TimeToDateTime (DateTime
value)
685 public static string TimeToString (DateTime
value)
687 return XmlConvert
.ToString (value, "HH:mm:ssZ");
691 public static string YearMonthDurationToDuration (TimeSpan
value)
693 return XmlConvert
.ToString (value);
697 public static string YearMonthDurationToString (TimeSpan
value)
699 return YearMonthDurationToDuration (value);
703 public static string StringToAnyUri (string value)
709 public static byte [] StringToBase64Binary (string value)
711 return Convert
.FromBase64String (value);
715 public static bool StringToBoolean (string value)
717 return XmlConvert
.ToBoolean (value);
721 public static DateTime
StringToDate (string value)
723 return XmlConvert
.ToDateTime (value);
727 public static DateTime
StringToDateTime (string value)
729 return XmlConvert
.ToDateTime (value);
733 public static TimeSpan
StringToDayTimeDuration (string value)
735 return XmlConvert
.ToTimeSpan (value);
739 public static decimal StringToDecimal (string value)
741 return XmlConvert
.ToDecimal (value);
745 public static double StringToDouble (string value)
747 return XmlConvert
.ToDouble (value);
751 public static string StringToDuration (string value)
753 return XmlConvert
.ToString (XmlConvert
.ToTimeSpan (value));
757 public static float StringToFloat (string value)
759 return XmlConvert
.ToSingle (value);
763 public static DateTime
StringToGDay (string value)
765 return XmlConvert
.ToDateTime (value);
769 public static DateTime
StringToGMonth (string value)
771 return XmlConvert
.ToDateTime (value);
775 public static DateTime
StringToGMonthDay (string value)
777 return XmlConvert
.ToDateTime (value);
781 public static DateTime
StringToGYear (string value)
783 return XmlConvert
.ToDateTime (value);
787 public static DateTime
StringToGYearMonth (string value)
789 return XmlConvert
.ToDateTime (value);
793 public static byte [] StringToHexBinary (string value)
795 return XmlConvert
.FromBinHexString (value);
799 public static int StringToInt (string value)
801 return XmlConvert
.ToInt32 (value);
805 public static long StringToInteger (string value)
807 return XmlConvert
.ToInt64 (value);
811 public static decimal StringToNonNegativeInteger (string value)
813 return XmlConvert
.ToDecimal (value);
817 public static decimal StringToNonPositiveInteger (string value)
819 return XmlConvert
.ToDecimal (value);
823 public static DateTime
StringToTime (string value)
825 return XmlConvert
.ToDateTime (value);
829 public static long StringToUnsignedInt (string value)
831 return XmlConvert
.ToInt32 (value);
835 public static decimal StringToUnsignedLong (string value)
837 return XmlConvert
.ToInt32 (value);
841 public static int StringToUnsignedShort (string value)
843 return XmlConvert
.ToInt32 (value);
847 public static TimeSpan
StringToYearMonthDuration (string value)
849 return XmlConvert
.ToTimeSpan (value);
853 public static string ItemToAnyUri (XPathItem
value)
859 public static byte [] ItemToBase64Binary (XPathItem
value)
861 return Convert
.FromBase64String (value.Value
);
865 public static bool ItemToBoolean (XPathItem
value)
867 return XmlConvert
.ToBoolean (value.Value
);
871 public static DateTime
ItemToDate (XPathItem
value)
873 return XmlConvert
.ToDateTime (value.Value
);
877 public static DateTime
ItemToDateTime (XPathItem
value)
879 return XmlConvert
.ToDateTime (value.Value
);
883 public static TimeSpan
ItemToDayTimeDuration (XPathItem
value)
885 return XmlConvert
.ToTimeSpan (value.Value
);
889 public static decimal ItemToDecimal (XPathItem
value)
891 return XmlConvert
.ToDecimal (value.Value
);
895 public static double ItemToDouble (XPathItem
value)
897 return XmlConvert
.ToDouble (value.Value
);
901 public static string ItemToDuration (XPathItem
value)
903 return XmlConvert
.ToString (XmlConvert
.ToTimeSpan (value.Value
));
907 public static float ItemToFloat (XPathItem
value)
909 return XmlConvert
.ToSingle (value.Value
);
913 public static DateTime
ItemToGDay (XPathItem
value)
915 return XmlConvert
.ToDateTime (value.Value
);
919 public static DateTime
ItemToGMonth (XPathItem
value)
921 return XmlConvert
.ToDateTime (value.Value
);
925 public static DateTime
ItemToGMonthDay (XPathItem
value)
927 return XmlConvert
.ToDateTime (value.Value
);
931 public static DateTime
ItemToGYear (XPathItem
value)
933 return XmlConvert
.ToDateTime (value.Value
);
937 public static DateTime
ItemToGYearMonth (XPathItem
value)
939 return XmlConvert
.ToDateTime (value.Value
);
943 public static byte [] ItemToHexBinary (XPathItem
value)
945 return XmlConvert
.FromBinHexString (value.Value
);
949 public static int ItemToInt (XPathItem
value)
951 return XmlConvert
.ToInt32 (value.Value
);
955 public static long ItemToInteger (XPathItem
value)
957 return XmlConvert
.ToInt64 (value.Value
);
961 public static XPathItem
ItemToItem (XPathItem
value, XmlSchemaType schemaTypeDest
)
963 return new XPathAtomicValue (value.Value
, schemaTypeDest
);
967 public static decimal ItemToNonNegativeInteger (XPathItem
value)
969 return XmlConvert
.ToDecimal (value.Value
);
973 public static decimal ItemToNonPositiveInteger (XPathItem
value)
975 return XmlConvert
.ToDecimal (value.Value
);
979 public static XmlQualifiedName
ItemToQName (XPathItem
value)
981 return (XmlQualifiedName
) value.TypedValue
;
985 public static string ItemToString (XPathItem
value)
987 if (value.ValueType
== typeof (DateTime
))
988 return XmlConvert
.ToString ((DateTime
) value.TypedValue
);
989 if (value.TypedValue
is XmlQualifiedName
)
990 throw new ArgumentException ("Invalid cast from schema QName type to string type.");
995 public static DateTime
ItemToTime (XPathItem
value)
997 return XmlConvert
.ToDateTime (value.Value
);
1001 public static long ItemToUnsignedInt (XPathItem
value)
1003 return XmlConvert
.ToInt32 (value.Value
);
1007 public static decimal ItemToUnsignedLong (XPathItem
value)
1009 return XmlConvert
.ToInt32 (value.Value
);
1013 public static int ItemToUnsignedShort (XPathItem
value)
1015 return XmlConvert
.ToInt32 (value.Value
);
1019 public static TimeSpan
ItemToYearMonthDuration (XPathItem
value)
1021 return XmlConvert
.ToTimeSpan (value.Value
);