Exemple #1
0
        private Either <Error, byte[]> ExportFromXml(IList <object> listItems, string xmlConfiguration,
                                                     int?numberOfHeader = 0, int?numberOfFooter = 0)
        {
            using (var xmlConfigStream = xmlConfiguration.ToStream())
            {
                var factory = StreamFactory.NewInstance();
                factory.Load(xmlConfigStream);

                var text   = new StringWriter();
                var writer = factory.CreateWriter(Constants.DefaultExportStream, text);

                // write header default values
                for (var headerI = 0; headerI < numberOfHeader; ++headerI)
                {
                    writer.Write(Constants.DefaultExportHeaderRecord + headerI.ToString(), null);
                }

                foreach (var item in listItems)
                {
                    writer.Write(item);
                }

                // write footer default values
                for (var footerI = 0; footerI < numberOfFooter; ++footerI)
                {
                    writer.Write(Constants.DefaultExportFooterRecord + footerI.ToString(), null);
                }

                writer.Flush();

                var result = Encoding.UTF8.GetBytes(text.ToString());
                return(Right <Error, byte[]>(result));
            }
        }
        public Either <Error, ImmutableList <T> > Parse(byte[] content)
        {
            return(ShouldNotNullOrEmpty(content)
                   .ToEither()
                   .Match(c =>
            {
                if (_fileLoaderService != null)
                {
                    content = _fileLoaderService.Transform(content);
                }

                using (var xmlConfigStream = new MemoryStream(Encoding.UTF8.GetBytes(_xmlConfiguration)))
                    using (var contentStream = new MemoryStream(content))
                    {
                        var factory = StreamFactory.NewInstance();
                        factory.Load(xmlConfigStream);
                        var reader = factory.CreateReader(Constants.DefaultImportStream,
                                                          new StreamReader(contentStream));
                        var listResult = new List <object>();
                        object record = null;

                        while ((record = reader.Read()) != null)
                        {
                            var castedRecord = record;
                            listResult.Add(castedRecord);
                        }

                        if (_beanMapper != null)
                        {
                            return _beanMapper
                            .Map(listResult.Cast <object>())
                            .Match(CastToExpectedType, error => error);
                        }
                        else if (_mapperSrcText != null && _mapperSrcText.SourceText.IsNotNullOrEmpty())
                        {
                            var dynamicMapperService = new DynamicBeanMapperService();
                            return dynamicMapperService
                            .MapFromSource(listResult.Cast <object>(), _mapperSrcText.SourceText)
                            .Match(CastToExpectedType, error => error);
                        }
                        else
                        {
                            return Right <Error, ImmutableList <T> >(listResult.Cast <T>().ToImmutableList());
                        }
                    }
            }, errors => errors.Join()));
        }
Exemple #3
0
 protected TestBasis()
 {
     _factory = StreamFactory.NewInstance();
     _factory.Load(Meldungen.LoadMeldungen());
     _namespace = GetType().Namespace;
 }