/// <summary> /// Initializes a new instance of the <see cref="InterchangeContext"/> class. /// This gets the default separators per format. /// </summary> /// <param name="format">The format</param> public InterchangeContext(EdiFormats format) { switch (format) { case EdiFormats.Edifact: ComponentDataElementSeparator = ":"; DataElementSeparator = "+"; ReleaseIndicator = "?"; RepetitionSeparator = "*"; SegmentTerminator = "'"; Format = EdiFormats.Edifact; break; case EdiFormats.X12: ComponentDataElementSeparator = ":"; DataElementSeparator = "*"; ReleaseIndicator = ""; RepetitionSeparator = "^"; SegmentTerminator = "~"; Format = EdiFormats.X12; break; default: throw new ParserException("Unsupported format: " + format); } }
/// <summary> /// Initializes a new instance of the <see cref="SegmentContext"/> class. /// </summary> /// <param name="ediSegment">The edi segment.</param> /// <param name="interchangeContext">The interchange context.</param> /// <param name="format">The edi format.</param> public SegmentContext(string ediSegment, InterchangeContext interchangeContext, EdiFormats format) { if (string.IsNullOrEmpty(ediSegment)) throw new ArgumentNullException("ediSegment"); if (interchangeContext == null) throw new ArgumentNullException("interchangeContext"); // Blank segments shouldn't have gone that far var splitted = ediSegment.Split(interchangeContext.DataElementSeparator.ToCharArray(), StringSplitOptions.None); if (splitted.Length < 2) throw new Exception("Segment is blank."); Name = splitted[0]; Value = null; ParentId = null; // UNA segments don't have values if (ediSegment.StartsWith(EdiSegments.Una)) { Name = EdiSegments.Una; } // Handle blank segments, e.g. BHT+' if (format == EdiFormats.Hipaa) { var splittedElement = splitted[1].Split(interchangeContext.ComponentDataElementSeparator.ToCharArray(), StringSplitOptions.None); Value = splittedElement[0]; if (ediSegment.StartsWith(EdiSegments.Hl) && !string.IsNullOrEmpty(splitted[2])) ParentId = splitted[2]; } }
/// <summary> /// Converts a message context into system type. /// </summary> /// <param name="format">Message format.</param> /// <param name="version">Message version.</param> /// <param name="tag">Message tag.</param> /// <param name="origin">Message origin.</param> /// <param name="definitionsAssemblyName">The assembly name of the project containing the classes and XSD.</param> /// <returns> /// The system type. /// </returns> private Type ToSystemType(EdiFormats format, string version, string tag, string origin, string definitionsAssemblyName) { if (string.IsNullOrEmpty(version)) { throw new NullReferenceException("version"); } if (string.IsNullOrEmpty(tag)) { throw new NullReferenceException("tag"); } var typeFullName = "EdiFabric.Definitions" + "." + format + "_" + version + "_" + tag; if (!string.IsNullOrEmpty(origin)) { typeFullName = typeFullName + "_" + origin; } typeFullName = typeFullName + ".M_" + tag; typeFullName = typeFullName + ", " + definitionsAssemblyName; var systemType = Type.GetType(typeFullName); if (systemType == null) { throw new ParserException(string.Format("Can't find type for type name = {0} .Please ensure that the correct class was added to the definitions project and that you pointed to the definitions project in your .config file.", typeFullName)); } return(systemType); }
/// <summary> /// Converts message context into system type /// </summary> /// <param name="format">Message format.</param> /// <param name="version">Message version.</param> /// <param name="tag">Message tag.</param> /// <param name="origin">Message origin.</param> /// <returns> /// The system type. /// </returns> private Type ToSystemType(EdiFormats format, string version, string tag, string origin) { const string definitions = "EdiFabric.Definitions"; if (string.IsNullOrEmpty(version)) { throw new NullReferenceException("version"); } if (string.IsNullOrEmpty(tag)) { throw new NullReferenceException("tag"); } var typeFullName = definitions + "." + format + "_" + version + "_" + tag; if (!string.IsNullOrEmpty(origin)) { typeFullName = typeFullName + "_" + origin; } typeFullName = typeFullName + ".M_" + tag; typeFullName = typeFullName + ", " + definitions; var systemType = Type.GetType(typeFullName); if (systemType == null) { throw new ParserException(string.Format("Can't find type for type name = {0}", typeFullName)); } return(systemType); }
public InterchangeContext(StreamReader _streamReader) { this._streamReader = _streamReader; ComponentDataElementSeparator = ":"; DataElementSeparator = "+"; ReleaseIndicator = "?"; RepetitionSeparator = "*"; SegmentTerminator = "'"; Format = EdiFormats.Edifact; }
/// <summary> /// Initializes a new instance of the <see cref="InterchangeContext"/> class. /// This extracts the separators from the contents of the edi message. /// </summary> /// <param name="contents">The edi message</param> public InterchangeContext(string contents) { if (contents == null) { throw new ArgumentNullException(nameof(contents)); } contents = contents.Replace(Environment.NewLine, string.Empty); var firstSegmentName = contents.ToUpper().Substring(0, 3); switch (firstSegmentName) { case EdiSegments.Unb: // Default Edifact separators ComponentDataElementSeparator = ":"; DataElementSeparator = "+"; ReleaseIndicator = "?"; RepetitionSeparator = "*"; SegmentTerminator = "'"; Format = EdiFormats.Edifact; break; case EdiSegments.Una: try { // Parse UNA separators var una = contents.Replace(EdiSegments.Una, "").Take(6).ToList(); ComponentDataElementSeparator = una[0].ToString(CultureInfo.InvariantCulture); DataElementSeparator = una[1].ToString(CultureInfo.InvariantCulture); ReleaseIndicator = una[3].ToString(CultureInfo.InvariantCulture); RepetitionSeparator = "*"; SegmentTerminator = una[5].ToString(CultureInfo.InvariantCulture); Format = EdiFormats.Edifact; } catch (Exception ex) { throw new ParserException("Can't find UNA interchange delimiters", ex); } break; default: throw new ParserException("Can't identify format by: " + firstSegmentName); } }
/// <summary> /// Validates an interchange context. /// Separators must be unique within the interchange context. /// </summary> /// <returns>If it is valid</returns> public bool IsValid(EdiFormats format) { if (format == EdiFormats.Edifact) { if (Format != EdiFormats.Edifact) { return(false); } } if (format == EdiFormats.X12) { if (Format != EdiFormats.X12) { return(false); } } var temp = new List <string>(); if (!string.IsNullOrEmpty(ComponentDataElementSeparator)) { temp.Add(ComponentDataElementSeparator); } if (!string.IsNullOrEmpty(DataElementSeparator)) { temp.Add(DataElementSeparator); } if (!string.IsNullOrEmpty(RepetitionSeparator)) { temp.Add(RepetitionSeparator); } if (!string.IsNullOrEmpty(SegmentTerminator)) { temp.Add(SegmentTerminator); } var b = temp.GroupBy(t => t).Where(g => g.Count() > 1).Select(v => v.Key).ToList(); return(!b.Any()); }
/// <summary> /// Initializes a new instance of the <see cref="SegmentContext"/> class. /// </summary> /// <param name="ediSegment">The EDI segment.</param> /// <param name="interchangeContext">The interchange context.</param> /// <param name="format">The EDI format.</param> public SegmentContext(string ediSegment, InterchangeContext interchangeContext, EdiFormats format) { if (string.IsNullOrEmpty(ediSegment)) { throw new ArgumentNullException("ediSegment"); } if (interchangeContext == null) { throw new ArgumentNullException("interchangeContext"); } // Blank segments shouldn't have gone that far var splitted = ediSegment.Split(interchangeContext.DataElementSeparator.ToCharArray(), StringSplitOptions.None); if (splitted.Length < 2) { throw new Exception("Segment is blank."); } Name = splitted[0]; FirstValue = null; SecondValue = null; ParentId = null; // UNA segments don't have values if (ediSegment.StartsWith(EdiSegments.Una)) { Name = EdiSegments.Una; } // Handle blank segments, e.g. BHT+' if (format == EdiFormats.Hipaa) { var splittedElement = splitted[1].Split(interchangeContext.ComponentDataElementSeparator.ToCharArray(), StringSplitOptions.None); FirstValue = splittedElement[0]; if (splitted.Length > 2) { var splittedSecondElement = splitted[2].Split(interchangeContext.ComponentDataElementSeparator.ToCharArray(), StringSplitOptions.None); SecondValue = splittedSecondElement[0]; } if (ediSegment.StartsWith(EdiSegments.Hl) && !string.IsNullOrEmpty(splitted[2])) { ParentId = splitted[2]; } } }
/// <summary> /// Converts message context into system type /// </summary> /// <param name="format">Message format.</param> /// <param name="version">Message version.</param> /// <param name="tag">Message tag.</param> /// <param name="origin">Message origin.</param> /// <returns> /// The system type. /// </returns> private Type ToSystemType(EdiFormats format, string version, string tag, string origin) { const string definitions = "EdiFabric.Definitions"; if (string.IsNullOrEmpty(version)) throw new NullReferenceException("version"); if (string.IsNullOrEmpty(tag)) throw new NullReferenceException("tag"); var typeFullName = definitions + "." + format + "_" + version + "_" + tag; if (!string.IsNullOrEmpty(origin)) typeFullName = typeFullName + "_" + origin; typeFullName = typeFullName + ".M_" + tag; typeFullName = typeFullName + ", " + definitions; var systemType = Type.GetType(typeFullName); if (systemType == null) throw new ParserException(string.Format("Can't find type for type name = {0}", typeFullName)); return systemType; }