Merge Writer and Controller, add Xml Writer
This commit is contained in:
@@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Remove="Model\NewFile1.cs" />
|
<Compile Remove="Model\NewFile1.cs" />
|
||||||
<Compile Remove="Model\Tag.cs" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml;
|
||||||
|
|
||||||
namespace BoerseDataConvert
|
namespace BoerseDataConvert
|
||||||
{
|
{
|
||||||
@@ -12,36 +13,60 @@ namespace BoerseDataConvert
|
|||||||
private static int count;
|
private static int count;
|
||||||
private static string cur_fileName;
|
private static string cur_fileName;
|
||||||
private TagsTable tagsTable;
|
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;
|
count = 1;
|
||||||
cur_fileName = fileName;
|
address = adr;
|
||||||
|
cur_fileName = fileName.Split('.').First();
|
||||||
|
Directory.CreateDirectory(address);
|
||||||
tagsTable = new TagsTable(tags);
|
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)
|
public static void NextFile(string fileName)
|
||||||
{
|
{
|
||||||
|
writer.WriteEndElement();
|
||||||
|
writer.WriteEndDocument();
|
||||||
|
writer.Close();
|
||||||
|
cur_fileName = fileName.Split('.').First();
|
||||||
count = 1;
|
count = 1;
|
||||||
cur_fileName = fileName;
|
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();
|
writer.WriteStartElement("record");
|
||||||
xmlRecord.Append($" <record id=\"{count}\">\n");
|
writer.WriteAttributeString("id", null, count.ToString());
|
||||||
foreach (var tagValue in record)
|
foreach (var tagValue in record)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string tag = CheckTagValue(tagValue.Key, tagValue.Value);
|
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)
|
catch (ArgumentException e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e.Message);
|
Console.WriteLine(e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xmlRecord.Append($" </record>");
|
writer.WriteEndElement();
|
||||||
count++;
|
count++;
|
||||||
return xmlRecord.ToString();
|
}
|
||||||
|
public static void EndFile()
|
||||||
|
{
|
||||||
|
writer.WriteEndElement();
|
||||||
|
writer.WriteEndDocument();
|
||||||
|
writer.Close();
|
||||||
}
|
}
|
||||||
private string CheckTagValue(int tag, string value)
|
private string CheckTagValue(int tag, string value)
|
||||||
{
|
{
|
||||||
@@ -50,7 +75,7 @@ namespace BoerseDataConvert
|
|||||||
string tagname = tagsTable.GetTagName(tag);
|
string tagname = tagsTable.GetTagName(tag);
|
||||||
if (value != "NULL" && tagsTable.HaveValueRanges(tag))//Checks if the tag have not a value ranges
|
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}");
|
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
|
else//Checks if the tag have a value ranges
|
||||||
{
|
{
|
||||||
string[] valueRange = tagLine[2].Split('#').ToArray();
|
if(!tagsTable.CheckValidValue(tag,value))throw new ArgumentException($"WARN: Value not in range \"{tag}\", \"{value}\", {cur_fileName} line {count + 1}");
|
||||||
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}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return tagname;
|
return tagname;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -6,8 +7,30 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace BoerseDataConvert
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,11 +44,11 @@ namespace BoerseDataConvert
|
|||||||
{
|
{
|
||||||
return table[tag].HaveValueRanges;
|
return table[tag].HaveValueRanges;
|
||||||
}
|
}
|
||||||
public int GetMaxValueLengthToBig(int tag)
|
public int GetMaxValueLength(int tag)
|
||||||
{
|
{
|
||||||
return table[tag].StringLength;
|
return table[tag].StringLength;
|
||||||
}
|
}
|
||||||
public bool CheckStringLength(int tag, int value)
|
public bool CheckStringLengthToBig(int tag, int value)
|
||||||
{
|
{
|
||||||
return table[tag].StringLength<value;
|
return table[tag].StringLength<value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,28 +44,26 @@ namespace BoerseDataConvert
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine(e.Message);
|
Console.Error.WriteLine(e.Message);
|
||||||
Environment.Exit();
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// only read filenames
|
// only read filenames
|
||||||
string[] fileNames = Directory.GetFiles(inputDir).Select(x => x.Split('\\', '/').Last()).ToArray();
|
string[] fileNames = Directory.GetFiles(inputDir).Select(x => x.Split('\\', '/').Last()).ToArray();
|
||||||
|
|
||||||
Reader reader = new Reader(inputDir, fileNames);
|
Reader reader = new Reader(inputDir, fileNames);
|
||||||
Writer writer = new Writer(outputDir, fileNames[0]);
|
RecordController a = new RecordController(outputDir,fileNames[0], tags);
|
||||||
RecordController a = new RecordController(fileNames[0], tags);
|
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Record record = reader.ReadLineRecord();
|
Record record = reader.ReadLineRecord();
|
||||||
string s = a.ConvertToXml(record);
|
a.WriteXmlRecord(record);
|
||||||
writer.WriteRecord(s);
|
|
||||||
}
|
}
|
||||||
catch (IndexOutOfRangeException)
|
catch (IndexOutOfRangeException)
|
||||||
{
|
{
|
||||||
Reader.EndFile();
|
Reader.EndFile();
|
||||||
Writer.EndFile();
|
RecordController.EndFile();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ namespace BoerseDataConvert
|
|||||||
EndFile();
|
EndFile();
|
||||||
reader = new StreamReader($@"{adr}/{filesNames[fileInd]}", CodePagesEncodingProvider.Instance.GetEncoding(1252));
|
reader = new StreamReader($@"{adr}/{filesNames[fileInd]}", CodePagesEncodingProvider.Instance.GetEncoding(1252));
|
||||||
RecordController.NextFile(filesNames[fileInd]);
|
RecordController.NextFile(filesNames[fileInd]);
|
||||||
Writer.NextFile(filesNames[fileInd]);
|
|
||||||
s = reader.ReadLine();
|
s = reader.ReadLine();
|
||||||
s = reader.ReadLine();
|
s = reader.ReadLine();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user