コード例 #1
0
        /// <summary>
        /// Reads the <see cref="RazorSourceDocument"/> from the specified <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> to read from.</param>
        /// <param name="fileName">The file name of the template.</param>
        /// <returns>The <see cref="RazorSourceDocument"/>.</returns>
        public static RazorSourceDocument ReadFrom(Stream stream, string fileName)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var properties = new RazorSourceDocumentProperties(fileName, relativePath: null);

            return(new StreamSourceDocument(stream, null, properties));
        }
コード例 #2
0
        // Internal for testing
        internal static RazorSourceDocument ConvertToSourceDocument(RazorProjectItem importItem)
        {
            if (importItem.Exists)
            {
                // Normal import, has file paths, content etc.
                return(RazorSourceDocument.ReadFrom(importItem));
            }

            // Marker import, doesn't exist, used as an identifier for "there could be something here".
            var sourceDocumentProperties = new RazorSourceDocumentProperties(importItem.FilePath, importItem.RelativePhysicalPath);

            return(RazorSourceDocument.Create(string.Empty, sourceDocumentProperties));
        }
コード例 #3
0
        public static RazorSourceDocument Create(
            string content,
            RazorSourceDocumentProperties properties,
            Encoding encoding      = null,
            bool normalizeNewLines = false)
        {
            if (normalizeNewLines)
            {
                content = NormalizeNewLines(content);
            }

            return(new StringSourceDocument(content, encoding ?? Encoding.UTF8, properties));
        }
コード例 #4
0
        /// <summary>
        /// Creates a <see cref="RazorSourceDocument"/> from the specified <paramref name="content"/>.
        /// </summary>
        /// <param name="content">The source document content.</param>
        /// <param name="properties">Properties to configure the <see cref="RazorSourceDocument"/>.</param>
        /// <returns>The <see cref="RazorSourceDocument"/>.</returns>
        /// <remarks>Uses <see cref="System.Text.Encoding.UTF8" /></remarks>
        public static RazorSourceDocument Create(string content, RazorSourceDocumentProperties properties)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            if (properties == null)
            {
                throw new ArgumentNullException(nameof(properties));
            }

            return(Create(content, Encoding.UTF8, properties));
        }
コード例 #5
0
        public void ReadFrom_WithProperties()
        {
            // Arrange
            var content    = TestRazorSourceDocument.CreateStreamContent(encoding: Encoding.UTF32);
            var properties = new RazorSourceDocumentProperties("c:\\myapp\\filePath.cshtml", "filePath.cshtml");

            // Act
            var document = RazorSourceDocument.ReadFrom(content, Encoding.UTF32, properties);

            // Assert
            Assert.Equal("c:\\myapp\\filePath.cshtml", document.FilePath);
            Assert.Equal("filePath.cshtml", document.RelativePath);
            Assert.Same(Encoding.UTF32, Assert.IsType <StreamSourceDocument>(document).Encoding);
        }
コード例 #6
0
        public void Create_WithProperties()
        {
            // Arrange
            var content    = "Hello world";
            var properties = new RazorSourceDocumentProperties("c:\\myapp\\filePath.cshtml", "filePath.cshtml");

            // Act
            var document = RazorSourceDocument.Create(content, Encoding.UTF32, properties);

            // Assert
            Assert.Equal("c:\\myapp\\filePath.cshtml", document.FilePath);
            Assert.Equal("filePath.cshtml", document.RelativePath);
            Assert.Equal(content, ReadContent(document));
            Assert.Same(Encoding.UTF32, Assert.IsType <StringSourceDocument>(document).Encoding);
        }
コード例 #7
0
        /// <summary>
        /// Creates a <see cref="RazorSourceDocument"/> from the specified <paramref name="content"/>.
        /// </summary>
        /// <param name="content">The source document content.</param>
        /// <param name="fileName">The file name of the <see cref="RazorSourceDocument"/>.</param>
        /// <param name="encoding">The <see cref="System.Text.Encoding"/> of the file <paramref name="content"/> was read from.</param>
        /// <returns>The <see cref="RazorSourceDocument"/>.</returns>
        public static RazorSourceDocument Create(string content, string fileName, Encoding encoding)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            var properties = new RazorSourceDocumentProperties(fileName, relativePath: null);

            return(new StringSourceDocument(content, encoding, properties));
        }
コード例 #8
0
        public static RazorSourceDocument Create(
            string content         = "Hello, world!",
            Encoding encoding      = null,
            bool normalizeNewLines = false,
            string filePath        = "test.cshtml",
            string relativePath    = "test.cshtml")
        {
            if (normalizeNewLines)
            {
                content = NormalizeNewLines(content);
            }

            var properties = new RazorSourceDocumentProperties(filePath, relativePath);

            return(new StringSourceDocument(content, encoding ?? Encoding.UTF8, properties));
        }
コード例 #9
0
        public StreamSourceDocument(Stream stream, Encoding encoding, RazorSourceDocumentProperties properties)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (properties == null)
            {
                throw new ArgumentNullException(nameof(properties));
            }

            // Notice we don't validate the encoding here. StreamSourceDocument can compute it.
            _checksum            = ComputeChecksum(stream);
            _innerSourceDocument = CreateInnerSourceDocument(stream, encoding, properties);
        }
コード例 #10
0
        public static RazorSourceDocument CreateResource(string resourcePath, Assembly assembly, Encoding encoding = null, bool normalizeNewLines = false)
        {
            var file = TestFile.Create(resourcePath, assembly);

            using (var input = file.OpenRead())
                using (var reader = new StreamReader(input))
                {
                    var content = reader.ReadToEnd();
                    if (normalizeNewLines)
                    {
                        content = NormalizeNewLines(content);
                    }

                    var properties = new RazorSourceDocumentProperties(resourcePath, resourcePath);
                    return(new StringSourceDocument(content, encoding ?? Encoding.UTF8, properties));
                }
        }
コード例 #11
0
        /// <summary>
        /// Reads the <see cref="RazorSourceDocument"/> from the specified <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> to read from.</param>
        /// <param name="encoding">The <see cref="System.Text.Encoding"/> to use to read the <paramref name="stream"/>.</param>
        /// <param name="properties">Properties to configure the <see cref="RazorSourceDocument"/>.</param>
        /// <returns>The <see cref="RazorSourceDocument"/>.</returns>
        public static RazorSourceDocument ReadFrom(Stream stream, Encoding encoding, RazorSourceDocumentProperties properties)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            if (properties == null)
            {
                throw new ArgumentNullException(nameof(properties));
            }

            return(new StreamSourceDocument(stream, encoding, properties));
        }
コード例 #12
0
        public StringSourceDocument(string content, Encoding encoding, RazorSourceDocumentProperties properties)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            if (properties == null)
            {
                throw new ArgumentNullException(nameof(properties));
            }

            _content     = content;
            Encoding     = encoding;
            FilePath     = properties.FilePath;
            RelativePath = properties.RelativePath;

            _lines = new DefaultRazorSourceLineCollection(this);
        }
コード例 #13
0
        public LargeTextSourceDocument(StreamReader reader, int chunkMaxLength, Encoding encoding, RazorSourceDocumentProperties properties)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            if (properties == null)
            {
                throw new ArgumentNullException(nameof(properties));
            }

            _chunkMaxLength = chunkMaxLength;
            Encoding        = encoding;
            FilePath        = properties.FilePath;
            RelativePath    = properties.RelativePath;

            ReadChunks(reader, _chunkMaxLength, out _length, out _chunks);
            _lines = new DefaultRazorSourceLineCollection(this);
        }
コード例 #14
0
        private static RazorSourceDocument CreateInnerSourceDocument(Stream stream, Encoding encoding, RazorSourceDocumentProperties properties)
        {
            var streamLength    = (int)stream.Length;
            var content         = string.Empty;
            var contentEncoding = encoding ?? Encoding.UTF8;

            if (streamLength > 0)
            {
                var bufferSize = Math.Min(streamLength, LargeObjectHeapLimitInChars);

                var reader = new StreamReader(
                    stream,
                    contentEncoding,
                    detectEncodingFromByteOrderMarks: true,
                    bufferSize: bufferSize,
                    leaveOpen: true);

                using (reader)
                {
                    reader.Peek();      // Just to populate the encoding

                    if (encoding == null)
                    {
                        contentEncoding = reader.CurrentEncoding;
                    }
                    else if (encoding != reader.CurrentEncoding)
                    {
                        throw new InvalidOperationException(
                                  Resources.FormatMismatchedContentEncoding(
                                      encoding.EncodingName,
                                      reader.CurrentEncoding.EncodingName));
                    }

                    if (streamLength > LargeObjectHeapLimitInChars)
                    {
                        // If the resulting string would end up on the large object heap, then use LargeTextSourceDocument.
                        return(new LargeTextSourceDocument(
                                   reader,
                                   LargeObjectHeapLimitInChars,
                                   contentEncoding,
                                   properties));
                    }

                    content = reader.ReadToEnd();
                }
            }

            return(new StringSourceDocument(content, contentEncoding, properties));
        }