-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
Расширен функционал чтения файлов дистрибутивов ФИАС
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System.Collections; | ||
using System.Xml; | ||
using YPermitin.FIASToolSet.DistributionReader.Models; | ||
|
||
namespace YPermitin.FIASToolSet.DistributionReader.DataReaders; | ||
|
||
public class NormativeDocKindReader : IEnumerable<NormativeDocKind> | ||
{ | ||
private readonly string _dataFilePath; | ||
private NormativeDocKindEnumerator _enumerator; | ||
|
||
public NormativeDocKindReader(string dataFilePath) | ||
Check warning on line 12 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs GitHub Actions / build
Check warning on line 12 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs GitHub Actions / build
|
||
{ | ||
_dataFilePath = dataFilePath; | ||
} | ||
|
||
public IEnumerator<NormativeDocKind> GetEnumerator() | ||
{ | ||
if (_enumerator == null) | ||
{ | ||
_enumerator = new NormativeDocKindEnumerator(_dataFilePath); | ||
} | ||
|
||
return _enumerator; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
public class NormativeDocKindEnumerator : IEnumerator<NormativeDocKind> | ||
{ | ||
private readonly string _dataFilePath; | ||
|
||
private NormativeDocKind _current; | ||
|
||
private XmlReader _reader; | ||
private XmlReader Reader { | ||
get | ||
{ | ||
if (_reader == null) | ||
{ | ||
_reader = XmlReader.Create(_dataFilePath); | ||
} | ||
return _reader; | ||
} | ||
} | ||
|
||
public NormativeDocKindEnumerator(string dataFilePath) | ||
Check warning on line 50 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs GitHub Actions / build
Check warning on line 50 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs GitHub Actions / build
Check warning on line 50 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs GitHub Actions / build
|
||
{ | ||
_dataFilePath = dataFilePath; | ||
} | ||
|
||
public NormativeDocKind Current => _current; | ||
|
||
object IEnumerator.Current => Current; | ||
|
||
public bool MoveNext() | ||
{ | ||
while (Reader.Read()) | ||
{ | ||
if (Reader.Name == "NDOCKIND") | ||
{ | ||
var idValue = Reader.GetAttribute("ID"); | ||
var nameValue = Reader.GetAttribute("NAME"); | ||
|
||
if(int.TryParse(idValue, out int id)) | ||
{ | ||
var newObject = new NormativeDocKind(id, nameValue); | ||
Check warning on line 70 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs GitHub Actions / build
|
||
_current = newObject; | ||
return true; | ||
} | ||
else | ||
{ | ||
_current = null; | ||
} | ||
} | ||
} | ||
|
||
DisposeXmlReader(); | ||
return false; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
DisposeXmlReader(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
DisposeXmlReader(); | ||
} | ||
|
||
private void DisposeXmlReader() | ||
{ | ||
if (_reader != null) | ||
{ | ||
if(_reader.ReadState != ReadState.Closed) | ||
_reader.Close(); | ||
|
||
_reader.Dispose(); | ||
|
||
_reader = null; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace YPermitin.FIASToolSet.DistributionReader.Exceptions; | ||
|
||
public class FIASBadDataException : Exception | ||
{ | ||
public FIASBadDataException(string message) : base(message) | ||
{ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace YPermitin.FIASToolSet.DistributionReader.Exceptions; | ||
|
||
public class FIASDataNotFoundException : Exception | ||
{ | ||
private readonly string FilePath; | ||
|
||
public FIASDataNotFoundException(string message, string filePath) : base(message) | ||
{ | ||
FilePath = filePath; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using YPermitin.FIASToolSet.DistributionReader.DataReaders; | ||
using YPermitin.FIASToolSet.DistributionReader.Exceptions; | ||
using YPermitin.FIASToolSet.DistributionReader.Models; | ||
|
||
namespace YPermitin.FIASToolSet.DistributionReader; | ||
|
||
public class FIASDistributionReader | ||
{ | ||
private readonly string _workingDirectory; | ||
|
||
public FIASDistributionReader(string workingDirectory) | ||
{ | ||
_workingDirectory = workingDirectory; | ||
} | ||
|
||
/// <summary> | ||
/// Получение версии дистрибутива ФИАС в строковм виде | ||
/// </summary> | ||
/// <returns>Строка с информацией о версии ФИАС</returns> | ||
/// <exception cref="FIASDataNotFoundException">Не удалось найти файл с информацией о версии</exception> | ||
public string GetVersionAsString() | ||
{ | ||
string fileVersionInfoPath = Path.Combine( | ||
_workingDirectory, | ||
"version.txt" | ||
); | ||
|
||
string version; | ||
|
||
if (File.Exists(fileVersionInfoPath)) | ||
{ | ||
version = File.ReadAllText(fileVersionInfoPath).Trim(); | ||
} | ||
else | ||
{ | ||
throw new FIASDataNotFoundException("Не обнаружен файл с информацией о версии.", fileVersionInfoPath); | ||
} | ||
|
||
return version; | ||
} | ||
|
||
/// <summary> | ||
/// Получение версии дистрибутива ФИАС в числовом виде | ||
/// </summary> | ||
/// <returns>Версия ФИАС числом</returns> | ||
/// <exception cref="FIASBadDataException">Не удалось конвертировать значение версии ФИАС в число</exception> | ||
public int GetVersion() | ||
{ | ||
string versionAsString = GetVersionAsString(); | ||
|
||
if (int.TryParse(versionAsString.Replace(".", string.Empty), out int versionAsInt)) | ||
{ | ||
return versionAsInt; | ||
} | ||
else | ||
{ | ||
var exceptionObject = new FIASBadDataException( | ||
$"Не удалось конвертировать значение версии \"{versionAsString}\" в числовой формат."); | ||
exceptionObject.Data.Add("VersionAsString", versionAsString); | ||
throw exceptionObject; | ||
} | ||
} | ||
|
||
public NormativeDocKindReader GetNormativeDocKinds() | ||
{ | ||
string fileNormativeDocKindsPath; | ||
var foundFiles = Directory.GetFiles(_workingDirectory, "AS_NORMATIVE_DOCS_KINDS_20230706_*.XML"); | ||
if (foundFiles.Length == 1) | ||
{ | ||
FileInfo foundFileInfo = new FileInfo(foundFiles[0]); | ||
fileNormativeDocKindsPath = Path.Combine( | ||
_workingDirectory, | ||
foundFileInfo.Name | ||
); | ||
} | ||
else | ||
{ | ||
throw new FIASDataNotFoundException( | ||
"Не обнаружен файл с информацией о видах нормативных документов.", | ||
"AS_NORMATIVE_DOCS_KINDS_20230706_*.XML"); | ||
} | ||
|
||
return new NormativeDocKindReader(fileNormativeDocKindsPath); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace YPermitin.FIASToolSet.DistributionReader.Models; | ||
|
||
/// <summary> | ||
/// Вид нормативного документа | ||
/// </summary> | ||
public class NormativeDocKind | ||
{ | ||
public readonly int Id; | ||
public readonly string Name; | ||
|
||
public NormativeDocKind(int id, string name) | ||
{ | ||
Id = id; | ||
Name = name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<DocumentationFile>YPermitin.FIASToolSet.DistributionReader.xml</DocumentationFile> | ||
<NoWarn>1701;1702;1591;IL2121</NoWarn> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DocumentationFile>YPermitin.FIASToolSet.DistributionReader.xml</DocumentationFile> | ||
<NoWarn>1701;1702;1591;IL2121</NoWarn> | ||
</PropertyGroup> | ||
|
||
</Project> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using YPermitin.FIASToolSet.DistributionReader.DataReaders; | ||
|
||
namespace YPermitin.FIASToolSet.DistributionReader.Tests.DataReaders; | ||
|
||
public class AddressObjectTypeCollectionTests | ||
{ | ||
private readonly string _dataFile; | ||
|
||
public AddressObjectTypeCollectionTests() | ||
{ | ||
_dataFile = Path.Combine( | ||
Directory.GetCurrentDirectory(), | ||
"SampleData", | ||
"AS_ADDR_OBJ_TYPES_20230706_42f95bf4-bebe-4e6a-ac02-00ab1f652505.XML" | ||
); | ||
} | ||
|
||
[Test] | ||
public void ReadItems() | ||
{ | ||
var collection = new AddressObjectTypeCollection(_dataFile); | ||
var allItems = collection.ToList(); | ||
|
||
Assert.NotNull(allItems); | ||
Assert.IsNotEmpty(allItems); | ||
Assert.AreEqual(421, allItems.Count); | ||
|
||
Assert.AreEqual(5, allItems[0].Id); | ||
Assert.AreEqual(1, allItems[0].Level); | ||
Assert.AreEqual("Автономная область", allItems[0].Name); | ||
Assert.AreEqual("Аобл", allItems[0].ShortName); | ||
Assert.AreEqual("Автономная область", allItems[0].Description); | ||
Assert.AreEqual(new DateOnly(1900, 1, 1), allItems[0].StartDate); | ||
Assert.AreEqual(new DateOnly(2015, 11, 5), allItems[0].EndDate); | ||
Assert.AreEqual(new DateOnly(1900, 1, 1), allItems[0].UpdateDate); | ||
} | ||
} |