-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement embedded resource decompressor
- Loading branch information
Showing
14 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
OfficeFlow.Word.OpenXml.Tests/Resources/Embedded/EmbeddedResourceDecompressorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Xml.Linq; | ||
using FluentAssertions; | ||
using OfficeFlow.Word.OpenXml.Resources.Embedded; | ||
using OfficeFlow.Word.OpenXml.Resources.Exceptions; | ||
using Xunit; | ||
|
||
namespace OfficeFlow.Word.OpenXml.Tests.Resources.Embedded | ||
{ | ||
public sealed class EmbeddedResourceDecompressorTests | ||
{ | ||
[Fact] | ||
public void Should_decompress_exists_embedded_resource_from_calling_assembly() | ||
{ | ||
// Arrange | ||
var expectedXml = new XElement | ||
( | ||
"document", | ||
new XElement | ||
( | ||
"body", | ||
new XText("Content") | ||
) | ||
); | ||
|
||
var sut = new EmbeddedResourceDecompressor(); | ||
|
||
// Act | ||
var actualXml = sut.Decompress("TestEmbeddedResource.xml.gz"); | ||
|
||
// Assert | ||
actualXml | ||
.Root | ||
.Should() | ||
.BeEquivalentTo(expectedXml); | ||
} | ||
|
||
[Fact] | ||
public void Should_throws_exception_if_resource_not_found_in_calling_assembly() | ||
=> new EmbeddedResourceDecompressor() | ||
.Invoking(decompressor => decompressor.Decompress("NotExistsResource.xml.gz")) | ||
.Should() | ||
.Throw<ResourceNotFoundException>(); | ||
} | ||
} |
Binary file added
BIN
+134 Bytes
OfficeFlow.Word.OpenXml.Tests/Resources/Embedded/TestEmbeddedResource.xml.gz
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+759 Bytes
OfficeFlow.Word.OpenXml/Resources/Embedded/Default/bulletNumbering.xml.gz
Binary file not shown.
Binary file added
BIN
+679 Bytes
OfficeFlow.Word.OpenXml/Resources/Embedded/Default/decimalNumbering.xml.gz
Binary file not shown.
Binary file added
BIN
+366 Bytes
OfficeFlow.Word.OpenXml/Resources/Embedded/Default/numbering.xml.gz
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+459 Bytes
OfficeFlow.Word.OpenXml/Resources/Embedded/Default/wordDocument.xml.gz
Binary file not shown.
Binary file not shown.
49 changes: 49 additions & 0 deletions
49
OfficeFlow.Word.OpenXml/Resources/Embedded/EmbeddedResourceDecompressor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Xml.Linq; | ||
using OfficeFlow.Word.OpenXml.Resources.Exceptions; | ||
using OfficeFlow.Word.OpenXml.Resources.Interfaces; | ||
|
||
namespace OfficeFlow.Word.OpenXml.Resources.Embedded | ||
{ | ||
public sealed class EmbeddedResourceDecompressor : IResourceDecompressor | ||
{ | ||
private readonly Assembly _assembly; | ||
|
||
private static readonly Lazy<EmbeddedResourceDecompressor> InstanceFactory = | ||
new Lazy<EmbeddedResourceDecompressor>(() => new EmbeddedResourceDecompressor()); | ||
|
||
public static EmbeddedResourceDecompressor Instance | ||
=> InstanceFactory.Value; | ||
|
||
public EmbeddedResourceDecompressor() | ||
: this(Assembly.GetCallingAssembly()) | ||
{ } | ||
|
||
public EmbeddedResourceDecompressor(Assembly assembly) | ||
=> _assembly = assembly; | ||
|
||
private Stream GetResourceStream(string resourceName) | ||
{ | ||
var resourcePath = _assembly | ||
.GetManifestResourceNames() | ||
.FirstOrDefault(name => name.EndsWith(resourceName)) | ||
?? throw new ResourceNotFoundException(resourceName); | ||
|
||
return _assembly.GetManifestResourceStream(resourcePath) | ||
?? throw new ResourceNotLoadedException(resourceName); | ||
} | ||
|
||
public XDocument Decompress(string resourceName) | ||
{ | ||
var stream = GetResourceStream(resourceName); | ||
using var zip = new GZipStream(stream, CompressionMode.Decompress); | ||
using var reader = new StreamReader(zip); | ||
|
||
return XDocument.Load(reader); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
OfficeFlow.Word.OpenXml/Resources/Exceptions/ResourceNotFoundException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace OfficeFlow.Word.OpenXml.Resources.Exceptions | ||
{ | ||
public sealed class ResourceNotFoundException : Exception | ||
{ | ||
public ResourceNotFoundException(string resourceName, Exception? innerException = null) | ||
: base($"Resource \"{resourceName}\" not found", innerException) | ||
{ } | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
OfficeFlow.Word.OpenXml/Resources/Exceptions/ResourceNotLoadedException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace OfficeFlow.Word.OpenXml.Resources.Exceptions | ||
{ | ||
public sealed class ResourceNotLoadedException : Exception | ||
{ | ||
public ResourceNotLoadedException(string resourceName, Exception? innerException = null) | ||
: base($"Failed to load resource \"{resourceName}\"", innerException) | ||
{ } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
OfficeFlow.Word.OpenXml/Resources/Interfaces/IResourceDecompressor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Xml.Linq; | ||
|
||
namespace OfficeFlow.Word.OpenXml.Resources.Interfaces | ||
{ | ||
public interface IResourceDecompressor | ||
{ | ||
XDocument Decompress(string resourceName); | ||
} | ||
} |