Skip to content

Commit

Permalink
Merge pull request #7 from YPermitin/develop
Browse files Browse the repository at this point in the history
Расширен функционал чтения файлов дистрибутивов ФИАС
  • Loading branch information
YPermitin authored Jul 9, 2023
2 parents c4bb162 + 137dfe9 commit 7c16ed7
Show file tree
Hide file tree
Showing 32 changed files with 939 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,19 @@

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;1591</NoWarn>
<DocumentationFile>YPermitin.FIASToolSet.DistributionBrowser.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<NoWarn>1701;1702;1591</NoWarn>
<DocumentationFile>YPermitin.FIASToolSet.DistributionBrowser.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Downloader" Version="3.0.6" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="YPermitin.FIASToolSet.Loader.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Exceptions\" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="GeneralResources.resx">
<Generator>ResXFileCodeGenerator</Generator>
Expand All @@ -45,4 +37,8 @@
</Compile>
</ItemGroup>

<ItemGroup>
<Folder Include="Exceptions\" />
</ItemGroup>

</Project>
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

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_enumerator' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 12 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_enumerator' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_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

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_current' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 50 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_reader' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 50 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_current' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 50 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_reader' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_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

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'name' in 'NormativeDocKind.NormativeDocKind(int id, string name)'.

Check warning on line 70 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'name' in 'NormativeDocKind.NormativeDocKind(int id, string name)'.
_current = newObject;
return true;
}
else
{
_current = null;

Check warning on line 76 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
}
}
}

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;

Check warning on line 104 in Libs/YPermitin.FIASToolSet.DistributionReader/DataReaders/NormativeDocKindReader.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
}
}
}
}
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);
}
}
Loading

0 comments on commit 7c16ed7

Please sign in to comment.