Esempio n. 1
0
        private static void TraversAllSegments(Interchange interchange, IReadOnlyList<Segment> segments, Errors errors)
        {
            if (segments == null || segments.Count == 0)
            {
                errors.NewError("No segments provided for parsing.");
                return;
            }

            Group currentGroup = null;
            Document currentDocument = null;

            if (
                !errors.Assert(() => segments[0].Name == "ISA",
                    "FATAL Error: The first segment ('" + segments[0].Name + "') is not ISA segment."))
                return; // FATAL error

            interchange.ISASegment = new ISASegment(segments[0].Elements);

            for (var i = 1; i < segments.Count; i++)
            {
                switch (segments[i].Name)
                {
                    case "ST":
                        currentDocument = new Document();
                        currentDocument.STSegment = new STSegment(segments[i].Elements);
                        break;
                    case "SE":
                        if (currentDocument != null)
                        {
                            currentDocument.SESegment = new SESegment(segments[i].Elements);
                            if (currentGroup != null)
                                currentGroup.Documents.Add(currentDocument);
                            else
                                errors.NewError(string.Format("Segment:'{0}' placed before GS segment",
                                    segments[i].AsString));
                            currentDocument = null;
                        }
                        else
                            errors.NewError(string.Format("Segment:'{0}' placed before ST segment", segments[i].AsString));
                        break;
                    case "GS":
                        currentGroup = new Group();
                        currentGroup.GSSegment = new GSSegment(segments[i].Elements);
                        break;
                    case "GE":
                        if (currentGroup != null)
                        {
                            currentGroup.GESegment = new GESegment(segments[i].Elements);
                            interchange.Groups.Add(currentGroup);
                            currentGroup = null;
                        }
                        else
                            errors.NewError(string.Format("Segment:'{0}' placed before GS segment", segments[i].AsString));
                        break;
                    case "IEA":
                        interchange.IEASegment = new IEASegment(segments[i].Elements);
                        break;
                    default:
                        if (currentDocument != null)
                            currentDocument.Segments.Add(segments[i]);
                        else
                            errors.NewError(string.Format("Segment:'{0}' placed before ST segment", segments[i].AsString));
                        break;
                }
            }

            // Validate a new interchange:
            interchange.Validate(errors);
        }
Esempio n. 2
0
        private static Segment ParseSegment(string line, char dataElementSeparator, Errors errors)
        {
            var elements = line.Split(dataElementSeparator);
            errors.Assert(() => elements.Length != 0,
                string.Format("Line '{0}' cannot be parsed. Is it empty?", line));

            errors.Assert(() => elements.Length != 1,
                string.Format("Line '{0}' cannot be parsed. With '{1}' separator it has only one element.", line,
                    dataElementSeparator));
            for (var index = 0; index < elements.Length; index++)
                elements[index] = elements[index].Trim();

            return new Segment(elements);
        }