Exemplo n.º 1
0
        public void FromStream(Stream s)
        {
            ExtractDelimiterInfo(s);

            head      = tail = 0;
            buffer    = new byte[BufferSize];
            EndOfFile = false;

            int docLine = 1;

            var segText = ReadNextLine(s);

            LoopsList currentLoopList  = null;
            LoopsList previousLoopList = null;

            while (segText != null)
            {
                List <string> segSplit = segText.Split(delimiters.Element).ToList();

                LoopSegment foundDef = FindSingleDefMatch(currentLoopList, segSplit, segText, ref docLine);

                if (foundDef != null)
                {
                    if (currentLoopList != null && foundDef.OwningLoop != null &&
                        currentLoopList.LoopName != foundDef.OwningLoop.LoopName)
                    {
                        previousLoopList = currentLoopList;
                    }

                    if ((currentLoopList == null && foundDef.OwningLoop != null) ||
                        (currentLoopList != null && foundDef.OwningLoop != null &&
                         currentLoopList.LoopName != foundDef.OwningLoop.LoopName))
                    {
                        currentLoopList = foundDef.OwningLoop;
                        currentLoopList.Add(new baseLoop(currentLoopList));
                    }

                    //if (thisLoopMatches == null || !thisLoopMatches.Any() || thisLoopMatches.Count() > 1)
                    //    thisLoopMatches = possibleSegmentMatches;

                    baseStdSegment newSegInst = foundDef.QualifiedSegments[0].CreateInstance
                                                    (segText, delimiters, foundDef.OwningLoop);

                    if (!newSegInst.Validate())
                    {
                        Errors.Add(new X12Error
                        {
                            DocumentLine    = docLine,
                            ErrorLevel      = X12ErrorLevel.Segment,
                            ErrorType       = X12ErrorTypes.ValidationFailed,
                            SegmentText     = segText,
                            CurrentLoopName = currentLoopList?.LoopName,
                        });
                    }
                }

                segText = ReadNextLine(s);
                docLine++;
            }
        }
Exemplo n.º 2
0
 public LoopsList(LoopsList parent, int repeats, string name)
 {
     Parent     = parent;
     LoopRepeat = repeats;
     LoopName   = name;
     DefineSegmentDefinition();
 }
Exemplo n.º 3
0
        public RawSegment(string value, Delimiters delims, LoopsList parentLoop, SegmentDefinition def)
        {
            FieldValues     = new List <List <string> >();
            ParsedValue     = value;
            delimiters      = delims;
            ParentLoopsList = parentLoop;
            ParentLoop      = parentLoop?.Last();
            DefiningSegment = def;

            var firstSplit = value.Split(delims.Element);

            foreach (string s in firstSplit)
            {
                var subs = new List <string>();
                if (s.Contains(delims.SubElement))
                {
                    subs = s.Split(delims.SubElement).ToList();
                }
                else
                {
                    subs.Add(s);
                }

                FieldValues.Add(subs);
            }
        }
Exemplo n.º 4
0
        public baseStdSegment CreateInstance(string value, Delimiters delims, LoopsList parentLoop)
        {
            var sg = (baseStdSegment)Activator.CreateInstance(SegmentType);

            sg.Prep(value, parentLoop, delims, this);
            //RawSegments.Add(sg.rawSegment);
            return(sg);
        }
Exemplo n.º 5
0
        public void Prep(string value, LoopsList parentLoop, Delimiters delims, SegmentDefinition def)
        {
            ParentLoops = parentLoop;
            Delimiter   = delims;
            DefinedSeg  = def;
            ParentLoop  = ParentLoops?.Last();

            rawSegment = new RawSegment(value, Delimiter, ParentLoops, DefinedSeg);
        }
Exemplo n.º 6
0
        private LoopSegment FindSingleDefMatch(LoopsList currentLoopList, List <string> segSplit,
                                               string segText, ref int docLine)
        {
            LoopSegment retVal = null;

            List <LoopSegment> possibleSegmentMatches = new List <LoopSegment>();

            if (currentLoopList == null) //this should be envelope segments
            {
                var segMatch = new LoopSegment
                {
                    OwningLoop        = null,
                    QualifiedSegments = EnvelopseDefinitions.Where(c => c.IsQualified(segSplit)).ToList()
                };

                if (segMatch.QualifiedSegments.Any())
                {
                    retVal = segMatch;
                    return(retVal);
                }
            }

            possibleSegmentMatches.AddRange(FindQualifiedSegments(segSplit));


            if (possibleSegmentMatches == null || !possibleSegmentMatches.Any())
            {
                Errors.Add(new X12Error
                {
                    DocumentLine    = docLine,
                    ErrorLevel      = X12ErrorLevel.Loop,
                    ErrorType       = X12ErrorTypes.NotDefined,
                    SegmentText     = segText,
                    CurrentLoopName = currentLoopList?.LoopName,
                });
            }

            List <LoopSegment> thisLoopMatches = null;

            if (currentLoopList != null && possibleSegmentMatches.Count() != 1)
            {
                //first find segments that are in the same loop
                thisLoopMatches =
                    possibleSegmentMatches.Where(c => c.OwningLoop.LoopName == currentLoopList.LoopName).ToList();

                if (thisLoopMatches.Count != 1)
                {
                    thisLoopMatches =
                        possibleSegmentMatches.Where(c => c.OwningLoop.StartingSegment.SegmentName == segSplit[0])
                        .ToList();

                    //see if the parent of one of our matches is the same as the loop we are in now
                    if (thisLoopMatches.Count != 1)
                    {
                        thisLoopMatches =
                            thisLoopMatches.Where(
                                c => c.OwningLoop.Parent.LoopName == currentLoopList.LoopName).ToList();
                    }

                    //maybe this is the start of another sub loop within the same parent loop
                    if (thisLoopMatches.Count != 1 && currentLoopList.Parent != null)
                    {
                        thisLoopMatches =
                            possibleSegmentMatches.Where(c => c.OwningLoop.StartingSegment.SegmentName == segSplit[0])
                            .ToList();

                        thisLoopMatches = thisLoopMatches.Where(c =>
                                                                currentLoopList.Parent[0].Parent.LoopName ==
                                                                c.OwningLoop.Parent.LoopName).ToList();
                    }

                    //it could be that this loop exists in 2 subloops that defined the same (IE 837.2000B.2300-ClmInfo and 837.2000C.2300-ClmInfo)
                    if (thisLoopMatches.Count != 1 && currentLoopList.Parent != null)
                    {
                        thisLoopMatches =
                            possibleSegmentMatches.Where(
                                c => c.OwningLoop.Parent.LoopName == currentLoopList.Parent.LoopName).ToList();
                    }
                }
            }
            //if the current loop is null but we have possibleSegment out of the Find function
            //this should a start segment for our first loop
            else if (currentLoopList == null && possibleSegmentMatches.Count == 1 &&
                     possibleSegmentMatches[0].OwningLoop.StartingSegment.SegmentName == segSplit[0])
            {
                thisLoopMatches = possibleSegmentMatches;
            }
            else if (currentLoopList != null && possibleSegmentMatches.Count == 1)
            {
                thisLoopMatches = possibleSegmentMatches;
            }

            if (thisLoopMatches != null && thisLoopMatches.Count == 1)
            {
                retVal = thisLoopMatches[0];
            }

            if (possibleSegmentMatches.Count > 1 && (thisLoopMatches == null || thisLoopMatches.Count != 1))
            {
                Errors.Add(new X12Error
                {
                    DocumentLine    = docLine,
                    ErrorLevel      = X12ErrorLevel.Loop,
                    ErrorType       = X12ErrorTypes.MultipleDefintions,
                    SegmentText     = segText,
                    CurrentLoopName = currentLoopList?.LoopName,
                });
            }

            return(retVal);
        }
Exemplo n.º 7
0
 public baseLoop(LoopsList parent)
 {
     Parent = parent;
 }
Exemplo n.º 8
0
 public baseStdSegment(string value, LoopsList parentLoop, Delimiters delims, SegmentDefinition def)
 {
     Prep(value, parentLoop, delims, def);
 }