Skip to content

Commit

Permalink
Improve message body xml display
Browse files Browse the repository at this point in the history
  • Loading branch information
John Simons committed May 15, 2015
1 parent a2c18ba commit 424012d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 110 deletions.
46 changes: 1 addition & 45 deletions src/ServiceInsight.Tests/XmlViewerViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System.Xml;
using Caliburn.Micro;
using Desktop.Framework;
using Desktop.MessageViewers.XmlViewer;
using Desktop.Models;
using NSubstitute;
Expand All @@ -16,32 +15,16 @@ public class XmlViewerViewModelTests
{
XmlMessageViewModel ViewModel;
IXmlMessageView View;
IContentDecoder<XmlDocument> XmlDecoder;
IClipboard Clipboard;
const string TestMessage = "<?xml version=\"1.0\"?><Test title=\"test title\"/>";

[SetUp]
public void TestInitialize()
{
XmlDecoder = Substitute.For<IContentDecoder<XmlDocument>>();
Clipboard = Substitute.For<IClipboard>();
View = Substitute.For<IXmlMessageView>();
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<byte[]>()).Returns(new DecoderResult<XmlDocument>(GetDocument(TestMessage)));

((IViewAware)ViewModel).AttachView(View);
ViewModel.SelectedMessage = new MessageBody { Body = TestMessage };

View.Received(1).Display(Arg.Any<string>());
XmlDecoder.Received(1).Decode(Arg.Any<byte[]>());
}

[Test]
public void should_clear_message_body_when_selected_message_is_unselected()
{
Expand All @@ -58,32 +41,5 @@ public void should_not_load_the_message_when_view_is_not_loaded()

View.DidNotReceive().Display(Arg.Any<string>());
}

[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<byte[]>()).Returns(new DecoderResult<XmlDocument>(GetDocument(TestMessage)));

ViewModel.CopyMessageXml();

Clipboard.Received().CopyTo(Arg.Any<string>());
}

static XmlDocument GetDocument(string content)
{
var document = new XmlDocument();
document.LoadXml(content);
return document;
}
}
}
34 changes: 13 additions & 21 deletions src/ServiceInsight/ExtensionMethods/XmlDocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}

Expand Down
48 changes: 5 additions & 43 deletions src/ServiceInsight/MessageViewers/XmlViewer/XmlMessageViewModel.cs
Original file line number Diff line number Diff line change
@@ -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<SelectedMessageChanged>
{
readonly IClipboard clipboard;
IContentDecoder<XmlDocument> xmlDecoder;
IXmlMessageView messageView;

public XmlMessageViewModel(
IContentDecoder<XmlDocument> xmlDecoder,
IClipboard clipboard)
{
this.clipboard = clipboard;
this.xmlDecoder = xmlDecoder;
}

protected override void OnActivate()
{
base.OnActivate();
Expand All @@ -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)
Expand All @@ -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;
}
}
}

0 comments on commit 424012d

Please sign in to comment.