Esempio n. 1
0
        private byte[] CompileTlv(TlvConfig cfg, String tagName, int lengthBytes, Object obj, String locMsg)
        {
            int tagLength = (cfg != null && cfg.TagsCount > 0) ? cfg.GetTag(0).Name.Length
                : (tagName.Length > 0 ? tagName.Length : 2);

            if (cfg != null && cfg.LengthBytes > 0)
            {
                lengthBytes = cfg.LengthBytes;
            }

            if (String.IsNullOrEmpty(tagName) && (cfg == null || cfg.TagsCount <= 0))
            {
                throw new MessageCompilerException(
                          "Cannot compile a tlv field. It cannot determine the tag ID. Check configuration "
                          + locMsg);
            }

            IList <TlvTagConfig> tagCfgs = null;

            if (cfg != null)
            {
                tagCfgs = cfg.Tags;
            }
            if (tagCfgs == null)
            {
                tagCfgs = new List <TlvTagConfig>();
            }
            if (tagCfgs.Count <= 0)
            {
                TlvTagConfig tagCfg = new TlvTagConfig();
                tagCfg.Name = tagName;
                tagCfgs.Add(tagCfg);
            }

            IDictionary <String, Object> tagMapVal = new Dictionary <String, Object>();

            if (obj is IDictionary <String, Object> )
            {
                IDictionary <String, Object> map = (IDictionary <String, Object>)obj;
                foreach (TlvTagConfig tagCfg in tagCfgs)
                {
                    if (map.ContainsKey(tagCfg.Name))
                    {
                        tagMapVal[tagCfg.Name] = map[tagCfg.Name];
                    }
                }
            }
            else if (obj != null)
            {
                foreach (TlvTagConfig tagCfg in tagCfgs)
                {
                    PropertyInfo property = obj.GetType().GetProperty(tagCfg.Name);
                    if (property != null)
                    {
                        tagMapVal[tagCfg.Name] = property.GetValue(obj, null);
                    }
                }
            }
            if (tagMapVal.Count <= 0 && tagCfgs.Count == 1)
            {
                tagMapVal[tagCfgs[0].Name] = obj;
            }

            StringBuilder sb = new StringBuilder();

            foreach (TlvTagConfig tagCfg in tagCfgs)
            {
                sb.Append(tagCfg.Name);
                String len    = "0";
                String strVal = "";
                if (tagMapVal.ContainsKey(tagCfg.Name) && tagMapVal[tagCfg.Name] != null)
                {
                    Object value = tagMapVal[tagCfg.Name];
                    if (tagCfg.Splitter != null)
                    {
                        if (value is IEnumerable <Object> )
                        {
                            strVal = Util.Join((IEnumerable <Object>)value, tagCfg.Splitter);
                        }
                        else
                        {
                            throw new MessageCompilerException(
                                      "Cannot compile a tlv field. The passed value is not an array. Check configuration "
                                      + locMsg + " for tag " + tagCfg.Name);
                        }
                    }
                    else if (tagCfg.BitContent != null)
                    {
                        byte[] val = CompileBitContent(tagCfg.BitContent, value);
                        strVal = System.Text.Encoding.ASCII.GetString(val);
                    }
                    else
                    {
                        strVal = value.ToString();
                    }
                }

                len = strVal.Length.ToString();
                if (len.Length > lengthBytes)
                {
                    throw new MessageCompilerException(
                              "Cannot compile a tlv field. The length's value is too big more than " + lengthBytes
                              + " digits. Check configuration " + locMsg + " for tag " + tagCfg.Name);
                }
                else if (len.Length < lengthBytes)
                {
                    len = len.PadLeft(lengthBytes, '0');
                }
                sb.Append(len);
                sb.Append(strVal);
            }

            return(MessageUtility.StringToAsciiArray(sb.ToString()));
        }
Esempio n. 2
0
 internal override void SetValue(String value)
 {
     SetValue(MessageUtility.StringToAsciiArray(value));
 }
Esempio n. 3
0
        private byte[] CompileBitContent(BitContentConfig cfg, Object obj)
        {
            if (cfg.ClassType != obj.GetType())
            {
                throw new MessageCompilerException("Cannot compile a BitContent field. Not match target type of object. The passed "
                                                   + "object has type of " + obj.GetType().FullName + " whereas the BitContent requires type of "
                                                   + cfg.ClassType.FullName + ". Please check configuration for BitContent (id=" + cfg.Id);
            }

            String[] locMsgs = new String[] { "BitContent element (id=" + cfg.Id + ") and field (name=",
                                              null, ")" };
            List <byte> bytes = new List <byte>();

            foreach (BitContentFieldConfig ccfg in cfg.Fields)
            {
                Object propVal = ccfg.PropertyInfo.GetValue(obj, null);
                if (propVal == null)
                {
                    if (!ccfg.IsOptional)
                    {
                        for (int i = 0; i < ccfg.Length; i++)
                        {
                            bytes.Add(ccfg.NullChar);
                        }
                    }
                }
                else if (propVal.GetType() == typeof(byte[]))
                {
                    bytes.AddRange((byte[])propVal);
                }
                else if (ccfg.Tlv != null || !String.IsNullOrEmpty(ccfg.TlvTagName))
                {
                    locMsgs[1] = ccfg.PropertyInfo.Name;
                    byte[] val = CompileTlv(ccfg.Tlv, ccfg.TlvTagName, ccfg.TlvLengthBytes, propVal,
                                            String.Join("", locMsgs));
                    bytes.AddRange(val);
                }
                else
                {
                    byte[] value = MessageUtility.StringToAsciiArray(propVal.ToString());
                    if (value.Length > ccfg.Length)
                    {
                        locMsgs[1] = ccfg.PropertyInfo.Name;
                        throw new MessageCompilerException(
                                  "Cannot compile a BitContent field. The length of field's value is too big more than "
                                  + ccfg.Length + " chars. Check configuration " + String.Join("", locMsgs));
                    }
                    int i = value.Length;
                    if (ccfg.Align == "right")
                    {
                        for (; i < ccfg.Length; i++)
                        {
                            bytes.Add(ccfg.PadChar);
                        }
                    }
                    bytes.AddRange(value);
                    if (ccfg.Align != "right")
                    {
                        for (; i < ccfg.Length; i++)
                        {
                            bytes.Add(ccfg.PadChar);
                        }
                    }
                }
            }
            return(bytes.ToArray());
        }