SqlXmlTextReader.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // System.Data.SqlClient.SqlXmlTextReader.cs
  3. //
  4. // Author:
  5. // Konstantin Triger ([email protected])
  6. //
  7. // Copyright (C) 2006 Mainsoft, corp. (http://www.mainsoft.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. using System;
  29. using System.IO;
  30. using System.Text;
  31. using System.Xml;
  32. namespace System.Data.SqlClient {
  33. internal sealed class SqlXmlTextReader : TextReader {
  34. #region FragmentXmlTextReader
  35. sealed class FragmentXmlTextReader : XmlTextReader {
  36. public FragmentXmlTextReader(System.IO.TextReader reader) : base(reader) {}
  37. public override bool Read() {
  38. do {
  39. if (!base.Read())
  40. return false;
  41. }while(base.Depth == 0);
  42. return true;
  43. }
  44. public override int Depth {
  45. get {
  46. int depth = base.Depth;
  47. if (depth >= 1)
  48. depth --;
  49. return depth;
  50. }
  51. }
  52. }
  53. #endregion
  54. #region Fields
  55. bool _hasPeekedChar;
  56. readonly char[] _peekedChar = new char[1];
  57. readonly SqlDataReader _reader;
  58. string _data;
  59. int _rootPosition;
  60. int _position = -1;
  61. bool _eof;
  62. static readonly char[] OpenRoot = new char[] {'<', 'X', '>'};
  63. const int OpenRootLength = 3;
  64. static readonly char[] CloseRoot = new char[] {'<', '/', 'X', '>'};
  65. const int CloseRootLength = 4;
  66. #endregion // Fields
  67. #region Constructors
  68. private SqlXmlTextReader (SqlDataReader reader) {
  69. _reader = reader;
  70. }
  71. #endregion
  72. #region Methods
  73. public static XmlReader Create(SqlDataReader dataReader) {
  74. return new FragmentXmlTextReader(new SqlXmlTextReader(dataReader));
  75. }
  76. public override void Close() {
  77. _reader.Close ();
  78. }
  79. public override int Peek () {
  80. if (!_hasPeekedChar) {
  81. int consumed = Read(_peekedChar, 0, 1);
  82. if (consumed < 0)
  83. return -1;
  84. _hasPeekedChar = true;
  85. }
  86. return _peekedChar[0];
  87. }
  88. public override int Read () {
  89. int c = Peek();
  90. _hasPeekedChar = false;
  91. return c;
  92. }
  93. public override int Read (char[] buffer, int index, int count) {
  94. if (buffer == null)
  95. throw new ArgumentNullException("buffer");
  96. if (index < 0)
  97. throw new ArgumentOutOfRangeException("index");
  98. if (count < 0)
  99. throw new ArgumentOutOfRangeException("count");
  100. if (count == 0)
  101. return 0;
  102. int got = 0;
  103. if (_hasPeekedChar) {
  104. buffer[index++] = _peekedChar[0];
  105. count--;
  106. _hasPeekedChar = false;
  107. got ++;
  108. }
  109. if (!_eof) {
  110. while (count > 0) {
  111. if (_rootPosition < OpenRootLength) {
  112. buffer[index++] = OpenRoot[_rootPosition++];
  113. count --;
  114. got ++;
  115. continue;
  116. }
  117. if (_position < 0) {
  118. if (_reader.Read()) {
  119. _position = 0;
  120. _data = _reader.GetString(0);
  121. }
  122. else {
  123. if(_reader.NextResult())
  124. continue;
  125. else {
  126. _rootPosition = 0;
  127. _eof = true;
  128. break;
  129. }
  130. }
  131. }
  132. int consumed = ((_position + count) > _data.Length) ? (_data.Length - (int)_position) : count;
  133. _data.CopyTo(_position, buffer, index, consumed);
  134. if (consumed > 0) {
  135. _position += consumed;
  136. got += consumed;
  137. index += consumed;
  138. count -= consumed;
  139. }
  140. else
  141. _position = -1;
  142. }
  143. }
  144. while (count > 0 && _rootPosition < CloseRootLength) {
  145. buffer[index++] = CloseRoot[_rootPosition++];
  146. count --;
  147. got ++;
  148. }
  149. return got;
  150. }
  151. #endregion // Methods
  152. }
  153. }