5using System.Xml.Schema;
8using System.Windows.Controls;
9using System.Windows.Media;
16using System.Threading.Tasks;
26 private readonly
string identifier;
27 private readonly
bool custom;
33 this.identifier = Identifier;
42 this.node?.RemoveSniffer(this.sniffer);
43 this.node?.ViewClosed();
47 public string Identifier => this.identifier;
48 public bool Custom => this.custom;
53 internal set => this.sniffer = value;
61 private Task AddItem(
object P)
63 this.SnifferListView.Items.Add((
SniffItem)P);
64 this.SnifferListView.ScrollIntoView(P);
65 return Task.CompletedTask;
68 public void NewButton_Click(
object Sender, RoutedEventArgs e)
70 this.SnifferListView.Items.Clear();
73 public void SaveButton_Click(
object Sender, RoutedEventArgs e)
75 this.SaveAsButton_Click(Sender, e);
78 public void SaveAsButton_Click(
object Sender, RoutedEventArgs e)
80 SaveFileDialog Dialog =
new SaveFileDialog()
83 CheckPathExists =
true,
86 Filter =
"XML Files (*.xml)|*.xml|HTML Files (*.html,*.htm)|*.html,*.htm|All Files (*.*)|*.*",
87 Title =
"Save sniff file"
90 bool? Result = Dialog.ShowDialog(
MainWindow.FindWindow(
this));
92 if (Result.HasValue && Result.Value)
96 if (Dialog.FilterIndex == 2)
98 StringBuilder Xml =
new StringBuilder();
104 string Html =
XSL.
Transform(Xml.ToString(), sniffToHtml);
106 File.WriteAllText(Dialog.FileName, Html, Encoding.UTF8);
110 using (FileStream f = File.Create(Dialog.FileName))
121 MessageBox.Show(
MainWindow.FindWindow(
this), ex.Message,
"Unable to save file.", MessageBoxButton.OK, MessageBoxImage.Error);
126 private static readonly XslCompiledTransform sniffToHtml =
XSL.
LoadTransform(
"Waher.Client.WPF.Transforms.SniffToHTML.xslt");
127 private static readonly XmlSchema schema =
XSL.
LoadSchema(
"Waher.Client.WPF.Schema.Sniff.xsd");
128 private const string sniffNamespace =
"http://waher.se/Schema/Sniff.xsd";
129 private const string sniffRoot =
"Sniff";
131 private void SaveAsXml(XmlWriter w)
133 w.WriteStartElement(sniffRoot, sniffNamespace);
135 foreach (
SniffItem Item
in this.SnifferListView.Items)
137 w.WriteStartElement(Item.
Type.ToString());
140 if (!(Item.
Data is
null))
141 w.WriteValue(Convert.ToBase64String(Item.
Data));
152 public void OpenButton_Click(
object Sender, RoutedEventArgs e)
156 OpenFileDialog Dialog =
new OpenFileDialog()
159 CheckFileExists =
true,
160 CheckPathExists =
true,
162 Filter =
"XML Files (*.xml)|*.xml|All Files (*.*)|*.*",
165 Title =
"Open sniff file"
168 bool? Result = Dialog.ShowDialog(
MainWindow.FindWindow(
this));
170 if (Result.HasValue && Result.Value)
172 XmlDocument Xml =
new XmlDocument()
174 PreserveWhitespace =
true
176 Xml.Load(Dialog.FileName);
178 this.Load(Xml, Dialog.FileName);
184 MessageBox.Show(ex.Message,
"Unable to load file.", MessageBoxButton.OK, MessageBoxImage.Error);
188 public void Load(XmlDocument Xml,
string FileName)
192 Color ForegroundColor;
193 Color BackgroundColor;
198 XSL.
Validate(FileName, Xml, sniffRoot, sniffNamespace, schema);
200 this.SnifferListView.Items.Clear();
202 foreach (XmlNode N
in Xml.DocumentElement.ChildNodes)
208 if (!Enum.TryParse(E.LocalName, out SniffItemType Type))
211 Timestamp =
XML.
Attribute(E,
"timestamp", DateTime.MinValue);
215 case SniffItemType.DataReceived:
216 ForegroundColor = Colors.White;
217 BackgroundColor = Colors.Navy;
221 case SniffItemType.DataTransmitted:
222 ForegroundColor = Colors.Black;
223 BackgroundColor = Colors.White;
227 case SniffItemType.TextReceived:
228 ForegroundColor = Colors.White;
229 BackgroundColor = Colors.Navy;
233 case SniffItemType.TextTransmitted:
234 ForegroundColor = Colors.Black;
235 BackgroundColor = Colors.White;
239 case SniffItemType.Information:
240 ForegroundColor = Colors.Yellow;
241 BackgroundColor = Colors.DarkGreen;
245 case SniffItemType.Warning:
246 ForegroundColor = Colors.Black;
247 BackgroundColor = Colors.Yellow;
251 case SniffItemType.Error:
252 ForegroundColor = Colors.Yellow;
253 BackgroundColor = Colors.Red;
257 case SniffItemType.Exception:
258 ForegroundColor = Colors.Yellow;
259 BackgroundColor = Colors.DarkRed;
269 Data = Convert.FromBase64String(E.InnerText);
275 Message = E.InnerText;
278 this.Add(
new SniffItem(Timestamp, Type, Message, Data, ForegroundColor, BackgroundColor));
282 private void UserControl_SizeChanged(
object Sender, SizeChangedEventArgs e)
284 if (this.SnifferListView.View is GridView GridView)
285 GridView.Columns[1].Width = Math.Max(this.ActualWidth - GridView.Columns[0].ActualWidth - SystemParameters.VerticalScrollBarWidth - 8, 10);
Interaction logic for SnifferView.xaml
void InitializeComponent()
InitializeComponent
Represents one item in a sniffer output.
byte[] Data
Optional binary data.
SniffItemType Type
Sniff item type.
DateTime Timestamp
Timestamp of event.
Interaction logic for xaml
Abstract base class for tree nodes in the connection view.
Helps with common XML-related tasks.
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
static string Encode(string s)
Encodes a string for use in XML.
static XmlWriterSettings WriterSettings(bool Indent, bool OmitXmlDeclaration)
Gets an XML writer settings object.
Static class managing loading of XSL resources stored as embedded resources or in content files.
static XmlSchema LoadSchema(string ResourceName)
Loads an XML schema from an embedded resource.
static XslCompiledTransform LoadTransform(string ResourceName)
Loads an XSL transformation from an embedded resource.
static string Transform(string XML, XslCompiledTransform Transform)
Transforms an XML document using an XSL transform.
static void Validate(string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
Static class managing the application event log. Applications and services log events on this static ...
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Interface for tab view user controls in the client.