ElementLogReader.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Globalization;
  3. using System.Xml;
  4. namespace WinSCP
  5. {
  6. internal class ElementLogReader : CustomLogReader
  7. {
  8. public ElementLogReader(CustomLogReader parentReader) :
  9. base(parentReader.Session)
  10. {
  11. _parentReader = parentReader;
  12. if ((NodeType != XmlNodeType.Element) ||
  13. IsEmptyElement)
  14. {
  15. throw Session.Logger.WriteException(new InvalidOperationException("Cannot use ElementLogReader with non-element node or empty element"));
  16. }
  17. _localName = _parentReader.Reader.LocalName;
  18. _depth = _parentReader.Reader.Depth;
  19. _token = _localName + "@" + _depth;
  20. _read = false;
  21. }
  22. public override void Dispose()
  23. {
  24. using (Session.Logger.CreateCallstack(_token))
  25. {
  26. try
  27. {
  28. ReadToEnd(0);
  29. }
  30. catch (Exception)
  31. {
  32. // swallow
  33. Session.Logger.WriteLine("Swallowing exception");
  34. }
  35. }
  36. base.Dispose();
  37. }
  38. public override bool Read(LogReadFlags flags)
  39. {
  40. if (_read)
  41. {
  42. throw Session.Logger.WriteException(
  43. new InvalidOperationException(
  44. string.Format(CultureInfo.CurrentCulture, "Element {0} already read to the end", _token)));
  45. }
  46. bool result = _parentReader.Read(flags);
  47. if (result &&
  48. IsEndElement(_localName) &&
  49. (Depth == _depth))
  50. {
  51. result = false;
  52. Session.Logger.WriteLineLevel(1, "Element {0} read to the end", _token);
  53. _read = true;
  54. }
  55. return result;
  56. }
  57. public void ReadToEnd(LogReadFlags flags)
  58. {
  59. using (Session.Logger.CreateCallstack(_token))
  60. {
  61. if (!_read)
  62. {
  63. while (Read(flags))
  64. {
  65. }
  66. }
  67. }
  68. }
  69. internal override XmlReader Reader
  70. {
  71. get { return _parentReader.Reader; }
  72. }
  73. private readonly CustomLogReader _parentReader;
  74. private readonly string _localName;
  75. private readonly int _depth;
  76. protected bool _read;
  77. private readonly string _token;
  78. }
  79. }