From 6904bda756321c67bea0108342fa9ba859dd66e9 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sat, 13 May 2017 17:43:51 +0200 Subject: xmpp-vala: improve namespace handling, add some tests --- xmpp-vala/src/core/stanza_node.vala | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'xmpp-vala/src/core/stanza_node.vala') diff --git a/xmpp-vala/src/core/stanza_node.vala b/xmpp-vala/src/core/stanza_node.vala index 67b8db35..341e67ba 100644 --- a/xmpp-vala/src/core/stanza_node.vala +++ b/xmpp-vala/src/core/stanza_node.vala @@ -300,6 +300,25 @@ public class StanzaNode : StanzaEntry { return this; } + public bool equals(StanzaNode other) { + if (other.name != name) return false; + if (other.val != val) return false; + if (name == "#text") return true; + if (other.ns_uri != ns_uri) return false; + + if (other.sub_nodes.size != sub_nodes.size) return false; + for (int i = 0; i < sub_nodes.size; i++) { + if (!other.sub_nodes[i].equals(sub_nodes[i])) return false; + } + + if (other.attributes.size != attributes.size) return false; + for (int i = 0; i < attributes.size; i++) { + if (!other.attributes[i].equals(attributes[i])) return false; + } + + return true; + } + private const string TAG_START_BEGIN_FORMAT = "%s<{%s}:%s"; private const string TAG_START_EMPTY_END = " />\n"; private const string TAG_START_CONTENT_END = ">\n"; @@ -358,12 +377,13 @@ public class StanzaNode : StanzaEntry { public string to_xml(NamespaceState? state = null) throws XmlError { NamespaceState my_state = state ?? new NamespaceState.for_stanza(); if (name == "#text") return val == null ? "" : (!)encoded_val; + my_state = my_state.push(); foreach (var xmlns in get_attributes_by_ns_uri (XMLNS_URI)) { if (xmlns.val == null) continue; if (xmlns.name == "xmlns") { - my_state = new NamespaceState.with_current(my_state, (!)xmlns.val); + my_state.set_current((!)xmlns.val); } else { - my_state = new NamespaceState.with_assoc(my_state, (!)xmlns.val, xmlns.name); + my_state.add_assoc((!)xmlns.val, xmlns.name); } } var sb = new StringBuilder(); @@ -391,6 +411,7 @@ public class StanzaNode : StanzaEntry { } } } + my_state = my_state.pop(); return sb.str; } } -- cgit v1.2.3-70-g09d2