Esempio n. 1
0
        /// <summary>
        /// Parse the given null terminated block of xml data. Passing in an encoding to this
        /// method (either TIXML_ENCODING_LEGACY or TIXML_ENCODING_UTF8 will force TinyXml
        /// to use that encoding, regardless of what TinyXml might otherwise try to detect.
        /// </summary>
        public override int Parse(string p, int index, TiXmlParsingData prevData /* = null*/, int encoding /* = TIXML_DEFAULT_ENCODING*/)
        {
            ClearError();

            // Parse away, at the document level. Since a document
            // contains nothing but other tags, most of what happens
            // here is skipping white space.
            //if ( !p || !*p )
            if (p == null || index < 0 || index >= p.Length)
            {
                SetError(ErrorType.TIXML_ERROR_DOCUMENT_EMPTY, null, 0, null, TiXmlEncoding.TIXML_ENCODING_UNKNOWN);
                return(0);
            }

            // Note that, for a document, this needs to come
            // before the while space skip, so that parsing
            // starts from the pointer we are given.
            location.Clear();
            if (prevData != null)
            {
                location.row = prevData.cursor.row;
                location.col = prevData.cursor.col;
            }
            else
            {
                location.row = 0;
                location.col = 0;
            }
            TiXmlParsingData data = new TiXmlParsingData(p, index, TabSize(), location.row, location.col);

            location = data.Cursor();
#if UNUSED
            if (encoding == TiXmlEncoding.TIXML_ENCODING_UNKNOWN)
            {
                // Check for the Microsoft UTF-8 lead bytes.
                const unsigned char *pU = (const unsigned char *)p;
                if (*(pU + 0) && *(pU + 0) == TIXML_UTF_LEAD_0 &&
                    *(pU + 1) && *(pU + 1) == TIXML_UTF_LEAD_1 &&
                    *(pU + 2) && *(pU + 2) == TIXML_UTF_LEAD_2)
                {
                    encoding        = TIXML_ENCODING_UTF8;
                    useMicrosoftBOM = true;
                }
            }
#endif
            index = SkipWhiteSpace(p, index, encoding);
            //if ( !p )
            if (index < 0)
            {
                SetError(ErrorType.TIXML_ERROR_DOCUMENT_EMPTY, null, 0, null, TiXmlEncoding.TIXML_ENCODING_UNKNOWN);
                return(-1);
            }

            while (index >= 0 && index < p.Length)
            {
                TiXmlNode node = Identify(p, index, encoding);
                if (node != null)
                {
                    index = node.Parse(p, index, data, encoding);
                    LinkEndChild(node);
                }
                else
                {
                    break;
                }

                // Did we get encoding info?
                if (encoding == TiXmlEncoding.TIXML_ENCODING_UNKNOWN && node.ToDeclaration() != null)
                {
                    TiXmlDeclaration dec = node.ToDeclaration();
                    string           enc = dec.Encoding();
                    //assert( enc );

                    if (enc.Length == 0 /**enc == 0 */)
                    {
                        encoding = TiXmlEncoding.TIXML_ENCODING_UTF8;
                    }
                    else if (StringEqual(enc, 0, "UTF-8", true, TiXmlEncoding.TIXML_ENCODING_UNKNOWN))
                    {
                        encoding = TiXmlEncoding.TIXML_ENCODING_UTF8;
                    }
                    else if (StringEqual(enc, 0, "UTF8", true, TiXmlEncoding.TIXML_ENCODING_UNKNOWN))
                    {
                        encoding = TiXmlEncoding.TIXML_ENCODING_UTF8;                           // incorrect, but be nice
                    }
                    else
                    {
                        encoding = TiXmlEncoding.TIXML_ENCODING_LEGACY;
                    }
                }

                index = SkipWhiteSpace(p, index, encoding);
            }

            // Was this empty?
            if (firstChild == null)
            {
                SetError(ErrorType.TIXML_ERROR_DOCUMENT_EMPTY, null, 0, null, encoding);
                return(INVALID_STRING_INDEX);
            }

            // All is well.
            return(index);
        }