Beispiel #1
0
        public ChoYamlReader(Stream inStream, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(inStream, "Stream");

            Configuration = configuration;
            Init();

            if (inStream is MemoryStream)
            {
                _textReader = new Lazy <TextReader>(() => new StreamReader(inStream));
            }
            else
            {
                _textReader = new Lazy <TextReader>(() =>
                {
                    if (Configuration.DetectEncodingFromByteOrderMarks == null)
                    {
                        return(new StreamReader(inStream, Configuration.GetEncoding(inStream), false, Configuration.BufferSize));
                    }
                    else
                    {
                        return(new StreamReader(inStream, Encoding.Default, Configuration.DetectEncodingFromByteOrderMarks.Value, Configuration.BufferSize));
                    }
                });
            }
            //_closeStreamOnDispose = true;
        }
Beispiel #2
0
        public static string ToText <TRec>(TRec record, ChoYamlRecordConfiguration configuration = null, TraceSwitch traceSwitch = null, string yamlPath = null)
        {
            if (record is DataTable)
            {
                StringBuilder xml = new StringBuilder();
                using (var w = new ChoYamlWriter(xml, configuration))
                    w.Write(record as DataTable);
                return(xml.ToString());
            }
            else if (record is IDataReader)
            {
                StringBuilder xml = new StringBuilder();
                using (var w = new ChoYamlWriter(xml, configuration))
                    w.Write(record as IDataReader);
                return(xml.ToString());
            }

            if (configuration == null)
            {
                configuration = new ChoYamlRecordConfiguration();
            }

            if (configuration.SingleDocument == null)
            {
                configuration.SingleDocument = true;
            }
            return(ToTextAll(ChoEnumerable.AsEnumerable <TRec>(record), configuration, traceSwitch, yamlPath));
        }
Beispiel #3
0
        public ChoYamlReader(YamlDocument yamlDoc, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(yamlDoc, "YamlDoc");

            Configuration = configuration;
            Init();
            _yamlDocObjects = new YamlDocument[] { yamlDoc };
        }
Beispiel #4
0
        public ChoYamlReader(YamlNode yamlNode, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(yamlNode, "YamlNode");

            Configuration = configuration;
            Init();
            _yamlObjects = new YamlNode[] { yamlNode };
        }
Beispiel #5
0
        public ChoYamlReader(IEnumerable <YamlDocument> yamlDocObjects, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(yamlDocObjects, "YamlDocObjects");

            Configuration = configuration;
            Init();
            _yamlDocObjects = yamlDocObjects;
        }
Beispiel #6
0
        public ChoYamlReader(YamlStream yamlStream, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(yamlStream, "YamlStream");

            Configuration = configuration;
            Init();

            _yamlStream = yamlStream;
        }
Beispiel #7
0
        public ChoYamlWriter(TextWriter textWriter, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(textWriter, "TextWriter");

            Configuration = configuration;
            Init();

            _textWriter = textWriter;
        }
Beispiel #8
0
        public ChoYamlReader(TextReader textReader, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(textReader, "TextReader");

            Configuration = configuration;
            Init();

            _textReader = new Lazy <TextReader>(() => textReader);
        }
Beispiel #9
0
        public static T LoadYamlNode(YamlNode yamlObject, ChoYamlRecordConfiguration configuration = null)
        {
            if (yamlObject == null)
            {
                return(default(T));
            }

            return(LoadYamlNodes(new YamlNode[] { yamlObject }, configuration).FirstOrDefault());
        }
Beispiel #10
0
        public static string ToTextAll <TRec>(IEnumerable <TRec> records, ChoYamlRecordConfiguration configuration = null, TraceSwitch traceSwitch = null, string yamlPath = null)
        {
            if (records == null)
            {
                return(null);
            }

            if (typeof(DataTable).IsAssignableFrom(typeof(TRec)))
            {
                StringBuilder json = new StringBuilder();

                foreach (var dt in records.Take(1))
                {
                    using (var w = new ChoYamlWriter(json, configuration))
                    {
                        w.Write(dt);
                    }
                }

                return(json.ToString());
            }
            else if (typeof(IDataReader).IsAssignableFrom(typeof(TRec)))
            {
                StringBuilder json = new StringBuilder();

                foreach (var dt in records.Take(1))
                {
                    using (var w = new ChoYamlWriter(json, configuration))
                    {
                        w.Write(dt);
                    }
                }

                return(json.ToString());
            }


            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var parser = new ChoYamlWriter <TRec>(writer, configuration)
                        {
                            TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitch : traceSwitch
                        })
                        {
                            parser.Configuration.YamlPath = yamlPath;

                            parser.Write(records);
                            parser.Close();
                            writer.Flush();

                            stream.Position = 0;

                            return(reader.ReadToEnd());
                        }
        }
Beispiel #11
0
        public ChoYamlWriter(string filePath, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNullOrEmpty(filePath, "FilePath");

            Configuration = configuration;

            Init();

            _textWriter           = new StreamWriter(ChoPath.GetFullPath(filePath), false, Configuration.Encoding, Configuration.BufferSize);
            _closeStreamOnDispose = true;
        }
Beispiel #12
0
        public ChoYamlReader(string filePath, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNullOrEmpty(filePath, "FilePath");

            Configuration = configuration;

            Init();

            _textReader           = new Lazy <TextReader>(() => new StreamReader(filePath, Configuration.GetEncoding(filePath), false, Configuration.BufferSize));
            _closeStreamOnDispose = true;
        }
Beispiel #13
0
        public ChoYamlWriter(string filePath, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNullOrEmpty(filePath, "FilePath");

            Configuration = configuration;

            Init();

            _textWriter           = new Lazy <TextWriter>(() => new StreamWriter(filePath, Configuration.Append, Configuration.Encoding, Configuration.BufferSize));
            _closeStreamOnDispose = true;
        }
Beispiel #14
0
        public ChoYamlReader(string filePath, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNullOrEmpty(filePath, "FilePath");

            Configuration = configuration;

            Init();

            _textReader           = new StreamReader(filePath);
            _closeStreamOnDispose = true;
        }
Beispiel #15
0
        private void Init()
        {
            var recordType = typeof(T).GetUnderlyingType();

            if (Configuration == null)
            {
                Configuration = new ChoYamlRecordConfiguration(recordType);
            }

            _writer              = new ChoYamlRecordWriter(recordType, Configuration);
            _writer.Writer       = this;
            _writer.RowsWritten += NotifyRowsWritten;
        }
Beispiel #16
0
        public ChoYamlWriter(Stream inStream, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(inStream, "Stream");

            Configuration = configuration;
            Init();
            if (inStream is MemoryStream)
            {
                _textWriter = new StreamWriter(inStream);
            }
            else
            {
                _textWriter = new StreamWriter(inStream, Configuration.Encoding, Configuration.BufferSize);
            }
            //_closeStreamOnDispose = true;
        }
Beispiel #17
0
        public ChoYamlReader(Stream inStream, ChoYamlRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(inStream, "Stream");

            Configuration = configuration;
            Init();

            if (inStream is MemoryStream)
            {
                _textReader = new Lazy <TextReader>(() => new StreamReader(inStream));
            }
            else
            {
                _textReader = new Lazy <TextReader>(() => new StreamReader(inStream, Configuration.GetEncoding(inStream), false, Configuration.BufferSize));
            }
            //_closeStreamOnDispose = true;
        }
Beispiel #18
0
        internal void Validate(ChoYamlRecordConfiguration config)
        {
            try
            {
                if (FieldName.IsNullOrWhiteSpace())
                {
                    FieldName = Name;
                }

                //if (YamlPath.IsNullOrWhiteSpace())
                //    throw new ChoRecordConfigurationException("Missing XPath.");
                if (FillChar != null)
                {
                    if (FillChar.Value == ChoCharEx.NUL)
                    {
                        throw new ChoRecordConfigurationException("Invalid '{0}' FillChar specified.".FormatString(FillChar));
                    }
                }

                if (Size != null && Size.Value <= 0)
                {
                    throw new ChoRecordConfigurationException("Size must be > 0.");
                }
                if (ErrorMode == null)
                {
                    ErrorMode = config.ErrorMode; // config.ErrorMode;
                }
                if (IgnoreFieldValueMode == null)
                {
                    IgnoreFieldValueMode = config.IgnoreFieldValueMode;
                }
                if (QuoteField == null)
                {
                    QuoteField = config.QuoteAllFields;
                }
            }
            catch (Exception ex)
            {
                throw new ChoRecordConfigurationException("Invalid configuration found at '{0}' field.".FormatString(Name), ex);
            }
        }
Beispiel #19
0
        private void Init()
        {
            _enumerator = new Lazy <IEnumerator <T> >(() => GetEnumerator());

            var recordType = typeof(T).GetUnderlyingType();

            if (Configuration == null)
            {
                Configuration = new ChoYamlRecordConfiguration(recordType);
            }
            else
            {
                Configuration.RecordType = recordType;
            }
            Configuration.IsDynamicObject = Configuration.RecordType.IsDynamicType();

            if (!ChoETLFrxBootstrap.IsSandboxEnvironment)
            {
                _prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
                System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture;
            }
        }
Beispiel #20
0
        public ChoYamlRecordWriter(Type recordType, ChoYamlRecordConfiguration configuration) : base(recordType, true)
        {
            ChoGuard.ArgumentNotNull(configuration, "Configuration");
            Configuration = configuration;

            _callbackRecordWrite         = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyRecordWrite>(recordType);
            _callbackFileWrite           = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyFileWrite>(recordType);
            _callbackRecordFieldWrite    = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyRecordFieldWrite>(recordType);
            _callbackRecordSeriablizable = ChoMetadataObjectCache.CreateMetadataObject <IChoRecordFieldSerializable>(recordType);
            System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture;

            //_recBuffer = new Lazy<List<object>>(() =>
            //{
            //    if (Writer != null)
            //    {
            //        var b = Writer.Context.ContainsKey("RecBuffer") ? Writer.Context.RecBuffer : null;
            //        if (b == null)
            //            Writer.Context.RecBuffer = new List<object>();

            //        return Writer.Context.RecBuffer;
            //    }
            //    else
            //        return new List<object>();
            //}, true);

            //Configuration.Validate();

            BeginWrite = new Lazy <bool>(() =>
            {
                TextWriter sw = _sw as TextWriter;
                if (sw != null)
                {
                    return(RaiseBeginWrite(sw));
                }

                return(false);
            });
        }
Beispiel #21
0
        internal static string ToText(object rec, ChoYamlRecordConfiguration configuration, Encoding encoding, int bufferSize, TraceSwitch traceSwitch = null)
        {
            if (rec is DataTable)
            {
                StringBuilder json = new StringBuilder();
                using (var w = new ChoYamlWriter(json, configuration))
                {
                    w.Write(rec as DataTable);
                }
                return(json.ToString());
            }
            else if (rec is IDataReader)
            {
                StringBuilder json = new StringBuilder();
                using (var w = new ChoYamlWriter(json, configuration))
                {
                    w.Write(rec as IDataReader);
                }
                return(json.ToString());
            }

            ChoYamlRecordWriter writer = new ChoYamlRecordWriter(rec.GetType(), configuration);

            writer.TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitchOff : traceSwitch;

            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var sw = new StreamWriter(stream, configuration.Encoding, configuration.BufferSize))
                    {
                        writer.WriteTo(sw, new object[] { rec }).Loop();
                        sw.Flush();
                        stream.Position = 0;

                        return(reader.ReadToEnd());
                    }
        }
Beispiel #22
0
        public static ChoYamlReader <T> LoadYamlNodes(IEnumerable <YamlNode> yamlObjects, ChoYamlRecordConfiguration configuration = null)
        {
            var r = new ChoYamlReader <T>(yamlObjects, configuration);

            return(r);
        }
Beispiel #23
0
 public ChoYamlWriter(ChoYamlRecordConfiguration configuration = null)
 {
     Configuration = configuration;
     Init();
 }
Beispiel #24
0
 public ChoYamlWriter(StringBuilder sb, ChoYamlRecordConfiguration configuration = null) : this(new StringWriter(sb), configuration)
 {
 }
Beispiel #25
0
 public static ChoYamlReader <T> LoadText(string inputText, ChoYamlRecordConfiguration configuration = null)
 {
     return(LoadText(inputText, null, configuration));
 }
Beispiel #26
0
        public static ChoYamlReader <T> LoadText(string inputText, Encoding encoding, ChoYamlRecordConfiguration configuration = null)
        {
            var r = new ChoYamlReader <T>(inputText.ToStream(encoding), configuration);

            r._closeStreamOnDispose = true;

            return(r);
        }
Beispiel #27
0
 public ChoYamlReader(StringBuilder sb, ChoYamlRecordConfiguration configuration = null) : this(new StringReader(sb.ToString()), configuration)
 {
 }