XQueryConvert.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. //
  2. // System.Xml.Query.XQueryConvert
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2004 Novell Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Globalization;
  32. using System.IO;
  33. using System.Text;
  34. using System.Xml;
  35. using System.Xml.Schema;
  36. namespace System.Xml
  37. {
  38. // basically this class is obsoleted and removed from .NET 2.0.
  39. internal class XQueryConvert
  40. {
  41. private XQueryConvert ()
  42. {
  43. }
  44. public static XmlTypeCode GetFallbackType (XmlTypeCode type)
  45. {
  46. switch (type) {
  47. case XmlTypeCode.AnyAtomicType:
  48. return XmlTypeCode.Item;
  49. case XmlTypeCode.UntypedAtomic:
  50. return XmlTypeCode.String;
  51. case XmlTypeCode.Notation:
  52. return XmlTypeCode.QName;
  53. case XmlTypeCode.NormalizedString:
  54. case XmlTypeCode.Token:
  55. case XmlTypeCode.Language:
  56. case XmlTypeCode.NmToken:
  57. case XmlTypeCode.Name:
  58. case XmlTypeCode.NCName:
  59. case XmlTypeCode.Id:
  60. case XmlTypeCode.Idref:
  61. case XmlTypeCode.Entity:
  62. return XmlTypeCode.String;
  63. case XmlTypeCode.NonPositiveInteger:
  64. return XmlTypeCode.Decimal;
  65. case XmlTypeCode.NegativeInteger:
  66. return XmlTypeCode.NonPositiveInteger;
  67. case XmlTypeCode.Long:
  68. return XmlTypeCode.Integer;
  69. case XmlTypeCode.Short:
  70. return XmlTypeCode.Int;
  71. case XmlTypeCode.Byte:
  72. return XmlTypeCode.Int;
  73. case XmlTypeCode.NonNegativeInteger:
  74. return XmlTypeCode.Decimal;
  75. case XmlTypeCode.UnsignedLong:
  76. return XmlTypeCode.NonNegativeInteger;
  77. case XmlTypeCode.UnsignedInt:
  78. return XmlTypeCode.Integer;
  79. case XmlTypeCode.UnsignedShort:
  80. return XmlTypeCode.Int;
  81. case XmlTypeCode.UnsignedByte:
  82. return XmlTypeCode.UnsignedShort;
  83. case XmlTypeCode.PositiveInteger:
  84. return XmlTypeCode.NonNegativeInteger;
  85. default:
  86. return XmlTypeCode.None;
  87. }
  88. }
  89. // Individual conversion
  90. public static string AnyUriToString (string value)
  91. {
  92. return value;
  93. }
  94. public static byte [] Base64BinaryToHexBinary (byte [] value)
  95. {
  96. return XmlConvert.FromBinHexString (Convert.ToBase64String (value));
  97. }
  98. public static string Base64BinaryToString (byte [] value)
  99. {
  100. return Convert.ToBase64String (value);
  101. }
  102. public static decimal BooleanToDecimal (bool value)
  103. {
  104. return Convert.ToDecimal (value);
  105. }
  106. public static double BooleanToDouble (bool value)
  107. {
  108. return Convert.ToDouble (value);
  109. }
  110. public static float BooleanToFloat (bool value)
  111. {
  112. return Convert.ToSingle (value);
  113. }
  114. public static int BooleanToInt (bool value)
  115. {
  116. return Convert.ToInt32 (value);
  117. }
  118. public static long BooleanToInteger (bool value)
  119. {
  120. return Convert.ToInt64 (value);
  121. }
  122. public static string BooleanToString (bool value)
  123. {
  124. // It looks not returning "True"
  125. return value ? "true" : "false";
  126. }
  127. public static DateTime DateTimeToDate (DateTime value)
  128. {
  129. return value.Date;
  130. }
  131. public static DateTime DateTimeToGDay (DateTime value)
  132. {
  133. return new DateTime (0, 0, value.Day);
  134. }
  135. public static DateTime DateTimeToGMonth (DateTime value)
  136. {
  137. return new DateTime (0, value.Month, 0);
  138. }
  139. public static DateTime DateTimeToGMonthDay (DateTime value)
  140. {
  141. return new DateTime (0, value.Month, value.Day);
  142. }
  143. public static DateTime DateTimeToGYear (DateTime value)
  144. {
  145. return new DateTime (value.Year, 0, 0);
  146. }
  147. public static DateTime DateTimeToGYearMonth (DateTime value)
  148. {
  149. return new DateTime (value.Year, value.Month, 0);
  150. }
  151. public static DateTime DateTimeToTime (DateTime value)
  152. {
  153. return new DateTime (value.TimeOfDay.Ticks);
  154. }
  155. public static DateTime DateToDateTime (DateTime value)
  156. {
  157. return value.Date;
  158. }
  159. public static DateTime DateToGDay (DateTime value)
  160. {
  161. return new DateTime (0, 0, value.Day);
  162. }
  163. public static DateTime DateToGMonth (DateTime value)
  164. {
  165. return new DateTime (0, value.Month, 0);
  166. }
  167. public static DateTime DateToGMonthDay (DateTime value)
  168. {
  169. return new DateTime (0, value.Month, value.Day);
  170. }
  171. public static DateTime DateToGYear (DateTime value)
  172. {
  173. return new DateTime (value.Year, 0, 0);
  174. }
  175. public static DateTime DateToGYearMonth (DateTime value)
  176. {
  177. return new DateTime (value.Year, value.Month, 0);
  178. }
  179. public static string DateToString (DateTime value)
  180. {
  181. return XmlConvert.ToString (value);
  182. }
  183. public static string DateTimeToString (DateTime value)
  184. {
  185. return XmlConvert.ToString (value);
  186. }
  187. public static string DayTimeDurationToDuration (TimeSpan value)
  188. {
  189. return XmlConvert.ToString (value);
  190. }
  191. public static string DayTimeDurationToString (TimeSpan value)
  192. {
  193. return DayTimeDurationToDuration (value);
  194. }
  195. public static bool DecimalToBoolean (decimal value)
  196. {
  197. return value != 0;
  198. }
  199. public static double DecimalToDouble (decimal value)
  200. {
  201. return Convert.ToDouble (value);
  202. }
  203. public static float DecimalToFloat (decimal value)
  204. {
  205. return Convert.ToSingle (value);
  206. }
  207. public static int DecimalToInt (decimal value)
  208. {
  209. return Convert.ToInt32 (value);
  210. }
  211. public static long DecimalToInteger (decimal value)
  212. {
  213. return Convert.ToInt64 (value);
  214. }
  215. public static string DecimalToString (decimal value)
  216. {
  217. return XmlConvert.ToString (value);
  218. }
  219. public static bool DoubleToBoolean (double value)
  220. {
  221. return value != 0;
  222. }
  223. public static decimal DoubleToDecimal (double value)
  224. {
  225. return (decimal) value;
  226. }
  227. public static float DoubleToFloat (double value)
  228. {
  229. return Convert.ToSingle (value);
  230. }
  231. public static int DoubleToInt (double value)
  232. {
  233. return Convert.ToInt32 (value);
  234. }
  235. public static long DoubleToInteger (double value)
  236. {
  237. return Convert.ToInt64 (value);
  238. }
  239. public static string DoubleToString (double value)
  240. {
  241. return XmlConvert.ToString (value);
  242. }
  243. public static TimeSpan DurationToDayTimeDuration (string value)
  244. {
  245. return XmlConvert.ToTimeSpan (value);
  246. }
  247. public static string DurationToString (string value)
  248. {
  249. return XmlConvert.ToString (XmlConvert.ToTimeSpan (value));
  250. }
  251. public static TimeSpan DurationToYearMonthDuration (string value)
  252. {
  253. return XmlConvert.ToTimeSpan (value);
  254. }
  255. public static bool FloatToBoolean (float value)
  256. {
  257. return value != 0;
  258. }
  259. public static decimal FloatToDecimal (float value)
  260. {
  261. return (decimal) value;
  262. }
  263. public static double FloatToDouble (float value)
  264. {
  265. return Convert.ToDouble (value);
  266. }
  267. public static int FloatToInt (float value)
  268. {
  269. return Convert.ToInt32 (value);
  270. }
  271. public static long FloatToInteger (float value)
  272. {
  273. return Convert.ToInt64 (value);
  274. }
  275. public static string FloatToString (float value)
  276. {
  277. return XmlConvert.ToString (value);
  278. }
  279. public static string GDayToString (DateTime value)
  280. {
  281. return XmlConvert.ToString (TimeSpan.FromDays (value.Day));
  282. }
  283. public static string GMonthDayToString (DateTime value)
  284. {
  285. return XmlConvert.ToString (new TimeSpan (value.Day, value.Hour, value.Minute, value.Second));
  286. }
  287. public static string GMonthToString (DateTime value)
  288. {
  289. return XmlConvert.ToString (new TimeSpan (0, value.Month, 1));
  290. }
  291. public static string GYearMonthToString (DateTime value)
  292. {
  293. return XmlConvert.ToString (new TimeSpan (value.Year, value.Month, 1));
  294. }
  295. public static string GYearToString (DateTime value)
  296. {
  297. return XmlConvert.ToString (new TimeSpan (new DateTime (value.Year, 1, 1).Ticks));
  298. }
  299. public static string HexBinaryToString (byte [] data)
  300. {
  301. return XmlConvert.ToBinHexString (data);
  302. }
  303. public static byte [] HexBinaryToBase64Binary (byte [] data)
  304. {
  305. return data;//XmlConvert.ToBinHexString (data);
  306. }
  307. public static bool IntegerToBoolean (long value)
  308. {
  309. return value != 0;
  310. }
  311. public static decimal IntegerToDecimal (long value)
  312. {
  313. return (decimal) value;
  314. }
  315. public static decimal IntegerToDecimal (ulong value)
  316. {
  317. return (decimal) value;
  318. }
  319. public static double IntegerToDouble (long value)
  320. {
  321. return Convert.ToDouble (value);
  322. }
  323. public static float IntegerToFloat (long value)
  324. {
  325. return Convert.ToSingle (value);
  326. }
  327. public static int IntegerToInt (long value)
  328. {
  329. return Convert.ToInt32 (value);
  330. }
  331. public static string IntegerToString (long value)
  332. {
  333. return XmlConvert.ToString (value);
  334. }
  335. public static bool IntToBoolean (int value)
  336. {
  337. return value != 0;
  338. }
  339. public static decimal IntToDecimal (int value)
  340. {
  341. return (decimal) value;
  342. }
  343. public static double IntToDouble (int value)
  344. {
  345. return Convert.ToDouble (value);
  346. }
  347. public static float IntToFloat (int value)
  348. {
  349. return Convert.ToSingle (value);
  350. }
  351. public static long IntToInteger (int value)
  352. {
  353. return value;
  354. }
  355. public static string IntToString (int value)
  356. {
  357. return XmlConvert.ToString (value);
  358. }
  359. public static string NonNegativeIntegerToString (decimal value)
  360. {
  361. return XmlConvert.ToString (value);
  362. }
  363. public static string NonPositiveIntegerToString (decimal value)
  364. {
  365. return XmlConvert.ToString (value);
  366. }
  367. public static DateTime TimeToDateTime (DateTime value)
  368. {
  369. return value;
  370. }
  371. public static string TimeToString (DateTime value)
  372. {
  373. return XmlConvert.ToString (value, "HH:mm:ssZ");
  374. }
  375. public static string YearMonthDurationToDuration (TimeSpan value)
  376. {
  377. return XmlConvert.ToString (value);
  378. }
  379. public static string YearMonthDurationToString (TimeSpan value)
  380. {
  381. return YearMonthDurationToDuration (value);
  382. }
  383. public static string StringToAnyUri (string value)
  384. {
  385. return value;
  386. }
  387. public static byte [] StringToBase64Binary (string value)
  388. {
  389. return Convert.FromBase64String (value);
  390. }
  391. public static bool StringToBoolean (string value)
  392. {
  393. return XmlConvert.ToBoolean (value);
  394. }
  395. public static DateTime StringToDate (string value)
  396. {
  397. return XmlConvert.ToDateTime (value);
  398. }
  399. public static DateTime StringToDateTime (string value)
  400. {
  401. return XmlConvert.ToDateTime (value);
  402. }
  403. public static TimeSpan StringToDayTimeDuration (string value)
  404. {
  405. return XmlConvert.ToTimeSpan (value);
  406. }
  407. public static decimal StringToDecimal (string value)
  408. {
  409. return XmlConvert.ToDecimal (value);
  410. }
  411. public static double StringToDouble (string value)
  412. {
  413. return XmlConvert.ToDouble (value);
  414. }
  415. public static string StringToDuration (string value)
  416. {
  417. return XmlConvert.ToString (XmlConvert.ToTimeSpan (value));
  418. }
  419. public static float StringToFloat (string value)
  420. {
  421. return XmlConvert.ToSingle (value);
  422. }
  423. public static DateTime StringToGDay (string value)
  424. {
  425. return XmlConvert.ToDateTime (value);
  426. }
  427. public static DateTime StringToGMonth (string value)
  428. {
  429. return XmlConvert.ToDateTime (value);
  430. }
  431. public static DateTime StringToGMonthDay (string value)
  432. {
  433. return XmlConvert.ToDateTime (value);
  434. }
  435. public static DateTime StringToGYear (string value)
  436. {
  437. return XmlConvert.ToDateTime (value);
  438. }
  439. public static DateTime StringToGYearMonth (string value)
  440. {
  441. return XmlConvert.ToDateTime (value);
  442. }
  443. public static byte [] StringToHexBinary (string value)
  444. {
  445. return XmlConvert.FromBinHexString (value);
  446. }
  447. public static int StringToInt (string value)
  448. {
  449. return XmlConvert.ToInt32 (value);
  450. }
  451. public static long StringToInteger (string value)
  452. {
  453. return XmlConvert.ToInt64 (value);
  454. }
  455. public static decimal StringToNonNegativeInteger (string value)
  456. {
  457. return XmlConvert.ToDecimal (value);
  458. }
  459. public static decimal StringToNonPositiveInteger (string value)
  460. {
  461. return XmlConvert.ToDecimal (value);
  462. }
  463. public static DateTime StringToTime (string value)
  464. {
  465. return XmlConvert.ToDateTime (value);
  466. }
  467. public static UInt32 StringToUnsignedInt (string value)
  468. {
  469. return XmlConvert.ToUInt32 (value);
  470. }
  471. public static UInt64 StringToUnsignedLong (string value)
  472. {
  473. return XmlConvert.ToUInt64 (value);
  474. }
  475. public static UInt16 StringToUnsignedShort (string value)
  476. {
  477. return XmlConvert.ToUInt16 (value);
  478. }
  479. public static TimeSpan StringToYearMonthDuration (string value)
  480. {
  481. return XmlConvert.ToTimeSpan (value);
  482. }
  483. }
  484. }
  485. #endif