Beispiel #1
0
        private XElement MapSegment(EdiSegment segment, Segment mapping)
        {
            var xml = new XElement(mapping != null ? mapping.Id : segment.Id);

            for (int i = 0; i < segment.Elements.Count; i++)
            {
                EdiElement element = segment.Elements[i];
                if (element == null)
                {
                    continue;
                }
                Element elementMapping = null;
                string  segmentId;
                if (mapping != null)
                {
                    if (mapping.Elements.Count > i)
                    {
                        elementMapping = mapping.Elements[i];
                    }
                    segmentId = mapping.Id;
                }
                else
                {
                    segmentId = segment.Id;
                }
                string defaultElementId = segmentId + (i + 1).ToString("d2");
                xml.Add(MapElement(element, elementMapping, defaultElementId));
            }
            return(xml);
        }
Beispiel #2
0
        private XElement Map(MapState mapState, Loop loop)
        {
            var    xml = new XElement(loop.Id);
            string previousSegmentId = null;
            var    loopState         = new MapState.LoopState(loop);

            mapState.LoopStates.Push(loopState);
            while (mapState.SegmentIndex < mapState.Segments.Count)
            {
                EdiSegment segment = mapState.Segments[mapState.SegmentIndex];
                if (loopState.VisitedSegmentIds.Contains(segment.Id))
                {
                    break;
                }
                Node mapping = loop.FindMatchingNode(segment);
                if (mapping == null && SegmentWasUnvisitedInOuterLoops(segment, mapState))
                {
                    break;
                }
                if (mapping is Loop)
                {
                    xml.Add(Map(mapState, (Loop)mapping));
                }
                else
                {
                    xml.Add(MapSegment(segment, (Segment)mapping));
                    mapState.SegmentIndex++;
                }
                if (previousSegmentId == null)
                {
                    if (loop != _root)
                    {
                        loopState.VisitedSegmentIds.Add(segment.Id);
                    }
                }
                else if (previousSegmentId != segment.Id)
                {
                    loopState.VisitedSegmentIds.Add(previousSegmentId);
                }
                previousSegmentId = segment.Id;
            }
            mapState.LoopStates.Pop();
            return(xml);
        }
Beispiel #3
0
        private void LoadSegment(XElement xml)
        {
            var segment = new EdiSegment(xml.Name.LocalName.ToUpper());

            foreach (XElement element in xml.Elements())
            {
                int elementIndex = GetElementIndex(element.Name.LocalName);
                if (elementIndex == -1)
                {
                    continue;
                }
                while (segment.Elements.Count <= elementIndex)
                {
                    segment.Elements.Add(null);
                }
                if (segment.Elements[elementIndex] == null)
                {
                    segment.Elements[elementIndex] = new EdiElement();
                }
                segment.Elements[elementIndex].Repetitions.Add(LoadRepetition(element));
            }
            Segments.Add(segment);
        }
Beispiel #4
0
 public Node FindMatchingNode(EdiSegment segment)
 {
     foreach (Node node in Nodes)
     {
         if (node is Segment)
         {
             var segmentMapping = (Segment)node;
             if (segmentMapping.IsMatch(segment))
             {
                 return(node);
             }
         }
         else
         {
             var     loop           = (Loop)node;
             Segment segmentMapping = loop.GetFirstSegment();
             if (segmentMapping != null && segmentMapping.IsMatch(segment))
             {
                 return(node);
             }
         }
     }
     return(null);
 }
Beispiel #5
0
        private EdiDocument(string edi, EdiOptions options)
        {
            if (options == null)
            {
                options = new EdiOptions();
                Options = options;
            }
            else
            {
                Options = new EdiOptions(options);
                options = new EdiOptions(options);
            }
            if (!options.SegmentTerminator.HasValue)
            {
                options.SegmentTerminator = GuessSegmentTerminator(edi);
            }
            if (!options.ElementSeparator.HasValue)
            {
                options.ElementSeparator = GuessElementSeparator(edi);
            }
            if (!options.ReleaseCharacter.HasValue)
            {
                options.ReleaseCharacter = GuessReleaseCharacter(edi);
            }

            Segments = new List <EdiSegment>();
            string[] rawSegments = SplitEdi(edi, options.SegmentTerminator.Value, options.ReleaseCharacter);
            for (int i = 0; i < rawSegments.Length; i++)
            {
                string rawSegment = rawSegments[i];
                if (i == rawSegments.Length - 1 && (rawSegment == null || rawSegment.Trim() == string.Empty))
                {
                    break;
                }
                EdiSegment segment = null;
                if (rawSegment.StartsWith("UNA", StringComparison.OrdinalIgnoreCase))
                {
                    segment = new EdiSegment(rawSegment.Substring(0, 3));
                    segment.Elements.Add(new EdiElement(rawSegment.Substring(3, 5)));
                    options.ComponentSeparator = rawSegment[3];
                    options.DecimalIndicator   = rawSegment[5];
                }
                else
                {
                    string[] rawElements = SplitEdi(rawSegment.TrimStart(), options.ElementSeparator.Value, options.ReleaseCharacter);
                    segment = new EdiSegment(rawElements[0]);
                    for (int j = 1; j < rawElements.Length; j++)
                    {
                        if (segment.Id.Equals("ISA", StringComparison.OrdinalIgnoreCase))
                        {
                            if (j == 16)
                            {
                                options.ComponentSeparator = rawElements[j][0];
                                segment.Elements.Add(new EdiElement(rawElements[j]));
                                continue;
                            }

                            if (j == 11)
                            {
                                if (string.CompareOrdinal(rawElements[12], "00402") >= 0 &&
                                    !char.IsLetterOrDigit(rawElements[j][0]))
                                {
                                    options.RepetitionSeparator = rawElements[j][0];
                                    segment.Elements.Add(new EdiElement(rawElements[j]));
                                    continue;
                                }
                                options.RepetitionSeparator = null;
                            }
                        }
                        segment.Elements.Add(rawElements[j] != string.Empty ? ParseElement(rawElements[j], options) : null);
                    }
                }
                Segments.Add(segment);
            }
        }
 /// <summary>
 /// Initializes a new instance of the EdiTransactionSet class.
 /// </summary>
 /// <param name="interchangeHeader">The interchange header segment associated with this transaction set.</param>
 /// <param name="functionalGroupHeader">The functional group header segment associated with this transaction set.</param>
 public EdiTransactionSet(EdiSegment interchangeHeader, EdiSegment functionalGroupHeader)
 {
     InterchangeHeader = interchangeHeader;
     FunctionalGroupHeader = functionalGroupHeader;
     Segments = new List<EdiSegment>();
 }
Beispiel #7
0
 public bool IsMatch(EdiSegment segment)
 {
     return Id.Equals(segment.Id, StringComparison.OrdinalIgnoreCase) &&
            !segment.Elements.Where((t, i) => Elements.Count > i && Elements[i] != null && !Elements[i].IsMatch(t)).Any();
 }
Beispiel #8
0
 public Node FindMatchingNode(EdiSegment segment)
 {
     foreach (Node node in Nodes)
     {
         if (node is Segment)
         {
             var segmentMapping = (Segment) node;
             if (segmentMapping.IsMatch(segment))
                 return node;
         }
         else
         {
             var loop = (Loop) node;
             Segment segmentMapping = loop.GetFirstSegment();
             if (segmentMapping != null && segmentMapping.IsMatch(segment))
                 return node;
         }
     }
     return null;
 }
Beispiel #9
0
 private XElement MapSegment(EdiSegment segment, Segment mapping)
 {
     var xml = new XElement(mapping != null ? mapping.Id : segment.Id);
     for (int i = 0; i < segment.Elements.Count; i++)
     {
         EdiElement element = segment.Elements[i];
         if (element == null)
             continue;
         Element elementMapping = null;
         string segmentId;
         if (mapping != null)
         {
             if (mapping.Elements.Count > i)
                 elementMapping = mapping.Elements[i];
             segmentId = mapping.Id;
         }
         else
             segmentId = segment.Id;
         string defaultElementId = segmentId + (i + 1).ToString("d2");
         xml.Add(MapElement(element, elementMapping, defaultElementId));
     }
     return xml;
 }
Beispiel #10
0
 private bool SegmentWasUnvisitedInOuterLoops(EdiSegment segment, MapState mapState)
 {
     return mapState.LoopStates.Any(state => !state.VisitedSegmentIds.Contains(segment.Id) &&
                                             state.Loop.FindMatchingNode(segment) != null);
 }
Beispiel #11
0
 private void LoadSegment(XElement xml)
 {
     var segment = new EdiSegment(xml.Name.LocalName.ToUpper());
     foreach (XElement element in xml.Elements())
     {
         int elementIndex = GetElementIndex(element.Name.LocalName);
         if (elementIndex == -1)
             continue;
         while (segment.Elements.Count <= elementIndex)
             segment.Elements.Add(null);
         if (segment.Elements[elementIndex] == null)
             segment.Elements[elementIndex] = new EdiElement();
         segment.Elements[elementIndex].Repetitions.Add(LoadRepetition(element));
     }
     Segments.Add(segment);
 }
Beispiel #12
0
        private EdiDocument(string edi, EdiOptions options)
        {
            if (options == null)
            {
                options = new EdiOptions();
                Options = options;
            }
            else
            {
                Options = new EdiOptions(options);
                options = new EdiOptions(options);
            }
            if (!options.SegmentTerminator.HasValue)
                options.SegmentTerminator = GuessSegmentTerminator(edi);
            if (!options.ElementSeparator.HasValue)
                options.ElementSeparator = GuessElementSeparator(edi);
            if (!options.ReleaseCharacter.HasValue)
                options.ReleaseCharacter = GuessReleaseCharacter(edi);

            Segments = new List<EdiSegment>();
            string[] rawSegments = SplitEdi(edi, options.SegmentTerminator.Value, options.ReleaseCharacter);
            for (int i = 0; i < rawSegments.Length; i++)
            {
                string rawSegment = rawSegments[i];
                if (i == rawSegments.Length - 1 && (rawSegment == null || rawSegment.Trim() == string.Empty))
                    break;
                EdiSegment segment = null;
                if (rawSegment.StartsWith("UNA", StringComparison.OrdinalIgnoreCase))
                {
                    segment = new EdiSegment(rawSegment.Substring(0, 3));
                    segment.Elements.Add(new EdiElement(rawSegment.Substring(3, 5)));
                    options.ComponentSeparator = rawSegment[3];
                    options.DecimalIndicator = rawSegment[5];
                }
                else
                {
                    string[] rawElements = SplitEdi(rawSegment.TrimStart(), options.ElementSeparator.Value, options.ReleaseCharacter);
                    segment = new EdiSegment(rawElements[0]);
                    for (int j = 1; j < rawElements.Length; j++)
                    {
                        if (segment.Id.Equals("ISA", StringComparison.OrdinalIgnoreCase))
                        {
                            if (j == 16)
                            {
                                options.ComponentSeparator = rawElements[j][0];
                                segment.Elements.Add(new EdiElement(rawElements[j]));
                                continue;
                            }

                            if (j == 11)
                            {
                                if (string.CompareOrdinal(rawElements[12], "00402") >= 0 &&
                                    !char.IsLetterOrDigit(rawElements[j][0]))
                                {
                                    options.RepetitionSeparator = rawElements[j][0];
                                    segment.Elements.Add(new EdiElement(rawElements[j]));
                                    continue;
                                }
                                options.RepetitionSeparator = null;
                            }
                        }
                        segment.Elements.Add(rawElements[j] != string.Empty ? ParseElement(rawElements[j], options) : null);
                    }
                }
                Segments.Add(segment);
            }
        }
Beispiel #13
0
 public bool IsMatch(EdiSegment segment)
 {
     return(Id.Equals(segment.Id, StringComparison.OrdinalIgnoreCase) &&
            !segment.Elements.Where((t, i) => Elements.Count > i && Elements[i] != null && !Elements[i].IsMatch(t)).Any());
 }
Beispiel #14
0
 private bool SegmentWasUnvisitedInOuterLoops(EdiSegment segment, MapState mapState)
 {
     return(mapState.LoopStates.Any(state => !state.VisitedSegmentIds.Contains(segment.Id) &&
                                    state.Loop.FindMatchingNode(segment) != null));
 }
Beispiel #15
0
 /// <summary>
 /// Initializes a new instance of the EdiTransactionSet class.
 /// </summary>
 /// <param name="interchangeHeader">The interchange header segment associated with this transaction set.</param>
 /// <param name="functionalGroupHeader">The functional group header segment associated with this transaction set.</param>
 public EdiTransactionSet(EdiSegment interchangeHeader, EdiSegment functionalGroupHeader)
 {
     InterchangeHeader     = interchangeHeader;
     FunctionalGroupHeader = functionalGroupHeader;
     Segments = new List <EdiSegment>();
 }