CMS 3D CMS Logo

Public Member Functions | Private Member Functions | Private Attributes

TiXmlPrinter Class Reference

#include <tinyxml.h>

Inheritance diagram for TiXmlPrinter:
TiXmlVisitor

List of all members.

Public Member Functions

const char * CStr ()
 Return the result.
const char * Indent ()
 Query the indention string.
const char * LineBreak ()
 Query the current line breaking string.
void SetIndent (const char *_indent)
void SetLineBreak (const char *_lineBreak)
void SetStreamPrinting ()
size_t Size ()
 Return the length of the result string.
const std::string & Str ()
 Return the result.
 TiXmlPrinter ()
virtual bool Visit (const TiXmlUnknown &unknown)
 Visit an unknow node.
virtual bool Visit (const TiXmlComment &comment)
 Visit a comment node.
virtual bool Visit (const TiXmlText &text)
 Visit a text node.
virtual bool Visit (const TiXmlDeclaration &declaration)
 Visit a declaration.
virtual bool VisitEnter (const TiXmlDocument &doc)
 Visit a document.
virtual bool VisitEnter (const TiXmlElement &element, const TiXmlAttribute *firstAttribute)
 Visit an element.
virtual bool VisitExit (const TiXmlDocument &doc)
 Visit a document.
virtual bool VisitExit (const TiXmlElement &element)
 Visit an element.

Private Member Functions

void DoIndent ()
void DoLineBreak ()

Private Attributes

TIXML_STRING buffer
int depth
TIXML_STRING indent
TIXML_STRING lineBreak
bool simpleTextPrint

Detailed Description

Print to memory functionality. The TiXmlPrinter is useful when you need to:

  1. Print to memory (especially in non-STL mode)
  2. Control formatting (line endings, etc.)

When constructed, the TiXmlPrinter is in its default "pretty printing" mode. Before calling Accept() you can call methods to control the printing of the XML document. After TiXmlNode::Accept() is called, the printed document can be accessed via the CStr(), Str(), and Size() methods.

TiXmlPrinter uses the Visitor API.

	TiXmlPrinter printer;
	printer.SetIndent( "\t" );

	doc.Accept( &printer );
	fprintf( stdout, "%s", printer.CStr() );
	

Definition at line 1736 of file tinyxml.h.


Constructor & Destructor Documentation

TiXmlPrinter::TiXmlPrinter ( ) [inline]

Definition at line 1739 of file tinyxml.h.

                       : depth( 0 ), simpleTextPrint( false ),
                                         buffer(), indent( "    " ), lineBreak( "\n" ) {}

Member Function Documentation

const char* TiXmlPrinter::CStr ( ) [inline]

Return the result.

Definition at line 1774 of file tinyxml.h.

References buffer.

{ return buffer.c_str(); }
void TiXmlPrinter::DoIndent ( ) [inline, private]

Definition at line 1784 of file tinyxml.h.

References buffer, depth, i, and indent.

Referenced by Visit(), VisitEnter(), and VisitExit().

                        {
                for( int i=0; i<depth; ++i )
                        buffer += indent;
        }
void TiXmlPrinter::DoLineBreak ( ) [inline, private]

Definition at line 1788 of file tinyxml.h.

References buffer, and lineBreak.

Referenced by Visit(), VisitEnter(), and VisitExit().

                           {
                buffer += lineBreak;
        }
const char* TiXmlPrinter::Indent ( ) [inline]

Query the indention string.

Definition at line 1758 of file tinyxml.h.

References indent.

{ return indent.c_str(); }
const char* TiXmlPrinter::LineBreak ( ) [inline]

Query the current line breaking string.

Definition at line 1765 of file tinyxml.h.

References lineBreak.

{ return lineBreak.c_str(); }
void TiXmlPrinter::SetIndent ( const char *  _indent) [inline]

Set the indent characters for printing. By default 4 spaces but tab () is also useful, or null/empty string for no indentation.

Definition at line 1756 of file tinyxml.h.

References indent.

{ indent = _indent ? _indent : "" ; }
void TiXmlPrinter::SetLineBreak ( const char *  _lineBreak) [inline]

Set the line breaking string. By default set to newline (
). Some operating systems prefer other characters, or can be set to the null/empty string for no indenation.

Definition at line 1763 of file tinyxml.h.

References lineBreak.

{ lineBreak = _lineBreak ? _lineBreak : ""; }
void TiXmlPrinter::SetStreamPrinting ( ) [inline]

Switch over to "stream printing" which is the most dense formatting without linebreaks. Common when the XML is needed for network transmission.

Definition at line 1770 of file tinyxml.h.

References indent, and lineBreak.

                                                                                { indent = "";
                                                                                                          lineBreak = "";
                                                                                                        }
size_t TiXmlPrinter::Size ( ) [inline]

Return the length of the result string.

Definition at line 1776 of file tinyxml.h.

References buffer.

{ return buffer.size(); }
const std::string& TiXmlPrinter::Str ( ) [inline]

Return the result.

Definition at line 1780 of file tinyxml.h.

References buffer.

{ return buffer; }
bool TiXmlPrinter::Visit ( const TiXmlUnknown ) [virtual]

Visit an unknow node.

Reimplemented from TiXmlVisitor.

Definition at line 1897 of file tinyxml.cc.

References buffer, DoIndent(), DoLineBreak(), and TiXmlNode::Value().

{
        DoIndent();
        buffer += "<";
        buffer += unknown.Value();
        buffer += ">";
        DoLineBreak();
        return true;
}
bool TiXmlPrinter::Visit ( const TiXmlComment ) [virtual]

Visit a comment node.

Reimplemented from TiXmlVisitor.

Definition at line 1886 of file tinyxml.cc.

References buffer, DoIndent(), DoLineBreak(), and TiXmlNode::Value().

{
        DoIndent();
        buffer += "<!--";
        buffer += comment.Value();
        buffer += "-->";
        DoLineBreak();
        return true;
}
bool TiXmlPrinter::Visit ( const TiXmlDeclaration ) [virtual]

Visit a declaration.

Reimplemented from TiXmlVisitor.

Definition at line 1877 of file tinyxml.cc.

References buffer, DoIndent(), DoLineBreak(), and TiXmlDeclaration::Print().

{
        DoIndent();
        declaration.Print( 0, 0, &buffer );
        DoLineBreak();
        return true;
}
bool TiXmlPrinter::Visit ( const TiXmlText ) [virtual]

Visit a text node.

Reimplemented from TiXmlVisitor.

Definition at line 1849 of file tinyxml.cc.

References buffer, TiXmlText::CDATA(), DoIndent(), DoLineBreak(), TiXmlBase::EncodeString(), simpleTextPrint, TIXML_STRING, TiXmlNode::Value(), and TiXmlNode::ValueTStr().

{
        if ( text.CDATA() )
        {
                DoIndent();
                buffer += "<![CDATA[";
                buffer += text.Value();
                buffer += "]]>";
                DoLineBreak();
        }
        else if ( simpleTextPrint )
        {
                TIXML_STRING str;
                TiXmlBase::EncodeString( text.ValueTStr(), &str );
                buffer += str;
        }
        else
        {
                DoIndent();
                TIXML_STRING str;
                TiXmlBase::EncodeString( text.ValueTStr(), &str );
                buffer += str;
                DoLineBreak();
        }
        return true;
}
bool TiXmlPrinter::VisitEnter ( const TiXmlDocument ) [virtual]

Visit a document.

Reimplemented from TiXmlVisitor.

Definition at line 1776 of file tinyxml.cc.

{
        return true;
}
bool TiXmlPrinter::VisitEnter ( const TiXmlElement ,
const TiXmlAttribute  
) [virtual]

Visit an element.

Reimplemented from TiXmlVisitor.

Definition at line 1786 of file tinyxml.cc.

References buffer, TiXmlText::CDATA(), depth, DoIndent(), DoLineBreak(), funct::false, TiXmlNode::FirstChild(), TiXmlNode::LastChild(), TiXmlAttribute::Next(), TiXmlAttribute::Print(), simpleTextPrint, TiXmlNode::ToText(), and TiXmlNode::Value().

{
        DoIndent();
        buffer += "<";
        buffer += element.Value();

        for( const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
        {
                buffer += " ";
                attrib->Print( 0, 0, &buffer );
        }

        if ( !element.FirstChild() )
        {
                buffer += " />";
                DoLineBreak();
        }
        else
        {
                buffer += ">";
                if (    element.FirstChild()->ToText()
                          && element.LastChild() == element.FirstChild()
                          && element.FirstChild()->ToText()->CDATA() == false )
                {
                        simpleTextPrint = true;
                        // no DoLineBreak()!
                }
                else
                {
                        DoLineBreak();
                }
        }
        ++depth;
        return true;
}
bool TiXmlPrinter::VisitExit ( const TiXmlElement ) [virtual]

Visit an element.

Reimplemented from TiXmlVisitor.

Definition at line 1823 of file tinyxml.cc.

References buffer, depth, DoIndent(), DoLineBreak(), TiXmlNode::FirstChild(), simpleTextPrint, and TiXmlNode::Value().

{
        --depth;
        if ( !element.FirstChild() )
        {
                // nothing.
        }
        else
        {
                if ( simpleTextPrint )
                {
                        simpleTextPrint = false;
                }
                else
                {
                        DoIndent();
                }
                buffer += "</";
                buffer += element.Value();
                buffer += ">";
                DoLineBreak();
        }
        return true;
}
bool TiXmlPrinter::VisitExit ( const TiXmlDocument ) [virtual]

Visit a document.

Reimplemented from TiXmlVisitor.

Definition at line 1781 of file tinyxml.cc.

{
        return true;
}

Member Data Documentation

TIXML_STRING TiXmlPrinter::buffer [private]

Definition at line 1794 of file tinyxml.h.

Referenced by CStr(), DoIndent(), DoLineBreak(), Size(), Str(), Visit(), VisitEnter(), and VisitExit().

int TiXmlPrinter::depth [private]

Definition at line 1792 of file tinyxml.h.

Referenced by DoIndent(), VisitEnter(), and VisitExit().

TIXML_STRING TiXmlPrinter::indent [private]

Definition at line 1795 of file tinyxml.h.

Referenced by DoIndent(), Indent(), SetIndent(), and SetStreamPrinting().

TIXML_STRING TiXmlPrinter::lineBreak [private]

Definition at line 1796 of file tinyxml.h.

Referenced by DoLineBreak(), LineBreak(), SetLineBreak(), and SetStreamPrinting().

Definition at line 1793 of file tinyxml.h.

Referenced by Visit(), VisitEnter(), and VisitExit().