1
0

CustomLogReader.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Xml;
  3. namespace WinSCP
  4. {
  5. [Flags]
  6. internal enum LogReadFlags
  7. {
  8. ThrowFailures = 0x01
  9. }
  10. internal abstract class CustomLogReader : IDisposable
  11. {
  12. public Session Session { get; private set; }
  13. public XmlNodeType NodeType { get { return Reader.NodeType; } }
  14. public string NamespaceURI { get { return Reader.NamespaceURI; } }
  15. public string LocalName { get { return Reader.LocalName; } }
  16. public bool IsEmptyElement { get { return Reader.IsEmptyElement; } }
  17. public int Depth { get { return Reader.Depth; } }
  18. public string Value { get { return Reader.Value; } }
  19. internal abstract XmlReader Reader { get; }
  20. public abstract bool Read(LogReadFlags flags);
  21. protected CustomLogReader(Session session)
  22. {
  23. Session = session;
  24. }
  25. public virtual void Dispose()
  26. {
  27. }
  28. public bool IsElement()
  29. {
  30. return
  31. (NodeType == XmlNodeType.Element) &&
  32. (NamespaceURI == Session.Namespace);
  33. }
  34. public bool IsElement(string localName)
  35. {
  36. return
  37. IsElement() &&
  38. (LocalName == localName);
  39. }
  40. public bool IsNonEmptyElement(string localName)
  41. {
  42. return
  43. IsElement(localName) &&
  44. !IsEmptyElement;
  45. }
  46. public bool GetEmptyElementValue(string localName, out string value)
  47. {
  48. bool result =
  49. IsElement(localName) &&
  50. IsEmptyElement;
  51. if (result)
  52. {
  53. value = GetAttribute("value");
  54. result = (value != null);
  55. }
  56. else
  57. {
  58. value = null;
  59. }
  60. return result;
  61. }
  62. public bool IsEndElement(string localName)
  63. {
  64. return
  65. (NodeType == XmlNodeType.EndElement) &&
  66. (NamespaceURI == Session.Namespace) &&
  67. (LocalName == localName);
  68. }
  69. public bool TryWaitForNonEmptyElement(string localName, LogReadFlags flags)
  70. {
  71. bool result = false;
  72. while (!result && Read(flags))
  73. {
  74. if (IsNonEmptyElement(localName))
  75. {
  76. result = true;
  77. }
  78. }
  79. return result;
  80. }
  81. public void WaitForNonEmptyElement(string localName, LogReadFlags flags)
  82. {
  83. if (!TryWaitForNonEmptyElement(localName, flags))
  84. {
  85. throw Session.Logger.WriteException(SessionLocalException.CreateElementNotFound(Session, localName));
  86. }
  87. }
  88. public bool TryWaitForEmptyElement(string localName, LogReadFlags flags)
  89. {
  90. bool result = false;
  91. while (!result && Read(flags))
  92. {
  93. if (IsElement(localName) &&
  94. IsEmptyElement)
  95. {
  96. result = true;
  97. }
  98. }
  99. return result;
  100. }
  101. public ElementLogReader CreateLogReader()
  102. {
  103. return new ElementLogReader(this);
  104. }
  105. public ElementLogReader WaitForNonEmptyElementAndCreateLogReader(string localName, LogReadFlags flags)
  106. {
  107. WaitForNonEmptyElement(localName, flags);
  108. return CreateLogReader();
  109. }
  110. public ElementLogReader WaitForGroupAndCreateLogReader()
  111. {
  112. return WaitForNonEmptyElementAndCreateLogReader("group", LogReadFlags.ThrowFailures);
  113. }
  114. public string GetAttribute(string name)
  115. {
  116. return Reader.GetAttribute(name);
  117. }
  118. }
  119. }