XmlValueConverter.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. //
  2. // XmlValueConverter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (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. namespace System.Xml.Schema
  32. {
  33. public abstract class XmlValueConverter
  34. {
  35. [MonoTODO]
  36. protected XmlValueConverter ()
  37. {
  38. }
  39. public abstract object ChangeType (bool value, Type type);
  40. public abstract object ChangeType (DateTime value, Type type);
  41. public abstract object ChangeType (decimal value, Type type);
  42. public abstract object ChangeType (double value, Type type);
  43. public abstract object ChangeType (int value, Type type);
  44. public abstract object ChangeType (long value, Type type);
  45. public abstract object ChangeType (object value, Type type);
  46. public abstract object ChangeType (float value, Type type);
  47. public abstract object ChangeType (string value, Type type);
  48. public abstract object ChangeType (object value, Type type, IXmlNamespaceResolver nsResolver);
  49. public abstract object ChangeType (string value, Type type, IXmlNamespaceResolver nsResolver);
  50. public abstract bool ToBoolean (bool value);
  51. public abstract bool ToBoolean (DateTime value);
  52. public abstract bool ToBoolean (decimal value);
  53. public abstract bool ToBoolean (double value);
  54. public abstract bool ToBoolean (int value);
  55. public abstract bool ToBoolean (long value);
  56. public abstract bool ToBoolean (object value);
  57. public abstract bool ToBoolean (float value);
  58. public abstract bool ToBoolean (string value);
  59. public abstract DateTime ToDateTime (bool value);
  60. public abstract DateTime ToDateTime (DateTime value);
  61. public abstract DateTime ToDateTime (decimal value);
  62. public abstract DateTime ToDateTime (double value);
  63. public abstract DateTime ToDateTime (int value);
  64. public abstract DateTime ToDateTime (long value);
  65. public abstract DateTime ToDateTime (object value);
  66. public abstract DateTime ToDateTime (float value);
  67. public abstract DateTime ToDateTime (string value);
  68. public abstract decimal ToDecimal (bool value);
  69. public abstract decimal ToDecimal (DateTime value);
  70. public abstract decimal ToDecimal (decimal value);
  71. public abstract decimal ToDecimal (double value);
  72. public abstract decimal ToDecimal (int value);
  73. public abstract decimal ToDecimal (long value);
  74. public abstract decimal ToDecimal (object value);
  75. public abstract decimal ToDecimal (float value);
  76. public abstract decimal ToDecimal (string value);
  77. public abstract double ToDouble (bool value);
  78. public abstract double ToDouble (DateTime value);
  79. public abstract double ToDouble (decimal value);
  80. public abstract double ToDouble (double value);
  81. public abstract double ToDouble (int value);
  82. public abstract double ToDouble (long value);
  83. public abstract double ToDouble (object value);
  84. public abstract double ToDouble (float value);
  85. public abstract double ToDouble (string value);
  86. public abstract int ToInt32 (bool value);
  87. public abstract int ToInt32 (DateTime value);
  88. public abstract int ToInt32 (decimal value);
  89. public abstract int ToInt32 (double value);
  90. public abstract int ToInt32 (int value);
  91. public abstract int ToInt32 (long value);
  92. public abstract int ToInt32 (object value);
  93. public abstract int ToInt32 (float value);
  94. public abstract int ToInt32 (string value);
  95. public abstract long ToInt64 (bool value);
  96. public abstract long ToInt64 (DateTime value);
  97. public abstract long ToInt64 (decimal value);
  98. public abstract long ToInt64 (double value);
  99. public abstract long ToInt64 (int value);
  100. public abstract long ToInt64 (long value);
  101. public abstract long ToInt64 (object value);
  102. public abstract long ToInt64 (float value);
  103. public abstract long ToInt64 (string value);
  104. public abstract float ToSingle (bool value);
  105. public abstract float ToSingle (DateTime value);
  106. public abstract float ToSingle (decimal value);
  107. public abstract float ToSingle (double value);
  108. public abstract float ToSingle (int value);
  109. public abstract float ToSingle (long value);
  110. public abstract float ToSingle (object value);
  111. public abstract float ToSingle (float value);
  112. public abstract float ToSingle (string value);
  113. public abstract string ToString (bool value);
  114. public abstract string ToString (DateTime value);
  115. public abstract string ToString (decimal value);
  116. public abstract string ToString (double value);
  117. public abstract string ToString (int value);
  118. public abstract string ToString (long value);
  119. public abstract string ToString (object value);
  120. public abstract string ToString (object value, IXmlNamespaceResolver nsResolver);
  121. public abstract string ToString (float value);
  122. public abstract string ToString (string value);
  123. public abstract string ToString (string value, IXmlNamespaceResolver nsResolver);
  124. }
  125. internal class XsdNonPermissiveConverter : XmlValueConverter
  126. {
  127. readonly XmlTypeCode typeCode;
  128. public XsdNonPermissiveConverter (XmlTypeCode typeCode)
  129. {
  130. this.typeCode = typeCode;
  131. }
  132. public XmlTypeCode Code {
  133. get { return typeCode; }
  134. }
  135. public override object ChangeType (bool value, Type type)
  136. {
  137. return ChangeType ((object) value, type);
  138. }
  139. public override object ChangeType (DateTime value, Type type)
  140. {
  141. return ChangeType ((object) value, type);
  142. }
  143. public override object ChangeType (decimal value, Type type)
  144. {
  145. return ChangeType ((object) value, type);
  146. }
  147. public override object ChangeType (double value, Type type)
  148. {
  149. return ChangeType ((object) value, type);
  150. }
  151. public override object ChangeType (int value, Type type)
  152. {
  153. return ChangeType ((object) value, type);
  154. }
  155. public override object ChangeType (long value, Type type)
  156. {
  157. return ChangeType ((object) value, type);
  158. }
  159. public override object ChangeType (float value, Type type)
  160. {
  161. return ChangeType ((object) value, type);
  162. }
  163. public override object ChangeType (string value, Type type)
  164. {
  165. return ChangeType ((object) value, type);
  166. }
  167. public override object ChangeType (object value, Type type)
  168. {
  169. return ChangeType (value, type, null);
  170. }
  171. [MonoTODO]
  172. public override object ChangeType (object value, Type type, IXmlNamespaceResolver nsResolver)
  173. {
  174. if (value == null)
  175. throw new ArgumentNullException ("value");
  176. if (type == null)
  177. throw new ArgumentNullException ("type");
  178. switch (Type.GetTypeCode (value.GetType ())) {
  179. case TypeCode.Boolean:
  180. bool bvalue = (bool) value;
  181. switch (Type.GetTypeCode (type)) {
  182. case TypeCode.Boolean:
  183. return ToBoolean (bvalue);
  184. case TypeCode.DateTime:
  185. return ToDateTime (bvalue);
  186. case TypeCode.Decimal:
  187. return ToDecimal (bvalue);
  188. case TypeCode.Double:
  189. return ToDouble (bvalue);
  190. case TypeCode.Int32:
  191. return ToInt32 (bvalue);
  192. case TypeCode.Int64:
  193. return ToInt64 (bvalue);
  194. case TypeCode.Single:
  195. return ToSingle (bvalue);
  196. case TypeCode.String:
  197. return ToString (bvalue);
  198. }
  199. break;
  200. // case TypeCode.Byte:
  201. // case TypeCode.Char:
  202. case TypeCode.DateTime:
  203. DateTime dtvalue = (DateTime) value;
  204. switch (Type.GetTypeCode (type)) {
  205. case TypeCode.Boolean:
  206. return ToBoolean (dtvalue);
  207. case TypeCode.DateTime:
  208. return ToDateTime (dtvalue);
  209. case TypeCode.Decimal:
  210. return ToDecimal (dtvalue);
  211. case TypeCode.Double:
  212. return ToDouble (dtvalue);
  213. case TypeCode.Int32:
  214. return ToInt32 (dtvalue);
  215. case TypeCode.Int64:
  216. return ToInt64 (dtvalue);
  217. case TypeCode.Single:
  218. return ToSingle (dtvalue);
  219. case TypeCode.String:
  220. return ToString (dtvalue);
  221. }
  222. break;
  223. // case TypeCode.DBNull:
  224. case TypeCode.Decimal:
  225. decimal decvalue = (decimal) value;
  226. switch (Type.GetTypeCode (type)) {
  227. case TypeCode.Boolean:
  228. return ToBoolean (decvalue);
  229. case TypeCode.DateTime:
  230. return ToDateTime (decvalue);
  231. case TypeCode.Decimal:
  232. return ToDecimal (decvalue);
  233. case TypeCode.Double:
  234. return ToDouble (decvalue);
  235. case TypeCode.Int32:
  236. return ToInt32 (decvalue);
  237. case TypeCode.Int64:
  238. return ToInt64 (decvalue);
  239. case TypeCode.Single:
  240. return ToSingle (decvalue);
  241. case TypeCode.String:
  242. return ToString (decvalue);
  243. }
  244. break;
  245. case TypeCode.Double:
  246. double dblvalue = (double) value;
  247. switch (Type.GetTypeCode (type)) {
  248. case TypeCode.Boolean:
  249. return ToBoolean (dblvalue);
  250. case TypeCode.DateTime:
  251. return ToDateTime (dblvalue);
  252. case TypeCode.Decimal:
  253. return ToDecimal (dblvalue);
  254. case TypeCode.Double:
  255. return ToDouble (dblvalue);
  256. case TypeCode.Int32:
  257. return ToInt32 (dblvalue);
  258. case TypeCode.Int64:
  259. return ToInt64 (dblvalue);
  260. case TypeCode.Single:
  261. return ToSingle (dblvalue);
  262. case TypeCode.String:
  263. return ToString (dblvalue);
  264. }
  265. break;
  266. // case TypeCode.Empty:
  267. // case TypeCode.Int16:
  268. case TypeCode.Int32:
  269. int ivalue = (int) value;
  270. switch (Type.GetTypeCode (type)) {
  271. case TypeCode.Boolean:
  272. return ToBoolean (ivalue);
  273. case TypeCode.DateTime:
  274. return ToDateTime (ivalue);
  275. case TypeCode.Decimal:
  276. return ToDecimal (ivalue);
  277. case TypeCode.Double:
  278. return ToDouble (ivalue);
  279. case TypeCode.Int32:
  280. return ToInt32 (ivalue);
  281. case TypeCode.Int64:
  282. return ToInt64 (ivalue);
  283. case TypeCode.Single:
  284. return ToSingle (ivalue);
  285. case TypeCode.String:
  286. return ToString (ivalue);
  287. }
  288. break;
  289. case TypeCode.Int64:
  290. long lvalue = (long) value;
  291. switch (Type.GetTypeCode (type)) {
  292. case TypeCode.Boolean:
  293. return ToBoolean (lvalue);
  294. case TypeCode.DateTime:
  295. return ToDateTime (lvalue);
  296. case TypeCode.Decimal:
  297. return ToDecimal (lvalue);
  298. case TypeCode.Double:
  299. return ToDouble (lvalue);
  300. case TypeCode.Int32:
  301. return ToInt32 (lvalue);
  302. case TypeCode.Int64:
  303. return ToInt64 (lvalue);
  304. case TypeCode.Single:
  305. return ToSingle (lvalue);
  306. case TypeCode.String:
  307. return ToString (lvalue);
  308. }
  309. break;
  310. // case TypeCode.Object:
  311. // case TypeCode.SByte:
  312. case TypeCode.Single:
  313. float fvalue = (float) value;
  314. switch (Type.GetTypeCode (type)) {
  315. case TypeCode.Boolean:
  316. return ToBoolean (fvalue);
  317. case TypeCode.DateTime:
  318. return ToDateTime (fvalue);
  319. case TypeCode.Decimal:
  320. return ToDecimal (fvalue);
  321. case TypeCode.Double:
  322. return ToDouble (fvalue);
  323. case TypeCode.Int32:
  324. return ToInt32 (fvalue);
  325. case TypeCode.Int64:
  326. return ToInt64 (fvalue);
  327. case TypeCode.Single:
  328. return ToSingle (fvalue);
  329. case TypeCode.String:
  330. return ToString (fvalue);
  331. }
  332. break;
  333. case TypeCode.String:
  334. string svalue = (string) value;
  335. switch (Type.GetTypeCode (type)) {
  336. case TypeCode.Boolean:
  337. return ToBoolean (svalue);
  338. case TypeCode.DateTime:
  339. return ToDateTime (svalue);
  340. case TypeCode.Decimal:
  341. return ToDecimal (svalue);
  342. case TypeCode.Double:
  343. return ToDouble (svalue);
  344. case TypeCode.Int32:
  345. return ToInt32 (svalue);
  346. case TypeCode.Int64:
  347. return ToInt64 (svalue);
  348. case TypeCode.Single:
  349. return ToSingle (svalue);
  350. case TypeCode.String:
  351. return ToString (svalue);
  352. }
  353. break;
  354. // case TypeCode.UInt16:
  355. // case TypeCode.UInt32:
  356. // case TypeCode.UInt64:
  357. default:
  358. if (type == typeof (TimeSpan))
  359. // xs:duration, xdt:yearMonthDuration,
  360. // xdt:dayTimeDuration. FIXME: yMD to
  361. // dTD and dTD to yMD are not allowed.
  362. return ToTimeSpan (value);
  363. if (value.GetType () == typeof (byte [])) {
  364. // xs:base64 by default
  365. if (type == typeof (string))
  366. return XQueryConvert.Base64BinaryToString ((byte []) value);
  367. else if (type == typeof (byte []))
  368. return value;
  369. }
  370. if (value.GetType () == type) {
  371. if (type == typeof (XmlQualifiedName)) {
  372. // xs:QName and xs:NOTATION
  373. throw new NotImplementedException ();
  374. }
  375. }
  376. break;
  377. }
  378. throw Error (value.GetType (), type);
  379. }
  380. public override object ChangeType (string value, Type type, IXmlNamespaceResolver nsResolver)
  381. {
  382. return ChangeType ((object) value, type, nsResolver);
  383. }
  384. public TimeSpan ToTimeSpan (bool value)
  385. {
  386. throw Error (typeof (bool), typeof (TimeSpan));
  387. }
  388. public TimeSpan ToTimeSpan (DateTime value)
  389. {
  390. throw Error (typeof (bool), typeof (TimeSpan));
  391. }
  392. public TimeSpan ToTimeSpan (decimal value)
  393. {
  394. throw Error (typeof (bool), typeof (TimeSpan));
  395. }
  396. public TimeSpan ToTimeSpan (double value)
  397. {
  398. throw Error (typeof (bool), typeof (TimeSpan));
  399. }
  400. public TimeSpan ToTimeSpan (int value)
  401. {
  402. throw Error (typeof (bool), typeof (TimeSpan));
  403. }
  404. public TimeSpan ToTimeSpan (long value)
  405. {
  406. throw Error (typeof (bool), typeof (TimeSpan));
  407. }
  408. public virtual TimeSpan ToTimeSpan (object value)
  409. {
  410. // Allow on overriden converter for xs:duration,
  411. // xdt:dayTimeDuration and xdt:yearMonthDuration.
  412. throw Error (typeof (bool), typeof (TimeSpan));
  413. }
  414. public TimeSpan ToTimeSpan (float value)
  415. {
  416. throw Error (typeof (bool), typeof (TimeSpan));
  417. }
  418. public TimeSpan ToTimeSpan (string value)
  419. {
  420. throw Error (typeof (bool), typeof (TimeSpan));
  421. }
  422. protected InvalidCastException Error (Type valueType, Type destType)
  423. {
  424. return new InvalidCastException (String.Format ("The conversion from {0} value to {1} type via {2} type is not allowed.", valueType, destType, typeCode));
  425. }
  426. public override bool ToBoolean (bool value)
  427. {
  428. throw Error (typeof (bool), typeof (bool));
  429. }
  430. public override bool ToBoolean (DateTime value)
  431. {
  432. throw Error (typeof (DateTime), typeof (bool));
  433. }
  434. public override bool ToBoolean (decimal value)
  435. {
  436. throw Error (typeof (decimal), typeof (bool));
  437. }
  438. public override bool ToBoolean (double value)
  439. {
  440. throw Error (typeof (double), typeof (bool));
  441. }
  442. public override bool ToBoolean (int value)
  443. {
  444. throw Error (typeof (int), typeof (bool));
  445. }
  446. public override bool ToBoolean (long value)
  447. {
  448. throw Error (typeof (long), typeof (bool));
  449. }
  450. public override bool ToBoolean (object value)
  451. {
  452. if (value == null)
  453. throw new ArgumentNullException ("value");
  454. throw Error (value.GetType (), typeof (bool));
  455. }
  456. public override bool ToBoolean (float value)
  457. {
  458. throw Error (typeof (float), typeof (bool));
  459. }
  460. public override bool ToBoolean (string value)
  461. {
  462. throw Error (typeof (string), typeof (bool));
  463. }
  464. public override DateTime ToDateTime (bool value)
  465. {
  466. throw Error (typeof (bool), typeof (DateTime));
  467. }
  468. public override DateTime ToDateTime (DateTime value)
  469. {
  470. throw Error (typeof (DateTime), typeof (DateTime));
  471. }
  472. public override DateTime ToDateTime (decimal value)
  473. {
  474. throw Error (typeof (decimal), typeof (DateTime));
  475. }
  476. public override DateTime ToDateTime (double value)
  477. {
  478. throw Error (typeof (double), typeof (DateTime));
  479. }
  480. public override DateTime ToDateTime (int value)
  481. {
  482. throw Error (typeof (int), typeof (DateTime));
  483. }
  484. public override DateTime ToDateTime (long value)
  485. {
  486. throw Error (typeof (long), typeof (DateTime));
  487. }
  488. public override DateTime ToDateTime (object value)
  489. {
  490. if (value == null)
  491. throw new ArgumentNullException ("value");
  492. throw Error (value.GetType (), typeof (DateTime));
  493. }
  494. public override DateTime ToDateTime (float value)
  495. {
  496. throw Error (typeof (float), typeof (DateTime));
  497. }
  498. public override DateTime ToDateTime (string value)
  499. {
  500. throw Error (typeof (string), typeof (DateTime));
  501. }
  502. public override decimal ToDecimal (bool value)
  503. {
  504. throw Error (typeof (bool), typeof (decimal));
  505. }
  506. public override decimal ToDecimal (DateTime value)
  507. {
  508. throw Error (typeof (DateTime), typeof (decimal));
  509. }
  510. public override decimal ToDecimal (decimal value)
  511. {
  512. throw Error (typeof (decimal), typeof (decimal));
  513. }
  514. public override decimal ToDecimal (double value)
  515. {
  516. throw Error (typeof (double), typeof (decimal));
  517. }
  518. public override decimal ToDecimal (int value)
  519. {
  520. throw Error (typeof (int), typeof (decimal));
  521. }
  522. public override decimal ToDecimal (long value)
  523. {
  524. throw Error (typeof (long), typeof (decimal));
  525. }
  526. public override decimal ToDecimal (object value)
  527. {
  528. if (value == null)
  529. throw new ArgumentNullException ("value");
  530. throw Error (value.GetType (), typeof (decimal));
  531. }
  532. public override decimal ToDecimal (float value)
  533. {
  534. throw Error (typeof (float), typeof (decimal));
  535. }
  536. public override decimal ToDecimal (string value)
  537. {
  538. throw Error (typeof (string), typeof (decimal));
  539. }
  540. public override double ToDouble (bool value)
  541. {
  542. throw Error (typeof (bool), typeof (double));
  543. }
  544. public override double ToDouble (DateTime value)
  545. {
  546. throw Error (typeof (DateTime), typeof (double));
  547. }
  548. public override double ToDouble (decimal value)
  549. {
  550. throw Error (typeof (decimal), typeof (double));
  551. }
  552. public override double ToDouble (double value)
  553. {
  554. throw Error (typeof (double), typeof (double));
  555. }
  556. public override double ToDouble (int value)
  557. {
  558. throw Error (typeof (int), typeof (double));
  559. }
  560. public override double ToDouble (long value)
  561. {
  562. throw Error (typeof (long), typeof (double));
  563. }
  564. public override double ToDouble (object value)
  565. {
  566. if (value == null)
  567. throw new ArgumentNullException ("value");
  568. throw Error (value.GetType (), typeof (double));
  569. }
  570. public override double ToDouble (float value)
  571. {
  572. throw Error (typeof (float), typeof (double));
  573. }
  574. public override double ToDouble (string value)
  575. {
  576. throw Error (typeof (string), typeof (double));
  577. }
  578. public override float ToSingle (bool value)
  579. {
  580. throw Error (typeof (bool), typeof (float));
  581. }
  582. public override float ToSingle (DateTime value)
  583. {
  584. throw Error (typeof (DateTime), typeof (float));
  585. }
  586. public override float ToSingle (decimal value)
  587. {
  588. throw Error (typeof (decimal), typeof (float));
  589. }
  590. public override float ToSingle (double value)
  591. {
  592. throw Error (typeof (double), typeof (float));
  593. }
  594. public override float ToSingle (int value)
  595. {
  596. throw Error (typeof (int), typeof (float));
  597. }
  598. public override float ToSingle (long value)
  599. {
  600. throw Error (typeof (long), typeof (float));
  601. }
  602. public override float ToSingle (object value)
  603. {
  604. if (value == null)
  605. throw new ArgumentNullException ("value");
  606. throw Error (value.GetType (), typeof (float));
  607. }
  608. public override float ToSingle (float value)
  609. {
  610. throw Error (typeof (float), typeof (float));
  611. }
  612. public override float ToSingle (string value)
  613. {
  614. throw Error (typeof (string), typeof (float));
  615. }
  616. public override int ToInt32 (bool value)
  617. {
  618. throw Error (typeof (bool), typeof (int));
  619. }
  620. public override int ToInt32 (DateTime value)
  621. {
  622. throw Error (typeof (DateTime), typeof (int));
  623. }
  624. public override int ToInt32 (decimal value)
  625. {
  626. throw Error (typeof (decimal), typeof (int));
  627. }
  628. public override int ToInt32 (double value)
  629. {
  630. throw Error (typeof (double), typeof (int));
  631. }
  632. public override int ToInt32 (int value)
  633. {
  634. throw Error (typeof (int), typeof (int));
  635. }
  636. public override int ToInt32 (long value)
  637. {
  638. throw Error (typeof (long), typeof (int));
  639. }
  640. public override int ToInt32 (object value)
  641. {
  642. if (value == null)
  643. throw new ArgumentNullException ("value");
  644. throw Error (value.GetType (), typeof (int));
  645. }
  646. public override int ToInt32 (float value)
  647. {
  648. throw Error (typeof (float), typeof (int));
  649. }
  650. public override int ToInt32 (string value)
  651. {
  652. throw Error (typeof (string), typeof (int));
  653. }
  654. public override long ToInt64 (bool value)
  655. {
  656. throw Error (typeof (bool), typeof (long));
  657. }
  658. public override long ToInt64 (DateTime value)
  659. {
  660. throw Error (typeof (DateTime), typeof (long));
  661. }
  662. public override long ToInt64 (decimal value)
  663. {
  664. throw Error (typeof (decimal), typeof (long));
  665. }
  666. public override long ToInt64 (double value)
  667. {
  668. throw Error (typeof (double), typeof (long));
  669. }
  670. public override long ToInt64 (int value)
  671. {
  672. throw Error (typeof (int), typeof (long));
  673. }
  674. public override long ToInt64 (long value)
  675. {
  676. throw Error (typeof (long), typeof (long));
  677. }
  678. public override long ToInt64 (object value)
  679. {
  680. if (value == null)
  681. throw new ArgumentNullException ("value");
  682. throw Error (value.GetType (), typeof (long));
  683. }
  684. public override long ToInt64 (float value)
  685. {
  686. throw Error (typeof (float), typeof (long));
  687. }
  688. public override long ToInt64 (string value)
  689. {
  690. throw Error (typeof (string), typeof (long));
  691. }
  692. public override string ToString (bool value)
  693. {
  694. throw Error (typeof (bool), typeof (string));
  695. }
  696. public override string ToString (DateTime value)
  697. {
  698. throw Error (typeof (DateTime), typeof (string));
  699. }
  700. public override string ToString (decimal value)
  701. {
  702. throw Error (typeof (decimal), typeof (string));
  703. }
  704. public override string ToString (double value)
  705. {
  706. throw Error (typeof (double), typeof (string));
  707. }
  708. public override string ToString (int value)
  709. {
  710. throw Error (typeof (int), typeof (string));
  711. }
  712. public override string ToString (long value)
  713. {
  714. throw Error (typeof (long), typeof (string));
  715. }
  716. public override string ToString (object value)
  717. {
  718. return ToString (value, null);
  719. }
  720. public override string ToString (object value, IXmlNamespaceResolver nsResolver)
  721. {
  722. if (value == null)
  723. throw new ArgumentNullException ("value");
  724. throw Error (value.GetType (), typeof (string));
  725. }
  726. public override string ToString (float value)
  727. {
  728. throw Error (typeof (float), typeof (string));
  729. }
  730. public override string ToString (string value)
  731. {
  732. return ToString (value, null);
  733. }
  734. public override string ToString (string value, IXmlNamespaceResolver nsResolver)
  735. {
  736. throw Error (typeof (string), typeof (string));
  737. }
  738. }
  739. internal class XsdLaxConverter : XsdNonPermissiveConverter
  740. {
  741. public XsdLaxConverter (XmlTypeCode code)
  742. : base (code)
  743. {
  744. }
  745. public override string ToString (bool value)
  746. {
  747. return XmlConvert.ToString (value);
  748. }
  749. public override string ToString (decimal value)
  750. {
  751. return XmlConvert.ToString (value);
  752. }
  753. public override string ToString (double value)
  754. {
  755. return XmlConvert.ToString (value);
  756. }
  757. public override string ToString (int value)
  758. {
  759. return XmlConvert.ToString (value);
  760. }
  761. public override string ToString (long value)
  762. {
  763. return XmlConvert.ToString (value);
  764. }
  765. public override string ToString (object value)
  766. {
  767. return ChangeType (value, typeof (string)) as string;
  768. }
  769. public override string ToString (float value)
  770. {
  771. return XmlConvert.ToString (value);
  772. }
  773. public override bool ToBoolean (bool value)
  774. {
  775. return value;
  776. }
  777. public override decimal ToDecimal (decimal value)
  778. {
  779. return value;
  780. }
  781. public override decimal ToDecimal (double value)
  782. {
  783. return (decimal) value;
  784. }
  785. public override decimal ToDecimal (int value)
  786. {
  787. return (decimal) value;
  788. }
  789. public override decimal ToDecimal (long value)
  790. {
  791. return (decimal) value;
  792. }
  793. [MonoTODO]
  794. public override decimal ToDecimal (object value)
  795. {
  796. return (decimal) ChangeType (value, typeof (decimal));
  797. }
  798. public override decimal ToDecimal (float value)
  799. {
  800. return (decimal) value;
  801. }
  802. public override double ToDouble (decimal value)
  803. {
  804. return (double) value;
  805. }
  806. public override double ToDouble (double value)
  807. {
  808. return (double) value;
  809. }
  810. public override double ToDouble (int value)
  811. {
  812. return (double) value;
  813. }
  814. public override double ToDouble (long value)
  815. {
  816. return (double) value;
  817. }
  818. [MonoTODO]
  819. public override double ToDouble (object value)
  820. {
  821. return (double) ChangeType (value, typeof (double));
  822. }
  823. public override double ToDouble (float value)
  824. {
  825. return (double) value;
  826. }
  827. public override float ToSingle (decimal value)
  828. {
  829. return (float) value;
  830. }
  831. public override float ToSingle (double value)
  832. {
  833. return (float) value;
  834. }
  835. public override float ToSingle (int value)
  836. {
  837. return (float) value;
  838. }
  839. public override float ToSingle (long value)
  840. {
  841. return (float) value;
  842. }
  843. [MonoTODO]
  844. public override float ToSingle (object value)
  845. {
  846. return (float) ChangeType (value, typeof (float));
  847. }
  848. public override float ToSingle (float value)
  849. {
  850. return (float) value;
  851. }
  852. public override int ToInt32 (int value)
  853. {
  854. return value;
  855. }
  856. public override int ToInt32 (long value)
  857. {
  858. return XQueryConvert.IntegerToInt (value);
  859. }
  860. [MonoTODO]
  861. public override int ToInt32 (object value)
  862. {
  863. return (int) ChangeType (value, typeof (int));
  864. }
  865. public override long ToInt64 (int value)
  866. {
  867. return value;
  868. }
  869. public override long ToInt64 (long value)
  870. {
  871. return value;
  872. }
  873. [MonoTODO]
  874. public override long ToInt64 (object value)
  875. {
  876. return (long) ChangeType (value, typeof (long));
  877. }
  878. }
  879. internal class XsdAnyTypeConverter : XsdNumericConverter
  880. {
  881. public XsdAnyTypeConverter (XmlTypeCode code)
  882. : base (code)
  883. {
  884. }
  885. #region boolean
  886. public override bool ToBoolean (decimal value)
  887. {
  888. return value != 0;
  889. }
  890. public override bool ToBoolean (double value)
  891. {
  892. return value != 0;
  893. }
  894. public override bool ToBoolean (int value)
  895. {
  896. return value != 0;
  897. }
  898. public override bool ToBoolean (long value)
  899. {
  900. return value != 0;
  901. }
  902. [MonoTODO]
  903. public override bool ToBoolean (object value)
  904. {
  905. return (bool) ChangeType (value, typeof (bool));
  906. }
  907. public override bool ToBoolean (float value)
  908. {
  909. return value != 0;
  910. }
  911. public override decimal ToDecimal (bool value)
  912. {
  913. return value ? 1 : 0;
  914. }
  915. public override double ToDouble (bool value)
  916. {
  917. return value ? 1 : 0;
  918. }
  919. public override float ToSingle (bool value)
  920. {
  921. return value ? 1 : 0;
  922. }
  923. public override int ToInt32 (bool value)
  924. {
  925. return value ? 1 : 0;
  926. }
  927. public override long ToInt64 (bool value)
  928. {
  929. return value ? 1 : 0;
  930. }
  931. #endregion
  932. #region string
  933. public override DateTime ToDateTime (DateTime value)
  934. {
  935. return value;
  936. }
  937. public override string ToString (DateTime value)
  938. {
  939. return XmlConvert.ToString (value);
  940. }
  941. public override string ToString (string value)
  942. {
  943. return value;
  944. }
  945. #endregion
  946. }
  947. internal class XsdStringConverter : XsdLaxConverter
  948. {
  949. public XsdStringConverter (XmlTypeCode code)
  950. : base (code)
  951. {
  952. }
  953. public override DateTime ToDateTime (DateTime value)
  954. {
  955. return value;
  956. }
  957. public override string ToString (DateTime value)
  958. {
  959. return XmlConvert.ToString (value);
  960. }
  961. public override string ToString (string value)
  962. {
  963. return value;
  964. }
  965. }
  966. internal class XsdNumericConverter : XsdLaxConverter
  967. {
  968. public XsdNumericConverter (XmlTypeCode code)
  969. : base (code)
  970. {
  971. }
  972. #region boolean
  973. public override bool ToBoolean (decimal value)
  974. {
  975. return value != 0;
  976. }
  977. public override bool ToBoolean (double value)
  978. {
  979. return value != 0;
  980. }
  981. public override bool ToBoolean (int value)
  982. {
  983. return value != 0;
  984. }
  985. public override bool ToBoolean (long value)
  986. {
  987. return value != 0;
  988. }
  989. [MonoTODO]
  990. public override bool ToBoolean (object value)
  991. {
  992. return (bool) ChangeType (value, typeof (bool));
  993. }
  994. public override bool ToBoolean (float value)
  995. {
  996. return value != 0;
  997. }
  998. public override decimal ToDecimal (bool value)
  999. {
  1000. return value ? 1 : 0;
  1001. }
  1002. public override double ToDouble (bool value)
  1003. {
  1004. return value ? 1 : 0;
  1005. }
  1006. public override float ToSingle (bool value)
  1007. {
  1008. return value ? 1 : 0;
  1009. }
  1010. public override int ToInt32 (bool value)
  1011. {
  1012. return value ? 1 : 0;
  1013. }
  1014. public override long ToInt64 (bool value)
  1015. {
  1016. return value ? 1 : 0;
  1017. }
  1018. #endregion
  1019. #region numeric with point to without point
  1020. public override int ToInt32 (decimal value)
  1021. {
  1022. return XQueryConvert.DecimalToInt (value);
  1023. }
  1024. public override int ToInt32 (double value)
  1025. {
  1026. return XQueryConvert.DoubleToInt (value);
  1027. }
  1028. public override int ToInt32 (float value)
  1029. {
  1030. return XQueryConvert.FloatToInt (value);
  1031. }
  1032. public override long ToInt64 (decimal value)
  1033. {
  1034. return XQueryConvert.DecimalToInteger (value);
  1035. }
  1036. public override long ToInt64 (double value)
  1037. {
  1038. return XQueryConvert.DoubleToInteger (value);
  1039. }
  1040. public override long ToInt64 (float value)
  1041. {
  1042. return XQueryConvert.FloatToInteger (value);
  1043. }
  1044. #endregion
  1045. }
  1046. internal class XsdDateTimeConverter : XsdNonPermissiveConverter
  1047. {
  1048. public XsdDateTimeConverter (XmlTypeCode code)
  1049. : base (code)
  1050. {
  1051. }
  1052. public override string ToString (DateTime value)
  1053. {
  1054. return XmlConvert.ToString (value);
  1055. }
  1056. public override DateTime ToDateTime (DateTime value)
  1057. {
  1058. return value;
  1059. }
  1060. }
  1061. internal class XsdBooleanConverter : XsdNumericConverter
  1062. {
  1063. public XsdBooleanConverter (XmlTypeCode code)
  1064. : base (code)
  1065. {
  1066. }
  1067. }
  1068. internal class XsdMiscBaseConverter : XsdNonPermissiveConverter
  1069. {
  1070. public XsdMiscBaseConverter (XmlTypeCode code)
  1071. : base (code)
  1072. {
  1073. }
  1074. public override string ToString (string value)
  1075. {
  1076. return value;
  1077. }
  1078. public override object ChangeType (object value, Type type, IXmlNamespaceResolver nsResolver)
  1079. {
  1080. if (Code == XmlTypeCode.HexBinary) {
  1081. if (value == null)
  1082. throw new ArgumentNullException ("value");
  1083. if (type == null)
  1084. throw new ArgumentNullException ("type");
  1085. if (value.GetType () == typeof (byte [])) {
  1086. if (type == typeof (string))
  1087. return XQueryConvert.HexBinaryToString ((byte []) value);
  1088. else if (type == typeof (byte []))
  1089. return value;
  1090. }
  1091. }
  1092. return base.ChangeType (value, type, nsResolver);
  1093. }
  1094. }
  1095. }
  1096. #endif