Bug 15574. XML deserialization recursion: add array type to known_types?
[mono.git] / mcs / class / System.XML / System.Xml / XQueryConvert.cs
1 //
2 // System.Xml.Query.XQueryConvert
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2004 Novell Inc.
8 //
9
10 //
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.
29 //
30 #if NET_2_0
31
32 using System;
33 using System.Globalization;
34 using System.IO;
35 using System.Text;
36 using System.Xml;
37 using System.Xml.Schema;
38
39 namespace System.Xml
40 {
41         // basically this class is obsoleted and removed from .NET 2.0.
42         internal class XQueryConvert
43         {
44                 private XQueryConvert ()
45                 {
46                 }
47
48                 public static XmlTypeCode GetFallbackType (XmlTypeCode type)
49                 {
50                         switch (type) {
51                         case XmlTypeCode.AnyAtomicType:
52                                 return XmlTypeCode.Item;
53                         case XmlTypeCode.UntypedAtomic:
54                                 return XmlTypeCode.String;
55                         case XmlTypeCode.Notation:
56                                 return XmlTypeCode.QName;
57                         case XmlTypeCode.NormalizedString:
58                         case XmlTypeCode.Token:
59                         case XmlTypeCode.Language:
60                         case XmlTypeCode.NmToken:
61                         case XmlTypeCode.Name:
62                         case XmlTypeCode.NCName:
63                         case XmlTypeCode.Id:
64                         case XmlTypeCode.Idref:
65                         case XmlTypeCode.Entity:
66                                 return XmlTypeCode.String;
67                         case XmlTypeCode.NonPositiveInteger:
68                                 return XmlTypeCode.Decimal;
69                         case XmlTypeCode.NegativeInteger:
70                                 return XmlTypeCode.NonPositiveInteger;
71                         case XmlTypeCode.Long:
72                                 return XmlTypeCode.Integer;
73                         case XmlTypeCode.Short:
74                                 return XmlTypeCode.Int;
75                         case XmlTypeCode.Byte:
76                                 return XmlTypeCode.Int;
77                         case XmlTypeCode.NonNegativeInteger:
78                                 return XmlTypeCode.Decimal;
79                         case XmlTypeCode.UnsignedLong:
80                                 return XmlTypeCode.NonNegativeInteger;
81                         case XmlTypeCode.UnsignedInt:
82                                 return XmlTypeCode.Integer;
83                         case XmlTypeCode.UnsignedShort:
84                                 return XmlTypeCode.Int;
85                         case XmlTypeCode.UnsignedByte:
86                                 return XmlTypeCode.UnsignedShort;
87                         case XmlTypeCode.PositiveInteger:
88                                 return XmlTypeCode.NonNegativeInteger;
89                         default:
90                                 return XmlTypeCode.None;
91                         }
92                 }
93
94                 // Individual conversion
95
96                 public static string AnyUriToString (string value)
97                 {
98                         return value;
99                 }
100
101                 public static byte [] Base64BinaryToHexBinary (byte [] value)
102                 {
103                         return XmlConvert.FromBinHexString (Convert.ToBase64String (value));
104                 }
105
106                 public static string Base64BinaryToString (byte [] value)
107                 {
108                         return Convert.ToBase64String (value);
109                 }
110
111                 public static decimal BooleanToDecimal (bool value)
112                 {
113                         return Convert.ToDecimal (value);
114                 }
115
116                 public static double BooleanToDouble (bool value)
117                 {
118                         return Convert.ToDouble (value);
119                 }
120
121                 public static float BooleanToFloat (bool value)
122                 {
123                         return Convert.ToSingle (value);
124                 }
125
126                 public static int BooleanToInt (bool value)
127                 {
128                         return Convert.ToInt32 (value);
129                 }
130
131                 public static long BooleanToInteger (bool value)
132                 {
133                         return Convert.ToInt64 (value);
134                 }
135
136                 public static string BooleanToString (bool value)
137                 {
138                         // It looks not returning "True"
139                         return value ? "true" : "false";
140                 }
141
142                 public static DateTime DateTimeToDate (DateTime value)
143                 {
144                         return value.Date;
145                 }
146
147                 public static DateTime DateTimeToGDay (DateTime value)
148                 {
149                         return new DateTime (0, 0, value.Day);
150                 }
151
152                 public static DateTime DateTimeToGMonth (DateTime value)
153                 {
154                         return new DateTime (0, value.Month, 0);
155                 }
156
157                 public static DateTime DateTimeToGMonthDay (DateTime value)
158                 {
159                         return new DateTime (0, value.Month, value.Day);
160                 }
161
162                 public static DateTime DateTimeToGYear (DateTime value)
163                 {
164                         return new DateTime (value.Year, 0, 0);
165                 }
166
167                 public static DateTime DateTimeToGYearMonth (DateTime value)
168                 {
169                         return new DateTime (value.Year, value.Month, 0);
170                 }
171
172                 public static DateTime DateTimeToTime (DateTime value)
173                 {
174                         return new DateTime (value.TimeOfDay.Ticks);
175                 }
176
177                 public static DateTime DateToDateTime (DateTime value)
178                 {
179                         return value.Date;
180                 }
181
182                 public static DateTime DateToGDay (DateTime value)
183                 {
184                         return new DateTime (0, 0, value.Day);
185                 }
186
187                 public static DateTime DateToGMonth (DateTime value)
188                 {
189                         return new DateTime (0, value.Month, 0);
190                 }
191
192                 public static DateTime DateToGMonthDay (DateTime value)
193                 {
194                         return new DateTime (0, value.Month, value.Day);
195                 }
196
197                 public static DateTime DateToGYear (DateTime value)
198                 {
199                         return new DateTime (value.Year, 0, 0);
200                 }
201
202                 public static DateTime DateToGYearMonth (DateTime value)
203                 {
204                         return new DateTime (value.Year, value.Month, 0);
205                 }
206
207                 public static string DateToString (DateTime value)
208                 {
209                         return XmlConvert.ToString (value);
210                 }
211
212                 public static string DateTimeToString (DateTime value)
213                 {
214                         return XmlConvert.ToString (value);
215                 }
216
217                 public static string DayTimeDurationToDuration (TimeSpan value)
218                 {
219                         return XmlConvert.ToString (value);
220                 }
221
222                 public static string DayTimeDurationToString (TimeSpan value)
223                 {
224                         return DayTimeDurationToDuration (value);
225                 }
226
227                 public static bool DecimalToBoolean (decimal value)
228                 {
229                         return value != 0;
230                 }
231
232                 public static double DecimalToDouble (decimal value)
233                 {
234                         return Convert.ToDouble (value);
235                 }
236
237                 public static float DecimalToFloat (decimal value)
238                 {
239                         return Convert.ToSingle (value);
240                 }
241
242                 public static int DecimalToInt (decimal value)
243                 {
244                         return Convert.ToInt32 (value);
245                 }
246
247                 public static long DecimalToInteger (decimal value)
248                 {
249                         return Convert.ToInt64 (value);
250                 }
251
252                 public static string DecimalToString (decimal value)
253                 {
254                         return XmlConvert.ToString (value);
255                 }
256
257                 public static bool DoubleToBoolean (double value)
258                 {
259                         return value != 0;
260                 }
261
262                 public static decimal DoubleToDecimal (double value)
263                 {
264                         return (decimal) value;
265                 }
266
267                 public static float DoubleToFloat (double value)
268                 {
269                         return Convert.ToSingle (value);
270                 }
271
272                 public static int DoubleToInt (double value)
273                 {
274                         return Convert.ToInt32 (value);
275                 }
276
277                 public static long DoubleToInteger (double value)
278                 {
279                         return Convert.ToInt64 (value);
280                 }
281
282                 public static string DoubleToString (double value)
283                 {
284                         return XmlConvert.ToString (value);
285                 }
286
287                 public static TimeSpan DurationToDayTimeDuration (string value)
288                 {
289                         return XmlConvert.ToTimeSpan (value);
290                 }
291
292                 public static string DurationToString (string value)
293                 {
294                         return XmlConvert.ToString (XmlConvert.ToTimeSpan (value));
295                 }
296
297                 public static TimeSpan DurationToYearMonthDuration (string value)
298                 {
299                         return XmlConvert.ToTimeSpan (value);
300                 }
301
302
303                 public static bool FloatToBoolean (float value)
304                 {
305                         return value != 0;
306                 }
307
308                 public static decimal FloatToDecimal (float value)
309                 {
310                         return (decimal) value;
311                 }
312
313                 public static double FloatToDouble (float value)
314                 {
315                         return Convert.ToDouble (value);
316                 }
317
318                 public static int FloatToInt (float value)
319                 {
320                         return Convert.ToInt32 (value);
321                 }
322
323                 public static long FloatToInteger (float value)
324                 {
325                         return Convert.ToInt64 (value);
326                 }
327
328                 public static string FloatToString (float value)
329                 {
330                         return XmlConvert.ToString (value);
331                 }
332
333                 public static string GDayToString (DateTime value)
334                 {
335                         return XmlConvert.ToString (TimeSpan.FromDays (value.Day));
336                 }
337
338                 public static string GMonthDayToString (DateTime value)
339                 {
340                         return XmlConvert.ToString (new TimeSpan (value.Day, value.Hour, value.Minute, value.Second));
341                 }
342
343                 public static string GMonthToString (DateTime value)
344                 {
345                         return XmlConvert.ToString (new TimeSpan (0, value.Month, 1));
346                 }
347
348                 public static string GYearMonthToString (DateTime value)
349                 {
350                         return XmlConvert.ToString (new TimeSpan (value.Year, value.Month, 1));
351                 }
352
353                 public static string GYearToString (DateTime value)
354                 {
355                         return XmlConvert.ToString (new TimeSpan (new DateTime (value.Year, 1, 1).Ticks));
356                 }
357
358                 public static string HexBinaryToString (byte [] data)
359                 {
360                         return XmlConvert.ToBinHexString (data);
361                 }
362
363                 public static byte [] HexBinaryToBase64Binary (byte [] data)
364                 {
365                         return data;//XmlConvert.ToBinHexString (data);
366                 }
367
368
369                 public static bool IntegerToBoolean (long value)
370                 {
371                         return value != 0;
372                 }
373
374                 public static decimal IntegerToDecimal (long value)
375                 {
376                         return (decimal) value;
377                 }
378
379                 public static decimal IntegerToDecimal (ulong value)
380                 {
381                         return (decimal) value;
382                 }
383
384                 public static double IntegerToDouble (long value)
385                 {
386                         return Convert.ToDouble (value);
387                 }
388
389                 public static float IntegerToFloat (long value)
390                 {
391                         return Convert.ToSingle (value);
392                 }
393
394                 public static int IntegerToInt (long value)
395                 {
396                         return Convert.ToInt32 (value);
397                 }
398
399                 public static string IntegerToString (long value)
400                 {
401                         return XmlConvert.ToString (value);
402                 }
403
404                 public static bool IntToBoolean (int value)
405                 {
406                         return value != 0;
407                 }
408
409                 public static decimal IntToDecimal (int value)
410                 {
411                         return (decimal) value;
412                 }
413
414                 public static double IntToDouble (int value)
415                 {
416                         return Convert.ToDouble (value);
417                 }
418
419                 public static float IntToFloat (int value)
420                 {
421                         return Convert.ToSingle (value);
422                 }
423
424                 public static long IntToInteger (int value)
425                 {
426                         return value;
427                 }
428
429                 public static string IntToString (int value)
430                 {
431                         return XmlConvert.ToString (value);
432                 }
433
434                 public static string NonNegativeIntegerToString (decimal value)
435                 {
436                         return XmlConvert.ToString (value);
437                 }
438
439                 public static string NonPositiveIntegerToString (decimal value)
440                 {
441                         return XmlConvert.ToString (value);
442                 }
443
444                 public static DateTime TimeToDateTime (DateTime value)
445                 {
446                         return value;
447                 }
448
449                 public static string TimeToString (DateTime value)
450                 {
451                         return XmlConvert.ToString (value, "HH:mm:ssZ");
452                 }
453
454                 public static string YearMonthDurationToDuration (TimeSpan value)
455                 {
456                         return XmlConvert.ToString (value);
457                 }
458
459                 public static string YearMonthDurationToString (TimeSpan value)
460                 {
461                         return YearMonthDurationToDuration (value);
462                 }
463
464                 public static string StringToAnyUri (string value)
465                 {
466                         return value;
467                 }
468
469                 public static byte [] StringToBase64Binary (string value)
470                 {
471                         return Convert.FromBase64String (value);
472                 }
473
474                 public static bool StringToBoolean (string value)
475                 {
476                         return XmlConvert.ToBoolean (value);
477                 }
478
479                 public static DateTime StringToDate (string value)
480                 {
481                         return XmlConvert.ToDateTime (value);
482                 }
483
484                 public static DateTime StringToDateTime (string value)
485                 {
486                         return XmlConvert.ToDateTime (value);
487                 }
488
489                 public static TimeSpan StringToDayTimeDuration (string value)
490                 {
491                         return XmlConvert.ToTimeSpan (value);
492                 }
493
494                 public static decimal StringToDecimal (string value)
495                 {
496                         return XmlConvert.ToDecimal (value);
497                 }
498
499                 public static double StringToDouble (string value)
500                 {
501                         return XmlConvert.ToDouble (value);
502                 }
503
504                 public static string StringToDuration (string value)
505                 {
506                         return XmlConvert.ToString (XmlConvert.ToTimeSpan (value));
507                 }
508
509                 public static float StringToFloat (string value)
510                 {
511                         return XmlConvert.ToSingle (value);
512                 }
513
514                 public static DateTime StringToGDay (string value)
515                 {
516                         return XmlConvert.ToDateTime (value);
517                 }
518
519                 public static DateTime StringToGMonth (string value)
520                 {
521                         return XmlConvert.ToDateTime (value);
522                 }
523
524                 public static DateTime StringToGMonthDay (string value)
525                 {
526                         return XmlConvert.ToDateTime (value);
527                 }
528
529                 public static DateTime StringToGYear (string value)
530                 {
531                         return XmlConvert.ToDateTime (value);
532                 }
533
534                 public static DateTime StringToGYearMonth (string value)
535                 {
536                         return XmlConvert.ToDateTime (value);
537                 }
538
539                 public static byte [] StringToHexBinary (string value)
540                 {
541                         return XmlConvert.FromBinHexString (value);
542                 }
543
544                 public static int StringToInt (string value)
545                 {
546                         return XmlConvert.ToInt32 (value);
547                 }
548
549                 public static long StringToInteger (string value)
550                 {
551                         return XmlConvert.ToInt64 (value);
552                 }
553
554                 public static decimal StringToNonNegativeInteger (string value)
555                 {
556                         return XmlConvert.ToDecimal (value);
557                 }
558
559                 public static decimal StringToNonPositiveInteger (string value)
560                 {
561                         return XmlConvert.ToDecimal (value);
562                 }
563
564                 public static DateTime StringToTime (string value)
565                 {
566                         return XmlConvert.ToDateTime (value);
567                 }
568
569                 public static long StringToUnsignedInt (string value)
570                 {
571                         return XmlConvert.ToUInt32 (value);
572                 }
573
574                 public static decimal StringToUnsignedLong (string value)
575                 {
576                         return XmlConvert.ToUInt64 (value);
577                 }
578
579                 public static int StringToUnsignedShort (string value)
580                 {
581                         return XmlConvert.ToUInt16 (value);
582                 }
583
584                 public static TimeSpan StringToYearMonthDuration (string value)
585                 {
586                         return XmlConvert.ToTimeSpan (value);
587                 }
588         }
589 }
590
591 #endif