Ejemplo n.º 1
0
        private async ValueTask <string> ReadStreamAsText(IXIncludeResource resource)
        {
            var stream = await resource.GetStream().ConfigureAwait(false);

            await using (stream.ConfigureAwait(false))
            {
                using var streamReader = new StreamReader(stream, GetEncoding(resource), detectEncodingFromByteOrderMarks: true);

                return(await streamReader.ReadToEndAsync().ConfigureAwait(false));
            }
        }
Ejemplo n.º 2
0
        private Encoding GetEncoding(IXIncludeResource resource)
        {
            if (resource.ContentType?.CharSet is { Length : > 0 } charSet)
            {
                return(Encoding.GetEncoding(charSet));
            }

            if (_encodingValue is { Length : > 0 } encoding)
            {
                return(Encoding.GetEncoding(encoding));
            }

            return(Encoding.UTF8);
        }
Ejemplo n.º 3
0
        private static bool IsXml(IXIncludeResource resource)
        {
            var mediaType = resource.ContentType?.MediaType;

            if (mediaType is null)
            {
                return(false);
            }

            const StringComparison ct             = StringComparison.OrdinalIgnoreCase;
            const string           textXml        = "text/xml";
            const string           applicationXml = "application/xml";
            const string           text           = "text/";
            const string           application    = "application/";
            const string           xml            = "+xml";

            return(string.Equals(mediaType, applicationXml, ct) ||
                   string.Equals(mediaType, textXml, ct) ||
                   (mediaType.StartsWith(text, ct) || mediaType.StartsWith(application, ct)) && mediaType.EndsWith(xml, ct));
        }
Ejemplo n.º 4
0
        private static async ValueTask <string> ReadStreamAsXml(IXIncludeResource resource)
        {
            var stream = await resource.GetStream().ConfigureAwait(false);

            await using (stream.ConfigureAwait(false))
            {
                using var xmlReader = Create(stream);

                var stringBuilder = new StringBuilder();
                using (var xmlWriter = XmlWriter.Create(stringBuilder))
                {
                    while (await xmlReader.ReadAsync().ConfigureAwait(false))
                    {
                        xmlWriter.WriteNode(xmlReader, defattr: false);
                    }
                }

                return(stringBuilder.ToString());
            }
        }