public Encoding GetEncoding(string fileName)
        {
            if (_encoding == null)
            {
                try
                {
                    ChoETLLog.Info("Determining '{0}' file encoding...".FormatString(fileName));
                    Encoding = ChoFile.GetEncodingFromFile(fileName);
                    ChoETLLog.Info("Found '{1}' encoding in '{0}' file.".FormatString(fileName, Encoding));
                }
                catch (Exception ex)
                {
                    Encoding = Encoding.UTF8;
                    ChoETLLog.Error("Error finding encoding in '{0}' file. Default to UTF8.".FormatString(fileName));
                    ChoETLLog.Error(ex.Message);
                }
            }

            return(Encoding);
        }
        public Encoding GetEncoding(Stream inStream)
        {
            if (_encoding == null)
            {
                try
                {
                    ChoETLLog.Info("Determining file encoding...");
                    Encoding = ChoFile.GetEncodingFromStream(inStream);
                    ChoETLLog.Info("Found {0} encoding in file.".FormatString(Encoding));
                }
                catch (Exception ex)
                {
                    Encoding = Encoding.UTF8;
                    ChoETLLog.Error("Error finding encoding in file. Default to UTF8.");
                    ChoETLLog.Error(ex.Message);
                }
            }

            return(Encoding);
        }
        internal void Validate(string line)
        {
            if (line.IsNullOrWhiteSpace())
            {
                throw new ChoHL7Exception("Empty line found.");
            }

            if (!line.StartsWith("MSH"))
            {
                throw new ChoHL7Exception("Missing Message Header (MSH) record.");
            }
            if (line.Length <= 3 || line[3] == ChoCharEx.NUL)
            {
                throw new ChoHL7Exception("Missing or NULL Field Separator found.");
            }
            else
            {
                FieldSeparator = line[3];
            }

            if (FieldSeparator.ToString().IsAlphaNumeric())
            {
                throw new ChoHL7Exception("Invalid '{0}' field separator found.".FormatString(FieldSeparator));
            }

            if (line.Length <= 8)
            {
                throw new ChoHL7Exception("Missing encoding characters.");
            }
            else
            {
                var encodingChars = line.Substring(4, 4);
                if (encodingChars != @"^~\&")
                {
                    throw new ChoHL7Exception("Invalid encoding characters found.");
                }
                if (encodingChars.Where(c => c == ChoCharEx.NUL).Any())
                {
                    throw new ChoHL7Exception("NULL encoding characters found.");
                }
            }

            string[] fields = line.Split(FieldSeparator);

            if (fields.Length < 11 || fields[11].IsNullOrWhiteSpace())
            {
                throw new ChoHL7Exception("Missing version.");
            }

            string versionTxt = fields[11];
            var    v          = versionTxt.ToEnum(typeof(ChoHL7Version));

            if (v == null)
            {
                throw new ChoHL7Exception("'{0}' version not supported.".FormatString(versionTxt));
            }

            if (LoadWithVersion == null)
            {
                Version = (ChoHL7Version)v;
                ChoETLLog.Info("Version found in message: {0}".FormatString(Version));
            }
            else
            {
                ChoETLLog.Info("Version found in message: {0}".FormatString((ChoHL7Version)v));
                Version = LoadWithVersion.Value;
                ChoETLLog.Info("But loading message with version: {0}".FormatString(Version));
            }

            if (fields[8].IsNullOrWhiteSpace())
            {
                throw new ChoHL7Exception("Missing Message Type.");
            }
            string mtText    = fields[8];
            var    mtEnumTxt = mtText.ToEnum(typeof(ChoHL7MessageType));

            if (mtEnumTxt == null)
            {
                throw new ChoHL7Exception("'{0}' Message Type not supported.".FormatString(mtText));
            }

            if (LoadWithMessageType == null)
            {
                MessageType = (ChoHL7MessageType)mtEnumTxt;
                ChoETLLog.Info("MessageType found in message: {0}".FormatString(MessageType));
            }
            else
            {
                ChoETLLog.Info("MessageType found in message: {0}".FormatString((ChoHL7MessageType)mtEnumTxt));
                MessageType = LoadWithMessageType.Value;
                ChoETLLog.Info("But loading message with MessageType: {0}".FormatString(MessageType));
            }
        }