This commit is contained in:
Simon Gruber
2024-01-08 16:21:10 +01:00
parent f3768348e9
commit b17044f959
51 changed files with 263 additions and 274 deletions
@@ -0,0 +1,59 @@
using ReportGeneration.Interface;
using System;
using System.IO;
using System.Text;
namespace ReportGeneration.Abstract;
public abstract class DocumentGeneratorBase : StreamWriterBase, IDocumentGenerator
{
/// <inheritdoc />
protected DocumentGeneratorBase() { }
/// <inheritdoc />
protected DocumentGeneratorBase(Stream stream) : base(stream) { }
/// <inheritdoc />
protected DocumentGeneratorBase(string filePath) : base(File.Open(filePath, FileMode.Create,
FileAccess.Write))
{ }
/// <inheritdoc />
protected DocumentGeneratorBase(Stream stream, Encoding encoding) : base(stream, encoding) { }
#region Writing
/// <inheritdoc />
public virtual IDocumentGenerator Append(string? text = default)
{
Write(text);
return this;
}
/// <inheritdoc />
public virtual IDocumentGenerator AppendLine(string? text = default)
{
WriteLine(text);
return this;
}
/// <inheritdoc />
public abstract IDocumentGenerator AppendHeading(int level, string text);
/// <inheritdoc />
public abstract IDocumentGenerator AppendParagraph(string? text = default);
/// <inheritdoc />
public IDocumentGenerator AppendTable(int columns, Action<ITableGenerator> table)
{
Write(() => MakeTable(columns, new MemoryStream()), table);
return this;
}
protected abstract ITableGenerator MakeTable(int columns, Stream stream);
#endregion
/// <inheritdoc />
public abstract string FormatImage(string path, IBounds? bounds = default);
}
@@ -0,0 +1,44 @@
using ReportGeneration.Interface;
namespace ReportGeneration.Abstract.Model;
public struct Bounds : IBounds
{
/// <inheritdoc />
public string Unit => "px";
/// <inheritdoc />
public int? MinWidth { get; set; } = null;
/// <inheritdoc />
public int? MinHeight { get; set; } = null;
/// <inheritdoc />
public int? MaxWidth { get; set; } = null;
/// <inheritdoc />
public int? MaxHeight { get; set; } = null;
/// <inheritdoc />
public int? Width { get; set; } = null;
/// <inheritdoc />
public int? Height { get; set; } = null;
public Bounds() { }
public Bounds(int? size)
{
Width = size;
Height = size;
}
public Bounds(int? min, int? max, int? size = null) : this(size)
{
MinWidth = min;
MinHeight = min;
MaxWidth = max;
MaxHeight = max;
}
}
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<IncludeSymbols>True</IncludeSymbols>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ReportGeneration.Interface\ReportGeneration.Interface.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,200 @@
using ReportGeneration.Interface;
using System;
using System.IO;
using System.Text;
namespace ReportGeneration.Abstract;
public abstract class StreamWriterBase : IStreamWriter
{
private bool _isOpen;
private bool _isClosed;
/// <summary>
/// Underlying <see cref="Stream"/>
/// </summary>
private Stream Stream { get; }
/// <summary>
/// Internal <see cref="StreamWriter"/> for generating the output <see cref="string"/>
/// </summary>
protected TextWriter Writer { get; }
/// <summary>
/// Constructor; Configures the
/// <see cref="StreamWriterBase"/> to write to the memory
/// </summary>
protected StreamWriterBase() : this(new MemoryStream()) { }
/// <inheritdoc cref="StreamWriterBase(System.IO.Stream, Encoding)"/>
protected StreamWriterBase(Stream stream) : this(stream, Encoding.UTF8) { }
/// <summary>
/// Constructor; Configures the <see cref="StreamWriterBase"/>
/// to write to the specified <paramref name="stream"/>
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to write to</param>
/// <param name="encoding">Text <see cref="Encoding"/> of the written data</param>
protected StreamWriterBase(Stream stream, Encoding encoding)
{
Stream = stream;
Writer = new StreamWriter(stream, encoding);
}
#region Control
public void Open()
{
if (_isOpen)
{
throw new InvalidOperationException($"{GetType()} has already been opened");
}
if (_isClosed)
{
throw new InvalidOperationException($"Cannot call open on a closed {GetType()}");
}
_isOpen = true;
OnOpen();
}
public void Close()
{
if (_isClosed)
{
throw new InvalidOperationException($"{GetType()} has already been closed");
}
if (!_isOpen)
{
throw new InvalidOperationException($"{GetType()} has never been opened");
}
_isClosed = true;
OnClose();
Writer.Flush();
}
/// <summary>
/// Called once the internal writer has been initialized
/// and the <see cref="Stream"/> is ready for writing
/// </summary>
protected virtual void OnOpen() { }
/// <summary>
/// Called once the document is about to be closed
/// </summary>
protected virtual void OnClose() { }
#endregion
#region Reading
/// <inheritdoc />
public StreamReader Read()
{
Writer.Flush();
Stream.Seek(0, SeekOrigin.Begin);
return new StreamReader(Stream, Writer.Encoding);
}
#endregion
#region Writing
public IStreamWriter Write(IStreamWriter writer)
{
using var reader = writer.Read();
return Write(reader);
}
public IStreamWriter Write(StreamReader reader)
{
int bytesRead;
char[] buffer = new char[4096];
while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
{
Writer.Write(buffer, 0, bytesRead);
}
return this;
}
public IStreamWriter Write(string? text = default)
{
Writer.Write(text);
return this;
}
public IStreamWriter WriteLine(string? text = default)
{
Writer.WriteLine(text);
return this;
}
/// <summary>
/// Creates and configures the <typeparamref name="T"/> using the given functions
/// </summary>
/// <typeparam name="T">Type of the <see cref="IStreamWriter"/></typeparam>
/// <param name="makeFunc">Function used to generate the <typeparamref name="T"/></param>
/// <param name="configFunc">Function used to configure the <typeparamref name="T"/></param>
/// <returns></returns>
public IStreamWriter Write<T>(Func<T> makeFunc, Action<T> configFunc)
where T : IStreamWriter
{
using var writer = makeFunc();
writer.Open();
configFunc(writer);
writer.Close();
Write(writer);
return this;
}
#endregion
#region IDisposable
private bool _disposed;
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
try
{
// Close document
Close();
}
catch (InvalidOperationException)
{
// ignore
}
finally
{
// Dispose stream
Stream.Dispose();
}
}
}
/// <inheritdoc />
public void Dispose()
{
if (_disposed)
{
return;
}
_disposed = true;
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
@@ -0,0 +1,70 @@
using ReportGeneration.Interface;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ReportGeneration.Abstract;
public abstract class TableGeneratorBase : StreamWriterBase, ITableGenerator
{
/// <inheritdoc />
public int Columns { get; }
/// <inheritdoc />
protected TableGeneratorBase(int columns) =>
Columns = columns;
/// <inheritdoc />
protected TableGeneratorBase(int columns, Stream stream)
: base(stream) => Columns = columns;
/// <inheritdoc />
protected TableGeneratorBase(int columns, Stream stream, Encoding encoding)
: base(stream, encoding) => Columns = columns;
#region Header
/// <inheritdoc />
public virtual ITableGenerator AppendHeader(string content) =>
AppendHeader(Enumerable.Range(0, Columns).Select(_ => content));
/// <inheritdoc />
public abstract ITableGenerator AppendHeader(IEnumerable<string> row);
/// <inheritdoc />
public virtual ITableGenerator AppendHeader(IEnumerable<IEnumerable<string>> rows)
{
foreach (var row in rows)
{
AppendHeader(row);
}
return this;
}
#endregion
#region Row
/// <inheritdoc />
public virtual ITableGenerator AppendRow(string content) =>
AppendRow(Enumerable.Range(0, Columns).Select(_ => content));
/// <inheritdoc />
public abstract ITableGenerator AppendRow(IEnumerable<string> row);
/// <inheritdoc />
public virtual ITableGenerator AppendRows(IEnumerable<IEnumerable<string>> rows)
{
foreach (var row in rows)
{
AppendRow(row);
}
return this;
}
#endregion
}