public string GetString(GenericProtocol pNode)
        {
            var dataOutput = new DataOutput();

            Write(pNode, pNode.Index, dataOutput);
            return(dataOutput.Data);
        }
 public byte[] GetBytes(GenericProtocol pNode)
 {
     using (var dataOutput = new DataOutputStream())
     {
         Write(pNode, pNode.Index, dataOutput);
         return(dataOutput.ToArray());
     }
 }
        private string ToString(GenericProtocol node, int index = 0)
        {
            var append = string.Empty;

            for (var i = 0; i < index; i++)
            {
                append += "\t";
            }

            var builder = new StringBuilder();

            builder.Append(append).Append(node.Name).Append(" (GenericProtocol)\n");
            builder.Append(append).Append("{\n");
            foreach (var pair in node._nodes)
            {
                var type = pair.Value == null ? "NULL" : pair.Value.GetType().Name;
                if (pair.Value != null && pair.Value.GetType() == typeof(List <object>))
                {
                    if (((List <object>)pair.Value).Count > 0)
                    {
                        type = string.Format("List<{0}>", ((List <object>)pair.Value)[0].GetType().Name);
                    }
                }
                builder.Append(append).Append("\t").Append(pair.Key).Append(" (").Append(type).Append(")\n");

                if (pair.Value != null && pair.Value.GetType() == typeof(GenericProtocol))
                {
                    builder.Append(ToString((GenericProtocol)pair.Value, index + 1));
                }
                else if (pair.Value != null && pair.Value.GetType() == typeof(List <object>))
                {
                    builder.Append(append).Append("\t{\n");

                    foreach (var val in (List <object>)pair.Value)
                    {
                        if (val.GetType() == typeof(GenericProtocol))
                        {
                            builder.Append(append).Append(ToString((GenericProtocol)val, index + 2)).Append("\n");
                        }
                        else
                        {
                            builder.Append(append).Append("\t\t").Append(val).Append("\n");
                        }
                    }

                    builder.Append(append).Append("\t}\n");
                }
                else
                {
                    builder.Append(append).Append("\t\t").Append(pair.Value).Append("\n");
                }
            }
            builder.Append(append).Append("}\n");

            return(builder.ToString());
        }
 private void Write(GenericProtocol pNode, int pIndex, IStreamOutput pOutput)
 {
     if (ByteData != null)
     {
         pOutput.Write(ByteData);
     }
     pOutput.WriteShort((short)pIndex);
     // Rekursive Methode:
     Write((object)pNode, pIndex, pOutput);
 }
        /// <summary>
        ///     Kopiert die Referenzen(Zeiger) von den Objekten in der Klasse.
        /// </summary>
        /// <returns></returns>
        public GenericProtocol CopyRef(int pNodeIndex)
        {
            var node = new GenericProtocol(pNodeIndex)
            {
                _nodeNames   = NodeNames,
                _nodeIndices = NodeIndices,
                _nodeValues  = NodeValues,
                ByteData     = ByteData,
                Hash         = Hash,
            };

            return(node);
        }
        /// <summary>
        ///     Wandelt eine StringDictionary in eine Liste mit 'KEY_VALUE'-Knoten um.
        /// </summary>
        /// <param name="pStringDictionary"></param>
        /// <returns></returns>
        public List <object> ToNodeFromStringDictionary(Dictionary <string, string> pStringDictionary)
        {
            var keyValueList = new List <object>();

            foreach (var keyValue in pStringDictionary)
            {
                GenericProtocol copyRef = CopyRef("KEY_VALUE");
                if (copyRef == null)
                {
                    continue;
                }
                copyRef.Add("KEY", keyValue.Key);
                copyRef.Add("VALUE", keyValue.Value);
                keyValueList.Add(copyRef);
            }
            return(keyValueList);
        }
        private object Read(IStreamInput pDataInput, int pIndex, GenericProtocol pNode)
        {
            if (pNode == null)
            {
                pNode = CopyRef(pIndex);
            }
            // --------------------------
            List <int> nodeIndices = pNode.NodeIndices[pIndex];

            for (int i = 0; i < nodeIndices.Count; i++)
            {
                int    localNodeIndex = nodeIndices[i];
                string localNodeName;
                switch (localNodeIndex)
                {
                case 0:
                    return(pDataInput.ReadByte());    // Byte

                case 1:
                    return(pDataInput.ReadBoolean());    // Bool(ean)

                case 2:
                    return(pDataInput.ReadByte());    // Byte

                case 3:
                    return(pDataInput.ReadShort());    // Short

                case 4:
                    return(pDataInput.ReadInt());    // Int

                case 5:
                    return(pDataInput.ReadLong());    // Long

                case 6:
                    return(pDataInput.ReadFloat());    // Float

                case 7:
                    return(pDataInput.ReadDouble());    // Double

                case 8:
                    return(pDataInput.ReadChar());    // Char

                case 9:
                    return(pDataInput.ReadUTF().Replace('\u20AD', 'K'));    // String

                case 10:
                    break;

                case 11:
                    i++;
                    localNodeIndex = nodeIndices[i];
                    localNodeName  = NodeNames[localNodeIndex];
                    var arrList = new List <object>();
                    pNode.Add(localNodeName, arrList);
                    while (pDataInput.ReadByte() == 11)
                    {
                        arrList.Add(Read(pDataInput, localNodeIndex, null));
                    }
                    i++;
                    break;

                case 12:     // Ende der Liste
                    break;

                case 13:     // new | seit ungefähr dem applet 90aeh
                    return(ReadChars(pDataInput));

                //break;
                default:
                    localNodeName = NodeNames[localNodeIndex];
                    pNode.Add(localNodeName, Read(pDataInput, localNodeIndex, null));
                    break;
                }
            }
            // --------------------------
            return(pNode);
        }