//        [Test]
//        [TestCase("http://www.w3.org/", "utf-8")]
        public void ShouldGetCharsetFromResponse(string url, string expectedCharset)
        {
            var analizer = new CharsetAnalizer();
            var charset  = analizer.GetCharsetForUrl(url);

            Assert.AreEqual(0, string.Compare(expectedCharset, charset, StringComparison.OrdinalIgnoreCase));
        }
        public void ShouldParseMetaTag(string input, string expectedCharset)
        {
            var analizer = new CharsetAnalizer();
            var charset  = analizer.GetCharsetFromMetadata(input);

            Assert.AreEqual(0, string.Compare(expectedCharset, charset, StringComparison.OrdinalIgnoreCase));
        }
        public void ShouldAutodetectCharset(string filePath, string expectedCharset)
        {
            var stream   = File.OpenRead(filePath);
            var analizer = new CharsetAnalizer();
            var charset  = analizer.AutoDetectCharset(stream);

            Assert.AreEqual(0, string.Compare(expectedCharset, charset, StringComparison.OrdinalIgnoreCase));
        }
示例#4
0
        private static string GetCharset(Stream stream)
        {
            stream.Seek(0, SeekOrigin.Begin);

            var sr   = new StreamReader(stream);
            var text = sr.ReadToEnd();

            var analizer = new CharsetAnalizer();

            var metaCharset = analizer.GetCharsetFromMetadata(text);

            if (!string.IsNullOrEmpty(metaCharset))
            {
                return(metaCharset);
            }

            stream.Seek(0, SeekOrigin.Begin);
            var autoCharset = analizer.AutoDetectCharset(stream);

            return(!string.IsNullOrEmpty(autoCharset)
                       ? autoCharset
                       : null);
        }
        private string GetCharset(byte[] content)
        {
            var analizer = new CharsetAnalizer();

            var asciiStr    = Encoding.ASCII.GetString(content);
            var metaCharset = analizer.GetCharsetFromMetadata(asciiStr);

            if (!string.IsNullOrEmpty(metaCharset))
            {
                return(metaCharset);
            }

            using (var ms = new MemoryStream(content))
            {
                var autoCharset = analizer.AutoDetectCharset(ms);
                if (!string.IsNullOrEmpty(autoCharset))
                {
                    return(autoCharset);
                }

                return(null);
            }
        }