internal override void SetValue(byte[] value) { if (VarLength && Length < 0) { Length = value.Length; } if (Length < 0) { throw new MessageProcessorException("The Length property is not yet set (still negative)."); } else if (value.Length > Length) { throw new MessageProcessorException("The length of the assigned value is greater than defined length (Length property)."); } else if (value.Length < Length) { byte[] val = new byte[Length - value.Length]; for (int i = 0; i < val.Length; i++) { val[i] = (byte)0x20; //Space } value = value.Concat(val).ToArray(); } _bytes = value; _string = MessageUtility.AsciiArrayToString(value); _decimal = null; }
private Object ParseBitContent(BitContentConfig bcc, byte[] bytesValue) { Object bitContent = Activator.CreateInstance(bcc.ClassType); int i = 0; String[] locMsgs = new String[] { "BitContent element (id=" + bcc.Id + ") and field (name=", null, ")" }; foreach (BitContentFieldConfig bcfc in bcc.Fields) { Object propValue = null; if (bcfc.Tlv != null || !String.IsNullOrEmpty(bcfc.TlvTagName)) { int len = bytesValue.Length - i; byte[] bytes = new byte[len]; Array.Copy(bytesValue, i, bytes, 0, len); locMsgs[1] = bcfc.PropertyInfo.Name; int tlvLength = 0; propValue = ParseTlv(bcfc.PropertyInfo.PropertyType, bcfc.Tlv, bcfc.TlvTagName, bcfc.TlvLengthBytes, bcfc.TlvClassType, bytes, String.Join("", locMsgs), true, ref tlvLength); i += tlvLength; } else { int len = (i + bcfc.Length > bytesValue.Length) ? (bytesValue.Length - i) : bcfc.Length; if (len <= 0) { break; } byte[] bytes = new byte[len]; Array.Copy(bytesValue, i, bytes, 0, len); String val = MessageUtility.AsciiArrayToString(bytes); if (val != null) { if (bcfc.IsTrim) { val = val.Trim(); } propValue = val; } else { Type bcfType = bcfc.PropertyInfo.PropertyType; propValue = Util.GetAssignableValue(bcfType, bytes); if (propValue == null) { throw new MessageParserException("Cannot parse BitContent field named " + bcfc.PropertyInfo.Name + ". Check configuration for BitContent element (id=" + bcc.Id + ")"); } } i += bcfc.Length; } bcfc.PropertyInfo.SetValue(bitContent, propValue, null); if (i >= bytesValue.Length) { break; } } return(bitContent); }
internal override void SetValue(byte[] value) { for (int i = 0; i < value.Length; i++) { if (value[i] < 0x30 || value[i] > 0x39) { throw new MessageProcessorException("Invalid assigned value, cannot converted to numeric"); } } if (VarLength && Length < 0) { Length = value.Length; } if (Length < 0) { throw new MessageProcessorException("The Length property is not yet set (still negative)."); } else if (value.Length > Length) { throw new MessageProcessorException("The length of the assigned value is greater than defined length (Length property)."); } else if (value.Length < Length) { byte[] val = new byte[Length - value.Length]; for (int i = 0; i < val.Length; i++) { val[i] = (byte)0x30; //'0' } value = val.Concat(value).ToArray(); } _bytes = value; _string = MessageUtility.AsciiArrayToString(value); if (FracDigits > 0) { _string = _string.Insert(_string.Length - FracDigits, "."); } _decimal = decimal.Parse(_string); }
private Object ParseTlv(Type propType, TlvConfig cfg, String tagName, int lengthBytes, Type tlvType, byte[] bytesValue, String locMsg, bool isAlwaysOneTag, ref int tlvLength) { 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 (cfg != null && cfg.ClassType != null) { tlvType = cfg.ClassType; } IDictionary <String, Object> tagValMap = new Dictionary <String, Object>(); int i = 0; byte[] bytes; while (i < bytesValue.Length) { CheckTlvToken(i, tagLength, bytesValue, locMsg); bytes = new byte[tagLength]; Array.Copy(bytesValue, i, bytes, 0, tagLength); i += tagLength; String tag = MessageUtility.AsciiArrayToString(bytes); if (tag == null) { throw new MessageParserException("Invalid tag value for a tlv field. Check configuration " + locMsg); } CheckTlvToken(i, lengthBytes, bytesValue, locMsg); bytes = new byte[lengthBytes]; Array.Copy(bytesValue, i, bytes, 0, lengthBytes); i += lengthBytes; int len = MessageUtility.AsciiArrayToInt(bytes); if (len < 0) { throw new MessageParserException("Invalid length value for a tlv field. Check configuration " + locMsg); } if (len > 0) { CheckTlvToken(i, len, bytesValue, locMsg); bytes = new byte[len]; Array.Copy(bytesValue, i, bytes, 0, len); i += len; TlvTagConfig tagCfg = cfg != null?cfg.GetTag(tag) : null; if (tagCfg != null && tagCfg.BitContent != null) { tagValMap[tag] = ParseBitContent(tagCfg.BitContent, bytes); } else { String value = System.Text.Encoding.ASCII.GetString(bytes); if (tagCfg != null && tagCfg.Splitter != null) { tagValMap[tag] = value.Split(new String[] { tagCfg.Splitter }, StringSplitOptions.None); } else { tagValMap[tag] = value; } } } if (isAlwaysOneTag) { break; } } tlvLength = i; if (tagValMap.Count <= 0) { return(null); } String convertFailedMsg = "Cannot convert a tlv field into the mapped property. Check " + locMsg; if (tlvType != null && propType.IsAssignableFrom(tlvType)) { Object propValue = Activator.CreateInstance(tlvType); foreach (KeyValuePair <String, Object> kvp in tagValMap) { PropertyInfo property = tlvType.GetProperty(kvp.Key); if (property == null) { throw new MessageParserException(convertFailedMsg); } Object propValue2 = kvp.Value; if (propValue2 != null) { propValue2 = Util.GetAssignableValue(property.PropertyType, kvp.Value); if (propValue2 == null) { throw new MessageParserException(convertFailedMsg); } } property.SetValue(propValue, propValue2, null); } return(propValue); } else { if (tagValMap.Count == 1) { foreach (Object value in tagValMap.Values) { if (value == null) { return(null); } Object val = Util.GetAssignableValue(propType, value); if (val != null) { return(val); } } } if (!propType.IsAssignableFrom(tagValMap.GetType())) { Object propValue = Activator.CreateInstance(propType); foreach (KeyValuePair <String, Object> kvp in tagValMap) { PropertyInfo property = propType.GetProperty(kvp.Key); if (property == null) { throw new MessageParserException(convertFailedMsg); } Object propValue2 = kvp.Value; if (propValue2 != null) { propValue2 = Util.GetAssignableValue(property.PropertyType, kvp.Value); if (propValue2 == null) { throw new MessageParserException(convertFailedMsg); } } property.SetValue(propValue, propValue2, null); } return(propValue); } } return(tagValMap); }