From 424012d979eaaef85bf674cd47767e936f4b0351 Mon Sep 17 00:00:00 2001 From: John Simons Date: Mon, 11 May 2015 11:37:50 +1000 Subject: [PATCH] Improve message body xml display --- .../XmlViewerViewModelTests.cs | 46 +----------------- .../ExtensionMethods/XmlDocumentExtensions.cs | 34 +++++-------- .../XmlViewer/XmlMessageView.xaml.cs | 21 +++++++- .../XmlViewer/XmlMessageViewModel.cs | 48 ++----------------- 4 files changed, 39 insertions(+), 110 deletions(-) diff --git a/src/ServiceInsight.Tests/XmlViewerViewModelTests.cs b/src/ServiceInsight.Tests/XmlViewerViewModelTests.cs index 2a18cf3b..dee3d71e 100644 --- a/src/ServiceInsight.Tests/XmlViewerViewModelTests.cs +++ b/src/ServiceInsight.Tests/XmlViewerViewModelTests.cs @@ -2,7 +2,6 @@ { using System.Xml; using Caliburn.Micro; - using Desktop.Framework; using Desktop.MessageViewers.XmlViewer; using Desktop.Models; using NSubstitute; @@ -16,32 +15,16 @@ public class XmlViewerViewModelTests { XmlMessageViewModel ViewModel; IXmlMessageView View; - IContentDecoder XmlDecoder; - IClipboard Clipboard; const string TestMessage = ""; [SetUp] public void TestInitialize() { - XmlDecoder = Substitute.For>(); - Clipboard = Substitute.For(); View = Substitute.For(); - ViewModel = new XmlMessageViewModel(XmlDecoder, Clipboard); + ViewModel = new XmlMessageViewModel(); ((IActivate)ViewModel).Activate(); } - [Test] - public void should_decode_message_as_xml_when_view_is_loaded() - { - XmlDecoder.Decode(Arg.Any()).Returns(new DecoderResult(GetDocument(TestMessage))); - - ((IViewAware)ViewModel).AttachView(View); - ViewModel.SelectedMessage = new MessageBody { Body = TestMessage }; - - View.Received(1).Display(Arg.Any()); - XmlDecoder.Received(1).Decode(Arg.Any()); - } - [Test] public void should_clear_message_body_when_selected_message_is_unselected() { @@ -58,32 +41,5 @@ public void should_not_load_the_message_when_view_is_not_loaded() View.DidNotReceive().Display(Arg.Any()); } - - [Test] - public void should_be_possible_to_copy_the_selected_message_with_copy_message_context_menu_item() - { - ViewModel.SelectedMessage = new MessageBody(); - - ViewModel.CanCopyMessageXml().ShouldBe(true); - } - - [Test] - public void clipboard_should_have_message_content_when_copying_message() - { - ViewModel.SelectedMessage = new MessageBody { Body = TestMessage }; - - XmlDecoder.Decode(Arg.Any()).Returns(new DecoderResult(GetDocument(TestMessage))); - - ViewModel.CopyMessageXml(); - - Clipboard.Received().CopyTo(Arg.Any()); - } - - static XmlDocument GetDocument(string content) - { - var document = new XmlDocument(); - document.LoadXml(content); - return document; - } } } \ No newline at end of file diff --git a/src/ServiceInsight/ExtensionMethods/XmlDocumentExtensions.cs b/src/ServiceInsight/ExtensionMethods/XmlDocumentExtensions.cs index e49dce7d..aa0f0c4b 100644 --- a/src/ServiceInsight/ExtensionMethods/XmlDocumentExtensions.cs +++ b/src/ServiceInsight/ExtensionMethods/XmlDocumentExtensions.cs @@ -6,27 +6,19 @@ public static class XmlDocumentExtensions { - public static string GetFormatted(this XmlDocument document) - { - if (document.InnerXml.IsEmpty()) return string.Empty; + public static string GetFormatted(this XmlDocument document) + { + var sb = new StringBuilder(); + using (var sw = new StringWriter(sb)) + using (var xtw = XmlWriter.Create(sw, new XmlWriterSettings + { + Indent = true + })) + { + document.WriteTo(xtw); + } - var xd = new XmlDocument(); - xd.LoadXml(document.InnerXml); - - var sb = new StringBuilder(); - var sw = new StringWriter(sb); - var xtw = new XmlTextWriter(sw) { Formatting = Formatting.Indented }; - - try - { - xd.WriteTo(xtw); - } - finally - { - xtw.Close(); - } - - return sb.ToString(); - } + return sb.ToString(); + } } } \ No newline at end of file diff --git a/src/ServiceInsight/MessageViewers/XmlViewer/XmlMessageView.xaml.cs b/src/ServiceInsight/MessageViewers/XmlViewer/XmlMessageView.xaml.cs index 765e7086..46dde0eb 100644 --- a/src/ServiceInsight/MessageViewers/XmlViewer/XmlMessageView.xaml.cs +++ b/src/ServiceInsight/MessageViewers/XmlViewer/XmlMessageView.xaml.cs @@ -2,7 +2,9 @@ { using System; using System.Windows.Media; + using System.Xml; using ICSharpCode.AvalonEdit.Folding; + using Particular.ServiceInsight.Desktop.ExtensionMethods; public partial class XmlMessageView : IXmlMessageView { @@ -21,9 +23,26 @@ public XmlMessageView() public virtual void Display(string message) { if (message == null) + { return; + } - document.Document.Text = message; + var doc = new XmlDocument(); + try + { + doc.LoadXml(message); + } + catch (XmlException) + { + // It looks like we having issues parsing the xml + // Best to do in this circunstances is to still diplay the text but turn off folding + FoldingManager.Uninstall(foldingManager); + document.Document.Text = message; + + return; + } + + document.Document.Text = doc.GetFormatted(); foldingStrategy.UpdateFoldings(foldingManager, document.Document); } diff --git a/src/ServiceInsight/MessageViewers/XmlViewer/XmlMessageViewModel.cs b/src/ServiceInsight/MessageViewers/XmlViewer/XmlMessageViewModel.cs index 84af12bc..2030214b 100644 --- a/src/ServiceInsight/MessageViewers/XmlViewer/XmlMessageViewModel.cs +++ b/src/ServiceInsight/MessageViewers/XmlViewer/XmlMessageViewModel.cs @@ -1,29 +1,14 @@ namespace Particular.ServiceInsight.Desktop.MessageViewers.XmlViewer { - using System.Text; - using System.Xml; using Caliburn.Micro; - using ExtensionMethods; - using Framework; - using Models; using Particular.ServiceInsight.Desktop.Framework.Events; - using Particular.ServiceInsight.Desktop.Framework.MessageDecoders; + using Particular.ServiceInsight.Desktop.Models; public class XmlMessageViewModel : Screen, IHandle { - readonly IClipboard clipboard; - IContentDecoder xmlDecoder; IXmlMessageView messageView; - public XmlMessageViewModel( - IContentDecoder xmlDecoder, - IClipboard clipboard) - { - this.clipboard = clipboard; - this.xmlDecoder = xmlDecoder; - } - protected override void OnActivate() { base.OnActivate(); @@ -44,21 +29,13 @@ public void OnSelectedMessageChanged() if (messageView == null) return; messageView.Clear(); - ShowMessageBody(); - } - public bool CanCopyMessageXml() - { - return SelectedMessage != null; - } - - public void CopyMessageXml() - { - var content = GetMessageBody(); - if (!content.IsEmpty()) + if (SelectedMessage == null || SelectedMessage.Body == null) { - clipboard.CopyTo(content); + return; } + + messageView.Display(SelectedMessage.Body); } public void Handle(SelectedMessageChanged @event) @@ -72,20 +49,5 @@ public void Handle(SelectedMessageChanged @event) SelectedMessage = @event.Message; } } - - void ShowMessageBody() - { - if (SelectedMessage == null) return; - messageView.Display(GetMessageBody()); - } - - string GetMessageBody() - { - if (SelectedMessage == null || SelectedMessage.Body == null) return string.Empty; - - var bytes = Encoding.Default.GetBytes(SelectedMessage.Body); - var xml = xmlDecoder.Decode(bytes); - return xml.IsParsed ? xml.Value.GetFormatted() : string.Empty; - } } } \ No newline at end of file