コード例 #1
0
        public void TestEncodeText()
        {
            CompositeField f = new CompositeField();

            f.AddValue(new IsoValue(IsoType.ALPHA, "One", 5));
            f.Values[0].Encoding = Encoding.UTF8;
            Assert.Equal("One  ", f.EncodeField(f));
            f.AddValue("Two", null, IsoType.LLVAR, 0);
            f.Values[1].Encoding = Encoding.UTF8;
            Assert.Equal("One  03Two", f.EncodeField(f));
            f.AddValue(999, null, IsoType.NUMERIC, 5);
            f.Values[2].Encoding = Encoding.UTF8;
            Assert.Equal("One  03Two00999", f.EncodeField(f));
            f.AddValue("X", null, IsoType.ALPHA, 1);
            Assert.Equal(textData, f.EncodeField(f));
        }
コード例 #2
0
        public void TestEncodeBinary()
        {
            CompositeField f = new CompositeField()
                               .AddValue(new IsoValue(IsoType.ALPHA, "One", 5));

            Assert.Equal(new sbyte[] { (sbyte)'O', (sbyte)'n', (sbyte)'e', 32, 32 }, f.EncodeBinaryField(f));
            f.AddValue(new IsoValue(IsoType.LLVAR, "Two"));
            Assert.Equal(
                new sbyte[]
            {
                (sbyte)'O', (sbyte)'n', (sbyte)'e', (sbyte)' ', (sbyte)' ', 3, (sbyte)'T', (sbyte)'w',
                (sbyte)'o'
            },
                f.EncodeBinaryField(f));
            f.AddValue(new IsoValue(IsoType.NUMERIC, 999L, 5));
            f.AddValue(new IsoValue(IsoType.ALPHA, "X", 1));
            Assert.Equal(binaryData, f.EncodeBinaryField(f));
        }
コード例 #3
0
        /// <summary>
        ///     Creates an IsoValue from the XML definition in a message template.
        ///     If it's for a toplevel field and the message factory has a codec for this field,
        ///     that codec is assigned to that field. For nested fields, a CompositeField is
        ///     created and populated.
        /// </summary>
        /// <returns>The template field.</returns>
        /// <param name="f">xml element</param>
        /// <param name="mfact">message factory</param>
        /// <param name="toplevel">If set to <c>true</c> toplevel.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        private static IsoValue GetTemplateField <T>(XmlElement f,
                                                     MessageFactory <T> mfact,
                                                     bool toplevel) where T : IsoMessage
        {
            var num     = int.Parse(f.GetAttribute("num"));
            var typedef = f.GetAttribute("type");

            if ("exclude".Equals(typedef))
            {
                return(null);
            }

            var length = 0;

            if (f.GetAttribute("length").Length > 0)
            {
                length = int.Parse(f.GetAttribute("length"));
            }

            var itype = Enumm.Parse <IsoType>(typedef);
            var subs  = f.GetElementsByTagName("field");

            if (subs.Count > 0)
            {
                var cf = new CompositeField();
                for (var j = 0; j < subs.Count; j++)
                {
                    var sub = (XmlElement)subs.Item(j);
                    if (sub != null && sub.ParentNode != f)
                    {
                        continue;
                    }
                    var sv = GetTemplateField(
                        sub,
                        mfact,
                        false);
                    if (sv == null)
                    {
                        continue;
                    }
                    sv.Encoding = mfact.Encoding;
                    cf.AddValue(sv);
                }

                Debug.Assert(itype != null, nameof(itype) + " != null");
                return(itype.Value.NeedsLength()
                    ? new IsoValue(
                           itype.Value,
                           cf,
                           length,
                           cf)
                    : new IsoValue(
                           itype.Value,
                           cf,
                           cf));
            }

            var v           = f.ChildNodes.Count == 0 ? string.Empty : f.ChildNodes.Item(0)?.Value;
            var customField = toplevel ? mfact.GetCustomField(num) : null;

            if (customField != null)
            {
                Debug.Assert(
                    itype != null,
                    "itype != null");

                return(itype.Value.NeedsLength()
                    ? new IsoValue(
                           itype.Value,
                           customField.DecodeField(v),
                           length,
                           customField)
                    : new IsoValue(
                           itype.Value,
                           customField.DecodeField(v),
                           customField));
            }

            Debug.Assert(
                itype != null,
                "itype != null");

            return(itype.Value.NeedsLength()
                ? new IsoValue(
                       itype.Value,
                       v,
                       length)
                : new IsoValue(
                       itype.Value,
                       v));
        }