Skip to content

Commit

Permalink
Bump version.
Browse files Browse the repository at this point in the history
- Fixed wrong sub classing around some elements.

- Added full control about XML formatting.

- Fixed overload around SetAttributeValue. net6.0 use TryParseHelper. .ne7+ use IParsable interface instead.

- All tests passing
  • Loading branch information
nathan130200 committed Apr 28, 2024
1 parent cbd95b9 commit 1804b92
Show file tree
Hide file tree
Showing 26 changed files with 344 additions and 153 deletions.
2 changes: 1 addition & 1 deletion XmppSharp.Test/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal static async Task<Element> ParseFromBuffer([StringSyntax("Xml")] string

internal static void PrintResult(Element e)
{
Debug.WriteLine("\nRESULT:\n" + e.ToString(true) + "\n");
Debug.WriteLine("\nRESULT:\n" + e.ToString(XmlFormatting.Indented) + "\n");
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion XmppSharp.Test/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public async Task CloneNodes()
var cloned = elem.Clone();
Assert.AreNotSame(elem, cloned);

var outXml = cloned.ToString(false);
var outXml = cloned.ToString(XmlFormatting.Default);

Assert.AreEqual(inXml, outXml);
}
Expand Down
7 changes: 5 additions & 2 deletions XmppSharp/Dom/Cdata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class Cdata : Node
public override Node Clone()
=> new Cdata(this);

public override void WriteTo(XmlWriter writer)
=> writer.WriteCData(this.Value);
public override void WriteTo(XmlWriter writer, in XmlFormatting formatting)
{
if (formatting.IncludeCdataNodes)
writer.WriteCData(this.Value);
}
}
7 changes: 5 additions & 2 deletions XmppSharp/Dom/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class Comment : Node
public override Node Clone()
=> new Comment(this);

public override void WriteTo(XmlWriter writer)
=> writer.WriteComment(this.Value);
public override void WriteTo(XmlWriter writer, in XmlFormatting formatting)
{
if (formatting.IncludeCommentNodes)
writer.WriteComment(this.Value);
}
}
154 changes: 107 additions & 47 deletions XmppSharp/Dom/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public Element(string qualifiedName, string namespaceURI) : this(qualifiedName)
}
}

public bool IsRootElement
=> Parent is null;

public IEnumerable<Node> Nodes()
{
lock (this._childNodes)
Expand Down Expand Up @@ -101,18 +104,40 @@ public override string Value
}
}

/// <summary>
/// Gets the XML string representation of the current element and its child nodes.
/// </summary>
/// <returns>Well-formed XML serialized with the entire XML tree.</returns>
public override string ToString()
=> this.ToString(false);
=> this.ToString(XmlFormatting.Default);

public string ToString(bool indented, char indentChar = ' ', int indentSize = 2)
/// <summary>
/// Gets the XML string representation of the current element and its child nodes.
/// </summary>
/// <param name="formatting">Determines which formatting will be used.</param>
/// <returns>Well-formed XML serialized with the entire XML tree.</returns>
public string ToString(XmlFormatting formatting)
{
using (StringBuilderPool.Rent(out var sb))
StringBuilderPool.Rent(out var sb);

try
{
using (var writer = Xml.CreateWriter(indented, sb, indentChar, indentSize))
using (var writer = Xml.CreateWriter(sb, formatting))

/* Unmerged change from project 'XmppSharp (net8.0)'
Before:
this.WriteTo(writer);
After:
this.WriteTo(writer, formatting);
*/
this.WriteTo(writer, formatting);

return sb.ToString();
}
finally
{
StringBuilderPool.Return(sb);
}
}

public string? DefaultNamespace
Expand Down Expand Up @@ -146,7 +171,7 @@ public override Element Clone()
return elem;
}

public override void WriteTo(XmlWriter writer)
public override void WriteTo(XmlWriter writer, in XmlFormatting formatting)
{
var ns = this.GetNamespace(this._prefix);

Expand All @@ -172,19 +197,19 @@ public override void WriteTo(XmlWriter writer)
if (!info.HasPrefix)
writer.WriteAttributeString(name, value);
else
writer.WriteAttributeString(info.Prefix, info.LocalName, info.Prefix switch
writer.WriteAttributeString(info.LocalName, info.Prefix switch
{
"xml" => Namespace.Xml,
"xmlns" => Namespace.Xmlns,
_ => this.GetNamespace(info.Prefix) ?? string.Empty
});
}, value);
}
}

lock (this._childNodes)
{
foreach (var node in this._childNodes)
node.WriteTo(writer);
node.WriteTo(writer, formatting);
}

writer.WriteEndElement();
Expand Down Expand Up @@ -281,18 +306,20 @@ public virtual void RemoveChild(Node n)
return;

lock (this._childNodes)
this._childNodes.Remove(n);
{
n._parent = null;

n._parent = null;
this._childNodes.Remove(n);

if (n is Element elem)
{
var prefix = elem.Prefix;
if (n is Element elem)
{
var prefix = elem.Prefix;

if (prefix != null)
elem.SetNamespace(prefix, this.GetNamespace(prefix));
else
elem.SetNamespace(this.GetNamespace());
if (prefix != null)
elem.SetNamespace(prefix, this.GetNamespace(prefix));
else
elem.SetNamespace(this.GetNamespace());
}
}
}

Expand Down Expand Up @@ -332,6 +359,16 @@ public void SetNamespace(string prefix, string uri)
return value;
}

public IReadOnlyDictionary<string, string> Attributes()
{
KeyValuePair<string, string>[] result;

lock (_attributes)
result = _attributes.ToArray();

return result.ToDictionary(x => x.Key, x => x.Value);
}

public Element SetAttribute(string name, object? value)
{
Require.NotNullOrWhiteSpace(name);
Expand All @@ -347,6 +384,29 @@ public Element SetAttribute(string name, object? value)
return this;
}

public void RemoveAllChildNodes()
{
lock (_childNodes)
{
foreach (var item in _childNodes)
item._parent = null;

_childNodes.Clear();
}
}

public void RemoveAllAttributes()
{
lock (_attributes)
_attributes.Clear();
}

public void Clear()
{
RemoveAllChildNodes();
RemoveAllAttributes();
}

public Element RemoveAttribute(string name)
{
Require.NotNullOrWhiteSpace(name);
Expand All @@ -367,49 +427,45 @@ public bool HasAttribute(string name)

public IEnumerable<Element> Children()
{
var result = new List<Element>();

lock (this._childNodes)
{
foreach (var node in this._childNodes)
foreach (var node in _childNodes)
{
if (node is Element e)
yield return e;
result.Add(e);
}
}

return result.AsEnumerable();
}

public IEnumerable<T> Children<T>()
=> this.Children().OfType<T>();

public IEnumerable<Element> Children(string? tagName, string namespaceURI)
public IEnumerable<Element> Children(string? tagName, string? namespaceURI = default)
{
Require.NotNull(tagName);

return this.Children(x => x.TagName == tagName && x.Prefix == null
return this.Children(x => x.TagName == tagName && namespaceURI == null || (x.Prefix == null
? x.GetNamespace() == namespaceURI
: x.GetNamespace(x.Prefix) == namespaceURI);
: x.GetNamespace(x.Prefix) == namespaceURI));
}

public IEnumerable<Element> Children(Func<Element, bool> predicate)
{
Require.NotNull(predicate);

lock (this._childNodes)
{
foreach (var node in this._childNodes)
{
if (node is Element child && predicate(child))
yield return child;
}
}
return Children().Where(predicate);
}

public Element Child(string tagName, string? namespaceURI)
public Element Child(string tagName, string? namespaceURI = default)
{
Require.NotNull(tagName);

return this.Children(x => x.TagName == tagName && x.Prefix == null
return this.Children(x => x.TagName == tagName && namespaceURI == null || (x.Prefix == null
? x.GetNamespace() == namespaceURI
: x.GetNamespace(x.Prefix) == namespaceURI)
: x.GetNamespace(x.Prefix) == namespaceURI))
.FirstOrDefault();
}

Expand Down Expand Up @@ -441,19 +497,7 @@ public void SetTag(string tagName)
this.AddChild(new Element(tagName));
}

public void SetTag(string tagName, object? value)
{
Require.NotNullOrWhiteSpace(tagName);

var elem = new Element(tagName);

if (value != null)
elem.Value = Convert.ToString(value, CultureInfo.InvariantCulture);

this.AddChild(elem);
}

public void SetTag(string tagName, string? namespaceURI, object? value)
public void SetTag(string tagName, string? namespaceURI = default, object? value = default)
{
Require.NotNullOrWhiteSpace(tagName);

Expand All @@ -476,4 +520,20 @@ public bool HasTag(string tagName, string? namespaceURI = default)
Require.NotNullOrWhiteSpace(tagName);
return this.Child(tagName, namespaceURI) is not null;
}

public void ReplaceWith(Element other)
{
Require.NotNull(other);

var parent = this.Parent;
this.Remove();
parent?.AddChild(other);
}

public void ReplaceFrom(ref Element other)
{
Require.NotNull(other);
other.Remove();
other = this;
}
}
2 changes: 1 addition & 1 deletion XmppSharp/Dom/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public virtual string Value
set;
}

public abstract void WriteTo(XmlWriter writer);
public abstract void WriteTo(XmlWriter writer, in XmlFormatting formatting);
public abstract Node Clone();

object ICloneable.Clone() => this.Clone();
Expand Down
7 changes: 5 additions & 2 deletions XmppSharp/Dom/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class Text : Node
public override Node Clone()
=> new Text(this);

public override void WriteTo(XmlWriter writer)
=> writer.WriteString(this.Value);
public override void WriteTo(XmlWriter writer, in XmlFormatting formatting)
{
if (formatting.IncludeTextNodes)
writer.WriteString(this.Value);
}
}
8 changes: 7 additions & 1 deletion XmppSharp/Jid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ public static bool TryParse(string input, out Jid result)
/// </summary>
public override string ToString()
{
using (StringBuilderPool.Rent(out var sb))
StringBuilderPool.Rent(out var sb);

try
{
if (this._local != null)
sb.Append(this._local).Append('@');
Expand All @@ -212,6 +214,10 @@ public override string ToString()

return sb.ToString();
}
finally
{
StringBuilderPool.Return(sb);
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion XmppSharp/Protocol/Base/Stanza.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace XmppSharp.Protocol.Base;

public abstract class Stanza : Element
public abstract class Stanza : DirectionalElement
{
protected Stanza(string qualifiedName) : base(qualifiedName)
{
Expand Down
Loading

0 comments on commit 1804b92

Please sign in to comment.