aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/core/namespace_state.vala
blob: 306b515e46ae62777ca7df2ce83359cd14a5ad76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using Gee;

namespace Xmpp {

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;

    private NamespaceState parent;

    public NamespaceState() {
        add_assoc(XMLNS_URI, "xmlns");
        add_assoc(XML_URI, "xml");
        current_ns_uri = XML_URI;
    }

    public NamespaceState.for_stanza() {
        this();
        add_assoc("http://etherx.jabber.org/streams", "stream");
        current_ns_uri = "jabber:client";
    }

    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);
    }

    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) {
        this.copy(old);
        set_current(current_ns_uri);
    }

    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) {
        this.current_ns_uri = current_ns_uri;
    }

    public string find_name(string ns_uri) throws IOError {
        if (uri_to_name.has_key(ns_uri)) {
            return uri_to_name[ns_uri];
        }
        throw new IOError.INVALID_DATA(@"XML: NS URI $ns_uri not found.");
    }

    public string find_uri(string name) throws IOError {
        if (name_to_uri.has_key(name)) {
            return name_to_uri[name];
        }
        throw new IOError.INVALID_DATA(@"XML: NS name $name not found.");
    }

    public NamespaceState push() {
        return new NamespaceState.with_parent(this);
    }

    public NamespaceState pop() {
        return parent;
    }

    public string to_string() {
        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;
    }
}

}