public EdifactWriter(Stream stream, Encoding encoding, EdifactCharacterSet edifactCharacterSet, char replacementCharacter, bool normalize = true)
     : base(stream, encoding)
 {
     this._fallbackChar = replacementCharacter;
     this._edifactCharacterSet = edifactCharacterSet;
     this._normalize = normalize;
 }
Example #2
0
 public EdifactWriter(Stream stream, Encoding encoding, EdifactCharacterSet edifactCharacterSet, char replacementCharacter, bool normalize = true)
     : base(stream, encoding)
 {
     this._fallbackChar        = replacementCharacter;
     this._edifactCharacterSet = edifactCharacterSet;
     this._normalize           = normalize;
 }
        public EdifactReader(Stream stream, Encoding encoding, EdifactCharacterSet charSet, char fallbackChar, bool normalize = true, bool removeControlChars = false)
            : base(stream, encoding)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (!stream.CanSeek)
            {
                throw new ArgumentNullException("stream");
            }

            this._removeControlChars = removeControlChars;
            this._fallbackChar       = fallbackChar;
            this._charSet            = charSet;
            this._normalize          = normalize;

            EdifactCharacterSet messageCharSet;

            if (TryReadEdiCharacterSet(stream, out messageCharSet, encoding))
            {
                this._charSet = messageCharSet;
            }
        }
Example #4
0
        internal static string ToSerializedValue(this EdifactCharacterSet value)
        {
            switch (value)
            {
            case EdifactCharacterSet.NotSpecified:
                return("NotSpecified");

            case EdifactCharacterSet.UNOB:
                return("UNOB");

            case EdifactCharacterSet.UNOA:
                return("UNOA");

            case EdifactCharacterSet.UNOC:
                return("UNOC");

            case EdifactCharacterSet.UNOD:
                return("UNOD");

            case EdifactCharacterSet.UNOE:
                return("UNOE");

            case EdifactCharacterSet.UNOF:
                return("UNOF");

            case EdifactCharacterSet.UNOG:
                return("UNOG");

            case EdifactCharacterSet.UNOH:
                return("UNOH");

            case EdifactCharacterSet.UNOI:
                return("UNOI");

            case EdifactCharacterSet.UNOJ:
                return("UNOJ");

            case EdifactCharacterSet.UNOK:
                return("UNOK");

            case EdifactCharacterSet.UNOX:
                return("UNOX");

            case EdifactCharacterSet.UNOY:
                return("UNOY");

            case EdifactCharacterSet.KECA:
                return("KECA");
            }
            return(null);
        }
 /// <summary>
 /// Initializes a new instance of the EdifactFramingSettings class.
 /// </summary>
 /// <param name="protocolVersion">The protocol version.</param>
 /// <param name="dataElementSeparator">The data element
 /// separator.</param>
 /// <param name="componentSeparator">The component separator.</param>
 /// <param name="segmentTerminator">The segment terminator.</param>
 /// <param name="releaseIndicator">The release indicator.</param>
 /// <param name="repetitionSeparator">The repetition separator.</param>
 /// <param name="characterSet">The EDIFACT frame setting characterSet.
 /// Possible values include: 'NotSpecified', 'UNOB', 'UNOA', 'UNOC',
 /// 'UNOD', 'UNOE', 'UNOF', 'UNOG', 'UNOH', 'UNOI', 'UNOJ', 'UNOK',
 /// 'UNOX', 'UNOY', 'KECA'</param>
 /// <param name="decimalPointIndicator">The EDIFACT frame setting
 /// decimal indicator. Possible values include: 'NotSpecified',
 /// 'Comma', 'Decimal'</param>
 /// <param name="segmentTerminatorSuffix">The EDIFACT frame setting
 /// segment terminator suffix. Possible values include: 'NotSpecified',
 /// 'None', 'CR', 'LF', 'CRLF'</param>
 /// <param name="serviceCodeListDirectoryVersion">The service code list
 /// directory version.</param>
 /// <param name="characterEncoding">The character encoding.</param>
 public EdifactFramingSettings(int protocolVersion, int dataElementSeparator, int componentSeparator, int segmentTerminator, int releaseIndicator, int repetitionSeparator, EdifactCharacterSet characterSet, EdifactDecimalIndicator decimalPointIndicator, SegmentTerminatorSuffix segmentTerminatorSuffix, string serviceCodeListDirectoryVersion = default(string), string characterEncoding = default(string))
 {
     ServiceCodeListDirectoryVersion = serviceCodeListDirectoryVersion;
     CharacterEncoding       = characterEncoding;
     ProtocolVersion         = protocolVersion;
     DataElementSeparator    = dataElementSeparator;
     ComponentSeparator      = componentSeparator;
     SegmentTerminator       = segmentTerminator;
     ReleaseIndicator        = releaseIndicator;
     RepetitionSeparator     = repetitionSeparator;
     CharacterSet            = characterSet;
     DecimalPointIndicator   = decimalPointIndicator;
     SegmentTerminatorSuffix = segmentTerminatorSuffix;
     CustomInit();
 }
        //public override int Read(char[] buffer, int index, int count)
        //{

        //    int readCount = base.Read(buffer, index, count);
        //    for (int i = index; i < readCount + index; i++)
        //    {
        //        buffer[i] = Clean(buffer[i]);

        //    }
        //    return readCount;
        //}

        private static bool TryReadEdiCharacterSet(Stream stream, out EdifactCharacterSet edifactCharacterSet, Encoding encoding)
        {
            edifactCharacterSet = EdifactCharacterSet.UNOA;

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var position = stream.Position;

            stream.Seek(0, SeekOrigin.Begin);

            try
            {
                var    sr = new StreamReader(stream, encoding);
                string line;

                while ((line = sr.ReadLine()) != null)
                {
                    var unoIndex = line.IndexOf("UNO");

                    if (unoIndex == -1 || line.Length < unoIndex + 4)
                    {
                        return(false);
                    }
                    var uno = line.Substring(unoIndex, 4);

                    if (Enum.TryParse <EdifactCharacterSet>(uno, out edifactCharacterSet))
                    {
                        return(true);
                        //var regex = new Regex(Regex.Escape(charsetIn));
                        //line = regex.Replace(line, TargetCharacterSet.ToString(), 1);
                    }
                }
            }
            catch
            {
            }
            finally
            {
                stream.Seek(position, SeekOrigin.Begin);
            }

            return(false);
        }
        public static char Translate(this char ch, EdifactCharacterSet charSet, char fallbackChar)
        {
            switch (charSet)
            {
            case EdifactCharacterSet.UNOA:
                return(ch.ToUnoa(fallbackChar));

            case EdifactCharacterSet.UNOB:
                return(ch.ToUnob(fallbackChar));

            case EdifactCharacterSet.UNOC:
                return(ch.ToUnoc(fallbackChar));

            case EdifactCharacterSet.UNOD:
                return(ch.ToUnoc(fallbackChar));

            case EdifactCharacterSet.UNOE:
                return(ch.ToUnoe(fallbackChar));

            case EdifactCharacterSet.UNOF:
                return(ch.ToUnof(fallbackChar));

            case EdifactCharacterSet.UNOG:
                return(ch.ToUnog(fallbackChar));

            case EdifactCharacterSet.UNOH:
                return(ch.ToUnoh(fallbackChar));

            case EdifactCharacterSet.UNOI:
                return(ch.ToUnoi(fallbackChar));

            case EdifactCharacterSet.UNOJ:
                return(ch.ToUnoj(fallbackChar));

            case EdifactCharacterSet.UNOK:
                return(ch.ToUnok(fallbackChar));

            case EdifactCharacterSet.UNOX:
            case EdifactCharacterSet.UNOY:
            case EdifactCharacterSet.KECA:
                return(ch);

            default:
                return(ch);
            }
        }
        public EdifactReader(Stream stream, Encoding encoding, EdifactCharacterSet charSet, char fallbackChar, bool normalize = true, bool removeControlChars = false)
            : base(stream, encoding)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");

            if (!stream.CanSeek)
                throw new ArgumentNullException("stream");

            this._removeControlChars = removeControlChars;
            this._fallbackChar = fallbackChar;
            this._charSet = charSet;
            this._normalize = normalize;

            EdifactCharacterSet messageCharSet;
            if (TryReadEdiCharacterSet(stream, out messageCharSet, encoding))
            {
                this._charSet = messageCharSet;
            }
        }
        //public override int Read(char[] buffer, int index, int count)
        //{
        //    int readCount = base.Read(buffer, index, count);
        //    for (int i = index; i < readCount + index; i++)
        //    {
        //        buffer[i] = Clean(buffer[i]);
        //    }
        //    return readCount;
        //}
        private static bool TryReadEdiCharacterSet(Stream stream, out EdifactCharacterSet edifactCharacterSet, Encoding encoding)
        {
            edifactCharacterSet = EdifactCharacterSet.UNOA;

            if (stream == null)
                throw new ArgumentNullException("stream");

            var position = stream.Position;
            stream.Seek(0, SeekOrigin.Begin);

            try
            {
                var sr = new StreamReader(stream, encoding);
                string line;

                while ((line = sr.ReadLine()) != null)
                {
                    var unoIndex = line.IndexOf("UNO");

                    if (unoIndex == -1 || line.Length < unoIndex + 4)
                    {
                        return false;
                    }
                    var uno = line.Substring(unoIndex, 4);

                    if (Enum.TryParse<EdifactCharacterSet>(uno, out edifactCharacterSet))
                    {
                        return true;
                        //var regex = new Regex(Regex.Escape(charsetIn));
                        //line = regex.Replace(line, TargetCharacterSet.ToString(), 1);
                    }

                }
            }
            catch
            {
            }
            finally
            {
                stream.Seek(position, SeekOrigin.Begin);

            }

            return false;
        }
        /// <summary>
        /// Non-streaming replace :)
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="encoding"></param>
        /// <param name="charSet"></param>
        /// <returns></returns>
        private static Stream ReplaceCharSet(Stream stream, Encoding encoding, EdifactCharacterSet charSet)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");

            var position = stream.Position;
            stream.Seek(0, SeekOrigin.Begin);

            using (var sr = new StreamReader(stream, encoding))
            {
                var edifact = sr.ReadToEnd();

                var unoIndex = edifact.IndexOf("UNO", StringComparison.Ordinal);

                if (unoIndex >= 0 && edifact.Length > 3)
                {
                    var charsetInString = edifact.Substring(unoIndex, 4);
                    EdifactCharacterSet charSetIn;

                    if (Enum.TryParse<EdifactCharacterSet>(charsetInString, out charSetIn))
                    {
                        var regex = new Regex(Regex.Escape(charsetInString));
                        edifact = regex.Replace(edifact, charSet.ToString(), 1);
                    }
                }

                var result = new MemoryStream();
                var writer = new StreamWriter(result);
                writer.Write(edifact);
                writer.Flush();
                result.Position = 0;
                return result;
            }
        }
 public static char Translate(this char ch, EdifactCharacterSet charSet, char fallbackChar)
 {
     switch (charSet)
     {
         case EdifactCharacterSet.UNOA:
             return ch.ToUnoa(fallbackChar);
         case EdifactCharacterSet.UNOB:
             return ch.ToUnob(fallbackChar);
         case EdifactCharacterSet.UNOC:
             return ch.ToUnoc(fallbackChar);
         case EdifactCharacterSet.UNOD:
             return ch.ToUnoc(fallbackChar);
         case EdifactCharacterSet.UNOE:
             return ch.ToUnoe(fallbackChar);
         case EdifactCharacterSet.UNOF:
             return ch.ToUnof(fallbackChar);
         case EdifactCharacterSet.UNOG:
             return ch.ToUnog(fallbackChar);
         case EdifactCharacterSet.UNOH:
             return ch.ToUnoh(fallbackChar);
         case EdifactCharacterSet.UNOI:
             return ch.ToUnoi(fallbackChar);
         case EdifactCharacterSet.UNOJ:
             return ch.ToUnoj(fallbackChar);
         case EdifactCharacterSet.UNOK:
             return ch.ToUnok(fallbackChar);
         case EdifactCharacterSet.UNOX:
         case EdifactCharacterSet.UNOY:
         case EdifactCharacterSet.KECA:
             return ch;
         default:
             return ch;
     }
 }