/// <summary> /// Converts edi interchange headers into message context. /// </summary> /// <param name="envelopes">The interchange headers.</param> /// <param name="interchangeContext">The interchange context.</param> /// <returns> /// The message context. /// </returns> private Tuple <string, string, string, EdiFormats> ToContextEdifact(IEnumerable <string> envelopes, InterchangeContext interchangeContext) { try { var unh = envelopes.Single(es => es.StartsWith(EdiSegments.Unh)); var ediCompositeDataElements = EdiHelper.GetEdiCompositeDataElements(unh, interchangeContext); var ediDataElements = EdiHelper.GetEdiComponentDataElements(ediCompositeDataElements[1], interchangeContext); return(new Tuple <string, string, string, EdiFormats>(ediDataElements[0], ediDataElements[1] + ediDataElements[2], null, EdiFormats.Edifact)); } catch (Exception ex) { throw new ParserException("Can't parse UNH segment.", ex); } }
/// <summary> /// Converts EDI interchange headers into message context. /// </summary> /// <param name="envelopes">The interchange headers.</param> /// <param name="interchangeContext">The interchange context.</param> private void ToContextEdifact(IEnumerable <string> envelopes, InterchangeContext interchangeContext) { try { var unh = envelopes.Single(es => es.StartsWith(EdiSegments.Unh)); var ediCompositeDataElements = EdiHelper.GetEdiCompositeDataElements(unh, interchangeContext); var ediDataElements = EdiHelper.GetEdiComponentDataElements(ediCompositeDataElements[1], interchangeContext); Tag = ediDataElements[0]; Version = ediDataElements[1] + ediDataElements[2]; Origin = null; Format = EdiFormats.Edifact; } catch (Exception ex) { throw new ParserException("Can't parse UNH segment.", ex); } }
/// <summary> /// Parses a data element /// </summary> /// <param name="parseTree">The dataelement grammar.</param> /// <param name="value">The dataelement line.</param> /// <param name="interchangeContext"></param> /// <returns>The parsed xml.</returns> private static XElement ParseElement(ParseTree parseTree, string value, InterchangeContext interchangeContext) { if (value == null) { throw new ArgumentNullException("value"); } if (!parseTree.IsComplex && !parseTree.IsSimple) { throw new Exception("Not a data element."); } XNamespace ns = interchangeContext.TargetNamespace; var result = new XElement(ns + parseTree.Name); if (parseTree.IsComplex) { if (value == string.Empty) { // Only deal with blank values for envelope headers if (parseTree.IsEnvelope) { foreach (var dataElement in parseTree.Children) { var element = new XElement(ns + dataElement.Name); element.SetValue(string.Empty); result.Add(element); } } } else { // Get the simple data elements var componentDataElements = EdiHelper.GetEdiComponentDataElements(value, interchangeContext).ToList(); // Index the composite data elements from the class definition var indexedGrammar = parseTree.Children.Select((g, p) => new { Grammar = g, Position = p }).ToList(); // Index the composite data elements from the edi string var indexedValues = componentDataElements.Select((v, p) => new { Value = v, Position = p }).ToList(); // This will try to parse the edi string into the class definition // Load a parse tree against each value // If there are more values in the edi string than in the class definition - they will be ignored // If there are less values in the edi string than in the class definition - it will throw an exception var indexedList = from ig in indexedGrammar from iv in indexedValues where ig.Position == iv.Position select new { ig.Position, iv.Value }; // Index the list so we can position each element //var indexed = componentDataElements.Select((a, i) => new {Item = a, Position = i}).ToList(); foreach (var dataElement in indexedList) { // Skip blank data elements otherwise this will produce blank XML nodes if (string.IsNullOrEmpty(dataElement.Value)) { if (!parseTree.IsEnvelope) { continue; } } // Handle the repetitions // If the children the the edi straing are more than the class definition, // Then the diff are considered repetitions of the last child in the class definition var objectToParse = dataElement.Position >= parseTree.Children.Count ? parseTree.Children.Last() : parseTree.Children[dataElement.Position]; var element = new XElement(ns + objectToParse.Name); // Set the value and escape to prevent faulty XML element.SetValue(SecurityElement.Escape(dataElement.Value) ?? string.Empty); result.Add(element); } } } else { // Prevent faulty XML result.SetValue(SecurityElement.Escape(value) ?? string.Empty); } return(result); }