示例#1
0
        public async MorestachioDocumentResultPromise CreateAsync(object data, CancellationToken token)
        {
            if (Errors.Any())
            {
                throw new AggregateException("You cannot Create this Template as there are one or more Errors. See Inner Exception for more infos.", Errors.Select(e => e.GetException())).Flatten();
            }

            if (Document is MorestachioDocument morestachioDocument && morestachioDocument.MorestachioVersion !=
                MorestachioDocument.GetMorestachioVersion())
            {
                throw new InvalidOperationException($"The supplied version in the Morestachio document " +
                                                    $"'{morestachioDocument.MorestachioVersion}'" +
                                                    $" is not compatible with the current morestachio version of " +
                                                    $"'{MorestachioDocument.GetMorestachioVersion()}'");
            }

            return(await CreateRenderer().RenderAsync(data, token));
        }
示例#2
0
        public CompilationResult Compile()
        {
            if (Errors.Any())
            {
                throw new AggregateException("You cannot Create this Template as there are one or more Errors. See Inner Exception for more infos.", Errors.Select(e => e.GetException())).Flatten();
            }

            if (Document is MorestachioDocument morestachioDocument && morestachioDocument.MorestachioVersion !=
                MorestachioDocument.GetMorestachioVersion())
            {
                throw new InvalidOperationException($"The supplied version in the Morestachio document " +
                                                    $"'{morestachioDocument.MorestachioVersion}'" +
                                                    $" is not compatible with the current morestachio version of " +
                                                    $"'{MorestachioDocument.GetMorestachioVersion()}'");
            }

            var compiledRenderer = new CompiledRenderer(Document, ParserOptions, CaptureVariables, new DocumentCompiler());

            compiledRenderer.PreCompile();
            return(async(data, token) => await compiledRenderer.RenderAsync(data, token));
        }
        public async Task <MorestachioDocumentResult> CreateAsync([NotNull] object data, CancellationToken token)
        {
            if (Errors.Any())
            {
                throw new AggregateException("You cannot Create this Template as there are one or more Errors. See Inner Exception for more infos.", Errors.Select(e => e.GetException())).Flatten();
            }

            if (Document is MorestachioDocument morestachioDocument && morestachioDocument.MorestachioVersion !=
                MorestachioDocument.GetMorestachioVersion())
            {
                throw new InvalidOperationException($"The supplied version in the Morestachio document " +
                                                    $"'{morestachioDocument.MorestachioVersion}'" +
                                                    $" is not compatible with the current morestachio version of " +
                                                    $"'{MorestachioDocument.GetMorestachioVersion()}'");
            }

            var timeoutCancellation = new CancellationTokenSource();

            if (ParserOptions.Timeout != TimeSpan.Zero)
            {
                timeoutCancellation.CancelAfter(ParserOptions.Timeout);
                var anyCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(token, timeoutCancellation.Token);
                token = anyCancellationToken.Token;
            }
            var sourceStream = ParserOptions.SourceFactory();

            try
            {
                if (sourceStream == null)
                {
                    throw new NullReferenceException("The created stream is null.");
                }

                if (!sourceStream.CanWrite)
                {
                    throw new InvalidOperationException($"The stream '{sourceStream.GetType()}' is ReadOnly.");
                }

                using (var byteCounterStream = new ByteCounterStream(sourceStream,
                                                                     ParserOptions.Encoding, BufferSize, true, ParserOptions))
                {
                    var context = new ContextObject(ParserOptions, "", null)
                    {
                        Value             = data,
                        CancellationToken = token
                    };

                    using (var scopeData = new ScopeData())
                    {
                        await MorestachioDocument.ProcessItemsAndChildren(new[] { Document }, byteCounterStream,
                                                                          context, scopeData);
                    }
                }

                if (timeoutCancellation.IsCancellationRequested)
                {
                    sourceStream.Dispose();
                    throw new TimeoutException($"The requested timeout of '{ParserOptions.Timeout:g}' for report generation was reached.");
                }
            }
            catch
            {
                //If there is any exception while generating the template we must dispose any data written to the stream as it will never returned and might
                //create a memory leak with this. This is also true for a timeout
                sourceStream?.Dispose();
                throw;
            }
            return(new MorestachioDocumentResult()
            {
                Stream = sourceStream
            });
        }
        public async MorestachioDocumentResultPromise CreateAsync([NotNull] object data, CancellationToken token)
        {
            if (Errors.Any())
            {
                throw new AggregateException("You cannot Create this Template as there are one or more Errors. See Inner Exception for more infos.", Errors.Select(e => e.GetException())).Flatten();
            }

            if (Document is MorestachioDocument morestachioDocument && morestachioDocument.MorestachioVersion !=
                MorestachioDocument.GetMorestachioVersion())
            {
                throw new InvalidOperationException($"The supplied version in the Morestachio document " +
                                                    $"'{morestachioDocument.MorestachioVersion}'" +
                                                    $" is not compatible with the current morestachio version of " +
                                                    $"'{MorestachioDocument.GetMorestachioVersion()}'");
            }

            var timeoutCancellation = new CancellationTokenSource();

            if (ParserOptions.Timeout != TimeSpan.Zero)
            {
                timeoutCancellation.CancelAfter(ParserOptions.Timeout);
                var anyCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(token, timeoutCancellation.Token);
                token = anyCancellationToken.Token;
            }

            PerformanceProfiler profiler = null;

            using (var byteCounterStream = ParserOptions.StreamFactory.GetByteCounterStream(ParserOptions))
            {
                if (byteCounterStream?.Stream == null)
                {
                    throw new NullReferenceException("The created stream is null.");
                }
                var context   = ParserOptions.CreateContextObject("", token, data);
                var scopeData = new ScopeData();
                try
                {
                    if (ParserOptions.ProfileExecution)
                    {
                        scopeData.Profiler = profiler = new PerformanceProfiler(true);
                    }
                    await MorestachioDocument.ProcessItemsAndChildren(new[] { Document }, byteCounterStream,
                                                                      context, scopeData);

                    if (timeoutCancellation.IsCancellationRequested)
                    {
                        throw new TimeoutException($"The requested timeout of '{ParserOptions.Timeout:g}' for template generation was reached.");
                    }
                }
                finally
                {
                    if (!CaptureVariables)
                    {
                        scopeData.Dispose();
                        scopeData.Variables.Clear();
                    }
                }

                return(new MorestachioDocumentResult(byteCounterStream.Stream,
                                                     profiler,
                                                     scopeData.Variables.ToDictionary(e => e.Key, e => scopeData.GetFromVariable(e.Value).Value)));
            }
        }