コード例 #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);
        }
コード例 #2
0
        /// <summary>
        /// [internal use]
        /// Reads the "value" of the element -- another element, or text.
        /// This should terminate with the current end tag.
        /// </summary>
        protected int ReadValue(string p, int index, TiXmlParsingData data, int encoding)
        {
            TiXmlDocument document = GetDocument();

            // Read in text and elements in any order.
            int pWithWhiteSpace = index;

            index = SkipWhiteSpace(p, index, encoding);

            while (index >= 0 && index < p.Length)
            {
                if (p[index] != '<')
                {
                    // Take what we have, make a text element.
                    TiXmlText textNode = new TiXmlText("");

                    if (textNode == null)
                    {
                        if (document == null)
                        {
                            document.SetError(ErrorType.TIXML_ERROR_OUT_OF_MEMORY, null, 0, null, encoding);
                        }
                        return(INVALID_STRING_INDEX);
                    }

                    if (IsWhiteSpaceCondensed())
                    {
                        index = textNode.Parse(p, index, data, encoding);
                    }
                    else
                    {
                        // Special case: we want to keep the white space
                        // so that leading spaces aren't removed.
                        index = textNode.Parse(p, pWithWhiteSpace, data, encoding);
                    }

                    if (!textNode.Blank())
                    {
                        LinkEndChild(textNode);
                    }
                    //else
                    //	delete textNode;
                }
                else
                {
                    // We hit a '<'
                    // Have we hit a new element or an end tag? This could also be
                    // a TiXmlText in the "CDATA" style.
                    if (StringEqual(p, index, "</", false, encoding))
                    {
                        return(index);
                    }
                    else
                    {
                        TiXmlNode node = Identify(p, index, encoding);
                        if (node != null)
                        {
                            index = node.Parse(p, index, data, encoding);
                            LinkEndChild(node);
                        }
                        else
                        {
                            return(-1);
                        }
                    }
                }
                pWithWhiteSpace = index;
                index           = SkipWhiteSpace(p, index, encoding);
            }

            if (index < 0)
            {
                if (document != null)
                {
                    document.SetError(ErrorType.TIXML_ERROR_READING_ELEMENT_VALUE, null, 0, null, encoding);
                }
            }
            return(index);
        }