List <DataSourceRecord> LoadDataSources(DataTable sourcesTable, List <DataFormatRecord> dataFormatsList) { var dataSourcesList = new List <DataSourceRecord>(); foreach (DataRow line in sourcesTable.Rows) { var record = new DataSourceRecord(); record.Name = line.Field <string>("Name").Trim(); if (!dataSourcesList.Contains(record)) { record.Id = line.Field <int>("Id"); record.DllFileName = line.Field <string>("DllFileName").Trim(); string dllPath = Path.Combine(dataSourcesDllsPath, record.DllFileName); record.SourceObj = LoadDllAndCreateObjectInstance <IDataSource>(dllPath); record.FormatRecord = dataFormatsList.First((e) => e.Id == line.Field <int>("DataFormatId")); if (record.FormatRecord.FormatObj == null) { string message = "Can't find specific DataFormat for DataSource with name " + record.Name; Logger.Critical(GetModuleName, message); throw new KeyNotFoundException(); } //Parse encoding of files string encLine = line.Field <string>("Encoding"); if (encLine != null && encLine.Length > 0) { Encoding enc = Encoding.GetEncoding(encLine.Trim().ToLowerInvariant()); if (enc == null) { string errMsg = "It seems there is an error in defenition of encoding for data source '" + record.Name + "'"; Logger.Critical(GetModuleName, errMsg); throw new SyntaxErrorException(errMsg); } else { record.Encoding = enc; } } else { record.Encoding = null; } dataSourcesList.Add(record); } } return(dataSourcesList); }
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; } }