Merge Writer and Controller, add Xml Writer

This commit is contained in:
lastvoidtemplar
2021-07-10 11:20:41 +03:00
parent 97692e08ae
commit cd9a43c218
7 changed files with 66 additions and 80 deletions

View File

@@ -7,7 +7,6 @@
<ItemGroup>
<Compile Remove="Model\NewFile1.cs" />
<Compile Remove="Model\Tag.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace BoerseDataConvert
{
@@ -12,36 +13,60 @@ namespace BoerseDataConvert
private static int count;
private static string cur_fileName;
private TagsTable tagsTable;
public RecordController(string fileName, string tags)
private static string address;
private static XmlWriter writer;
public RecordController(string adr,string fileName, string tags)
{
count = 1;
cur_fileName = fileName;
address = adr;
cur_fileName = fileName.Split('.').First();
Directory.CreateDirectory(address);
tagsTable = new TagsTable(tags);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create($@"{address}/{cur_fileName}.xml", settings);
writer.WriteStartDocument();
writer.WriteStartElement("table");
writer.WriteAttributeString("name", null, "file");
}
public static void NextFile(string fileName)
{
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
cur_fileName = fileName.Split('.').First();
count = 1;
cur_fileName = fileName;
writer.WriteStartDocument();
writer.WriteStartElement("table");
writer.WriteAttributeString("name", null, "file");
}
public string ConvertToXml(Record record)
public void WriteXmlRecord (Record record)
{
StringBuilder xmlRecord = new StringBuilder();
xmlRecord.Append($" <record id=\"{count}\">\n");
writer.WriteStartElement("record");
writer.WriteAttributeString("id", null, count.ToString());
foreach (var tagValue in record)
{
try
{
string tag = CheckTagValue(tagValue.Key, tagValue.Value);
xmlRecord.Append($" <{tag}>{tagValue.Value}</{tag}>\n");
writer.WriteStartElement(tag);
writer.WriteValue(tagValue.Value);
writer.WriteEndElement();
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
}
xmlRecord.Append($" </record>");
writer.WriteEndElement();
count++;
return xmlRecord.ToString();
}
public static void EndFile()
{
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
private string CheckTagValue(int tag, string value)
{
@@ -50,7 +75,7 @@ namespace BoerseDataConvert
string tagname = tagsTable.GetTagName(tag);
if (value != "NULL" && tagsTable.HaveValueRanges(tag))//Checks if the tag have not a value ranges
{
if (tagsTable.IsString(tag) && tagsTable.CheckStringLength(tag,value.Length))//Checks if value type is string
if (tagsTable.IsString(tag) && tagsTable.CheckStringLengthToBig(tag,value.Length))//Checks if value type is string
{
throw new ArgumentException($"WARN: Too long value \"{tag}\", \"{value}\", max allowed \"{tagsTable.GetTagName(tag)}\", {cur_fileName} line {count + 1}");
}
@@ -69,21 +94,7 @@ namespace BoerseDataConvert
}
else//Checks if the tag have a value ranges
{
string[] valueRange = tagLine[2].Split('#').ToArray();
bool countain = false;
if (value == "") return tagname;
for (int i = 0; i < valueRange.Length; i++)
{
if (valueRange[i] == value)//Checks if the value is in value ranges
{
countain = true;
break;
}
}
if (!countain)
{
throw new ArgumentException($"WARN: Value not in range \"{tag}\", \"{value}\", {cur_fileName} line {count + 1}");
}
if(!tagsTable.CheckValidValue(tag,value))throw new ArgumentException($"WARN: Value not in range \"{tag}\", \"{value}\", {cur_fileName} line {count + 1}");
}
return tagname;
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -6,8 +7,30 @@ using System.Threading.Tasks;
namespace BoerseDataConvert
{
public class Record
public class Record : IEnumerable
{
public List<KeyValuePair<int, string>>TagsValues;
private List<KeyValuePair<int, string>> TagsValues;
public Record()
{
TagsValues = new List<KeyValuePair<int, string>>();
}
public void Add(int tag, string value)
{
TagsValues.Add(new KeyValuePair<int, string>(tag, value));
}
public IEnumerator<KeyValuePair<int, string>> GetEnumerator()
{
foreach (var item in TagsValues)
{
yield return item;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}

View File

@@ -44,11 +44,11 @@ namespace BoerseDataConvert
{
return table[tag].HaveValueRanges;
}
public int GetMaxValueLengthToBig(int tag)
public int GetMaxValueLength(int tag)
{
return table[tag].StringLength;
}
public bool CheckStringLength(int tag, int value)
public bool CheckStringLengthToBig(int tag, int value)
{
return table[tag].StringLength<value;
}

View File

@@ -44,28 +44,26 @@ namespace BoerseDataConvert
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
Environment.Exit();
Environment.Exit(0);
}
// only read filenames
string[] fileNames = Directory.GetFiles(inputDir).Select(x => x.Split('\\', '/').Last()).ToArray();
Reader reader = new Reader(inputDir, fileNames);
Writer writer = new Writer(outputDir, fileNames[0]);
RecordController a = new RecordController(fileNames[0], tags);
RecordController a = new RecordController(outputDir,fileNames[0], tags);
while (true)
{
try
{
Record record = reader.ReadLineRecord();
string s = a.ConvertToXml(record);
writer.WriteRecord(s);
a.WriteXmlRecord(record);
}
catch (IndexOutOfRangeException)
{
Reader.EndFile();
Writer.EndFile();
RecordController.EndFile();
break;
}
}

View File

@@ -31,7 +31,6 @@ namespace BoerseDataConvert
EndFile();
reader = new StreamReader($@"{adr}/{filesNames[fileInd]}", CodePagesEncodingProvider.Instance.GetEncoding(1252));
RecordController.NextFile(filesNames[fileInd]);
Writer.NextFile(filesNames[fileInd]);
s = reader.ReadLine();
s = reader.ReadLine();
}

View File

@@ -1,44 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BoerseDataConvert
{
public class Writer
{
private static string curFilesName;
private static StreamWriter writer;
private static string address;
public Writer(string _address, string filesName)
{
address = _address;
curFilesName = filesName;
Directory.CreateDirectory(address);
string[] file = curFilesName.Split('.').ToArray();
writer = new StreamWriter($@"{address}/{file[0]}.xml");
writer.WriteLine($"<table name=\"{file[0]}\">");
}
public void WriteRecord(string record)
{
writer.WriteLine(record);
}
public static void NextFile(string fileName)
{
EndFile();
curFilesName = fileName;
string[] file = curFilesName.Split('.').ToArray();
writer = new StreamWriter($@"{address}/{file[0]}.xml");
writer.WriteLine($"<table name=\"{file[0]}\">");
}
public static void EndFile()
{
writer.WriteLine("</table>");
writer.Close();
}
}
}