Beispiel #1
0
        public override bool VisitEnter(TiXmlElement element, TiXmlAttribute firstAttribute)
        {
            DoIndent();
            buffer.Append("<");
            buffer.Append(element.Value());

            for (TiXmlAttribute attrib = firstAttribute; attrib != null; attrib = attrib.Next())
            {
                buffer.Append(" ");
                attrib.Print(buffer, 0);
            }

            if (element.FirstChild() == null)
            {
                buffer.Append(" />");
                DoLineBreak();
            }
            else
            {
                buffer.Append(">");
                if (element.FirstChild().ToText() != null &&
                    element.LastChild() == element.FirstChild() &&
                    element.FirstChild().ToText().CDATA() == false)
                {
                    simpleTextPrint = true;
                    // no DoLineBreak()!
                }
                else
                {
                    DoLineBreak();
                }
            }
            ++depth;
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Sets an attribute of name to a given value. The attribute will be created if it does not exist, or changed if it does.
        /// </summary>
        public void SetAttribute(string cname, string cvalue)
        {
            TiXmlAttribute node = attributeSet.Find(cname);

            if (node != null)
            {
                node.SetValue(cvalue);
                return;
            }

            TiXmlAttribute attrib = new TiXmlAttribute(cname, cvalue);

            if (attrib != null)
            {
                attributeSet.Add(attrib);
            }
            else
            {
                TiXmlDocument document = GetDocument();
                if (document != null)
                {
                    document.SetError(ErrorType.TIXML_ERROR_OUT_OF_MEMORY, null, 0, null, TiXmlEncoding.TIXML_ENCODING_UNKNOWN);
                }
            }
        }
Beispiel #3
0
        public void Add(TiXmlAttribute addMe)
        {
            //assert( !Find( addMe.Name() ) );	// Shouldn't be multiply adding to the set.
            addMe.next = sentinel;
            addMe.prev = sentinel.prev;

            sentinel.prev.next = addMe;
            sentinel.prev      = addMe;
        }
Beispiel #4
0
        /// <summary>
        /// QueryIntAttribute examines the attribute - it is an alternative to the Attribute() method with richer error checking.
        /// If the attribute is an integer, it is stored in 'value' and the call returns TIXML_SUCCESS. If it is not an integer,
        /// it returns TIXML_WRONG_TYPE. If the attribute does not exist, then TIXML_NO_ATTRIBUTE is returned.
        /// </summary>
        public TiXmlQueryResult QueryIntAttribute(string name, ref int ival)
        {
            TiXmlAttribute node = attributeSet.Find(name);

            if (node == null)
            {
                return(TiXmlQueryResult.TIXML_NO_ATTRIBUTE);
            }
            return(node.QueryIntValue(ref ival));
        }
Beispiel #5
0
 /// <summary>
 /// Like clear, but initializes 'this' object as well
 /// </summary>
 protected void ClearThis()
 {
     Clear();
     while (attributeSet.First() != null)
     {
         TiXmlAttribute node = attributeSet.First();
         attributeSet.Remove(node);
         //delete node;
     }
 }
Beispiel #6
0
        /// <summary>
        /// Given an attribute name, Attribute() returns the value for the attribute of that name, or null if none exists.
        /// </summary>
        public string Attribute(string name)
        {
            TiXmlAttribute node = attributeSet.Find(name);

            if (node != null)
            {
                return(node.Value());
            }
            return(null);
        }
Beispiel #7
0
        /// <summary>
        /// Deletes an attribute with the given name.
        /// </summary>
        public void RemoveAttribute(string name)
        {
            TiXmlAttribute node = attributeSet.Find(name);

            if (node != null)
            {
                attributeSet.Remove(node);
                //delete node;
            }
        }
Beispiel #8
0
 public TiXmlAttribute Find(string _name)
 {
     for (TiXmlAttribute node = sentinel.next; node != sentinel; node = node.next)
     {
         if (string.Compare(node.Name(), _name) == 0)
         {
             return(node);
         }
     }
     return(null);
 }
Beispiel #9
0
        public void Remove(TiXmlAttribute removeMe)
        {
            TiXmlAttribute node;

            for (node = sentinel.next; node != sentinel; node = node.next)
            {
                if (node == removeMe)
                {
                    node.prev.next = node.next;
                    node.next.prev = node.prev;
                    node.next      = null;
                    node.prev      = null;
                    return;
                }
            }
            //assert( 0 );		// we tried to remove a non-linked attribute.
        }
Beispiel #10
0
        protected void CopyTo(TiXmlElement target)
        {
            // superclass:
            base.CopyTo(target);

            // Element class:
            // Clone the attributes, then clone the children.
            TiXmlAttribute attribute = null;

            for (attribute = attributeSet.First(); attribute != null; attribute = attribute.Next())
            {
                target.SetAttribute(attribute.Name(), attribute.Value());
            }

            TiXmlNode node = null;

            for (node = firstChild; node != null; node = node.NextSibling())
            {
                target.LinkEndChild(node.Clone());
            }
        }
Beispiel #11
0
        /*	Attribtue parsing starts: next char past '<'
         *                                       returns: next char past '>'
         */
        public override int Parse(string p, int index, TiXmlParsingData data, int encoding)
        {
            index = SkipWhiteSpace(p, index, encoding);
            TiXmlDocument document = GetDocument();

            if (p == null || index < 0 || index >= p.Length)
            {
                if (document != null)
                {
                    document.SetError(ErrorType.TIXML_ERROR_PARSING_ELEMENT, null, 0, null, encoding);
                }
                return(0);
            }

            if (data != null)
            {
                data.Stamp(p, index, encoding);
                location = data.Cursor();
            }

            if (p[index] != '<')
            {
                if (document != null)
                {
                    document.SetError(ErrorType.TIXML_ERROR_PARSING_ELEMENT, p, index, data, encoding);
                }
                return(0);
            }

            index = SkipWhiteSpace(p, index + 1, encoding);

            // Read the name.
            int pErr = index;

            index = ReadName(p, index, ref value, encoding);
            if (index < 0 || index >= p.Length)
            {
                if (document != null)
                {
                    document.SetError(ErrorType.TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME, p, pErr, data, encoding);
                }
                return(INVALID_STRING_INDEX);
            }

            string endTag = "</";

            endTag += value;
            endTag += ">";

            // Check for and read attributes. Also look for an empty
            // tag or an end tag.
            while (index >= 0 && index < p.Length)
            {
                pErr  = index;
                index = SkipWhiteSpace(p, index, encoding);
                if (index < 0 || index >= p.Length)
                {
                    if (document != null)
                    {
                        document.SetError(ErrorType.TIXML_ERROR_READING_ATTRIBUTES, p, pErr, data, encoding);
                    }
                    return(-1);
                }
                if (p[index] == '/')
                {
                    ++index;
                    // Empty tag.
                    if (p[index] != '>')
                    {
                        if (document != null)
                        {
                            document.SetError(ErrorType.TIXML_ERROR_PARSING_EMPTY, p, index, data, encoding);
                        }
                        return(0);
                    }
                    return(index + 1);
                }
                else if (p[index] == '>')
                {
                    // Done with attributes (if there were any.)
                    // Read the value -- which can include other
                    // elements -- read the end tag, and return.
                    ++index;
                    index = ReadValue(p, index, data, encoding); // Note this is an Element method, and will set the error if one happens.
                    if (index < 0 || index >= p.Length)
                    {                                            // We were looking for the end tag, but found nothing.
                                                                 // Fix for [ 1663758 ] Failure to report error on bad XML
                        if (document != null)
                        {
                            document.SetError(ErrorType.TIXML_ERROR_READING_END_TAG, p, index, data, encoding);
                        }
                        return(INVALID_STRING_INDEX);
                    }

                    // We should find the end tag now
                    if (StringEqual(p, index, endTag, false, encoding))
                    {
                        index += endTag.Length;
                        return(index);
                    }
                    else
                    {
                        if (document != null)
                        {
                            document.SetError(ErrorType.TIXML_ERROR_READING_END_TAG, p, index, data, encoding);
                        }
                        return(INVALID_STRING_INDEX);
                    }
                }
                else
                {
                    // Try to read an attribute:
                    TiXmlAttribute attrib = new TiXmlAttribute();
                    if (attrib == null)
                    {
                        if (document != null)
                        {
                            document.SetError(ErrorType.TIXML_ERROR_OUT_OF_MEMORY, p, pErr, data, encoding);
                        }
                        return(-1);
                    }

                    attrib.SetDocument(document);
                    pErr  = index;
                    index = attrib.Parse(p, index, data, encoding);

                    if (index < 0 || index >= p.Length)
                    {
                        if (document != null)
                        {
                            document.SetError(ErrorType.TIXML_ERROR_PARSING_ELEMENT, p, pErr, data, encoding);
                        }
                        //delete attrib;
                        return(INVALID_STRING_INDEX);
                    }

                    // Handle the strange case of double attributes:
                    TiXmlAttribute node = attributeSet.Find(attrib.Name());
                    if (node != null)
                    {
                        node.SetValue(attrib.Value());
                        //delete attrib;
                        return(INVALID_STRING_INDEX);
                    }

                    attributeSet.Add(attrib);
                }
            }
            return(index);
        }
Beispiel #12
0
        /// <summary>
        /// Print the Element to a FILE stream.
        /// </summary>
        public override void Print(StringBuilder cfile, int depth)
        {
            //assert( cfile );
            for (int i = 0; i < depth; i++)
            {
                //fprintf( cfile, "    " );
                cfile.Append("    ");
            }

            cfile.Append("<"); cfile.Append(value);
            //fprintf( cfile, "<%s", value.c_str() );

            for (TiXmlAttribute attrib = attributeSet.First(); attrib != null; attrib = attrib.Next())
            {
                cfile.Append(" ");
                //fprintf(cfile, " ");
                attrib.Print(cfile, depth);
            }

            // There are 3 different formatting approaches:
            // 1) An element without children is printed as a <foo /> node
            // 2) An element with only a text child is printed as <foo> text </foo>
            // 3) An element with children is printed on multiple lines.
            if (firstChild == null)
            {
                //fprintf(cfile, " />");
                cfile.Append(" />");
            }
            else if (firstChild == lastChild && firstChild.ToText() != null)
            {
                //fprintf(cfile, ">");
                cfile.Append(">");
                firstChild.Print(cfile, depth + 1);
                //fprintf(cfile, "</%s>", value.c_str());
                cfile.Append("</"); cfile.Append(value); cfile.Append(">");
            }
            else
            {
                //fprintf(cfile, ">");
                cfile.Append(">");

                for (TiXmlNode node = firstChild; node != null; node = node.NextSibling())
                {
                    if (node.ToText() == null)
                    {
                        //fprintf(cfile, "\n");
                        cfile.Append("\n");
                    }
                    node.Print(cfile, depth + 1);
                }
                //fprintf(cfile, "\n");
                cfile.Append("\n");
                for (int i = 0; i < depth; ++i)
                {
                    //fprintf(cfile, "    ");
                    cfile.Append("    ");
                }
                //fprintf(cfile, "</%s>", value.c_str());
                cfile.Append("</"); cfile.Append(value); cfile.Append(">");
            }
        }
Beispiel #13
0
 /// <summary>
 /// Visit an element.
 /// </summary>
 public virtual bool VisitEnter(TiXmlElement element, TiXmlAttribute firstAttribute)
 {
     return(true);
 }
Beispiel #14
0
        public override int Parse(string p, int index, TiXmlParsingData data, int _encoding)
        {
            index = SkipWhiteSpace(p, index, _encoding);
            // Find the beginning, find the end, and look for
            // the stuff in-between.
            TiXmlDocument document = GetDocument();

            if (index < 0 || index >= p.Length || !StringEqual(p, index, "<?xml", true, _encoding))
            {
                if (document != null)
                {
                    document.SetError(ErrorType.TIXML_ERROR_PARSING_DECLARATION, null, 0, null, _encoding);
                }
                return(INVALID_STRING_INDEX);
            }
            if (data != null)
            {
                data.Stamp(p, index, _encoding);
                location = data.Cursor();
            }
            p += 5;

            version    = "";
            encoding   = "";
            standalone = "";

            while (index >= 0 && index < p.Length)
            {
                if (p[index] == '>')
                {
                    ++index;
                    return(index);
                }

                index = SkipWhiteSpace(p, index, _encoding);
                if (StringEqual(p, index, "version", true, _encoding))
                {
                    TiXmlAttribute attrib = new TiXmlAttribute();
                    index   = attrib.Parse(p, index, data, _encoding);
                    version = attrib.Value();
                }
                else if (StringEqual(p, index, "encoding", true, _encoding))
                {
                    TiXmlAttribute attrib = new TiXmlAttribute();
                    index    = attrib.Parse(p, index, data, _encoding);
                    encoding = attrib.Value();
                }
                else if (StringEqual(p, index, "standalone", true, _encoding))
                {
                    TiXmlAttribute attrib = new TiXmlAttribute();
                    index      = attrib.Parse(p, index, data, _encoding);
                    standalone = attrib.Value();
                }
                else
                {
                    // Read over whatever it is.
                    while (index >= 0 && index < p.Length && p[index] != '>' && !IsWhiteSpace(p[index]))
                    {
                        ++index;
                    }
                }
            }
            return(INVALID_STRING_INDEX);
        }