Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Clean-Up Serialization #308

Merged
merged 3 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/************************************************************************
/************************************************************************
AvalonDock

Copyright (C) 2007-2013 Xceed Software Inc.
Expand Down Expand Up @@ -155,13 +155,13 @@ public override void ReadXml(XmlReader reader)

XmlSerializer serializer;
if (reader.LocalName.Equals(nameof(LayoutAnchorablePaneGroup)))
serializer = new XmlSerializer(typeof(LayoutAnchorablePaneGroup));
serializer = XmlSerializer.FromTypes(new[] { typeof(LayoutAnchorablePaneGroup) })[0];
else
{
var type = LayoutRoot.FindType(reader.LocalName);
if (type == null)
throw new ArgumentException("AvalonDock.LayoutAnchorableFloatingWindow doesn't know how to deserialize " + reader.LocalName);
serializer = new XmlSerializer(type);
serializer = XmlSerializer.FromTypes(new[] { type })[0];
}
RootPanel = (LayoutAnchorablePaneGroup)serializer.Deserialize(reader);
}
Expand Down
4 changes: 2 additions & 2 deletions source/Components/AvalonDock/Layout/LayoutFloatingWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/************************************************************************
/************************************************************************
AvalonDock

Copyright (C) 2007-2013 Xceed Software Inc.
Expand Down Expand Up @@ -61,7 +61,7 @@ public virtual void WriteXml(XmlWriter writer)
foreach (var child in Children)
{
var type = child.GetType();
var serializer = new XmlSerializer(type);
var serializer = XmlSerializer.FromTypes(new[] { type })[0];
serializer.Serialize(writer, child);
}
}
Expand Down
37 changes: 11 additions & 26 deletions source/Components/AvalonDock/Layout/LayoutGroup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/************************************************************************
/************************************************************************
AvalonDock

Copyright (C) 2007-2013 Xceed Software Inc.
Expand Down Expand Up @@ -150,31 +150,16 @@ public virtual void ReadXml(System.Xml.XmlReader reader)
continue;
}

XmlSerializer serializer = null;
if (reader.LocalName == nameof(LayoutAnchorablePaneGroup))
serializer = new XmlSerializer(typeof(LayoutAnchorablePaneGroup));
else if (reader.LocalName == nameof(LayoutAnchorablePane))
serializer = new XmlSerializer(typeof(LayoutAnchorablePane));
else if (reader.LocalName == nameof(LayoutAnchorable))
serializer = new XmlSerializer(typeof(LayoutAnchorable));
else if (reader.LocalName == nameof(LayoutDocumentPaneGroup))
serializer = new XmlSerializer(typeof(LayoutDocumentPaneGroup));
else if (reader.LocalName == nameof(LayoutDocumentPane))
serializer = new XmlSerializer(typeof(LayoutDocumentPane));
else if (reader.LocalName == nameof(LayoutDocument))
serializer = new XmlSerializer(typeof(LayoutDocument));
else if (reader.LocalName == nameof(LayoutAnchorGroup))
serializer = new XmlSerializer(typeof(LayoutAnchorGroup));
else if (reader.LocalName == nameof(LayoutPanel))
serializer = new XmlSerializer(typeof(LayoutPanel));
else
{
var type = FindType(reader.LocalName);
if (type == null)
throw new ArgumentException("AvalonDock.LayoutGroup doesn't know how to deserialize " + reader.LocalName);
serializer = new XmlSerializer(type);
}
string fullName = String.Format("{0}.{1}", GetType().Namespace, reader.LocalName);
Type typeForSerializer = Type.GetType(fullName);

if (typeForSerializer == null)
typeForSerializer = FindType(reader.LocalName);

if (typeForSerializer == null)
throw new ArgumentException("AvalonDock.LayoutGroup doesn't know how to deserialize " + reader.LocalName);

XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeForSerializer })[0];
Children.Add((T)serializer.Deserialize(reader));
}

Expand All @@ -188,7 +173,7 @@ public virtual void WriteXml(System.Xml.XmlWriter writer)
foreach (var child in Children)
{
var type = child.GetType();
var serializer = new XmlSerializer(type);
var serializer = XmlSerializer.FromTypes(new[] { type })[0];
serializer.Serialize(writer, child);
}
}
Expand Down
31 changes: 15 additions & 16 deletions source/Components/AvalonDock/Layout/LayoutRoot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/************************************************************************
/************************************************************************
AvalonDock

Copyright (C) 2007-2013 Xceed Software Inc.
Expand Down Expand Up @@ -885,47 +885,47 @@ private object ReadElement(XmlReader reader)
while (reader.NodeType == XmlNodeType.Whitespace) reader.Read();
if (reader.NodeType == XmlNodeType.EndElement) return null;

XmlSerializer serializer;
Type typeToSerialize;
switch (reader.LocalName)
{
case nameof(LayoutAnchorablePaneGroup):
serializer = new XmlSerializer(typeof(LayoutAnchorablePaneGroup));
typeToSerialize = typeof(LayoutAnchorablePaneGroup);
break;

case nameof(LayoutAnchorablePane):
serializer = new XmlSerializer(typeof(LayoutAnchorablePane));
typeToSerialize = typeof(LayoutAnchorablePane);
break;

case nameof(LayoutAnchorable):
serializer = new XmlSerializer(typeof(LayoutAnchorable));
typeToSerialize = typeof(LayoutAnchorable);
break;

case nameof(LayoutDocumentPaneGroup):
serializer = new XmlSerializer(typeof(LayoutDocumentPaneGroup));
typeToSerialize = typeof(LayoutDocumentPaneGroup);
break;

case nameof(LayoutDocumentPane):
serializer = new XmlSerializer(typeof(LayoutDocumentPane));
typeToSerialize = typeof(LayoutDocumentPane);
break;

case nameof(LayoutDocument):
serializer = new XmlSerializer(typeof(LayoutDocument));
typeToSerialize = typeof(LayoutDocument);
break;

case nameof(LayoutAnchorGroup):
serializer = new XmlSerializer(typeof(LayoutAnchorGroup));
typeToSerialize = typeof(LayoutAnchorGroup);
break;

case nameof(LayoutPanel):
serializer = new XmlSerializer(typeof(LayoutPanel));
typeToSerialize = typeof(LayoutPanel);
break;

case nameof(LayoutDocumentFloatingWindow):
serializer = new XmlSerializer(typeof(LayoutDocumentFloatingWindow));
typeToSerialize = typeof(LayoutDocumentFloatingWindow);
break;

case nameof(LayoutAnchorableFloatingWindow):
serializer = new XmlSerializer(typeof(LayoutAnchorableFloatingWindow));
typeToSerialize = typeof(LayoutAnchorableFloatingWindow);
break;

case nameof(LeftSide):
Expand All @@ -940,13 +940,12 @@ private object ReadElement(XmlReader reader)
return reader.Read();

default:
var type = FindType(reader.LocalName);
if (type == null)
typeToSerialize = FindType(reader.LocalName);
if (typeToSerialize == null)
throw new ArgumentException("AvalonDock.LayoutRoot doesn't know how to deserialize " + reader.LocalName);
serializer = new XmlSerializer(type);
break;
}

XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeToSerialize })[0];
return serializer.Deserialize(reader);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/************************************************************************
/************************************************************************
AvalonDock

Copyright (C) 2007-2013 Xceed Software Inc.
Expand Down Expand Up @@ -76,6 +76,9 @@ protected virtual void FixupLayout(LayoutRoot layout)
if (lcToFix.ContentId != null)
previousAchorable = _previousAnchorables.FirstOrDefault(a => a.ContentId == lcToFix.ContentId);

if (previousAchorable != null && previousAchorable.Title != null)
lcToFix.Title = previousAchorable.Title;

if (LayoutSerializationCallback != null)
{
// Ask client application via callback if item should be deserialized
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/************************************************************************
/************************************************************************
AvalonDock

Copyright (C) 2007-2013 Xceed Software Inc.
Expand Down Expand Up @@ -29,30 +29,53 @@ public XmlLayoutSerializer(DockingManager manager)

#endregion Constructors

#region Private Methods
/// <returns>Desererialized LayoutRoot</returns>
/// <summary> Function for LayoutRoot deserialization. </summary>

private delegate LayoutRoot DeserializeFunction();

/// <summary> Deserializes layout with the given function <see cref="DeserializeFunction"/>.</summary>
/// <param name="function"></param>
private void DeserializeCommon(DeserializeFunction function)
{
try
{
StartDeserialization();
var layout = function();
FixupLayout(layout);
Manager.Layout = layout;
}
finally
{
EndDeserialization();
}
}
#endregion

#region Public Methods

readonly XmlSerializer _serializer = XmlSerializer.FromTypes(new[] { typeof(LayoutRoot) })[0];

/// <summary>Serialize the layout into a <see cref="XmlWriter"/>.</summary>
/// <param name="writer"></param>
public void Serialize(XmlWriter writer)
{
var serializer = new XmlSerializer(typeof(LayoutRoot));
serializer.Serialize(writer, Manager.Layout);
_serializer.Serialize(writer, Manager.Layout);
}

/// <summary>Serialize the layout into a <see cref="TextWriter"/>.</summary>
/// <param name="writer"></param>
public void Serialize(TextWriter writer)
{
var serializer = new XmlSerializer(typeof(LayoutRoot));
serializer.Serialize(writer, Manager.Layout);
_serializer.Serialize(writer, Manager.Layout);
}

/// <summary>Serialize the layout into a <see cref="Stream"/>.</summary>
/// <param name="stream"></param>
public void Serialize(Stream stream)
{
var serializer = new XmlSerializer(typeof(LayoutRoot));
serializer.Serialize(stream, Manager.Layout);
_serializer.Serialize(stream, Manager.Layout);
}

/// <summary>Serialize the layout into a file using a <see cref="StreamWriter"/>.</summary>
Expand All @@ -67,54 +90,26 @@ public void Serialize(string filepath)
/// <param name="stream"></param>
public void Deserialize(System.IO.Stream stream)
{
try
{
StartDeserialization();
var serializer = new XmlSerializer(typeof(LayoutRoot));
var layout = serializer.Deserialize(stream) as LayoutRoot;
FixupLayout(layout);
Manager.Layout = layout;
}
finally
{
EndDeserialization();
}
LayoutRoot function() => _serializer.Deserialize(stream) as LayoutRoot;
DeserializeCommon(function);
}

/// <summary>Deserialize the layout a file from a <see cref="TextReader"/>.</summary>
/// <param name="reader"></param>
public void Deserialize(TextReader reader)
{
try
{
StartDeserialization();
var serializer = new XmlSerializer(typeof(LayoutRoot));
var layout = serializer.Deserialize(reader) as LayoutRoot;
FixupLayout(layout);
Manager.Layout = layout;
}
finally
{
EndDeserialization();
}

LayoutRoot function() => _serializer.Deserialize(reader) as LayoutRoot;
DeserializeCommon(function);
}

/// <summary>Deserialize the layout a file from a <see cref="XmlReader"/>.</summary>
/// <param name="reader"></param>
public void Deserialize(XmlReader reader)
{
try
{
StartDeserialization();
var serializer = new XmlSerializer(typeof(LayoutRoot));
var layout = serializer.Deserialize(reader) as LayoutRoot;
FixupLayout(layout);
Manager.Layout = layout;
}
finally
{
EndDeserialization();
}

LayoutRoot function() => _serializer.Deserialize(reader) as LayoutRoot;
DeserializeCommon(function);
}

/// <summary>Deserialize the layout from a file using a <see cref="StreamReader"/>.</summary>
Expand Down