CustomLogReader.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 SessionLocalException.CreateElementNotFound(Session, localName);
  86. }
  87. }
  88. public ElementLogReader CreateLogReader()
  89. {
  90. return new ElementLogReader(this);
  91. }
  92. public ElementLogReader WaitForNonEmptyElementAndCreateLogReader(string localName, LogReadFlags flags)
  93. {
  94. WaitForNonEmptyElement(localName, flags);
  95. return CreateLogReader();
  96. }
  97. public ElementLogReader WaitForGroupAndCreateLogReader()
  98. {
  99. return WaitForNonEmptyElementAndCreateLogReader("group", LogReadFlags.ThrowFailures);
  100. }
  101. public string GetAttribute(string name)
  102. {
  103. return Reader.GetAttribute(name);
  104. }
  105. }
  106. }