示例#1
0
        void ProcessFile(DataSourceRecord record, string fileName)
        {
            Logger.Info(GetModuleName, "Started processing of file '" + fileName + "'");

            //Read file and convert it to list of strings
            string        inputFilePath          = Path.Combine(dataRootPath, record.Name, inputDir, fileName);
            string        errorMessageDataFormat = "There is an error while reading file '" + fileName + "'";
            List <string> rawStrings             = null;
            IDataFormat   dataFormat             = record.FormatRecord.FormatObj;

            try
            {
                rawStrings = dataFormat.ReadFile(inputFilePath, Logger, record.Encoding);
                if (rawStrings == null || rawStrings.Count == 0)
                {
                    throw new IOException(errorMessageDataFormat);
                }

                Logger.Info(dataFormat.GetModuleName, "Read " + rawStrings.Count + " lines from file '" + fileName + "'");
            }
            catch
            {
                Logger.Error(dataFormat.GetModuleName, errorMessageDataFormat);
                return;
            }

            //Parse list of strings to final view
            string          errorMessageSource = "There is an error while parsing file '" + fileName + "'";
            List <string[]> processedStrings   = null;
            IDataSource     dataSource         = record.SourceObj;

            try
            {
                processedStrings = dataSource.ParseStrings(rawStrings, Logger);
                if (processedStrings == null || processedStrings.Count == 0)
                {
                    throw new InvalidDataException(errorMessageSource);
                }
                Logger.Info(dataSource.GetModuleName, "Parsed " + processedStrings.Count + " lines from file '" + fileName + "'");
            }
            catch
            {
                Logger.Error(dataSource.GetModuleName, errorMessageSource);
                return;
            }

            //Write strings to output file, move input file to processed
            try
            {
                string outputFilePath    = Path.Combine(dataRootPath, record.Name, outputDir, fileName);
                string processedFilePath = Path.Combine(dataRootPath, record.Name, processedDir, fileName);

                string[] joinedStrings = new string[processedStrings.Count];
                for (int i = 0; i < processedStrings.Count; i++)
                {
                    joinedStrings[i] = string.Join(separator, processedStrings[i]);
                }

                //Check if output file exists
                string tempPath = outputFilePath;
                int    n        = 1;
                while (File.Exists(tempPath))
                {
                    tempPath = Path.Combine(Path.GetDirectoryName(outputFilePath),
                                            Path.GetFileNameWithoutExtension(outputFilePath) + "_" + n + Path.GetExtension(outputFilePath));
                    n++;
                }
                outputFilePath = tempPath;

                //Check if processed file exists
                tempPath = processedFilePath;
                n        = 1;
                while (File.Exists(tempPath))
                {
                    tempPath = Path.Combine(Path.GetDirectoryName(processedFilePath),
                                            Path.GetFileNameWithoutExtension(processedFilePath) + "_" + n + Path.GetExtension(processedFilePath));
                    n++;
                }
                processedFilePath = tempPath;

                if (outputEncoding == null)
                {
                    File.WriteAllLines(outputFilePath, joinedStrings);
                }
                else
                {
                    File.WriteAllLines(outputFilePath, joinedStrings, outputEncoding);
                }
                File.Move(inputFilePath, processedFilePath);
                Logger.Info(GetModuleName, "Written " + joinedStrings.Count() + " lines to file '" + Path.GetFileName(outputFilePath) + "'");
            }
            catch
            {
                Logger.Error(GetModuleName, "There is an error while writing processed file '" + fileName + "'");
                return;
            }
        }