XmlDictionaryReader.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. protected bool IsTextNode (XmlNodeType nodeType)
  137. {
  138. switch (nodeType) {
  139. case XmlNodeType.Attribute: // wow, it isn't indeed.
  140. case XmlNodeType.Text:
  141. case XmlNodeType.CDATA:
  142. case XmlNodeType.Whitespace:
  143. case XmlNodeType.SignificantWhitespace:
  144. return true;
  145. default:
  146. return false;
  147. }
  148. }
  149. XmlException XmlError (string message)
  150. {
  151. IXmlLineInfo li = this as IXmlLineInfo;
  152. if (li == null || !li.HasLineInfo ())
  153. return new XmlException (message);
  154. else
  155. return new XmlException (String.Format ("{0} in {1} , at ({2},{3})", message, BaseURI, li.LineNumber, li.LinePosition));
  156. }
  157. public virtual void MoveToStartElement ()
  158. {
  159. MoveToContent ();
  160. if (NodeType != XmlNodeType.Element)
  161. throw XmlError (String.Format ("Element node is expected, but got {0} node.", NodeType));
  162. }
  163. public virtual void MoveToStartElement (string name)
  164. {
  165. if (name == null)
  166. throw new ArgumentNullException ("name");
  167. MoveToStartElement ();
  168. if (Name != name)
  169. throw XmlError (String.Format ("Element node '{0}' is expected, but got '{1}' element.", name, Name));
  170. }
  171. public virtual void MoveToStartElement (
  172. string localName, string namespaceUri)
  173. {
  174. if (localName == null)
  175. throw new ArgumentNullException ("localName");
  176. if (namespaceUri == null)
  177. throw new ArgumentNullException ("namespaceUri");
  178. MoveToStartElement ();
  179. if (LocalName != localName || NamespaceURI != namespaceUri)
  180. throw XmlError (String.Format ("Element node '{0}' in namespace '{1}' is expected, but got '{2}' in namespace '{3}' element.", localName, namespaceUri, LocalName, NamespaceURI));
  181. }
  182. public virtual void MoveToStartElement (
  183. XmlDictionaryString localName,
  184. XmlDictionaryString namespaceUri)
  185. {
  186. if (localName == null)
  187. throw new ArgumentNullException ("localName");
  188. if (namespaceUri == null)
  189. throw new ArgumentNullException ("namespaceUri");
  190. MoveToStartElement (localName.Value, namespaceUri.Value);
  191. }
  192. [MonoTODO]
  193. public virtual void StartCanonicalization (
  194. Stream stream, bool includeComments,
  195. string [] inclusivePrefixes)
  196. {
  197. throw new NotSupportedException ();
  198. }
  199. public virtual bool TryGetArrayLength (out int count)
  200. {
  201. count = -1;
  202. return false;
  203. }
  204. public virtual bool TryGetBase64ContentLength (out int count)
  205. {
  206. count = -1;
  207. return false;
  208. }
  209. public virtual bool TryGetLocalNameAsDictionaryString (
  210. out XmlDictionaryString localName)
  211. {
  212. localName = null;
  213. return false;
  214. }
  215. public virtual bool TryGetNamespaceUriAsDictionaryString (
  216. out XmlDictionaryString namespaceUri)
  217. {
  218. namespaceUri = null;
  219. return false;
  220. }
  221. #region Content Reader Methods
  222. [MonoTODO]
  223. public override object ReadContentAs (Type type, IXmlNamespaceResolver nsResolver)
  224. {
  225. return base.ReadContentAs (type, nsResolver);
  226. }
  227. public virtual byte [] ReadContentAsBase64 ()
  228. {
  229. int len;
  230. if (!TryGetBase64ContentLength (out len))
  231. return Convert.FromBase64String (ReadContentAsString ());
  232. byte [] bytes = new byte [len];
  233. ReadContentAsBase64 (bytes, 0, len);
  234. return bytes;
  235. }
  236. MethodInfo xmlconv_from_bin_hex = typeof (XmlConvert).GetMethod ("FromBinHexString", BindingFlags.Static | BindingFlags.NonPublic, null, new Type [] {typeof (string)}, null);
  237. byte [] FromBinHexString (string s)
  238. {
  239. return (byte []) xmlconv_from_bin_hex.Invoke (null, new object [] {s});
  240. }
  241. public virtual byte [] ReadContentAsBinHex ()
  242. {
  243. int len;
  244. if (!TryGetArrayLength (out len))
  245. return FromBinHexString (ReadContentAsString ());
  246. return ReadContentAsBinHex (len);
  247. }
  248. protected byte [] ReadContentAsBinHex (int maxByteArrayContentLength)
  249. {
  250. byte [] bytes = new byte [maxByteArrayContentLength];
  251. ReadContentAsBinHex (bytes, 0, maxByteArrayContentLength);
  252. return bytes;
  253. }
  254. [MonoTODO]
  255. public virtual int ReadContentAsChars (char [] chars, int offset, int count)
  256. {
  257. throw new NotImplementedException ();
  258. }
  259. [MonoTODO]
  260. public override decimal ReadContentAsDecimal ()
  261. {
  262. return base.ReadContentAsDecimal ();
  263. }
  264. [MonoTODO]
  265. public override float ReadContentAsFloat ()
  266. {
  267. return base.ReadContentAsFloat ();
  268. }
  269. public virtual Guid ReadContentAsGuid ()
  270. {
  271. return XmlConvert.ToGuid (ReadContentAsString ());
  272. }
  273. public virtual void ReadContentAsQualifiedName (out string localName, out string namespaceUri)
  274. {
  275. XmlQualifiedName qname = (XmlQualifiedName) ReadContentAs (typeof (XmlQualifiedName), this as IXmlNamespaceResolver);
  276. localName = qname.Name;
  277. namespaceUri = qname.Namespace;
  278. }
  279. public override string ReadContentAsString ()
  280. {
  281. return ReadContentAsString (Quotas.MaxStringContentLength);
  282. }
  283. [MonoTODO]
  284. protected string ReadContentAsString (int maxStringContentLength)
  285. {
  286. return base.ReadContentAsString ();
  287. }
  288. [MonoTODO]
  289. public virtual string ReadContentAsString (string [] strings, out int index)
  290. {
  291. throw new NotImplementedException ();
  292. }
  293. [MonoTODO]
  294. public virtual string ReadContentAsString (XmlDictionaryString [] strings, out int index)
  295. {
  296. throw new NotImplementedException ();
  297. }
  298. public virtual TimeSpan ReadContentAsTimeSpan ()
  299. {
  300. return XmlConvert.ToTimeSpan (ReadContentAsString ());
  301. }
  302. public virtual UniqueId ReadContentAsUniqueId ()
  303. {
  304. return new UniqueId (ReadContentAsString ());
  305. }
  306. public virtual byte [] ReadElementContentAsBase64 ()
  307. {
  308. ReadStartElement ();
  309. byte [] ret = ReadContentAsBase64 ();
  310. ReadEndElement ();
  311. return ret;
  312. }
  313. public virtual byte [] ReadElementContentAsBinHex ()
  314. {
  315. ReadStartElement ();
  316. byte [] ret = ReadContentAsBinHex ();
  317. ReadEndElement ();
  318. return ret;
  319. }
  320. public virtual Guid ReadElementContentAsGuid ()
  321. {
  322. ReadStartElement ();
  323. Guid ret = ReadContentAsGuid ();
  324. ReadEndElement ();
  325. return ret;
  326. }
  327. public virtual TimeSpan ReadElementContentAsTimeSpan ()
  328. {
  329. ReadStartElement ();
  330. TimeSpan ret = ReadContentAsTimeSpan ();
  331. ReadEndElement ();
  332. return ret;
  333. }
  334. public virtual UniqueId ReadElementContentAsUniqueId ()
  335. {
  336. ReadStartElement ();
  337. UniqueId ret = ReadContentAsUniqueId ();
  338. ReadEndElement ();
  339. return ret;
  340. }
  341. public override string ReadElementContentAsString ()
  342. {
  343. if (IsEmptyElement) {
  344. Read ();
  345. return String.Empty;
  346. } else {
  347. ReadStartElement ();
  348. string s;
  349. if (NodeType == XmlNodeType.EndElement)
  350. s = String.Empty;
  351. else
  352. s = ReadContentAsString ();
  353. ReadEndElement ();
  354. return s;
  355. }
  356. }
  357. public virtual void ReadFullStartElement ()
  358. {
  359. if (!IsStartElement ())
  360. throw new XmlException ("Current node is not a start element");
  361. ReadStartElement ();
  362. }
  363. public virtual void ReadFullStartElement (string name)
  364. {
  365. if (!IsStartElement (name))
  366. throw new XmlException (String.Format ("Current node is not a start element '{0}'", name));
  367. ReadStartElement (name);
  368. }
  369. public virtual void ReadFullStartElement (string localName, string namespaceUri)
  370. {
  371. if (!IsStartElement (localName, namespaceUri))
  372. throw new XmlException (String.Format ("Current node is not a start element '{0}' in namesapce '{1}'", localName, namespaceUri));
  373. ReadStartElement (localName, namespaceUri);
  374. }
  375. public virtual void ReadFullStartElement (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  376. {
  377. if (!IsStartElement (localName, namespaceUri))
  378. throw new XmlException (String.Format ("Current node is not a start element '{0}' in namesapce '{1}'", localName, namespaceUri));
  379. ReadStartElement (localName.Value, namespaceUri.Value);
  380. }
  381. public override string ReadString ()
  382. {
  383. return ReadString (Quotas.MaxStringContentLength);
  384. }
  385. [MonoTODO]
  386. protected string ReadString (int maxStringContentLength)
  387. {
  388. return base.ReadString ();
  389. }
  390. public virtual byte [] ReadValueAsBase64 (byte [] bytes, int start, int length)
  391. {
  392. throw new NotSupportedException (); // as it is documented ...
  393. }
  394. public virtual bool TryGetValueAsDictionaryString (out XmlDictionaryString value)
  395. {
  396. throw new NotSupportedException (); // as documented
  397. }
  398. #endregion
  399. #region Factory Methods
  400. public static XmlDictionaryReader CreateBinaryReader (
  401. byte [] buffer, XmlDictionaryReaderQuotas quotas)
  402. {
  403. return CreateBinaryReader (buffer, 0, buffer.Length, quotas);
  404. }
  405. public static XmlDictionaryReader CreateBinaryReader (
  406. byte [] buffer, int offset, int count,
  407. XmlDictionaryReaderQuotas quotas)
  408. {
  409. return CreateBinaryReader (buffer, offset, count, new XmlDictionary (), quotas);
  410. }
  411. public static XmlDictionaryReader CreateBinaryReader (
  412. byte [] buffer, int offset, int count,
  413. IXmlDictionary dictionary,
  414. XmlDictionaryReaderQuotas quotas)
  415. {
  416. return CreateBinaryReader (buffer, offset, count,
  417. dictionary, quotas,
  418. new XmlBinaryReaderSession (), null);
  419. }
  420. public static XmlDictionaryReader CreateBinaryReader (
  421. byte [] buffer, int offset, int count,
  422. IXmlDictionary dictionary,
  423. XmlDictionaryReaderQuotas quotas,
  424. XmlBinaryReaderSession session)
  425. {
  426. return CreateBinaryReader (buffer, offset, count,
  427. dictionary, quotas,
  428. session, null);
  429. }
  430. public static XmlDictionaryReader CreateBinaryReader (
  431. byte [] buffer, int offset, int count,
  432. IXmlDictionary dictionary,
  433. XmlDictionaryReaderQuotas quotas,
  434. XmlBinaryReaderSession session,
  435. OnXmlDictionaryReaderClose onClose)
  436. {
  437. return new XmlBinaryDictionaryReader (buffer,
  438. offset, count,
  439. dictionary, quotas, session, onClose);
  440. }
  441. public static XmlDictionaryReader CreateBinaryReader (
  442. Stream stream, XmlDictionaryReaderQuotas quotas)
  443. {
  444. return CreateBinaryReader (stream, new XmlDictionary (), quotas);
  445. }
  446. public static XmlDictionaryReader CreateBinaryReader (
  447. Stream stream, IXmlDictionary dictionary,
  448. XmlDictionaryReaderQuotas quotas)
  449. {
  450. return CreateBinaryReader (stream, dictionary, quotas,
  451. new XmlBinaryReaderSession (), null);
  452. }
  453. public static XmlDictionaryReader CreateBinaryReader (
  454. Stream stream, IXmlDictionary dictionary,
  455. XmlDictionaryReaderQuotas quotas,
  456. XmlBinaryReaderSession session)
  457. {
  458. return CreateBinaryReader (stream, dictionary, quotas,
  459. session, null);
  460. }
  461. public static XmlDictionaryReader CreateBinaryReader (
  462. Stream stream, IXmlDictionary dictionary,
  463. XmlDictionaryReaderQuotas quotas,
  464. XmlBinaryReaderSession session,
  465. OnXmlDictionaryReaderClose onClose)
  466. {
  467. return new XmlBinaryDictionaryReader (stream,
  468. dictionary, quotas, session, onClose);
  469. }
  470. public static XmlDictionaryReader CreateDictionaryReader (
  471. XmlReader reader)
  472. {
  473. return new XmlSimpleDictionaryReader (reader);
  474. }
  475. [MonoTODO]
  476. public static XmlDictionaryReader CreateMtomReader (
  477. Stream stream, Encoding encoding,
  478. XmlDictionaryReaderQuotas quotas)
  479. {
  480. throw new NotImplementedException ();
  481. }
  482. [MonoTODO]
  483. public static XmlDictionaryReader CreateMtomReader (
  484. Stream stream, Encoding [] encodings,
  485. XmlDictionaryReaderQuotas quotas)
  486. {
  487. throw new NotImplementedException ();
  488. }
  489. [MonoTODO]
  490. public static XmlDictionaryReader CreateMtomReader (
  491. Stream stream, Encoding [] encodings, string contentType,
  492. XmlDictionaryReaderQuotas quotas)
  493. {
  494. throw new NotImplementedException ();
  495. }
  496. [MonoTODO]
  497. public static XmlDictionaryReader CreateMtomReader (
  498. Stream stream, Encoding [] encodings, string contentType,
  499. XmlDictionaryReaderQuotas quotas,
  500. int maxBufferSize,
  501. OnXmlDictionaryReaderClose onClose)
  502. {
  503. throw new NotImplementedException ();
  504. }
  505. [MonoTODO]
  506. public static XmlDictionaryReader CreateMtomReader (
  507. byte [] buffer, int offset, int count,
  508. Encoding encoding, XmlDictionaryReaderQuotas quotas)
  509. {
  510. throw new NotImplementedException ();
  511. }
  512. [MonoTODO]
  513. public static XmlDictionaryReader CreateMtomReader (
  514. byte [] buffer, int offset, int count,
  515. Encoding [] encodings, XmlDictionaryReaderQuotas quotas)
  516. {
  517. throw new NotImplementedException ();
  518. }
  519. [MonoTODO]
  520. public static XmlDictionaryReader CreateMtomReader (
  521. byte [] buffer, int offset, int count,
  522. Encoding [] encodings, string contentType,
  523. XmlDictionaryReaderQuotas quotas)
  524. {
  525. throw new NotImplementedException ();
  526. }
  527. [MonoTODO]
  528. public static XmlDictionaryReader CreateMtomReader (
  529. byte [] buffer, int offset, int count,
  530. Encoding [] encodings, string contentType,
  531. XmlDictionaryReaderQuotas quotas,
  532. int maxBufferSize,
  533. OnXmlDictionaryReaderClose onClose)
  534. {
  535. throw new NotImplementedException ();
  536. }
  537. public static XmlDictionaryReader CreateTextReader (byte [] buffer, XmlDictionaryReaderQuotas quotas)
  538. {
  539. return CreateTextReader (buffer, 0, buffer.Length, quotas);
  540. }
  541. public static XmlDictionaryReader CreateTextReader (
  542. byte [] buffer, int offset, int count,
  543. XmlDictionaryReaderQuotas quotas)
  544. {
  545. return CreateTextReader (buffer, offset, count,
  546. Encoding.UTF8, quotas, null);
  547. }
  548. public static XmlDictionaryReader CreateTextReader (
  549. byte [] buffer, int offset, int count,
  550. Encoding encoding,
  551. XmlDictionaryReaderQuotas quotas,
  552. OnXmlDictionaryReaderClose onClose)
  553. {
  554. return CreateTextReader (new MemoryStream (buffer, offset, count), encoding, quotas, onClose);
  555. }
  556. public static XmlDictionaryReader CreateTextReader (
  557. Stream stream, XmlDictionaryReaderQuotas quotas)
  558. {
  559. return CreateTextReader (stream, Encoding.UTF8, quotas, null);
  560. }
  561. public static XmlDictionaryReader CreateTextReader (
  562. Stream stream, Encoding encoding,
  563. XmlDictionaryReaderQuotas quotas,
  564. OnXmlDictionaryReaderClose onClose)
  565. {
  566. XmlReaderSettings s = new XmlReaderSettings ();
  567. XmlNameTable nt = new NameTable ();
  568. XmlParserContext c = new XmlParserContext (nt, new XmlNamespaceManager (nt), String.Empty, XmlSpace.None, encoding);
  569. XmlDictionaryReader res = new XmlSimpleDictionaryReader (XmlReader.Create (stream, s, c), null, onClose);
  570. res.quotas = quotas;
  571. return res;
  572. }
  573. #endregion
  574. }
  575. }
  576. #endif