/// <summary>
        /// Parses the XML document firing the events to the handler.
        /// @throws IOException on error
        /// </summary>
        /// <param name="doc">the document handler</param>
        /// <param name="inp">the document. The encoding is deduced from the stream. The stream is not closed</param>
        public static void Parse(ISimpleXmlDocHandler doc, Stream inp)
        {
            var b4    = new byte[4];
            var count = inp.Read(b4, 0, b4.Length);

            if (count != 4)
            {
                throw new IOException("Insufficient length.");
            }

            var    encoding = getEncodingName(b4);
            string decl     = null;

            if (encoding.Equals("UTF-8"))
            {
                var sb = new StringBuilder();
                int c;
                while ((c = inp.ReadByte()) != -1)
                {
                    if (c == '>')
                    {
                        break;
                    }

                    sb.Append((char)c);
                }
                decl = sb.ToString();
            }
            else if (encoding.Equals("CP037"))
            {
                var bi = new MemoryStream();
                int c;
                while ((c = inp.ReadByte()) != -1)
                {
                    if (c == 0x6e) // that's '>' in ebcdic
                    {
                        break;
                    }

                    bi.WriteByte((byte)c);
                }
                decl = EncodingsRegistry.Instance.GetEncoding(37).GetString(bi.ToArray());//cp037 ebcdic
            }
            if (decl != null)
            {
                decl = getDeclaredEncoding(decl);
                if (decl != null)
                {
                    encoding = decl;
                }
            }
            Parse(doc, new StreamReader(inp, IanaEncodings.GetEncodingEncoding(encoding)));
        }
Пример #2
0
        /**
         * Parses the XML document firing the events to the handler.
         * @param doc the document handler
         * @param in the document. The encoding is deduced from the stream. The stream is not closed
         * @throws IOException on error
         */
        public static void Parse(ISimpleXMLDocHandler doc, Stream inp)
        {
            byte[] b4    = new byte[4];
            int    count = inp.Read(b4, 0, b4.Length);

            if (count != 4)
            {
                throw new IOException(MessageLocalization.GetComposedMessage("insufficient.length"));
            }
            String encoding = XMLUtil.GetEncodingName(b4);
            String decl     = null;

            if (encoding.Equals("UTF-8"))
            {
                StringBuilder sb = new StringBuilder();
                int           c;
                while ((c = inp.ReadByte()) != -1)
                {
                    if (c == '>')
                    {
                        break;
                    }
                    sb.Append((char)c);
                }
                decl = sb.ToString();
            }
            else if (encoding.Equals("CP037"))
            {
                MemoryStream bi = new MemoryStream();
                int          c;
                while ((c = inp.ReadByte()) != -1)
                {
                    if (c == 0x6e) // that's '>' in ebcdic
                    {
                        break;
                    }
                    bi.WriteByte((byte)c);
                }
                decl = Encoding.GetEncoding(37).GetString(bi.ToArray());//cp037 ebcdic
            }
            if (decl != null)
            {
                decl = GetDeclaredEncoding(decl);
                if (decl != null)
                {
                    encoding = decl;
                }
            }
            Parse(doc, new StreamReader(inp, IanaEncodings.GetEncodingEncoding(encoding)));
        }