Menu

[r40]: / trunk / WhatsappClient / WhatsAppProtocol / Helper Classes / ProtocolTreeNode.cs  Maximize  Restore  History

Download this file

123 lines (111 with data), 3.9 kB

  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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class ProtocolTreeNode
{
public string tag;
public IEnumerable<KeyValue> attributeHash;
public IEnumerable<ProtocolTreeNode> children;
public byte[] data;
public ProtocolTreeNode(string tag, IEnumerable<KeyValue> attributeHash, IEnumerable<ProtocolTreeNode> children = null, byte[] data = null)
{
this.tag = tag ?? "";
this.attributeHash = attributeHash ?? new KeyValue[0];
this.children = children ?? new ProtocolTreeNode[0];
this.data = new byte[0];
if (data != null)
this.data = data;
}
public ProtocolTreeNode(string tag, IEnumerable<KeyValue> attributeHash, ProtocolTreeNode children = null)
{
this.tag = tag ?? "";
this.attributeHash = attributeHash ?? new KeyValue[0];
this.children = children != null ? new ProtocolTreeNode[] { children } : new ProtocolTreeNode[0];
this.data = new byte[0];
}
public ProtocolTreeNode(string tag, IEnumerable<KeyValue> attributeHash, byte[] data = null) : this(tag, attributeHash, new ProtocolTreeNode[0], data)
{
}
public ProtocolTreeNode(string tag, IEnumerable<KeyValue> attributeHash) : this(tag, attributeHash, new ProtocolTreeNode[0], null)
{
}
public string NodeString(string indent = "")
{
string ret = "\n" + indent + "<" + this.tag;
if (this.attributeHash != null)
{
foreach (var item in this.attributeHash)
{
ret += string.Format(" {0}=\"{1}\"", item.Key, item.Value);
}
}
ret += ">";
if (this.data.Length > 0)
{
ret += Encoding.UTF8.GetString(this.data);
}
if (this.children != null && this.children.Count() > 0)
{
foreach (var item in this.children)
{
ret += item.NodeString(indent + " ");
}
ret += "\n" + indent;
}
ret += "</" + this.tag + ">";
return ret;
}
public string GetAttribute(string attribute)
{
var ret = this.attributeHash.FirstOrDefault(x => x.Key.Equals(attribute));
return (ret == null) ? null : ret.Value;
}
public ProtocolTreeNode GetChild(string tag)
{
if (this.children != null && this.children.Any())
{
foreach (var item in this.children)
{
if (tag.Equals(item.tag, StringComparison.InvariantCultureIgnoreCase))
{
return item;
}
ProtocolTreeNode ret = item.GetChild(tag);
if (ret != null)
{
return ret;
}
}
}
return null;
}
public IEnumerable<ProtocolTreeNode> GetAllChildren(string tag)
{
var tmpReturn = new List<ProtocolTreeNode>();
if (this.children != null && this.children.Any())
{
foreach (var item in this.children)
{
if (tag.Equals(item.tag, StringComparison.InvariantCultureIgnoreCase))
{
tmpReturn.Add(item);
}
tmpReturn.AddRange(item.GetAllChildren(tag));
}
}
return tmpReturn.ToArray();
}
public IEnumerable<ProtocolTreeNode> GetAllChildren()
{
return this.children.ToArray();
}
public byte[] GetData()
{
return this.data;
}
public static bool TagEquals(ProtocolTreeNode node, string _string)
{
return (((node != null) && (node.tag != null)) && node.tag.Equals(_string, StringComparison.OrdinalIgnoreCase));
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.