/// <summary> /// Returns the EDI for this component element, optionally specifying separator characters. /// </summary> /// <param name="options">An EdiOptions that specifies separator characters.</param> /// <returns>A string containing the EDI.</returns> public string ToString(EdiOptions options) { char segmentTerminator = options != null && options.SegmentTerminator.HasValue ? options.SegmentTerminator.Value : EdiOptions.DefaultSegmentTerminator; char elementSeparator = options != null && options.ElementSeparator.HasValue ? options.ElementSeparator.Value : EdiOptions.DefaultElementSeparator; char componentSeparator = options != null && options.ComponentSeparator.HasValue ? options.ComponentSeparator.Value : EdiOptions.DefaultComponentSeparator; if (options != null && options.ReleaseCharacter.HasValue) { return(_value.Replace(options.ReleaseCharacter.ToString(), options.ReleaseCharacter.ToString() + options.ReleaseCharacter.ToString()) .Replace(segmentTerminator.ToString(), options.ReleaseCharacter.ToString() + segmentTerminator) .Replace(elementSeparator.ToString(), options.ReleaseCharacter.ToString() + elementSeparator) .Replace(componentSeparator.ToString(), options.ReleaseCharacter.ToString() + componentSeparator)); } if (_value.IndexOf(segmentTerminator) != -1) { throw new FormatException(string.Format("'{0}' contains the segment terminator.", _value)); } if (_value.IndexOf(elementSeparator) != -1) { throw new FormatException(string.Format("'{0}' contains the element separator.", _value)); } if (options != null && options.RepetitionSeparator.HasValue && _value.IndexOf(options.RepetitionSeparator.Value) != -1) { throw new FormatException(string.Format("'{0}' contains the repetition separator.", _value)); } if (_value.IndexOf(componentSeparator) != -1) { throw new FormatException(string.Format("'{0}' contains the component separator.", _value)); } return(_value); }
/// <summary> /// Serialize this EdiDocument to a TextWriter. /// </summary> /// <param name="writer">A TextWriter that the EdiDocument will be written to.</param> public void Save(TextWriter writer) { var options = new EdiOptions(Options); foreach (EdiSegment segment in Segments) { if (segment.Id.Equals("ISA", StringComparison.OrdinalIgnoreCase)) { if (segment[11] != null && string.CompareOrdinal(segment[12], "00402") >= 0 && !char.IsLetterOrDigit(segment[11][0])) { options.RepetitionSeparator = segment[11][0]; } else { options.RepetitionSeparator = null; } if (segment[16] != null) { options.ComponentSeparator = segment[16][0]; } } writer.Write(segment.ToString(options)); if (options.AddLineBreaks) { writer.WriteLine(); } } writer.Flush(); }
/// <summary> /// Creates a new EdiDocument instance by using the specified stream, optionally specifying separator characters. /// </summary> /// <param name="stream">The stream that contains the EDI data.</param> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> /// <returns>An EdiDocument object that reads the data that is contained in the stream.</returns> public static EdiDocument Load(Stream stream, EdiOptions options = null) { using (var reader = new StreamReader(stream)) { string edi = reader.ReadToEnd(); return(new EdiDocument(edi, options)); } }
private EdiElement ParseElement(string rawElement, EdiOptions options) { var element = new EdiElement(); string[] repetitions = options.RepetitionSeparator.HasValue ? SplitEdi(rawElement, options.RepetitionSeparator.Value, options.ReleaseCharacter) : new[] { rawElement }; foreach (string rawRepetition in repetitions) { if (rawRepetition != string.Empty) { element.Repetitions.Add(ParseRepetition(rawRepetition, options)); } } return(element); }
/// <summary> /// Returns the EDI for this element, optionally specifying separator characters. /// </summary> /// <param name="options">An EdiOptions that specifies separator characters.</param> /// <returns>A string containing the EDI.</returns> public string ToString(EdiOptions options) { var edi = new StringBuilder(); for (int i = 0; i < Repetitions.Count; i++) { if (i > 0) { edi.Append(options != null && options.RepetitionSeparator.HasValue ? options.RepetitionSeparator.Value : EdiOptions.DefaultRepetitionSeparator); } edi.Append(Repetitions[i].ToString(options)); } return(edi.ToString()); }
private EdiRepetition ParseRepetition(string rawRepetition, EdiOptions options) { var repetition = new EdiRepetition(); string[] components = options.ComponentSeparator.HasValue ? SplitEdi(rawRepetition, options.ComponentSeparator.Value, options.ReleaseCharacter) : new[] { rawRepetition }; foreach (string rawComponent in components) { if (rawComponent != string.Empty) { repetition.Components.Add(new EdiComponent(options.ReleaseCharacter.HasValue ? UnescapeEdi(rawComponent, options.ReleaseCharacter.Value) : rawComponent)); } else { repetition.Components.Add(null); } } return(repetition); }
/// <summary> /// Returns the EDI for this element repetition, optionally specifying separator characters. /// </summary> /// <param name="options">An EdiOptions that specifies separator characters.</param> /// <returns>A string containing the EDI.</returns> public string ToString(EdiOptions options) { var edi = new StringBuilder(); int lastComponentIndex = GetLastComponentIndex(); for (int i = 0; i <= lastComponentIndex; i++) { if (i > 0) { edi.Append(options != null && options.ComponentSeparator.HasValue ? options.ComponentSeparator.Value : EdiOptions.DefaultComponentSeparator); } if (Components[i] == null) { continue; } edi.Append(Components[i].ToString(options)); } return(edi.ToString()); }
/// <summary> /// Returns the EDI for this segment, optionally specifying separator characters. /// </summary> /// <param name="options">An EdiOptions that specifies separator characters.</param> /// <returns>A string containing the EDI.</returns> public string ToString(EdiOptions options) { var edi = new StringBuilder(Id); if (Id.Equals("UNA", StringComparison.OrdinalIgnoreCase)) { edi.Append(options != null && options.ComponentSeparator.HasValue ? options.ComponentSeparator : EdiOptions.DefaultComponentSeparator) .Append(options != null && options.ElementSeparator.HasValue ? options.ElementSeparator : EdiOptions.DefaultElementSeparator) .Append(options != null && options.DecimalIndicator.HasValue ? options.DecimalIndicator : '.') .Append(options != null && options.ReleaseCharacter.HasValue ? options.ReleaseCharacter : ' ') .Append(' '); } else { int lastElementIndex = GetLastElementIndex(); for (int i = 0; i <= lastElementIndex; i++) { edi.Append(options != null && options.ElementSeparator.HasValue ? options.ElementSeparator : EdiOptions.DefaultElementSeparator); if (Elements[i] == null) { continue; } if (Id.Equals("ISA", StringComparison.OrdinalIgnoreCase) && Elements[i].Value.Length == 1 && (i == 15 && Elements[i].Value[0] == (options != null && options.ComponentSeparator.HasValue ? options.ComponentSeparator.Value : EdiOptions.DefaultComponentSeparator) || i == 10 && options != null && Elements[i].Value[0] == options.RepetitionSeparator) && Elements[i].Repetitions.Count == 1 && Elements[i].Components.Count == 1) { edi.Append(Elements[i].Value); } else { edi.Append(Elements[i].ToString(options)); } } } edi.Append(options != null && options.SegmentTerminator.HasValue ? options.SegmentTerminator : EdiOptions.DefaultSegmentTerminator); return(edi.ToString()); }
/// <summary> /// Returns the EDI for this component element, optionally specifying separator characters. /// </summary> /// <param name="options">An EdiOptions that specifies separator characters.</param> /// <returns>A string containing the EDI.</returns> public string ToString(EdiOptions options) { char segmentTerminator = options != null && options.SegmentTerminator.HasValue ? options.SegmentTerminator.Value : EdiOptions.DefaultSegmentTerminator; char elementSeparator = options != null && options.ElementSeparator.HasValue ? options.ElementSeparator.Value : EdiOptions.DefaultElementSeparator; char componentSeparator = options != null && options.ComponentSeparator.HasValue ? options.ComponentSeparator.Value : EdiOptions.DefaultComponentSeparator; if (options != null && options.ReleaseCharacter.HasValue) { return _value.Replace(options.ReleaseCharacter.ToString(), options.ReleaseCharacter.ToString() + options.ReleaseCharacter.ToString()) .Replace(segmentTerminator.ToString(), options.ReleaseCharacter.ToString() + segmentTerminator) .Replace(elementSeparator.ToString(), options.ReleaseCharacter.ToString() + elementSeparator) .Replace(componentSeparator.ToString(), options.ReleaseCharacter.ToString() + componentSeparator); } if (_value.IndexOf(segmentTerminator) != -1) throw new FormatException(string.Format("'{0}' contains the segment terminator.", _value)); if (_value.IndexOf(elementSeparator) != -1) throw new FormatException(string.Format("'{0}' contains the element separator.", _value)); if (options != null && options.RepetitionSeparator.HasValue && _value.IndexOf(options.RepetitionSeparator.Value) != -1) throw new FormatException(string.Format("'{0}' contains the repetition separator.", _value)); if (_value.IndexOf(componentSeparator) != -1) throw new FormatException(string.Format("'{0}' contains the component separator.", _value)); return _value; }
/// <summary> /// Creates a new EdiDocument from a TextReader, optionally specifying separator characters. /// </summary> /// <param name="reader">A TextReader that contains the content for the EdiDocument.</param> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> /// <returns>An EdiDocument that contains the contents of the specified TextReader.</returns> public static EdiDocument Load(TextReader reader, EdiOptions options = null) { string edi = reader.ReadToEnd(); return(new EdiDocument(edi, options)); }
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> /// Creates a new EdiDocument from a file, optionally specifying separator characters. /// </summary> /// <param name="fileName">A file name that references the file to load into a new EdiDocument.</param> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> /// <returns>An EdiDocument that contains the contents of the specified file.</returns> public static EdiDocument Load(string fileName, EdiOptions options = null) { string edi = File.ReadAllText(fileName); return(new EdiDocument(edi, options)); }
/// <summary> /// Creates a new EdiDocument from a file, optionally specifying separator characters. /// </summary> /// <param name="fileName">A file name that references the file to load into a new EdiDocument.</param> /// <param name="encoding">The character encoding to use.</param> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> /// <returns>An EdiDocument that contains the contents of the specified file.</returns> public static EdiDocument Load(string fileName, System.Text.Encoding encoding, EdiOptions options = null) { string edi = File.ReadAllText(fileName, encoding); return(new EdiDocument(edi, options)); }
/// <summary> /// Initializes a new instance of class EdiDocument, optionally specifying separator characters. /// </summary> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> public EdiDocument(EdiOptions options = null) { Options = options == null ? new EdiOptions() : new EdiOptions(options); Segments = new List <EdiSegment>(); }
/// <summary> /// Creates a new EdiDocument from a string, optionally specifying separator characters. /// </summary> /// <param name="edi">A string that contains EDI.</param> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> /// <returns>An EdiDocument populated from the string that contains EDI.</returns> public static EdiDocument Parse(string edi, EdiOptions options = null) { return(new EdiDocument(edi, options)); }
/// <summary> /// Returns the EDI for this element, optionally specifying separator characters. /// </summary> /// <param name="options">An EdiOptions that specifies separator characters.</param> /// <returns>A string containing the EDI.</returns> public string ToString(EdiOptions options) { var edi = new StringBuilder(); for (int i = 0; i < Repetitions.Count; i++) { if (i > 0) edi.Append(options != null && options.RepetitionSeparator.HasValue ? options.RepetitionSeparator.Value : EdiOptions.DefaultRepetitionSeparator); edi.Append(Repetitions[i].ToString(options)); } return edi.ToString(); }
/// <summary> /// Initializes a new instance of class EdiDocument, optionally specifying separator characters. /// </summary> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> public EdiDocument(EdiOptions options = null) { Options = options == null ? new EdiOptions() : new EdiOptions(options); Segments = new List<EdiSegment>(); }
private EdiDocument(XDocument xml) { Options = new EdiOptions(); Segments = new List <EdiSegment>(); LoadLoop(xml.Root); }
/// <summary> /// Creates a new EdiDocument from a file, optionally specifying separator characters. /// </summary> /// <param name="fileName">A file name that references the file to load into a new EdiDocument.</param> /// <param name="encoding">The character encoding to use.</param> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> /// <returns>An EdiDocument that contains the contents of the specified file.</returns> public static EdiDocument Load(string fileName, System.Text.Encoding encoding, EdiOptions options = null) { string edi = File.ReadAllText(fileName, encoding); return new EdiDocument(edi, options); }
private EdiElement ParseElement(string rawElement, EdiOptions options) { var element = new EdiElement(); string[] repetitions = options.RepetitionSeparator.HasValue ? SplitEdi(rawElement, options.RepetitionSeparator.Value, options.ReleaseCharacter) : new[] {rawElement}; foreach (string rawRepetition in repetitions) { if (rawRepetition != string.Empty) element.Repetitions.Add(ParseRepetition(rawRepetition, options)); } return element; }
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> /// Creates a new EdiDocument instance by using the specified stream, optionally specifying separator characters. /// </summary> /// <param name="stream">The stream that contains the EDI data.</param> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> /// <returns>An EdiDocument object that reads the data that is contained in the stream.</returns> public static EdiDocument Load(Stream stream, EdiOptions options = null) { using (var reader = new StreamReader(stream)) { string edi = reader.ReadToEnd(); return new EdiDocument(edi, options); } }
/// <summary> /// Creates a new EdiDocument from a TextReader, optionally specifying separator characters. /// </summary> /// <param name="reader">A TextReader that contains the content for the EdiDocument.</param> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> /// <returns>An EdiDocument that contains the contents of the specified TextReader.</returns> public static EdiDocument Load(TextReader reader, EdiOptions options = null) { string edi = reader.ReadToEnd(); return new EdiDocument(edi, options); }
/// <summary> /// Creates a new EdiDocument from a file, optionally specifying separator characters. /// </summary> /// <param name="fileName">A file name that references the file to load into a new EdiDocument.</param> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> /// <returns>An EdiDocument that contains the contents of the specified file.</returns> public static EdiDocument Load(string fileName, EdiOptions options = null) { string edi = File.ReadAllText(fileName); return new EdiDocument(edi, options); }
private EdiDocument(XDocument xml) { Options = new EdiOptions(); Segments = new List<EdiSegment>(); LoadLoop(xml.Root); }
/// <summary> /// Returns the EDI for this element repetition, optionally specifying separator characters. /// </summary> /// <param name="options">An EdiOptions that specifies separator characters.</param> /// <returns>A string containing the EDI.</returns> public string ToString(EdiOptions options) { var edi = new StringBuilder(); int lastComponentIndex = GetLastComponentIndex(); for (int i = 0; i <= lastComponentIndex; i++) { if (i > 0) edi.Append(options != null && options.ComponentSeparator.HasValue ? options.ComponentSeparator.Value : EdiOptions.DefaultComponentSeparator); if (Components[i] == null) continue; edi.Append(Components[i].ToString(options)); } return edi.ToString(); }
private EdiRepetition ParseRepetition(string rawRepetition, EdiOptions options) { var repetition = new EdiRepetition(); string[] components = options.ComponentSeparator.HasValue ? SplitEdi(rawRepetition, options.ComponentSeparator.Value, options.ReleaseCharacter) : new[] {rawRepetition}; foreach (string rawComponent in components) { if (rawComponent != string.Empty) repetition.Components.Add(new EdiComponent(options.ReleaseCharacter.HasValue ? UnescapeEdi(rawComponent, options.ReleaseCharacter.Value) : rawComponent)); else repetition.Components.Add(null); } return repetition; }
/// <summary> /// Creates a new EdiDocument from a string, optionally specifying separator characters. /// </summary> /// <param name="edi">A string that contains EDI.</param> /// <param name="options">An EdiOptions containing separator characters to use when saving this document.</param> /// <returns>An EdiDocument populated from the string that contains EDI.</returns> public static EdiDocument Parse(string edi, EdiOptions options = null) { return new EdiDocument(edi, options); }
/// <summary> /// Serialize this EdiDocument to a TextWriter. /// </summary> /// <param name="writer">A TextWriter that the EdiDocument will be written to.</param> public void Save(TextWriter writer) { var options = new EdiOptions(Options); foreach (EdiSegment segment in Segments) { if (segment.Id.Equals("ISA", StringComparison.OrdinalIgnoreCase)) { if (segment[11] != null && string.CompareOrdinal(segment[12], "00402") >= 0 && !char.IsLetterOrDigit(segment[11][0])) options.RepetitionSeparator = segment[11][0]; else options.RepetitionSeparator = null; if (segment[16] != null) options.ComponentSeparator = segment[16][0]; } writer.Write(segment.ToString(options)); if (options.AddLineBreaks) writer.WriteLine(); } writer.Flush(); }
/// <summary> /// Returns the EDI for this segment, optionally specifying separator characters. /// </summary> /// <param name="options">An EdiOptions that specifies separator characters.</param> /// <returns>A string containing the EDI.</returns> public string ToString(EdiOptions options) { var edi = new StringBuilder(Id); if (Id.Equals("UNA", StringComparison.OrdinalIgnoreCase)) { edi.Append(options != null && options.ComponentSeparator.HasValue ? options.ComponentSeparator : EdiOptions.DefaultComponentSeparator) .Append(options != null && options.ElementSeparator.HasValue ? options.ElementSeparator : EdiOptions.DefaultElementSeparator) .Append(options != null && options.DecimalIndicator.HasValue ? options.DecimalIndicator : '.') .Append(options != null && options.ReleaseCharacter.HasValue ? options.ReleaseCharacter : ' ') .Append(' '); } else { int lastElementIndex = GetLastElementIndex(); for (int i = 0; i <= lastElementIndex; i++) { edi.Append(options != null && options.ElementSeparator.HasValue ? options.ElementSeparator : EdiOptions.DefaultElementSeparator); if (Elements[i] == null) continue; if (Id.Equals("ISA", StringComparison.OrdinalIgnoreCase) && Elements[i].Value.Length == 1 && (i == 15 && Elements[i].Value[0] == (options != null && options.ComponentSeparator.HasValue ? options.ComponentSeparator.Value : EdiOptions.DefaultComponentSeparator) || i == 10 && options != null && Elements[i].Value[0] == options.RepetitionSeparator) && Elements[i].Repetitions.Count == 1 && Elements[i].Components.Count == 1) { edi.Append(Elements[i].Value); } else edi.Append(Elements[i].ToString(options)); } } edi.Append(options != null && options.SegmentTerminator.HasValue ? options.SegmentTerminator : EdiOptions.DefaultSegmentTerminator); return edi.ToString(); }