aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src
diff options
context:
space:
mode:
authorMarvin W <git@larma.de>2017-05-13 17:43:51 +0200
committerMarvin W <git@larma.de>2017-08-12 11:59:38 +0200
commit6904bda756321c67bea0108342fa9ba859dd66e9 (patch)
tree24517bf54d6188af401afedd343c42b6ece3d1c6 /xmpp-vala/src
parentdd88db7556a20707f6fe3c81b3c58df42a0f5224 (diff)
downloaddino-6904bda756321c67bea0108342fa9ba859dd66e9.tar.gz
dino-6904bda756321c67bea0108342fa9ba859dd66e9.zip
xmpp-vala: improve namespace handling, add some tests
Diffstat (limited to 'xmpp-vala/src')
-rw-r--r--xmpp-vala/src/core/namespace_state.vala43
-rw-r--r--xmpp-vala/src/core/stanza_attribute.vala7
-rw-r--r--xmpp-vala/src/core/stanza_node.vala25
-rw-r--r--xmpp-vala/src/core/stanza_reader.vala27
-rw-r--r--xmpp-vala/src/core/xmpp_stream.vala2
5 files changed, 74 insertions, 30 deletions
diff --git a/xmpp-vala/src/core/namespace_state.vala b/xmpp-vala/src/core/namespace_state.vala
index e71607fa..b55812b4 100644
--- a/xmpp-vala/src/core/namespace_state.vala
+++ b/xmpp-vala/src/core/namespace_state.vala
@@ -1,68 +1,80 @@
using Gee;
namespace Xmpp.Core {
+
public class NamespaceState {
private HashMap<string, string> uri_to_name = new HashMap<string, string> ();
private HashMap<string, string> name_to_uri = new HashMap<string, string> ();
public string current_ns_uri;
- public NamespaceState () {
+ private NamespaceState parent;
+
+ public NamespaceState() {
add_assoc(XMLNS_URI, "xmlns");
- add_assoc("http://www.w3.org/XML/1998/namespace", "xml");
- current_ns_uri = "http://www.w3.org/XML/1998/namespace";
+ add_assoc(XML_URI, "xml");
+ current_ns_uri = XML_URI;
}
- public NamespaceState.for_stanza () {
+ public NamespaceState.for_stanza() {
this();
add_assoc("http://etherx.jabber.org/streams", "stream");
current_ns_uri = "jabber:client";
}
- public NamespaceState.copy (NamespaceState old) {
+ private NamespaceState.copy(NamespaceState old) {
foreach (string key in old.uri_to_name.keys) {
add_assoc(key, old.uri_to_name[key]);
}
set_current(old.current_ns_uri);
}
- public NamespaceState.with_assoc (NamespaceState old, string ns_uri, string name) {
+ private NamespaceState.with_parent(NamespaceState parent) {
+ this.copy(parent);
+ this.parent = parent;
+ }
+
+ public NamespaceState.with_assoc(NamespaceState old, string ns_uri, string name) {
this.copy(old);
add_assoc(ns_uri, name);
}
- public NamespaceState.with_current (NamespaceState old, string current_ns_uri) {
+ public NamespaceState.with_current(NamespaceState old, string current_ns_uri) {
this.copy(old);
set_current(current_ns_uri);
}
- public void add_assoc (string ns_uri, string name) {
+ public void add_assoc(string ns_uri, string name) {
name_to_uri[name] = ns_uri;
uri_to_name[ns_uri] = name;
}
- public void set_current (string current_ns_uri) {
+ public void set_current(string current_ns_uri) {
this.current_ns_uri = current_ns_uri;
}
- public string find_name (string ns_uri) throws XmlError {
+ public string find_name(string ns_uri) throws XmlError {
if (uri_to_name.has_key(ns_uri)) {
return uri_to_name[ns_uri];
}
throw new XmlError.NS_DICT_ERROR(@"NS URI $ns_uri not found.");
}
- public string find_uri (string name) throws XmlError {
+ public string find_uri(string name) throws XmlError {
if (name_to_uri.has_key(name)) {
return name_to_uri[name];
}
throw new XmlError.NS_DICT_ERROR(@"NS name $name not found.");
}
- public NamespaceState clone() {
- return new NamespaceState.copy(this);
+ public NamespaceState push() {
+ return new NamespaceState.with_parent(this);
}
- public string to_string () {
+ public NamespaceState pop() {
+ return parent;
+ }
+
+ public string to_string() {
StringBuilder sb = new StringBuilder ();
sb.append ("NamespaceState{");
foreach (string key in uri_to_name.keys) {
@@ -77,4 +89,5 @@ public class NamespaceState {
return sb.str;
}
}
-} \ No newline at end of file
+
+}
diff --git a/xmpp-vala/src/core/stanza_attribute.vala b/xmpp-vala/src/core/stanza_attribute.vala
index f751c801..ea776110 100644
--- a/xmpp-vala/src/core/stanza_attribute.vala
+++ b/xmpp-vala/src/core/stanza_attribute.vala
@@ -20,6 +20,13 @@ public class StanzaAttribute : StanzaEntry {
this.val = val;
}
+ public bool equals(StanzaAttribute other) {
+ if (other.ns_uri != ns_uri) return false;
+ if (other.name != name) return false;
+ if (other.val != val) return false;
+ return true;
+ }
+
internal string printf(string fmt, bool no_ns = false, string? ns_name = null) {
if (no_ns) {
return fmt.printf(name, (!)val);
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;
}
}
diff --git a/xmpp-vala/src/core/stanza_reader.vala b/xmpp-vala/src/core/stanza_reader.vala
index dd284fa6..f4b900d1 100644
--- a/xmpp-vala/src/core/stanza_reader.vala
+++ b/xmpp-vala/src/core/stanza_reader.vala
@@ -1,7 +1,9 @@
using Gee;
namespace Xmpp.Core {
+
public const string XMLNS_URI = "http://www.w3.org/2000/xmlns/";
+public const string XML_URI = "http://www.w3.org/XML/1998/namespace";
public const string JABBER_URI = "jabber:client";
public errordomain XmlError {
@@ -85,7 +87,7 @@ public class StanzaReader {
private string read_until_ws() throws XmlError {
var res = new StringBuilder();
var what = peek_single();
- while(!is_ws(what)) {
+ while (!is_ws(what)) {
res.append_c(read_single());
what = peek_single();
}
@@ -95,7 +97,7 @@ public class StanzaReader {
private string read_until_char_or_ws(char x, char y = 0) throws XmlError {
var res = new StringBuilder();
var what = peek_single();
- while(what != x && what != y && !is_ws(what)) {
+ while (what != x && what != y && !is_ws(what)) {
res.append_c(read_single());
what = peek_single();
}
@@ -105,11 +107,11 @@ public class StanzaReader {
private string read_until_char(char x) throws XmlError {
var res = new StringBuilder();
var what = peek_single();
- while(what != x) {
+ while (what != x) {
res.append_c(read_single());
what = peek_single();
}
- return res.str;
+ return res.str;
}
private StanzaAttribute read_attribute() throws XmlError {
@@ -169,7 +171,7 @@ public class StanzaReader {
eof = true;
skip_single();
res.name = read_until_char_or_ws('>');
- while(peek_single() != '>') {
+ while (peek_single() != '>') {
skip_single();
}
skip_single();
@@ -184,7 +186,7 @@ public class StanzaReader {
res.attributes.add(read_attribute());
skip_until_non_ws();
}
- if (read_single() == '/' || res.pseudo ) {
+ if (read_single() == '/' || res.pseudo) {
res.has_nodes = false;
skip_single();
} else {
@@ -215,8 +217,8 @@ public class StanzaReader {
}
}
- public StanzaNode read_stanza_node(NamespaceState? baseNs = null) throws XmlError {
- ns_state = baseNs ?? new NamespaceState.for_stanza();
+ public StanzaNode read_stanza_node() throws XmlError {
+ ns_state = ns_state.push();
var res = read_node_start();
if (res.has_nodes) {
bool finishNodeSeen = false;
@@ -238,8 +240,7 @@ public class StanzaReader {
}
finishNodeSeen = true;
} else {
- res.sub_nodes.add(read_stanza_node(ns_state.clone()));
- ns_state = baseNs ?? new NamespaceState.for_stanza();
+ res.sub_nodes.add(read_stanza_node());
}
} else {
res.sub_nodes.add(read_text_node());
@@ -247,16 +248,18 @@ public class StanzaReader {
} while (!finishNodeSeen);
if (res.sub_nodes.size == 0) res.has_nodes = false;
}
+ ns_state = ns_state.pop();
return res;
}
- public StanzaNode read_node(NamespaceState? baseNs = null) throws XmlError {
+ public StanzaNode read_node() throws XmlError {
skip_until_non_ws();
if (peek_single() == '<') {
- return read_stanza_node(baseNs ?? new NamespaceState.for_stanza());
+ return read_stanza_node();
} else {
return read_text_node();
}
}
}
+
}
diff --git a/xmpp-vala/src/core/xmpp_stream.vala b/xmpp-vala/src/core/xmpp_stream.vala
index eb6ffaa5..a8201a22 100644
--- a/xmpp-vala/src/core/xmpp_stream.vala
+++ b/xmpp-vala/src/core/xmpp_stream.vala
@@ -11,7 +11,7 @@ public errordomain IOStreamError {
}
public class XmppStream {
- private static string NS_URI = "http://etherx.jabber.org/streams";
+ public const string NS_URI = "http://etherx.jabber.org/streams";
public string remote_name;
public XmppLog log = new XmppLog();