XmlDictionaryReader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. //
  2. // XmlDictionaryReader.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005, 2007 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Text;
  33. using System.Xml;
  34. namespace System.Xml
  35. {
  36. public abstract partial class XmlDictionaryReader : XmlReader
  37. {
  38. protected XmlDictionaryReader ()
  39. {
  40. }
  41. XmlDictionaryReaderQuotas quotas;
  42. public virtual bool CanCanonicalize {
  43. get { return false; }
  44. }
  45. public virtual XmlDictionaryReaderQuotas Quotas {
  46. get {
  47. if (quotas == null)
  48. quotas = new XmlDictionaryReaderQuotas ();
  49. return quotas;
  50. }
  51. }
  52. [MonoTODO]
  53. public virtual void EndCanonicalization ()
  54. {
  55. throw new NotSupportedException ();
  56. }
  57. public virtual string GetAttribute (
  58. XmlDictionaryString localName,
  59. XmlDictionaryString namespaceUri)
  60. {
  61. if (localName == null)
  62. throw new ArgumentNullException ("localName");
  63. if (namespaceUri == null)
  64. throw new ArgumentNullException ("namespaceUri");
  65. return GetAttribute (localName.Value, namespaceUri.Value);
  66. }
  67. public virtual int IndexOfLocalName (
  68. string [] localNames, string namespaceUri)
  69. {
  70. if (localNames == null)
  71. throw new ArgumentNullException ("localNames");
  72. if (namespaceUri == null)
  73. throw new ArgumentNullException ("namespaceUri");
  74. if (NamespaceURI != namespaceUri)
  75. return -1;
  76. for (int i = 0; i < localNames.Length; i++)
  77. if (localNames [i] == LocalName)
  78. return i;
  79. return -1;
  80. }
  81. public virtual int IndexOfLocalName (
  82. XmlDictionaryString [] localNames,
  83. XmlDictionaryString namespaceUri)
  84. {
  85. if (localNames == null)
  86. throw new ArgumentNullException ("localNames");
  87. if (namespaceUri == null)
  88. throw new ArgumentNullException ("namespaceUri");
  89. if (NamespaceURI != namespaceUri.Value)
  90. return -1;
  91. XmlDictionaryString localName;
  92. if (!TryGetLocalNameAsDictionaryString (out localName))
  93. return -1;
  94. IXmlDictionary dict = localName.Dictionary;
  95. XmlDictionaryString iter;
  96. for (int i = 0; i < localNames.Length; i++)
  97. if (dict.TryLookup (localNames [i], out iter) && object.ReferenceEquals (iter, localName))
  98. return i;
  99. return -1;
  100. }
  101. public virtual bool IsLocalName (string localName)
  102. {
  103. return LocalName == localName;
  104. }
  105. public virtual bool IsLocalName (XmlDictionaryString localName)
  106. {
  107. if (localName == null)
  108. throw new ArgumentNullException ("localName");
  109. return LocalName == localName.Value;
  110. }
  111. public virtual bool IsNamespaceUri (string namespaceUri)
  112. {
  113. return NamespaceURI == namespaceUri;
  114. }
  115. public virtual bool IsNamespaceUri (XmlDictionaryString namespaceUri)
  116. {
  117. if (namespaceUri == null)
  118. throw new ArgumentNullException ("namespaceUri");
  119. return NamespaceURI == namespaceUri.Value;
  120. }
  121. [MonoTODO]
  122. public virtual bool IsStartArray (out Type type)
  123. {
  124. throw new NotImplementedException ();
  125. }
  126. public virtual bool IsStartElement (
  127. XmlDictionaryString localName,
  128. XmlDictionaryString namespaceUri)
  129. {
  130. if (localName == null)
  131. throw new ArgumentNullException ("localName");
  132. if (namespaceUri == null)
  133. throw new ArgumentNullException ("namespaceUri");
  134. return IsStartElement (localName.Value, namespaceUri.Value);
  135. }
  136. [MonoTODO]
  137. public virtual void MoveToStartElement ()
  138. {
  139. throw new NotImplementedException ();
  140. }
  141. [MonoTODO]
  142. public virtual void MoveToStartElement (string name)
  143. {
  144. throw new NotImplementedException ();
  145. }
  146. [MonoTODO]
  147. public virtual void MoveToStartElement (
  148. string localName, string namespaceUri)
  149. {
  150. throw new NotImplementedException ();
  151. }
  152. [MonoTODO]
  153. public virtual void MoveToStartElement (
  154. XmlDictionaryString localName,
  155. XmlDictionaryString namespaceUri)
  156. {
  157. throw new NotImplementedException ();
  158. }
  159. [MonoTODO]
  160. public virtual void StartCanonicalization (
  161. Stream stream, bool includeComments,
  162. string [] inclusivePrefixes)
  163. {
  164. throw new NotSupportedException ();
  165. }
  166. public virtual bool TryGetArrayLength (out int count)
  167. {
  168. count = -1;
  169. return false;
  170. }
  171. public virtual bool TryGetBase64ContentLength (out int count)
  172. {
  173. count = -1;
  174. return false;
  175. }
  176. [MonoTODO]
  177. public virtual bool TryGetLocalNameAsDictionaryString (
  178. out XmlDictionaryString localName)
  179. {
  180. localName = null;
  181. return false;
  182. }
  183. [MonoTODO]
  184. public virtual bool TryGetNamespaceUriAsDictionaryString (
  185. out XmlDictionaryString namespaceUri)
  186. {
  187. namespaceUri = null;
  188. return false;
  189. }
  190. #region Content Reader Methods
  191. [MonoTODO]
  192. public override object ReadContentAs (Type type, IXmlNamespaceResolver nsResolver)
  193. {
  194. return base.ReadContentAs (type, nsResolver);
  195. }
  196. public virtual byte [] ReadContentAsBase64 ()
  197. {
  198. int len;
  199. if (!TryGetBase64ContentLength (out len))
  200. return Convert.FromBase64String (ReadContentAsString ());
  201. byte [] bytes = new byte [len];
  202. ReadContentAsBase64 (bytes, 0, len);
  203. return bytes;
  204. }
  205. MethodInfo xmlconv_from_bin_hex = typeof (XmlConvert).GetMethod ("FromBinHexString", BindingFlags.Static | BindingFlags.NonPublic, null, new Type [] {typeof (string)}, null);
  206. byte [] FromBinHexString (string s)
  207. {
  208. return (byte []) xmlconv_from_bin_hex.Invoke (null, new object [] {s});
  209. }
  210. public virtual byte [] ReadContentAsBinHex ()
  211. {
  212. int len;
  213. if (!TryGetArrayLength (out len))
  214. return FromBinHexString (ReadContentAsString ());
  215. return ReadContentAsBinHex (len);
  216. }
  217. protected byte [] ReadContentAsBinHex (int maxByteArrayContentLength)
  218. {
  219. byte [] bytes = new byte [maxByteArrayContentLength];
  220. ReadContentAsBinHex (bytes, 0, maxByteArrayContentLength);
  221. return bytes;
  222. }
  223. [MonoTODO]
  224. public virtual int ReadContentAsChars (char [] chars, int offset, int count)
  225. {
  226. throw new NotImplementedException ();
  227. }
  228. [MonoTODO]
  229. public override decimal ReadContentAsDecimal ()
  230. {
  231. return base.ReadContentAsDecimal ();
  232. }
  233. [MonoTODO]
  234. public override float ReadContentAsFloat ()
  235. {
  236. return base.ReadContentAsFloat ();
  237. }
  238. public virtual Guid ReadContentAsGuid ()
  239. {
  240. return XmlConvert.ToGuid (ReadContentAsString ());
  241. }
  242. [MonoTODO]
  243. public virtual void ReadContentAsQualifiedName (out string localName, out string namespaceUri)
  244. {
  245. throw new NotImplementedException ();
  246. }
  247. public override string ReadContentAsString ()
  248. {
  249. return ReadContentAsString (Quotas.MaxStringContentLength);
  250. }
  251. [MonoTODO]
  252. protected string ReadContentAsString (int maxStringContentLength)
  253. {
  254. return base.ReadContentAsString ();
  255. }
  256. [MonoTODO]
  257. public virtual string ReadContentAsString (string [] strings, out int index)
  258. {
  259. throw new NotImplementedException ();
  260. }
  261. [MonoTODO]
  262. public virtual string ReadContentAsString (XmlDictionaryString [] strings, out int index)
  263. {
  264. throw new NotImplementedException ();
  265. }
  266. public virtual TimeSpan ReadContentAsTimeSpan ()
  267. {
  268. return XmlConvert.ToTimeSpan (ReadContentAsString ());
  269. }
  270. public virtual UniqueId ReadContentAsUniqueId ()
  271. {
  272. return new UniqueId (ReadContentAsString ());
  273. }
  274. public virtual byte [] ReadElementContentAsBase64 ()
  275. {
  276. ReadStartElement ();
  277. byte [] ret = ReadContentAsBase64 ();
  278. ReadEndElement ();
  279. return ret;
  280. }
  281. public virtual byte [] ReadElementContentAsBinHex ()
  282. {
  283. ReadStartElement ();
  284. byte [] ret = ReadContentAsBinHex ();
  285. ReadEndElement ();
  286. return ret;
  287. }
  288. public virtual Guid ReadElementContentAsGuid ()
  289. {
  290. ReadStartElement ();
  291. Guid ret = ReadContentAsGuid ();
  292. ReadEndElement ();
  293. return ret;
  294. }
  295. public virtual TimeSpan ReadElementContentAsTimeSpan ()
  296. {
  297. ReadStartElement ();
  298. TimeSpan ret = ReadContentAsTimeSpan ();
  299. ReadEndElement ();
  300. return ret;
  301. }
  302. public virtual UniqueId ReadElementContentAsUniqueId ()
  303. {
  304. ReadStartElement ();
  305. UniqueId ret = ReadContentAsUniqueId ();
  306. ReadEndElement ();
  307. return ret;
  308. }
  309. public override string ReadElementContentAsString ()
  310. {
  311. if (IsEmptyElement) {
  312. Read ();
  313. return String.Empty;
  314. } else {
  315. ReadStartElement ();
  316. string s;
  317. if (NodeType == XmlNodeType.EndElement)
  318. s = String.Empty;
  319. else
  320. s = ReadContentAsString ();
  321. ReadEndElement ();
  322. return s;
  323. }
  324. }
  325. public override string ReadString ()
  326. {
  327. return ReadString (Quotas.MaxStringContentLength);
  328. }
  329. [MonoTODO]
  330. protected string ReadString (int maxStringContentLength)
  331. {
  332. return base.ReadString ();
  333. }
  334. [MonoTODO]
  335. public virtual byte [] ReadValueAsBase64 ()
  336. {
  337. return ReadContentAsBase64 ();
  338. }
  339. #endregion
  340. #region Factory Methods
  341. public static XmlDictionaryReader CreateBinaryReader (
  342. byte [] buffer, XmlDictionaryReaderQuotas quotas)
  343. {
  344. return CreateBinaryReader (buffer, 0, buffer.Length, quotas);
  345. }
  346. public static XmlDictionaryReader CreateBinaryReader (
  347. byte [] buffer, int offset, int count,
  348. XmlDictionaryReaderQuotas quotas)
  349. {
  350. return CreateBinaryReader (buffer, offset, count, new XmlDictionary (), quotas);
  351. }
  352. public static XmlDictionaryReader CreateBinaryReader (
  353. byte [] buffer, int offset, int count,
  354. IXmlDictionary dictionary,
  355. XmlDictionaryReaderQuotas quotas)
  356. {
  357. return CreateBinaryReader (buffer, offset, count,
  358. dictionary, quotas,
  359. new XmlBinaryReaderSession (), null);
  360. }
  361. public static XmlDictionaryReader CreateBinaryReader (
  362. byte [] buffer, int offset, int count,
  363. IXmlDictionary dictionary,
  364. XmlDictionaryReaderQuotas quotas,
  365. XmlBinaryReaderSession session)
  366. {
  367. return CreateBinaryReader (buffer, offset, count,
  368. dictionary, quotas,
  369. session, null);
  370. }
  371. public static XmlDictionaryReader CreateBinaryReader (
  372. byte [] buffer, int offset, int count,
  373. IXmlDictionary dictionary,
  374. XmlDictionaryReaderQuotas quotas,
  375. XmlBinaryReaderSession session,
  376. OnXmlDictionaryReaderClose onClose)
  377. {
  378. return new XmlBinaryDictionaryReader (buffer,
  379. offset, count,
  380. dictionary, quotas, session, onClose);
  381. }
  382. public static XmlDictionaryReader CreateBinaryReader (
  383. Stream stream, XmlDictionaryReaderQuotas quotas)
  384. {
  385. return CreateBinaryReader (stream, new XmlDictionary (), quotas);
  386. }
  387. public static XmlDictionaryReader CreateBinaryReader (
  388. Stream stream, IXmlDictionary dictionary,
  389. XmlDictionaryReaderQuotas quotas)
  390. {
  391. return CreateBinaryReader (stream, dictionary, quotas,
  392. new XmlBinaryReaderSession (), null);
  393. }
  394. public static XmlDictionaryReader CreateBinaryReader (
  395. Stream stream, IXmlDictionary dictionary,
  396. XmlDictionaryReaderQuotas quotas,
  397. XmlBinaryReaderSession session)
  398. {
  399. return CreateBinaryReader (stream, dictionary, quotas,
  400. session, null);
  401. }
  402. [MonoTODO]
  403. public static XmlDictionaryReader CreateBinaryReader (
  404. Stream stream, IXmlDictionary dictionary,
  405. XmlDictionaryReaderQuotas quotas,
  406. XmlBinaryReaderSession session,
  407. OnXmlDictionaryReaderClose onClose)
  408. {
  409. return new XmlBinaryDictionaryReader (stream,
  410. dictionary, quotas, session, onClose);
  411. }
  412. [MonoTODO]
  413. public static XmlDictionaryReader CreateDictionaryReader (
  414. XmlReader reader)
  415. {
  416. return new XmlSimpleDictionaryReader (reader);
  417. }
  418. [MonoTODO]
  419. public static XmlDictionaryReader CreateMtomReader (
  420. Stream stream, Encoding encoding,
  421. XmlDictionaryReaderQuotas quotas)
  422. {
  423. throw new NotImplementedException ();
  424. }
  425. [MonoTODO]
  426. public static XmlDictionaryReader CreateMtomReader (
  427. Stream stream, Encoding [] encodings,
  428. XmlDictionaryReaderQuotas quotas)
  429. {
  430. throw new NotImplementedException ();
  431. }
  432. [MonoTODO]
  433. public static XmlDictionaryReader CreateMtomReader (
  434. Stream stream, Encoding [] encodings, string contentType,
  435. XmlDictionaryReaderQuotas quotas)
  436. {
  437. throw new NotImplementedException ();
  438. }
  439. [MonoTODO]
  440. public static XmlDictionaryReader CreateMtomReader (
  441. Stream stream, Encoding [] encodings, string contentType,
  442. XmlDictionaryReaderQuotas quotas,
  443. int maxBufferSize,
  444. OnXmlDictionaryReaderClose onClose)
  445. {
  446. throw new NotImplementedException ();
  447. }
  448. [MonoTODO]
  449. public static XmlDictionaryReader CreateMtomReader (
  450. byte [] buffer, int offset, int count,
  451. Encoding encoding, XmlDictionaryReaderQuotas quotas)
  452. {
  453. throw new NotImplementedException ();
  454. }
  455. [MonoTODO]
  456. public static XmlDictionaryReader CreateMtomReader (
  457. byte [] buffer, int offset, int count,
  458. Encoding [] encodings, XmlDictionaryReaderQuotas quotas)
  459. {
  460. throw new NotImplementedException ();
  461. }
  462. [MonoTODO]
  463. public static XmlDictionaryReader CreateMtomReader (
  464. byte [] buffer, int offset, int count,
  465. Encoding [] encodings, string contentType,
  466. XmlDictionaryReaderQuotas quotas)
  467. {
  468. throw new NotImplementedException ();
  469. }
  470. [MonoTODO]
  471. public static XmlDictionaryReader CreateMtomReader (
  472. byte [] buffer, int offset, int count,
  473. Encoding [] encodings, string contentType,
  474. XmlDictionaryReaderQuotas quotas,
  475. int maxBufferSize,
  476. OnXmlDictionaryReaderClose onClose)
  477. {
  478. throw new NotImplementedException ();
  479. }
  480. public static XmlDictionaryReader CreateTextReader (byte [] buffer, XmlDictionaryReaderQuotas quotas)
  481. {
  482. return CreateTextReader (buffer, 0, buffer.Length, quotas);
  483. }
  484. public static XmlDictionaryReader CreateTextReader (
  485. byte [] buffer, int offset, int count,
  486. XmlDictionaryReaderQuotas quotas)
  487. {
  488. return CreateTextReader (buffer, offset, count,
  489. Encoding.UTF8, quotas, null);
  490. }
  491. [MonoTODO]
  492. public static XmlDictionaryReader CreateTextReader (
  493. byte [] buffer, int offset, int count,
  494. Encoding encoding,
  495. XmlDictionaryReaderQuotas quotas,
  496. OnXmlDictionaryReaderClose onClose)
  497. {
  498. return CreateTextReader (new MemoryStream (buffer, offset, count), encoding, quotas, onClose);
  499. }
  500. public static XmlDictionaryReader CreateTextReader (
  501. Stream stream, XmlDictionaryReaderQuotas quotas)
  502. {
  503. return CreateTextReader (stream, Encoding.UTF8, quotas, null);
  504. }
  505. [MonoTODO]
  506. public static XmlDictionaryReader CreateTextReader (
  507. Stream stream, Encoding encoding,
  508. XmlDictionaryReaderQuotas quotas,
  509. OnXmlDictionaryReaderClose onClose)
  510. {
  511. XmlReaderSettings s = new XmlReaderSettings ();
  512. XmlNameTable nt = new NameTable ();
  513. XmlParserContext c = new XmlParserContext (nt, new XmlNamespaceManager (nt), String.Empty, XmlSpace.None, encoding);
  514. XmlDictionaryReader res = new XmlSimpleDictionaryReader (XmlReader.Create (stream, s, c), null, onClose);
  515. res.quotas = quotas;
  516. return res;
  517. }
  518. #endregion
  519. }
  520. }
  521. #endif