Debian Codegraph @

xmpp-vala/src/core/stanza_node.vala

raw
1 using Gee;
2
3 namespace Xmpp {
4
5 public abstract class StanzaEntry {
6 protected const string ANSI_COLOR_END = "\x1b[0m";
7 protected const string ANSI_COLOR_GREEN = "\x1b[32m";
8 protected const string ANSI_COLOR_YELLOW = "\x1b[33m";
9 protected const string ANSI_COLOR_GRAY = "\x1b[37m";
10
11 public string? ns_uri;
12 public string name;
13 public string? val;
14
15 public string? encoded_val {
16 owned get {
17 if (val == null) return null;
18 return ((!)val).replace("&", "&amp;").replace("\"", "&quot;").replace("'", "&apos;").replace("<", "&lt;").replace(">", "&gt;");
19 }
20 set {
21 if (value == null) {
22 val = null;
23 return;
24 }
25 string tmp = ((!)value).replace("&gt;", ">").replace("&lt;", "<").replace("&apos;","'").replace("&quot;","\"");
26 while (tmp.contains("&#")) {
27 int start = tmp.index_of("&#");
28 int end = tmp.index_of(";", start);
29 if (end < start) break;
30 unichar num = -1;
31 if (tmp[start+2]=='x') {
32 tmp.substring(start+3, start-end-3).scanf("%x", &num);
33 } else {
34 num = int.parse(tmp.substring(start+2, start-end-2));
35 }
36 tmp = tmp.splice(start, end, num.to_string());
37 }
38 val = tmp.replace("&amp;", "&");
39 }
40 }
41
42 public virtual unowned string? get_string_content() {
43 return val;
44 }
45
46 public virtual string to_string(int i = 0) {
47 return get_string_content() ?? "(null)";
48 }
49 }
50
51 public class StanzaNode : StanzaEntry {
52 public Gee.List<StanzaNode> sub_nodes = new ArrayList<StanzaNode>();
53 public Gee.List<StanzaAttribute> attributes = new ArrayList<StanzaAttribute>();
54 public bool has_nodes = false;
55 public bool pseudo = false;
56
57 internal StanzaNode() {
58 }
59
60 public StanzaNode.build(string name, string ns_uri = "jabber:client", ArrayList<StanzaNode>? nodes = null, ArrayList<StanzaAttribute>? attrs = null) {
61 this.ns_uri = ns_uri;
62 this.name = name;
63 if (nodes != null) this.sub_nodes.add_all((!)nodes);
64 if (attrs != null) this.attributes.add_all((!)attrs);
65 }
66
67 public StanzaNode.text(string text) {
68 this.name = "#text";
69 this.val = text;
70 }
71
72 public StanzaNode.encoded_text(string text) {
73 this.name = "#text";
74 this.encoded_val = text;
75 }
76
77 public StanzaNode add_self_xmlns() {
78 if (ns_uri == null) return this;
79 return put_attribute("xmlns", (!)ns_uri);
80 }
81
82 public unowned string? get_attribute(string name, string? ns_uri = null) {
83 string _name = name;
84 string? _ns_uri = ns_uri;
85 if (_ns_uri == null) {
86 if (_name.contains(":")) {
87 var lastIndex = _name.last_index_of_char(':');
88 _ns_uri = _name.substring(0, lastIndex);
89 _name = _name.substring(lastIndex + 1);
90 } else {
91 _ns_uri = this.ns_uri;
92 }
93 }
94 foreach (var attr in attributes) {
95 if (attr.ns_uri == (!)_ns_uri && attr.name == _name) return attr.val;
96 }
97 return null;
98 }
99
100 public int get_attribute_int(string name, int def = -1, string? ns_uri = null) {
101 string? attribute_str = get_attribute(name, ns_uri);
102 if (attribute_str == null) return def;
103
104 int res = def;
105 bool parse_success = int.try_parse(attribute_str, out res, null, 10);
106 if (!parse_success) {
107 info("Could not parse int attribute %s: %s", name, attribute_str);
108 return def;
109 }
110 return res;
111 }
112
113 public uint get_attribute_uint(string name, uint def = 0, string? ns_uri = null) {
114 string? attribute_str = get_attribute(name, ns_uri);
115 if (attribute_str == null || attribute_str.strip().has_prefix("-")) return def;
116
117 uint res = def;
118 bool parse_success = uint.try_parse(attribute_str, out res, null, 10);
119 if (!parse_success) {
120 info("Could not parse uint attribute %s: %s", name, attribute_str);
121 return def;
122 }
123 return res;
124 }
125
126 public bool get_attribute_bool(string name, bool def = false, string? ns_uri = null) {
127 string? res = get_attribute(name, ns_uri);
128 if (res == null) return def;
129 return ((!)res).down() == "true" || res == "1";
130 }
131
132 public StanzaAttribute? get_attribute_raw(string name, string? ns_uri = null) {
133 string _name = name;
134 string? _ns_uri = ns_uri;
135 if (_ns_uri == null) {
136 if (_name.contains(":")) {
137 var lastIndex = _name.last_index_of_char(':');
138 _ns_uri = _name.substring(0, lastIndex);
139 _name = _name.substring(lastIndex + 1);
140 } else {
141 _ns_uri = this.ns_uri;
142 }
143 }
144 foreach (var attr in attributes) {
145 if (attr.ns_uri == _ns_uri && attr.name == _name) return attr;
146 }
147 return null;
148 }
149
150 public Gee.List<StanzaAttribute> get_attributes_by_ns_uri(string ns_uri) {
151 ArrayList<StanzaAttribute> ret = new ArrayList<StanzaAttribute> ();
152 foreach (var attr in attributes) {
153 if (attr.ns_uri == ns_uri) ret.add(attr);
154 }
155 return ret;
156 }
157
158 public unowned string? get_deep_attribute(...) {
159 va_list l = va_list();
160 StanzaAttribute? res = get_deep_attribute_(va_list.copy(l));
161 if (res == null) return null;
162 return ((!)res).val;
163 }
164
165 public StanzaAttribute? get_deep_attribute_(va_list l) {
166 StanzaNode node = this;
167 string? attribute_name = l.arg();
168 if (attribute_name == null) return null;
169 while (true) {
170 string? s = l.arg();
171 if (s == null) break;
172 StanzaNode? node_tmp = node.get_subnode((!)attribute_name);
173 if (node_tmp == null) return null;
174 node = (!)node_tmp;
175 attribute_name = s;
176 }
177 return node.get_attribute_raw((!)attribute_name);
178 }
179
180 public StanzaNode? get_subnode(string name, string? ns_uri = null, bool recurse = false) {
181 string _name = name;
182 string? _ns_uri = ns_uri;
183 if (ns_uri == null) {
184 if (_name.contains(":")) {
185 var lastIndex = _name.last_index_of_char(':');
186 _ns_uri = _name.substring(0, lastIndex);
187 _name = _name.substring(lastIndex + 1);
188 } else {
189 _ns_uri = this.ns_uri;
190 }
191 }
192 foreach (var node in sub_nodes) {
193 if (node.ns_uri == _ns_uri && node.name == _name) return node;
194 if (recurse) {
195 var x = node.get_subnode(_name, _ns_uri, recurse);
196 if (x != null) return x;
197 }
198 }
199 return null;
200 }
201
202 public Gee.List<StanzaNode> get_subnodes(string name, string? ns_uri = null, bool recurse = false) {
203 ArrayList<StanzaNode> ret = new ArrayList<StanzaNode>();
204 string _name = name;
205 string? _ns_uri = ns_uri;
206 if (ns_uri == null) {
207 if (_name.contains(":")) {
208 var lastIndex = _name.last_index_of_char(':');
209 _ns_uri = _name.substring(0, lastIndex);
210 _name = _name.substring(lastIndex + 1);
211 } else {
212 _ns_uri = this.ns_uri;
213 }
214 }
215 foreach (var node in sub_nodes) {
216 if (node.ns_uri == _ns_uri && node.name == _name) ret.add(node);
217 if (recurse) {
218 ret.add_all(node.get_subnodes(_name, _ns_uri, recurse));
219 }
220 }
221 return ret;
222 }
223
224 public StanzaNode? get_deep_subnode(...) {
225 va_list l = va_list();
226 return get_deep_subnode_(va_list.copy(l));
227 }
228
229 public StanzaNode? get_deep_subnode_(va_list l) {
230 StanzaNode node = this;
231 while (true) {
232 string? s = l.arg();
233 if (s == null) break;
234 StanzaNode? node_tmp = node.get_subnode((!)s);
235 if (node_tmp == null) return null;
236 node = (!)node_tmp;
237 }
238 return node;
239 }
240
241 public Gee.List<StanzaNode> get_deep_subnodes(...) {
242 va_list l = va_list();
243 return get_deep_subnodes_(va_list.copy(l));
244 }
245
246 public Gee.List<StanzaNode> get_deep_subnodes_(va_list l) {
247 StanzaNode node = this;
248 string? subnode_name = l.arg();
249 if (subnode_name == null) return new ArrayList<StanzaNode>();
250 while (true) {
251 string? s = l.arg();
252 if (s == null) break;
253 StanzaNode? node_tmp = node.get_subnode((!)subnode_name);
254 if (node_tmp == null) return new ArrayList<StanzaNode>();
255 node = (!)node_tmp;
256 subnode_name = s;
257 }
258 return node.get_subnodes((!)subnode_name);
259 }
260
261 public Gee.List<StanzaNode> get_all_subnodes() {
262 return sub_nodes;
263 }
264
265 public Gee.List<StanzaNode> get_deep_all_subnodes(...) {
266 va_list l = va_list();
267 StanzaNode? node = get_deep_subnode_(va_list.copy(l));
268 if (node != null) return ((!)node).get_all_subnodes();
269 return new ArrayList<StanzaNode>();
270 }
271
272 public void add_attribute(StanzaAttribute attr) {
273 attributes.add(attr);
274 }
275
276 public override unowned string? get_string_content() {
277 if (val != null) return val;
278 if (sub_nodes.size == 1) return sub_nodes[0].get_string_content();
279 return null;
280 }
281
282 public unowned string? get_deep_string_content(...) {
283 va_list l = va_list();
284 StanzaNode? node = get_deep_subnode_(va_list.copy(l));
285 if (node != null) return ((!)node).get_string_content();
286 return null;
287 }
288
289 public StanzaNode put_attribute(string name, string val, string? ns_uri = null) {
290 string? _ns_uri = ns_uri;
291 if (name == "xmlns") _ns_uri = XMLNS_URI;
292 if (_ns_uri == null) _ns_uri = this.ns_uri;
293 if (_ns_uri == null) return this;
294 attributes.add(new StanzaAttribute.build((!)_ns_uri, name, val));
295 return this;
296 }
297
298 /**
299 * Set only occurrence
300 **/
301 public void set_attribute(string name, string val, string? ns_uri = null) {
302 if (ns_uri == null) ns_uri = this.ns_uri;
303 foreach (var attr in attributes) {
304 if (attr.ns_uri == ns_uri && attr.name == name) {
305 attr.val = val;
306 return;
307 }
308 }
309 put_attribute(name, val, ns_uri);
310 }
311
312 public StanzaNode put_node(StanzaNode node) {
313 sub_nodes.add(node);
314 return this;
315 }
316
317 public bool equals(StanzaNode other) {
318 if (other.name != name) return false;
319 if (other.val != val) return false;
320 if (name == "#text") return true;
321 if (other.ns_uri != ns_uri) return false;
322
323 if (other.sub_nodes.size != sub_nodes.size) return false;
324 for (int i = 0; i < sub_nodes.size; i++) {
325 if (!other.sub_nodes[i].equals(sub_nodes[i])) return false;
326 }
327
328 if (other.attributes.size != attributes.size) return false;
329 for (int i = 0; i < attributes.size; i++) {
330 if (!other.attributes[i].equals(attributes[i])) return false;
331 }
332
333 return true;
334 }
335
336 private const string TAG_START_BEGIN_FORMAT = "%s<{%s}:%s";
337 private const string TAG_START_EMPTY_END = " />\n";
338 private const string TAG_START_CONTENT_END = ">\n";
339 private const string TAG_END_FORMAT = "%s</{%s}:%s>\n";
340 private const string TAG_ANSI_START_BEGIN_FORMAT = "%s"+ANSI_COLOR_YELLOW+"<"+ANSI_COLOR_GRAY+"{%s}:"+ANSI_COLOR_YELLOW+"%s"+ANSI_COLOR_END;
341 private const string TAG_ANSI_START_BEGIN_NO_NS_FORMAT = "%s"+ANSI_COLOR_YELLOW+"<%s"+ANSI_COLOR_END;
342 private const string TAG_ANSI_START_EMPTY_END = ANSI_COLOR_YELLOW+" />"+ANSI_COLOR_END+"\n";
343 private const string TAG_ANSI_START_CONTENT_END = ANSI_COLOR_YELLOW+">"+ANSI_COLOR_END+"\n";
344 private const string TAG_ANSI_END_FORMAT = "%s"+ANSI_COLOR_YELLOW+"</"+ANSI_COLOR_GRAY+"{%s}:"+ANSI_COLOR_YELLOW+"%s>"+ANSI_COLOR_END+"\n";
345 private const string TAG_ANSI_END_NO_NS_FORMAT = "%s"+ANSI_COLOR_YELLOW+"</%s>"+ANSI_COLOR_END+"\n";
346
347 internal string printf(int i, string fmt_start_begin, string start_empty_end, string start_content_end, string fmt_end, string fmt_attr, bool no_ns = false) {
348 string indent = string.nfill (i * 2, ' ');
349 if (name == "#text") {
350 if (((!)val).length > 1000) {
351 return indent + "[... retracted for brevity ...]\n";
352 }
353 return indent + ((!)val).replace("\n", "\n" + indent) + "\n";
354 }
355 var sb = new StringBuilder();
356 if (no_ns) {
357 sb.append_printf(fmt_start_begin, indent, name);
358 } else {
359 sb.append_printf(fmt_start_begin, indent, (!)ns_uri, name);
360 }
361 foreach (StanzaAttribute attr in attributes) {
362 sb.append_printf(" %s", attr.printf(fmt_attr, no_ns));
363 }
364 if (!has_nodes && sub_nodes.size == 0) {
365 sb.append(start_empty_end);
366 } else {
367 sb.append(start_content_end);
368 if (sub_nodes.size != 0) {
369 foreach (StanzaNode subnode in sub_nodes) {
370 sb.append(subnode.printf(i+1, fmt_start_begin, start_empty_end, start_content_end, fmt_end, fmt_attr, no_ns));
371 }
372 if (no_ns) {
373 sb.append_printf(fmt_end, indent, name);
374 } else {
375 sb.append_printf(fmt_end, indent, (!)ns_uri, name);
376 }
377 }
378 }
379 return sb.str;
380 }
381
382 public override string to_string(int i = 0) {
383 return printf(i, TAG_START_BEGIN_FORMAT, TAG_START_EMPTY_END, TAG_START_CONTENT_END, TAG_END_FORMAT, StanzaAttribute.ATTRIBUTE_STRING_FORMAT);
384 }
385
386 public string to_ansi_string(bool hide_ns = false, int i = 0) {
387 if (hide_ns) {
388 return printf(i, TAG_ANSI_START_BEGIN_NO_NS_FORMAT, TAG_ANSI_START_EMPTY_END, TAG_ANSI_START_CONTENT_END, TAG_ANSI_END_NO_NS_FORMAT, StanzaAttribute.ATTRIBUTE_STRING_ANSI_NO_NS_FORMAT, true);
389 } else {
390 return printf(i, TAG_ANSI_START_BEGIN_FORMAT, TAG_ANSI_START_EMPTY_END, TAG_ANSI_START_CONTENT_END, TAG_ANSI_END_FORMAT, StanzaAttribute.ATTRIBUTE_STRING_ANSI_FORMAT);
391 }
392 }
393
394 public string to_xml(NamespaceState? state = null) throws IOError {
395 NamespaceState my_state = state ?? new NamespaceState.for_stanza();
396 if (name == "#text") return val == null ? "" : (!)encoded_val;
397 my_state = my_state.push();
398 foreach (var xmlns in get_attributes_by_ns_uri (XMLNS_URI)) {
399 if (xmlns.val == null) continue;
400 if (xmlns.name == "xmlns") {
401 my_state.set_current((!)xmlns.val);
402 } else {
403 my_state.add_assoc((!)xmlns.val, xmlns.name);
404 }
405 }
406 var sb = new StringBuilder();
407 if (ns_uri == my_state.current_ns_uri) {
408 sb.append_printf("<%s", name);
409 } else {
410 sb.append_printf("<%s:%s", my_state.find_name ((!)ns_uri), name);
411 }
412 var attr_ns_state = new NamespaceState.with_current(my_state, (!)ns_uri);
413 foreach (StanzaAttribute attr in attributes) {
414 sb.append_printf(" %s", attr.to_xml(attr_ns_state));
415 }
416 if (!has_nodes && sub_nodes.size == 0) {
417 sb.append("/>");
418 } else {
419 sb.append(">");
420 if (sub_nodes.size != 0) {
421 foreach (StanzaNode subnode in sub_nodes) {
422 sb.append(subnode.to_xml(my_state));
423 }
424 if (ns_uri == my_state.current_ns_uri) {
425 sb.append(@"</$name>");
426 } else {
427 sb.append_printf("</%s:%s>", my_state.find_name ((!)ns_uri), name);
428 }
429 }
430 }
431 my_state = my_state.pop();
432 return sb.str;
433 }
434 }
435
436 }