예제 #1
0
        public static ExtendedParseInformation ParseWithOptions([NotNull] ParserOptions parsingOptions)
        {
            if (parsingOptions == null)
            {
                throw new ArgumentNullException(nameof(parsingOptions));
            }

            if (parsingOptions.SourceFactory == null)
            {
                throw new ArgumentNullException(nameof(parsingOptions), "The given Stream is null");
            }

            var tokens        = new Queue <TokenPair>(Tokenizer.Tokenize(parsingOptions));
            var inferredModel = new InferredTemplateModel();

            var extendedParseInformation = new ExtendedParseInformation(inferredModel, parsingOptions, tokens);

            if (parsingOptions.WithModelInference)
            {
                //we preparse the template once to get the model
                var s = extendedParseInformation.InternalTemplate.Value;
            }

            return(extendedParseInformation);
        }
예제 #2
0
        public static async Task <Stream> CreateTemplateStreamAsync([NotNull] ExtendedParseInformation parseOutput, [NotNull] object data, CancellationToken token)
        {
            var timeoutCancellation = new CancellationTokenSource();

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

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

                using (var byteCounterStreamWriter = new ByteCounterStreamWriter(sourceStream,
                                                                                 parseOutput.ParserOptions.Encoding, BufferSize, true))
                {
                    var context = new ContextObject(parseOutput.ParserOptions, "")
                    {
                        Value             = data,
                        CancellationToken = token
                    };
                    await parseOutput.InternalTemplate.Value(byteCounterStreamWriter, context);
                }

                if (timeoutCancellation.IsCancellationRequested)
                {
                    sourceStream.Dispose();
                    throw new TimeoutException($"The requested timeout of {parseOutput.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(sourceStream);
        }
예제 #3
0
        public static Stream CreateTemplateStream([NotNull] ExtendedParseInformation parseOutput, [NotNull] object data, CancellationToken token)
        {
            var sourceStream = parseOutput.ParserOptions.SourceFactory();

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

            using (var ByteCounterStreamWriter = new ByteCounterStreamWriter(sourceStream, parseOutput.ParserOptions.Encoding, BufferSize, true))
            {
                var context = new ContextObject(parseOutput.ParserOptions)
                {
                    Value             = data,
                    Key               = "",
                    CancellationToken = token
                };
                parseOutput.InternalTemplate.Value(ByteCounterStreamWriter, context);
            }

            return(sourceStream);
        }