Example #1
0
        internal override bool TryReadControl(string segmentName, out string probed, out Separators separators)
        {
            probed     = "";
            separators = null;

            try
            {
                if (segmentName == "UNB")
                {
                    probed = segmentName + StreamReader.Read(6);
                    if (IsUnb(probed, Separators.DefaultSeparatorsEdifact().DataElement,
                              Separators.DefaultSeparatorsEdifact().ComponentDataElement))
                    {
                        separators = Separators.DefaultSeparatorsEdifact();
                        return(true);
                    }
                }

                if (segmentName == "UNA")
                {
                    var una = StreamReader.Read(6);
                    probed = segmentName + una;
                    var  unaChars             = una.ToArray();
                    var  componentDataElement = unaChars[0];
                    var  dataElement          = unaChars[1];
                    char?escape = unaChars[3];
                    var  repetitionDataElement = Separators.DefaultSeparatorsEdifact().RepetitionDataElement;
                    var  segment = unaChars[5];

                    var tmp = StreamReader.Read(9, Trims);
                    probed += tmp;
                    if (IsUnb(tmp, dataElement, componentDataElement))
                    {
                        separators = Separators.SeparatorsEdifact(segment, componentDataElement, dataElement,
                                                                  repetitionDataElement, escape);
                        probed = tmp;
                        return(true);
                    }
                }
            }
            catch
            {
                // ignored
            }

            return(false);
        }
Example #2
0
        public void TestGenerateWithNonDefaultSeparators()
        {
            // ARRANGE
            const string sample            = "EdiFabric.Tests.Edi.Edifact_INVOIC_D00A.txt";
            const string expectedResult    = "EdiFabric.Tests.Edi.Edifact_INVOIC_D00A_NonDefaultSeparators.txt";
            var          interchange       = TestHelper.GenerateEdifact <M_INVOIC>(sample);
            var          defaultSeparators = Separators.DefaultSeparatorsEdifact();
            var          newSeparators     = Separators.SeparatorsEdifact(defaultSeparators.Segment,
                                                                          defaultSeparators.ComponentDataElement, '|', defaultSeparators.RepetitionDataElement,
                                                                          defaultSeparators.Escape);

            // ACT
            var ediSegments = interchange.GenerateEdi(newSeparators);

            // ASSERT
            Assert.AreEqual(TestHelper.AsString(expectedResult), TestHelper.AsString(ediSegments, Environment.NewLine));
        }
Example #3
0
        internal static Tuple <string, Separators> ReadHeader(this StreamReader reader, string segmentName)
        {
            char       dataElement;
            char       componentDataElement;
            char       repetitionDataElement;
            char       segment;
            char?      escape;
            string     header     = null;
            Separators separators = null;

            switch (segmentName.ToSegmentTag())
            {
            case SegmentTags.ISA:
                try
                {
                    dataElement = reader.Read(1)[0];
                    var isa         = reader.ReadIsa(dataElement);
                    var isaElements = isa.Split(dataElement);
                    if (isaElements[15].Count() != 2)
                    {
                        throw new Exception("Can't find the segment terminator.");
                    }
                    componentDataElement  = isaElements[15].First();
                    repetitionDataElement = isaElements[10].First() != 'U'
                            ? isaElements[10].First()
                            : Separators.DefaultSeparatorsX12().RepetitionDataElement;
                    segment    = isaElements[15].Last();
                    separators = Separators.SeparatorsX12(segment, componentDataElement, dataElement,
                                                          repetitionDataElement);
                    header = segmentName + dataElement + isa;
                }
                catch (Exception ex)
                {
                    throw new ParserException("Unable to extract X12 interchange delimiters", ex);
                }
                break;

            case SegmentTags.UNB:
                var defaultSeparators = Separators.DefaultSeparatorsEdifact();
                componentDataElement = defaultSeparators.ComponentDataElement;
                dataElement          = defaultSeparators.DataElement;
                escape = defaultSeparators.Escape;
                repetitionDataElement = defaultSeparators.RepetitionDataElement;
                segment = defaultSeparators.Segment;

                separators = Separators.SeparatorsEdifact(segment, componentDataElement, dataElement,
                                                          repetitionDataElement, escape);
                header = segmentName + reader.ReadSegment(separators);
                break;

            case SegmentTags.UNA:
                try
                {
                    var una      = reader.Read(6);
                    var unaChars = una.ToArray();
                    componentDataElement = unaChars[0];
                    dataElement          = unaChars[1];
                    escape = unaChars[3];
                    repetitionDataElement = Separators.DefaultSeparatorsEdifact().RepetitionDataElement;
                    segment = unaChars[5];

                    separators = Separators.SeparatorsEdifact(segment, componentDataElement, dataElement,
                                                              repetitionDataElement, escape);
                    header = segmentName + una;
                }
                catch (Exception ex)
                {
                    throw new ParserException("Unable to extract UNA interchange delimiters", ex);
                }
                break;
            }

            return(new Tuple <string, Separators>(header, separators));
        }