XQueryConvert.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. using System.Xml.XPath;
  37. namespace System.Xml
  38. {
  39. internal class XQueryConvert
  40. {
  41. private XQueryConvert ()
  42. {
  43. }
  44. public static bool ShouldCheckValueFacets (XmlSchemaType schemaTypeDest)
  45. {
  46. XmlSchemaObjectCollection facets = null;
  47. XmlSchemaSimpleType st = schemaTypeDest as XmlSchemaSimpleType;
  48. if (st != null) {
  49. XmlSchemaSimpleTypeRestriction r = st.Content
  50. as XmlSchemaSimpleTypeRestriction;
  51. if (r != null)
  52. facets = r.Facets;
  53. }
  54. else {
  55. XmlSchemaComplexType ct =
  56. schemaTypeDest as XmlSchemaComplexType;
  57. XmlSchemaSimpleContent sc = ct.ContentModel
  58. as XmlSchemaSimpleContent;
  59. if (sc != null) {
  60. XmlSchemaSimpleContentRestriction r =
  61. sc.Content as XmlSchemaSimpleContentRestriction;
  62. if (r != null)
  63. facets = r.Facets;
  64. }
  65. }
  66. return facets != null && facets.Count > 0;
  67. }
  68. public static XmlTypeCode GetFallbackType (XmlTypeCode type)
  69. {
  70. switch (type) {
  71. case XmlTypeCode.AnyAtomicType:
  72. return XmlTypeCode.Item;
  73. case XmlTypeCode.UntypedAtomic:
  74. return XmlTypeCode.String;
  75. case XmlTypeCode.Notation:
  76. return XmlTypeCode.QName;
  77. case XmlTypeCode.NormalizedString:
  78. case XmlTypeCode.Token:
  79. case XmlTypeCode.Language:
  80. case XmlTypeCode.NmToken:
  81. case XmlTypeCode.Name:
  82. case XmlTypeCode.NCName:
  83. case XmlTypeCode.Id:
  84. case XmlTypeCode.Idref:
  85. case XmlTypeCode.Entity:
  86. return XmlTypeCode.String;
  87. case XmlTypeCode.NonPositiveInteger:
  88. return XmlTypeCode.Decimal;
  89. case XmlTypeCode.NegativeInteger:
  90. return XmlTypeCode.NonPositiveInteger;
  91. case XmlTypeCode.Long:
  92. return XmlTypeCode.Integer;
  93. case XmlTypeCode.Short:
  94. return XmlTypeCode.Int;
  95. case XmlTypeCode.Byte:
  96. return XmlTypeCode.Int;
  97. case XmlTypeCode.NonNegativeInteger:
  98. return XmlTypeCode.Decimal;
  99. case XmlTypeCode.UnsignedLong:
  100. return XmlTypeCode.NonNegativeInteger;
  101. case XmlTypeCode.UnsignedInt:
  102. return XmlTypeCode.Integer;
  103. case XmlTypeCode.UnsignedShort:
  104. return XmlTypeCode.Int;
  105. case XmlTypeCode.UnsignedByte:
  106. return XmlTypeCode.UnsignedShort;
  107. case XmlTypeCode.PositiveInteger:
  108. return XmlTypeCode.NonNegativeInteger;
  109. default:
  110. return XmlTypeCode.None;
  111. }
  112. }
  113. [MonoTODO]
  114. // See XQuery & XPath 2.0 functions & operators section 17.
  115. public static bool CanConvert (XPathItem item, XmlSchemaType schemaTypeDest)
  116. {
  117. if (item == null)
  118. throw new ArgumentNullException ("item");
  119. if (schemaTypeDest == null)
  120. throw new ArgumentNullException ("schemaTypeDest");
  121. XmlTypeCode src = item.XmlType.TypeCode;
  122. XmlTypeCode dst = schemaTypeDest.TypeCode;
  123. // Notation cannot be converted from other than Notation
  124. if (src == XmlTypeCode.Notation && dst != XmlTypeCode.Notation)
  125. return false;
  126. // untypedAtomic and string are convertable unless source type is QName.
  127. switch (dst) {
  128. case XmlTypeCode.UntypedAtomic:
  129. case XmlTypeCode.String:
  130. return src != XmlTypeCode.QName;
  131. }
  132. switch (src) {
  133. case XmlTypeCode.None:
  134. case XmlTypeCode.Item:
  135. case XmlTypeCode.Node:
  136. case XmlTypeCode.Document:
  137. case XmlTypeCode.Element:
  138. case XmlTypeCode.Attribute:
  139. case XmlTypeCode.Namespace:
  140. case XmlTypeCode.ProcessingInstruction:
  141. case XmlTypeCode.Comment:
  142. case XmlTypeCode.Text:
  143. throw new NotImplementedException (); // FIXME: check what happens
  144. case XmlTypeCode.AnyAtomicType:
  145. throw new NotImplementedException (); // FIXME: check what happens
  146. case XmlTypeCode.UntypedAtomic:
  147. case XmlTypeCode.String:
  148. // 'M'
  149. throw new NotImplementedException (); // FIXME: check what happens
  150. case XmlTypeCode.Boolean:
  151. case XmlTypeCode.Decimal:
  152. switch (dst) {
  153. case XmlTypeCode.Float:
  154. case XmlTypeCode.Double:
  155. case XmlTypeCode.Decimal:
  156. case XmlTypeCode.Boolean:
  157. return true;
  158. }
  159. return false;
  160. case XmlTypeCode.Float:
  161. case XmlTypeCode.Double:
  162. if (dst == XmlTypeCode.Decimal)
  163. // 'M'
  164. throw new NotImplementedException (); // FIXME: check what happens
  165. goto case XmlTypeCode.Decimal;
  166. case XmlTypeCode.Duration:
  167. switch (dst) {
  168. case XmlTypeCode.Duration:
  169. case XmlTypeCode.YearMonthDuration:
  170. case XmlTypeCode.DayTimeDuration:
  171. return true;
  172. }
  173. return false;
  174. case XmlTypeCode.DateTime:
  175. switch (dst) {
  176. case XmlTypeCode.DateTime:
  177. case XmlTypeCode.Time:
  178. case XmlTypeCode.Date:
  179. case XmlTypeCode.GYearMonth:
  180. case XmlTypeCode.GYear:
  181. case XmlTypeCode.GMonthDay:
  182. case XmlTypeCode.GDay:
  183. case XmlTypeCode.GMonth:
  184. return true;
  185. }
  186. return false;
  187. case XmlTypeCode.Time:
  188. switch (dst) {
  189. case XmlTypeCode.Time:
  190. case XmlTypeCode.Date:
  191. return true;
  192. }
  193. return false;
  194. case XmlTypeCode.Date:
  195. if (dst == XmlTypeCode.Time)
  196. return false;
  197. goto case XmlTypeCode.DateTime;
  198. case XmlTypeCode.GYearMonth:
  199. case XmlTypeCode.GYear:
  200. case XmlTypeCode.GMonthDay:
  201. case XmlTypeCode.GDay:
  202. case XmlTypeCode.GMonth:
  203. return src == dst;
  204. case XmlTypeCode.HexBinary:
  205. case XmlTypeCode.Base64Binary:
  206. if (src == dst)
  207. return true;
  208. switch (dst) {
  209. case XmlTypeCode.HexBinary:
  210. case XmlTypeCode.Base64Binary:
  211. return true;
  212. }
  213. return false;
  214. case XmlTypeCode.AnyUri:
  215. case XmlTypeCode.QName:
  216. case XmlTypeCode.Notation:
  217. return src == dst;
  218. case XmlTypeCode.NormalizedString:
  219. case XmlTypeCode.Token:
  220. case XmlTypeCode.Language:
  221. case XmlTypeCode.NmToken:
  222. case XmlTypeCode.Name:
  223. case XmlTypeCode.NCName:
  224. case XmlTypeCode.Id:
  225. case XmlTypeCode.Idref:
  226. case XmlTypeCode.Entity:
  227. case XmlTypeCode.Integer:
  228. case XmlTypeCode.NonPositiveInteger:
  229. case XmlTypeCode.NegativeInteger:
  230. case XmlTypeCode.Long:
  231. case XmlTypeCode.Int:
  232. case XmlTypeCode.Short:
  233. case XmlTypeCode.Byte:
  234. case XmlTypeCode.NonNegativeInteger:
  235. case XmlTypeCode.UnsignedLong:
  236. case XmlTypeCode.UnsignedInt:
  237. case XmlTypeCode.UnsignedShort:
  238. case XmlTypeCode.UnsignedByte:
  239. case XmlTypeCode.PositiveInteger:
  240. throw new NotImplementedException ();
  241. // xdt:*
  242. case XmlTypeCode.YearMonthDuration:
  243. if (dst == XmlTypeCode.DayTimeDuration)
  244. return false;
  245. goto case XmlTypeCode.Duration;
  246. case XmlTypeCode.DayTimeDuration:
  247. if (dst == XmlTypeCode.YearMonthDuration)
  248. return false;
  249. goto case XmlTypeCode.Duration;
  250. }
  251. throw new NotImplementedException ();
  252. }
  253. // Individual conversion
  254. public static string AnyUriToString (string value)
  255. {
  256. return value;
  257. }
  258. public static byte [] Base64BinaryToHexBinary (byte [] value)
  259. {
  260. return XmlConvert.FromBinHexString (Convert.ToBase64String (value));
  261. }
  262. public static string Base64BinaryToString (byte [] value)
  263. {
  264. return Convert.ToBase64String (value);
  265. }
  266. public static decimal BooleanToDecimal (bool value)
  267. {
  268. return Convert.ToDecimal (value);
  269. }
  270. public static double BooleanToDouble (bool value)
  271. {
  272. return Convert.ToDouble (value);
  273. }
  274. public static float BooleanToFloat (bool value)
  275. {
  276. return Convert.ToSingle (value);
  277. }
  278. public static int BooleanToInt (bool value)
  279. {
  280. return Convert.ToInt32 (value);
  281. }
  282. public static long BooleanToInteger (bool value)
  283. {
  284. return Convert.ToInt64 (value);
  285. }
  286. public static string BooleanToString (bool value)
  287. {
  288. // It looks not returning "True"
  289. return value ? "true" : "false";
  290. }
  291. public static DateTime DateTimeToDate (DateTime value)
  292. {
  293. return value.Date;
  294. }
  295. public static DateTime DateTimeToGDay (DateTime value)
  296. {
  297. return new DateTime (0, 0, value.Day);
  298. }
  299. public static DateTime DateTimeToGMonth (DateTime value)
  300. {
  301. return new DateTime (0, value.Month, 0);
  302. }
  303. public static DateTime DateTimeToGMonthDay (DateTime value)
  304. {
  305. return new DateTime (0, value.Month, value.Day);
  306. }
  307. public static DateTime DateTimeToGYear (DateTime value)
  308. {
  309. return new DateTime (value.Year, 0, 0);
  310. }
  311. public static DateTime DateTimeToGYearMonth (DateTime value)
  312. {
  313. return new DateTime (value.Year, value.Month, 0);
  314. }
  315. public static DateTime DateTimeToTime (DateTime value)
  316. {
  317. return new DateTime (value.TimeOfDay.Ticks);
  318. }
  319. public static DateTime DateToDateTime (DateTime value)
  320. {
  321. return value.Date;
  322. }
  323. public static DateTime DateToGDay (DateTime value)
  324. {
  325. return new DateTime (0, 0, value.Day);
  326. }
  327. public static DateTime DateToGMonth (DateTime value)
  328. {
  329. return new DateTime (0, value.Month, 0);
  330. }
  331. public static DateTime DateToGMonthDay (DateTime value)
  332. {
  333. return new DateTime (0, value.Month, value.Day);
  334. }
  335. public static DateTime DateToGYear (DateTime value)
  336. {
  337. return new DateTime (value.Year, 0, 0);
  338. }
  339. public static DateTime DateToGYearMonth (DateTime value)
  340. {
  341. return new DateTime (value.Year, value.Month, 0);
  342. }
  343. public static string DateToString (DateTime value)
  344. {
  345. return XmlConvert.ToString (value);
  346. }
  347. public static string DateTimeToString (DateTime value)
  348. {
  349. return XmlConvert.ToString (value);
  350. }
  351. public static string DayTimeDurationToDuration (TimeSpan value)
  352. {
  353. return XmlConvert.ToString (value);
  354. }
  355. public static string DayTimeDurationToString (TimeSpan value)
  356. {
  357. return DayTimeDurationToDuration (value);
  358. }
  359. public static bool DecimalToBoolean (decimal value)
  360. {
  361. return value != 0;
  362. }
  363. public static double DecimalToDouble (decimal value)
  364. {
  365. return Convert.ToDouble (value);
  366. }
  367. public static float DecimalToFloat (decimal value)
  368. {
  369. return Convert.ToSingle (value);
  370. }
  371. public static int DecimalToInt (decimal value)
  372. {
  373. return Convert.ToInt32 (value);
  374. }
  375. public static long DecimalToInteger (decimal value)
  376. {
  377. return Convert.ToInt64 (value);
  378. }
  379. [MonoTODO] // what if value was negative?
  380. public static decimal DecimalToNonNegativeInteger (decimal value)
  381. {
  382. // MS has a bug that does not reject negative values.
  383. throw new NotImplementedException ();
  384. }
  385. [MonoTODO] // what if value was positive?
  386. public static decimal DecimalToNonPositiveInteger (decimal value)
  387. {
  388. throw new NotImplementedException ();
  389. }
  390. public static string DecimalToString (decimal value)
  391. {
  392. return XmlConvert.ToString (value);
  393. }
  394. public static bool DoubleToBoolean (double value)
  395. {
  396. return value != 0;
  397. }
  398. public static decimal DoubleToDecimal (double value)
  399. {
  400. return (decimal) value;
  401. }
  402. public static float DoubleToFloat (double value)
  403. {
  404. return Convert.ToSingle (value);
  405. }
  406. public static int DoubleToInt (double value)
  407. {
  408. return Convert.ToInt32 (value);
  409. }
  410. public static long DoubleToInteger (double value)
  411. {
  412. return Convert.ToInt64 (value);
  413. }
  414. [MonoTODO] // what if value was negative?
  415. public static decimal DoubleToNonNegativeInteger (double value)
  416. {
  417. // MS has a bug that does not reject negative values.
  418. throw new NotImplementedException ();
  419. }
  420. [MonoTODO] // what if value was positive?
  421. public static decimal DoubleToNonPositiveInteger (double value)
  422. {
  423. throw new NotImplementedException ();
  424. }
  425. public static string DoubleToString (double value)
  426. {
  427. return XmlConvert.ToString (value);
  428. }
  429. public static TimeSpan DurationToDayTimeDuration (string value)
  430. {
  431. return XmlConvert.ToTimeSpan (value);
  432. }
  433. public static string DurationToString (string value)
  434. {
  435. return XmlConvert.ToString (XmlConvert.ToTimeSpan (value));
  436. }
  437. public static TimeSpan DurationToYearMonthDuration (string value)
  438. {
  439. return XmlConvert.ToTimeSpan (value);
  440. }
  441. public static bool FloatToBoolean (float value)
  442. {
  443. return value != 0;
  444. }
  445. public static decimal FloatToDecimal (float value)
  446. {
  447. return (decimal) value;
  448. }
  449. public static double FloatToDouble (float value)
  450. {
  451. return Convert.ToDouble (value);
  452. }
  453. public static int FloatToInt (float value)
  454. {
  455. return Convert.ToInt32 (value);
  456. }
  457. public static long FloatToInteger (float value)
  458. {
  459. return Convert.ToInt64 (value);
  460. }
  461. [MonoTODO] // what if value was negative?
  462. public static decimal FloatToNonNegativeInteger (float value)
  463. {
  464. // MS has a bug that does not reject negative values.
  465. throw new NotImplementedException ();
  466. }
  467. [MonoTODO] // what if value was positive?
  468. public static decimal FloatToNonPositiveInteger (float value)
  469. {
  470. throw new NotImplementedException ();
  471. }
  472. public static string FloatToString (float value)
  473. {
  474. return XmlConvert.ToString (value);
  475. }
  476. public static string GDayToString (DateTime value)
  477. {
  478. return XmlConvert.ToString (TimeSpan.FromDays (value.Day));
  479. }
  480. public static string GMonthDayToString (DateTime value)
  481. {
  482. return XmlConvert.ToString (new TimeSpan (value.Day, value.Hour, value.Minute, value.Second));
  483. }
  484. public static string GMonthToString (DateTime value)
  485. {
  486. return XmlConvert.ToString (new TimeSpan (0, value.Month, 0));
  487. }
  488. public static string GYearMonthToString (DateTime value)
  489. {
  490. return XmlConvert.ToString (new TimeSpan (value.Year, value.Month, 0));
  491. }
  492. public static string GYearToString (DateTime value)
  493. {
  494. return XmlConvert.ToString (new TimeSpan (new DateTime (value.Year, 0, 0).Ticks));
  495. }
  496. public static string HexBinaryToString (byte [] data)
  497. {
  498. return XmlConvert.ToBinHexString (data);
  499. }
  500. public static byte [] HexBinaryToBase64Binary (byte [] data)
  501. {
  502. return data;//XmlConvert.ToBinHexString (data);
  503. }
  504. public static bool IntegerToBoolean (long value)
  505. {
  506. return value != 0;
  507. }
  508. public static decimal IntegerToDecimal (long value)
  509. {
  510. return (decimal) value;
  511. }
  512. public static double IntegerToDouble (long value)
  513. {
  514. return Convert.ToDouble (value);
  515. }
  516. public static float IntegerToFloat (long value)
  517. {
  518. return Convert.ToSingle (value);
  519. }
  520. public static int IntegerToInt (long value)
  521. {
  522. return Convert.ToInt32 (value);
  523. }
  524. public static string IntegerToString (long value)
  525. {
  526. return XmlConvert.ToString (value);
  527. }
  528. public static bool IntToBoolean (int value)
  529. {
  530. return value != 0;
  531. }
  532. public static decimal IntToDecimal (int value)
  533. {
  534. return (decimal) value;
  535. }
  536. public static double IntToDouble (int value)
  537. {
  538. return Convert.ToDouble (value);
  539. }
  540. public static float IntToFloat (int value)
  541. {
  542. return Convert.ToSingle (value);
  543. }
  544. public static long IntToInteger (int value)
  545. {
  546. return value;
  547. }
  548. public static string IntToString (int value)
  549. {
  550. return XmlConvert.ToString (value);
  551. }
  552. public static string NonNegativeIntegerToString (decimal value)
  553. {
  554. return XmlConvert.ToString (value);
  555. }
  556. public static string NonPositiveIntegerToString (decimal value)
  557. {
  558. return XmlConvert.ToString (value);
  559. }
  560. public static DateTime TimeToDateTime (DateTime value)
  561. {
  562. return value;
  563. }
  564. public static string TimeToString (DateTime value)
  565. {
  566. return XmlConvert.ToString (value, "HH:mm:ssZ");
  567. }
  568. public static string YearMonthDurationToDuration (TimeSpan value)
  569. {
  570. return XmlConvert.ToString (value);
  571. }
  572. public static string YearMonthDurationToString (TimeSpan value)
  573. {
  574. return YearMonthDurationToDuration (value);
  575. }
  576. public static string StringToAnyUri (string value)
  577. {
  578. return value;
  579. }
  580. public static byte [] StringToBase64Binary (string value)
  581. {
  582. return Convert.FromBase64String (value);
  583. }
  584. public static bool StringToBoolean (string value)
  585. {
  586. return XmlConvert.ToBoolean (value);
  587. }
  588. public static DateTime StringToDate (string value)
  589. {
  590. return XmlConvert.ToDateTime (value);
  591. }
  592. public static DateTime StringToDateTime (string value)
  593. {
  594. return XmlConvert.ToDateTime (value);
  595. }
  596. public static TimeSpan StringToDayTimeDuration (string value)
  597. {
  598. return XmlConvert.ToTimeSpan (value);
  599. }
  600. public static decimal StringToDecimal (string value)
  601. {
  602. return XmlConvert.ToDecimal (value);
  603. }
  604. public static double StringToDouble (string value)
  605. {
  606. return XmlConvert.ToDouble (value);
  607. }
  608. public static string StringToDuration (string value)
  609. {
  610. return XmlConvert.ToString (XmlConvert.ToTimeSpan (value));
  611. }
  612. public static float StringToFloat (string value)
  613. {
  614. return XmlConvert.ToSingle (value);
  615. }
  616. public static DateTime StringToGDay (string value)
  617. {
  618. return XmlConvert.ToDateTime (value);
  619. }
  620. public static DateTime StringToGMonth (string value)
  621. {
  622. return XmlConvert.ToDateTime (value);
  623. }
  624. public static DateTime StringToGMonthDay (string value)
  625. {
  626. return XmlConvert.ToDateTime (value);
  627. }
  628. public static DateTime StringToGYear (string value)
  629. {
  630. return XmlConvert.ToDateTime (value);
  631. }
  632. public static DateTime StringToGYearMonth (string value)
  633. {
  634. return XmlConvert.ToDateTime (value);
  635. }
  636. public static byte [] StringToHexBinary (string value)
  637. {
  638. return XmlConvert.FromBinHexString (value);
  639. }
  640. public static int StringToInt (string value)
  641. {
  642. return XmlConvert.ToInt32 (value);
  643. }
  644. public static long StringToInteger (string value)
  645. {
  646. return XmlConvert.ToInt64 (value);
  647. }
  648. public static decimal StringToNonNegativeInteger (string value)
  649. {
  650. return XmlConvert.ToDecimal (value);
  651. }
  652. public static decimal StringToNonPositiveInteger (string value)
  653. {
  654. return XmlConvert.ToDecimal (value);
  655. }
  656. public static DateTime StringToTime (string value)
  657. {
  658. return XmlConvert.ToDateTime (value);
  659. }
  660. public static long StringToUnsignedInt (string value)
  661. {
  662. return XmlConvert.ToUInt32 (value);
  663. }
  664. public static decimal StringToUnsignedLong (string value)
  665. {
  666. return XmlConvert.ToUInt64 (value);
  667. }
  668. public static int StringToUnsignedShort (string value)
  669. {
  670. return XmlConvert.ToUInt16 (value);
  671. }
  672. public static TimeSpan StringToYearMonthDuration (string value)
  673. {
  674. return XmlConvert.ToTimeSpan (value);
  675. }
  676. public static string ItemToAnyUri (XPathItem value)
  677. {
  678. return value.Value;
  679. }
  680. public static byte [] ItemToBase64Binary (XPathItem value)
  681. {
  682. return Convert.FromBase64String (value.Value);
  683. }
  684. public static bool ItemToBoolean (XPathItem value)
  685. {
  686. return XmlConvert.ToBoolean (value.Value);
  687. }
  688. public static DateTime ItemToDate (XPathItem value)
  689. {
  690. return XmlConvert.ToDateTime (value.Value);
  691. }
  692. public static DateTime ItemToDateTime (XPathItem value)
  693. {
  694. return XmlConvert.ToDateTime (value.Value);
  695. }
  696. public static TimeSpan ItemToDayTimeDuration (XPathItem value)
  697. {
  698. return XmlConvert.ToTimeSpan (value.Value);
  699. }
  700. public static decimal ItemToDecimal (XPathItem value)
  701. {
  702. return XmlConvert.ToDecimal (value.Value);
  703. }
  704. public static double ItemToDouble (XPathItem value)
  705. {
  706. return XmlConvert.ToDouble (value.Value);
  707. }
  708. public static string ItemToDuration (XPathItem value)
  709. {
  710. return XmlConvert.ToString (XmlConvert.ToTimeSpan (value.Value));
  711. }
  712. public static float ItemToFloat (XPathItem value)
  713. {
  714. return XmlConvert.ToSingle (value.Value);
  715. }
  716. public static DateTime ItemToGDay (XPathItem value)
  717. {
  718. return XmlConvert.ToDateTime (value.Value);
  719. }
  720. public static DateTime ItemToGMonth (XPathItem value)
  721. {
  722. return XmlConvert.ToDateTime (value.Value);
  723. }
  724. public static DateTime ItemToGMonthDay (XPathItem value)
  725. {
  726. return XmlConvert.ToDateTime (value.Value);
  727. }
  728. public static DateTime ItemToGYear (XPathItem value)
  729. {
  730. return XmlConvert.ToDateTime (value.Value);
  731. }
  732. public static DateTime ItemToGYearMonth (XPathItem value)
  733. {
  734. return XmlConvert.ToDateTime (value.Value);
  735. }
  736. public static byte [] ItemToHexBinary (XPathItem value)
  737. {
  738. return XmlConvert.FromBinHexString (value.Value);
  739. }
  740. public static int ItemToInt (XPathItem value)
  741. {
  742. return XmlConvert.ToInt32 (value.Value);
  743. }
  744. public static long ItemToInteger (XPathItem value)
  745. {
  746. return XmlConvert.ToInt64 (value.Value);
  747. }
  748. public static XPathItem ItemToItem (XPathItem value, XmlSchemaType schemaTypeDest)
  749. {
  750. return new XmlAtomicValue (value.Value, schemaTypeDest);
  751. }
  752. public static decimal ItemToNonNegativeInteger (XPathItem value)
  753. {
  754. return XmlConvert.ToDecimal (value.Value);
  755. }
  756. public static decimal ItemToNonPositiveInteger (XPathItem value)
  757. {
  758. return XmlConvert.ToDecimal (value.Value);
  759. }
  760. public static XmlQualifiedName ItemToQName (XPathItem value)
  761. {
  762. return (XmlQualifiedName) value.TypedValue;
  763. }
  764. public static string ItemToString (XPathItem value)
  765. {
  766. if (value.ValueType == typeof (DateTime))
  767. return XmlConvert.ToString ((DateTime) value.TypedValue);
  768. if (value.TypedValue is XmlQualifiedName)
  769. throw new ArgumentException ("Invalid cast from schema QName type to string type.");
  770. return value.Value;
  771. }
  772. public static DateTime ItemToTime (XPathItem value)
  773. {
  774. return XmlConvert.ToDateTime (value.Value);
  775. }
  776. [MonoTODO]
  777. public static long ItemToUnsignedInt (XPathItem value)
  778. {
  779. // FIXME: signed
  780. return XmlConvert.ToInt32 (value.Value);
  781. }
  782. [MonoTODO]
  783. public static decimal ItemToUnsignedLong (XPathItem value)
  784. {
  785. // FIXME: signed
  786. return XmlConvert.ToInt32 (value.Value);
  787. }
  788. [MonoTODO]
  789. public static int ItemToUnsignedShort (XPathItem value)
  790. {
  791. // FIXME: signed
  792. return XmlConvert.ToInt32 (value.Value);
  793. }
  794. public static TimeSpan ItemToYearMonthDuration (XPathItem value)
  795. {
  796. return XmlConvert.ToTimeSpan (value.Value);
  797. }
  798. }
  799. }
  800. #endif