|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using CSF.Screenplay.Reporting.Models; |
| 4 | +using CSF.Screenplay.ReportModel; |
| 5 | +using CSF.Zpt; |
| 6 | +using CSF.Zpt.Rendering; |
| 7 | + |
| 8 | +namespace CSF.Screenplay.Reporting |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Implementation of <see cref="IRendersReport"/> which writes Screenplay reports to a standalone HTML page. |
| 12 | + /// This includes a number of features for ease of reading, including limited filtering and collapsing of scenarios. |
| 13 | + /// </summary> |
| 14 | + /// <remarks> |
| 15 | + /// <para> |
| 16 | + /// This report writer essentially works like a miniature MVC application, using ZPT-Sharp to render the report. |
| 17 | + /// You will find, in the <c>Views</c> directory, a number of embedded resources which make up the MVC view for |
| 18 | + /// presenting the report. |
| 19 | + /// </para> |
| 20 | + /// <para> |
| 21 | + /// The report received within the <see cref="Render"/> method of this type is passed to an instance of |
| 22 | + /// <see cref="ReportDocument"/>. which serves as the MVC model type. |
| 23 | + /// </para> |
| 24 | + /// <para> |
| 25 | + /// This class is then essentially the controller, which creates an instance of that view (as a ZPT document) |
| 26 | + /// renders it using ZPT-Sharp. |
| 27 | + /// </para> |
| 28 | + /// </remarks> |
| 29 | + public class HtmlReportRenderer : IRendersReport, IDisposable |
| 30 | + { |
| 31 | + readonly TextWriter writer; |
| 32 | + readonly IZptDocumentFactory documentFactory; |
| 33 | + readonly bool disposeWriter; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Write the specified report to the destination. |
| 37 | + /// </summary> |
| 38 | + /// <param name="report">Report model.</param> |
| 39 | + public void Render(IReport report) |
| 40 | + { |
| 41 | + var doc = GetDocument(); |
| 42 | + var emptyDoc = GetDocument(); |
| 43 | + var model = GetDocumentModel(report, emptyDoc); |
| 44 | + |
| 45 | + doc.Render(model, writer); |
| 46 | + |
| 47 | + writer.Flush(); |
| 48 | + } |
| 49 | + |
| 50 | + IZptDocument GetDocument() |
| 51 | + { |
| 52 | + using(var stream = GetDocumentStream()) |
| 53 | + { |
| 54 | + return documentFactory.CreateDocument(stream, RenderingMode.Html); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + ReportDocument GetDocumentModel(IReport reportModel, IZptDocument document) |
| 59 | + => new ReportDocument(reportModel, document); |
| 60 | + |
| 61 | + Stream GetDocumentStream() |
| 62 | + => Views.ViewProvider.GetDocumentTemplate(); |
| 63 | + |
| 64 | + #region IDisposable Support |
| 65 | + bool disposedValue = false; |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Dispose of the current instance |
| 69 | + /// </summary> |
| 70 | + /// <param name="disposing">If set to <c>true</c> then this disposal is explicit.</param> |
| 71 | + protected virtual void Dispose(bool disposing) |
| 72 | + { |
| 73 | + if(!disposedValue) |
| 74 | + { |
| 75 | + if(disposing) |
| 76 | + { |
| 77 | + if(disposeWriter) |
| 78 | + writer.Dispose(); |
| 79 | + } |
| 80 | + |
| 81 | + disposedValue = true; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Releases all resource used by the <see cref="T:CSF.Screenplay.Reporting.TextReportWriter"/> object. |
| 87 | + /// </summary> |
| 88 | + /// <remarks>Call <see cref="Dispose()"/> when you are finished using the |
| 89 | + /// <see cref="T:CSF.Screenplay.Reporting.TextReportWriter"/>. The <see cref="Dispose()"/> method leaves the |
| 90 | + /// <see cref="T:CSF.Screenplay.Reporting.TextReportWriter"/> in an unusable state. After calling |
| 91 | + /// <see cref="Dispose()"/>, you must release all references to the |
| 92 | + /// <see cref="T:CSF.Screenplay.Reporting.TextReportWriter"/> so the garbage collector can reclaim the memory that |
| 93 | + /// the <see cref="T:CSF.Screenplay.Reporting.TextReportWriter"/> was occupying.</remarks> |
| 94 | + public void Dispose() |
| 95 | + { |
| 96 | + Dispose(true); |
| 97 | + } |
| 98 | + #endregion |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Initializes a new instance of the <see cref="T:CSF.Screenplay.Reporting.HtmlReportWriter"/> class. |
| 102 | + /// </summary> |
| 103 | + /// <param name="writer">A text writer into which the report should be written.</param> |
| 104 | + /// <param name="disposeWriter">Indicates whether or not the <paramref name="writer"/> should be diposed with this instance.</param> |
| 105 | + public HtmlReportRenderer(TextWriter writer, bool disposeWriter = true) |
| 106 | + { |
| 107 | + if(writer == null) |
| 108 | + throw new ArgumentNullException(nameof(writer)); |
| 109 | + |
| 110 | + this.writer = writer; |
| 111 | + this.disposeWriter = disposeWriter; |
| 112 | + |
| 113 | + // TODO: Once the blocking issues are resolved, pass a hard coded configuration to the ZptDocumentFactory |
| 114 | + // |
| 115 | + // This is blocked by https://github.com/csf-dev/CSF.Screenplay/issues/141. |
| 116 | + // In turn, that is blocked by the upstream ZPT-Sharp issue https://github.com/csf-dev/ZPT-Sharp/issues/252 |
| 117 | + // Once all of this is resolved, clients won't need to put all of their configuration for ZPT-Sharp |
| 118 | + // into their application configuration files. The code which achieves this is commented-out below: |
| 119 | + // |
| 120 | + // var pluginConfig = new HardCodedZptSharpConfigurationProvider(); |
| 121 | + // documentFactory = new ZptDocumentFactory(new ZptDocumentProviderService(pluginConfig)); |
| 122 | + |
| 123 | + documentFactory = new ZptDocumentFactory(); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Write the report to a file path. |
| 128 | + /// </summary> |
| 129 | + /// <param name="report">The report.</param> |
| 130 | + /// <param name="path">Destination file path.</param> |
| 131 | + public static void WriteToFile(IReport report, string path) |
| 132 | + { |
| 133 | + using(var reportWriter = new HtmlReportRenderer(new StreamWriter(path))) |
| 134 | + { |
| 135 | + reportWriter.Render(report); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments