2017-03-02 14:37:32 +00:00
|
|
|
using Gee;
|
|
|
|
|
2018-01-12 20:03:09 +00:00
|
|
|
namespace Xmpp {
|
2017-05-13 15:43:51 +00:00
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
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;
|
|
|
|
|
2017-05-13 15:43:51 +00:00
|
|
|
private NamespaceState parent;
|
|
|
|
|
|
|
|
public NamespaceState() {
|
2017-03-02 14:37:32 +00:00
|
|
|
add_assoc(XMLNS_URI, "xmlns");
|
2017-05-13 15:43:51 +00:00
|
|
|
add_assoc(XML_URI, "xml");
|
|
|
|
current_ns_uri = XML_URI;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 15:43:51 +00:00
|
|
|
public NamespaceState.for_stanza() {
|
2017-03-02 14:37:32 +00:00
|
|
|
this();
|
|
|
|
add_assoc("http://etherx.jabber.org/streams", "stream");
|
|
|
|
current_ns_uri = "jabber:client";
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:43:51 +00:00
|
|
|
private NamespaceState.copy(NamespaceState old) {
|
2017-03-02 14:37:32 +00:00
|
|
|
foreach (string key in old.uri_to_name.keys) {
|
|
|
|
add_assoc(key, old.uri_to_name[key]);
|
|
|
|
}
|
|
|
|
set_current(old.current_ns_uri);
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:43:51 +00:00
|
|
|
private NamespaceState.with_parent(NamespaceState parent) {
|
|
|
|
this.copy(parent);
|
|
|
|
this.parent = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public NamespaceState.with_assoc(NamespaceState old, string ns_uri, string name) {
|
2017-03-02 14:37:32 +00:00
|
|
|
this.copy(old);
|
|
|
|
add_assoc(ns_uri, name);
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:43:51 +00:00
|
|
|
public NamespaceState.with_current(NamespaceState old, string current_ns_uri) {
|
2017-03-02 14:37:32 +00:00
|
|
|
this.copy(old);
|
|
|
|
set_current(current_ns_uri);
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:43:51 +00:00
|
|
|
public void add_assoc(string ns_uri, string name) {
|
2017-03-02 14:37:32 +00:00
|
|
|
name_to_uri[name] = ns_uri;
|
|
|
|
uri_to_name[ns_uri] = name;
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:43:51 +00:00
|
|
|
public void set_current(string current_ns_uri) {
|
2017-03-02 14:37:32 +00:00
|
|
|
this.current_ns_uri = current_ns_uri;
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:43:51 +00:00
|
|
|
public string find_name(string ns_uri) throws XmlError {
|
2017-03-02 14:37:32 +00:00
|
|
|
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.");
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:43:51 +00:00
|
|
|
public string find_uri(string name) throws XmlError {
|
2017-03-02 14:37:32 +00:00
|
|
|
if (name_to_uri.has_key(name)) {
|
|
|
|
return name_to_uri[name];
|
|
|
|
}
|
|
|
|
throw new XmlError.NS_DICT_ERROR(@"NS name $name not found.");
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:43:51 +00:00
|
|
|
public NamespaceState push() {
|
|
|
|
return new NamespaceState.with_parent(this);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 15:43:51 +00:00
|
|
|
public NamespaceState pop() {
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string to_string() {
|
2017-03-02 14:37:32 +00:00
|
|
|
StringBuilder sb = new StringBuilder ();
|
|
|
|
sb.append ("NamespaceState{");
|
|
|
|
foreach (string key in uri_to_name.keys) {
|
|
|
|
sb.append(key);
|
|
|
|
sb.append_c('=');
|
|
|
|
sb.append(uri_to_name[key]);
|
|
|
|
sb.append_c(',');
|
|
|
|
}
|
|
|
|
sb.append("current=");
|
|
|
|
sb.append(current_ns_uri);
|
|
|
|
sb.append_c('}');
|
|
|
|
return sb.str;
|
|
|
|
}
|
|
|
|
}
|
2017-05-13 15:43:51 +00:00
|
|
|
|
|
|
|
}
|