示例#1
0
 public void Write <T>(
     IEnumerable <T> values,
     TextWriter stream,
     ExportFileDescription fileDescription)
 {
     WriteData <T>(values, null, stream, fileDescription);
 }
示例#2
0
 /// ///////////////////////////////////////////////////////////////////////
 /// FieldMapper
 ///
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="fileDescription"></param>
 public FieldMapperReading(
     ExportFileDescription fileDescription,
     string fileName,
     bool writingFile, ushort?Key)
     : base(fileDescription, fileName, writingFile, Key)
 {
 }
示例#3
0
        /*
         * public void Write<T>(
         * IEnumerable<T> values,
         * string fileName,
         * ExportFileDescription fileDescription)
         * {
         *   using (StreamWriter sw = new StreamWriter(
         *                                       fileName,
         *                                       false,
         *                                       fileDescription.TextEncoding))
         *   {
         *       WriteData<T>(values, fileName, sw, fileDescription);
         *   }
         * }
         *
         *
         * public void Write<T>(
         *   IEnumerable<T> values,
         *   TextWriter stream)
         * {
         *   Write<T>(values, stream, new ExportFileDescription());
         * }
         *
         * public void Write<T>(
         *   IEnumerable<T> values,
         *   string fileName)
         * {
         *   Write<T>(values, fileName, new ExportFileDescription());
         * }
         *
         * public void Write<T>(
         *   IEnumerable<T> values,
         *   TextWriter stream,
         *   ExportFileDescription fileDescription)
         * {
         *   WriteData<T>(values, null, stream, fileDescription);
         * }
         *
         */

        private void WriteData <T>(
            IEnumerable <T> values,
            string fileName,
            TextWriter stream,
            ExportFileDescription fileDescription, Func <int, bool> myMethodName = null)
        {
            FieldMapper <T> fm  = new FieldMapper <T>(fileDescription, fileName, true, Key);
            ExportStream    cs  = new ExportStream(null, stream, fileDescription);
            List <string>   row = new List <string>();


            // If first line has to carry the field names, write the field names now.
            if (fileDescription.FirstLineHasColumnNames)
            {
                fm.WriteNames(ref row);
                cs.WriteRow(row);
            }


            // -----
            int i = 0;

            foreach (T obj in values)
            {
                i++;
                if (myMethodName != null)
                {
                    myMethodName(i);
                }

                // Convert obj to row
                fm.WriteObject(obj, ref row);
                cs.WriteRow(row);
            }
        }
示例#4
0
 public void Write <T>(
     IEnumerable <T> values,
     TextWriter stream,
     ExportFileDescription fileDescription, Func <int, bool> myMethodName)
 {
     WriteData <T>(values, null, stream, fileDescription, myMethodName);
 }
示例#5
0
        public IEnumerable <T> Read <T>(string fileName, ExportFileDescription fileDescription) where T : class, new()
        {
            // Note that ReadData will not be called right away, but when the returned
            // IEnumerable<T> actually gets accessed.

            IEnumerable <T> ie = ReadData <T>(fileName, null, fileDescription);

            return(ie);
        }
示例#6
0
 public void Write <T>(
     IEnumerable <T> values,
     string fileName,
     ExportFileDescription fileDescription)
 {
     using (StreamWriter sw = new StreamWriter(
                fileName,
                false,
                fileDescription.TextEncoding))
     {
         WriteData <T>(values, fileName, sw, fileDescription);
     }
 }
示例#7
0
        public ExportStream(TextReader inStream, TextWriter outStream, ExportFileDescription fileDescription)
        {
            m_instream        = inStream;
            m_outStream       = outStream;
            m_fileDescription = fileDescription;

            if (fileDescription.SeparatorChar.HasValue)
            {
                m_SpecialChars = ("\"\x0A\x0D" + fileDescription.SeparatorChar.Value.ToString()).ToCharArray();
            }
            else
            {
                m_SpecialChars = ("\"\x0A\x0D").ToCharArray();
            }
            m_lineNbr = 1;
        }
示例#8
0
        public IEnumerable <T> ReadData <T>(
            string fileName,
            StreamReader stream,
            ExportFileDescription fileDescription) where T : class, new()
        {
            // If T implements IDataRow, then we're reading raw data rows
            bool readingRawDataRows = typeof(IDataRow).IsAssignableFrom(typeof(T));

#if DEBUG
            List <T> ret = new List <T>();
#endif

            // The constructor for FieldMapper_Reading will throw an exception if there is something
            // wrong with type T. So invoke that constructor before you open the file, because if there
            // is an exception, the file will not be closed.
            //
            // If T implements IDataRow, there is no need for a FieldMapper, because in that case we're returning
            // raw data rows.
            FieldMapperReading <T> fm = null;

            if (!readingRawDataRows)
            {
                fm = new FieldMapperReading <T>(fileDescription, fileName, false, Key);
            }

            // -------
            // Each time the IEnumerable<T> that is returned from this method is
            // accessed in a foreach, ReadData is called again (not the original Read overload!)
            //
            // So, open the file here, or rewind the stream.

            bool readingFile = !string.IsNullOrEmpty(fileName);

            if (readingFile)
            {
                stream = new StreamReader(
                    fileName,
                    fileDescription.TextEncoding,
                    fileDescription.DetectEncodingFromByteOrderMarks);
            }
            else
            {
                // Rewind the stream

                if ((stream == null) || (!stream.BaseStream.CanSeek))
                {
                    // throw new BadStreamException();
                    throw new Exception();
                }

                stream.BaseStream.Seek(0, SeekOrigin.Begin);
            }

            // ----------

            ExportStream cs = new ExportStream(stream, null, fileDescription);

            // If we're reading raw data rows, instantiate a T so we return objects
            // of the type specified by the caller.
            // Otherwise, instantiate a DataRow, which also implements IDataRow.
            IDataRow row = null;
            if (readingRawDataRows)
            {
                row = new T() as IDataRow;
            }
            else
            {
                row = new DataRow();
            }

            AggregatedException ae =
                new AggregatedException(typeof(T).ToString(), fileName, fileDescription.MaximumNbrExceptions);

            try
            {
                bool firstRow = true;
                while (cs.ReadRow(ref row))
                {
                    // Skip empty lines.
                    // Important. If there is a newline at the end of the last data line, the code
                    // thinks there is an empty line after that last data line.
                    if ((row.Count == 1) &&
                        ((row[0].Value == null) ||
                         (string.IsNullOrEmpty(row[0].Value.Trim()))))
                    {
                        continue;
                    }


                    fm.CheckValid(row, ae);

                    if (firstRow && fileDescription.FirstLineHasColumnNames)
                    {
                        if (!readingRawDataRows)
                        {
                            fm.ReadNames(row);
                        }
                    }
                    else
                    {
                        T obj = default(T);
                        try
                        {
                            if (readingRawDataRows)
                            {
                                obj = row as T;
                            }
                            else
                            {
                                obj = fm.ReadObject(row, ae);
                            }
                        }
                        catch (FatalFormatException fex)
                        {
                            throw fex;
                        }
                        catch (AggregatedException ae2)
                        {
                            // Seeing that the AggregatedException was thrown, maximum number of exceptions
                            // must have been reached, so rethrow.
                            // Catch here, so you don't add an AggregatedException to an AggregatedException
                            throw ae2;
                        }
                        catch (Exception ex)
                        {
                            // Store the exception in the AggregatedException ae.
                            // That way, if a file has many errors leading to exceptions,
                            // you get them all in one go, packaged in a single aggregated exception.
                            ae.AddException(ex);
                        }
#if DEBUG
                        ret.Add(obj);
#else
                        yield return(obj);
#endif
                    }
                    firstRow = false;
                }
            }
            finally
            {
                if (readingFile)
                {
                    stream.Close();
                }

                // If any exceptions were raised while reading the data from the file,
                // they will have been stored in the AggregatedException ae.
                // In that case, time to throw ae.
                //ae.ThrowIfExceptionsStored();
            }
#if DEBUG
            return(ret);
#endif
        }
示例#9
0
 public IEnumerable <T> Read <T>(StreamReader stream, ExportFileDescription fileDescription) where T : class, new()
 {
     return(ReadData <T>(null, stream, fileDescription));
 }
示例#10
0
        public FieldMapper(ExportFileDescription fileDescription, string fileName, bool writingFile, ushort?Key = null)
        {
            if (!fileDescription.IsValid)
            {
                throw new Exception("Description Invalid configuration.");
            }

            m_fileDescription = fileDescription;
            m_fileName        = fileName;

            m_ListColumn = new HashSet <FileColumnAttribute>();

            m_ListColumn.Clear();

            IEnumerable <FileColumnAttribute> ListOfAttribute = typeof(T).GetCustomAttributes(typeof(FileColumnAttribute), true).Cast <FileColumnAttribute>();

            if (ListOfAttribute.Count() < 1)
            {
                throw new Exception(string.Format("The class {0} don't have FileColumn declaration.", typeof(T).ToString()));
            }

            if (Key.HasValue)
            {
                ListOfAttribute = ListOfAttribute.Where(p => p.Key == Key.Value);
            }

            var listMember = typeof(T).GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
                             .Where(m => (m.MemberType == MemberTypes.Field) || (m.MemberType == MemberTypes.Property));

            IEnumerable <string> MissingAttr = listMember.Select(m => m.Name).Except(ListOfAttribute.Select(p => p.Property));

            //if (fileDescription.EnforceAllField && !ListOfAttribute.All(p => listMember.Any(v => v.Name == p.Property)))
            if (fileDescription.EnforceAllField && MissingAttr.Any())
            {
                throw new Exception("EnforceCsvColumnAttribute is true, but some Menbers don't get FileColumn");
            }

            foreach (FileColumnAttribute mi in ListOfAttribute)
            {
                if (fileDescription.FixColunm.HasValue && fileDescription.FixColunm.Value && mi.MaxLength == ushort.MaxValue)
                {
                    throw new Exception(string.Format("FixColunm is true, but {0} column missing MaxLength", mi.Property));
                }

                if (!fileDescription.SeparatorChar.HasValue && mi.MaxLength == ushort.MaxValue)
                {
                    throw new Exception(string.Format("SeparatorChar is false, but {0} column missing MaxLength", mi.Property));
                }

                if (mi.Property == null)
                {
                    if (fileDescription.AllowSkipColunm)
                    {
                        m_ListColumn.Add(mi);
                        continue;
                    }
                    else
                    {
                        throw new Exception("AllowSkipColunm is false, but some one column missing Property");
                    }
                }
                else if (mi.UsePropertyAsName && mi.Name == null)
                {
                    mi.Name = mi.Property;
                }

                var Field = listMember.Where(at => at.Name == mi.Property).FirstOrDefault();

                if (Field == null)
                {
                    throw new Exception(string.Format("The field {0} is missing on the class {1}", mi.Property, typeof(T).ToString()));
                }


                if (!m_fileDescription.FixColunm.HasValue && mi.MaxLength == UInt16.MaxValue)
                {
                    throw new Exception(string.Format("{0} don't have a Maxlength, needed for a file With/without separator in FixColunm", mi.Property));
                }


                if (!m_fileDescription.SeparatorChar.HasValue && mi.MaxLength == UInt16.MaxValue)
                {
                    throw new Exception(string.Format("{0} don't have a Maxlength, needed for a file without separator", mi.Property));
                }


                if (Field is PropertyInfo)
                {
                    mi.fieldType  = ((PropertyInfo)Field).PropertyType;
                    mi.MemberInfo = Field;
                }
                else
                {
                    mi.fieldType  = ((FieldInfo)Field).FieldType;
                    mi.MemberInfo = Field;
                }

#if NET45
                Type theType = mi.fieldType.IsGenericType && mi.fieldType.GenericTypeArguments.Any() ? mi.fieldType.GenericTypeArguments[0] : mi.fieldType;
#else
                Type theType = mi.fieldType.IsGenericType ? mi.fieldType.GetGenericTypeDefinition() : mi.fieldType;
#endif


                mi.parseNumberMethod = theType.GetMethod(
                    "Parse", new Type[] { typeof(String), typeof(NumberStyles), typeof(IFormatProvider) });
                mi.parseDateMethod = theType.GetMethod(
                    "ParseExact", new Type[] { typeof(String), typeof(String), typeof(IFormatProvider) });


                if (mi.parseNumberMethod == null && mi.parseDateMethod == null)
                {
                    mi.typeConverter = TypeDescriptor.GetConverter(mi.fieldType);
                }
                else
                {
                    mi.typeConverter = null;
                }

                if (!string.IsNullOrEmpty(mi.Name) && m_ListColumn.Any(c => c.Name == mi.Name))
                {
                    throw new Exception(string.Format("The Name '{0}' allready exist", mi.Name));
                }
                else
                {
                    m_ListColumn.Add(mi);
                }
            }

            m_Array = m_ListColumn.OrderBy(p => p.FieldIndex).ToArray();
        }