xmlTiny::TiXmlPrinter Class Reference

Print to memory functionality. More...

#include <OBTtinyxml.h>

Inheritance diagram for xmlTiny::TiXmlPrinter:

Inheritance graph
[legend]
Collaboration diagram for xmlTiny::TiXmlPrinter:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 TiXmlPrinter ()
virtual bool VisitEnter (const TiXmlDocument &doc)
 Visit a document.
virtual bool VisitExit (const TiXmlDocument &doc)
 Visit a document.
virtual bool VisitEnter (const TiXmlElement &element, const TiXmlAttribute *firstAttribute)
 Visit an element.
virtual bool VisitExit (const TiXmlElement &element)
 Visit an element.
virtual bool Visit (const TiXmlDeclaration &declaration)
 Visit a declaration.
virtual bool Visit (const TiXmlText &text)
 Visit a text node.
virtual bool Visit (const TiXmlComment &comment)
 Visit a comment node.
virtual bool Visit (const TiXmlUnknown &unknown)
 Visit an unknow node.
void SetIndent (const char *_indent)
 Set the indent characters for printing.
const char * Indent ()
 Query the indention string.
void SetLineBreak (const char *_lineBreak)
 Set the line breaking string.
const char * LineBreak ()
 Query the current line breaking string.
void SetStreamPrinting ()
 Switch over to "stream printing" which is the most dense formatting without linebreaks.
const char * CStr ()
 Return the result.
size_t Size ()
 Return the length of the result string.
const std::string & Str ()
 Return the result.

Private Member Functions

void DoIndent ()
void DoLineBreak ()

Private Attributes

int depth
bool simpleTextPrint
TIXML_STRING buffer
TIXML_STRING indent
TIXML_STRING lineBreak


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 1738 of file tiny/tmp/OBTtinyxml.h.


Constructor & Destructor Documentation

xmlTiny::TiXmlPrinter::TiXmlPrinter (  )  [inline]

Definition at line 1741 of file tiny/tmp/OBTtinyxml.h.

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


Member Function Documentation

bool xmlTiny::TiXmlPrinter::VisitEnter ( const TiXmlDocument  )  [virtual]

Visit a document.

Reimplemented from xmlTiny::TiXmlVisitor.

Definition at line 1761 of file tiny/tmp/OBTtinyxml.cpp.

01762 {
01763         return true;
01764 }

bool xmlTiny::TiXmlPrinter::VisitExit ( const TiXmlDocument  )  [virtual]

Visit a document.

Reimplemented from xmlTiny::TiXmlVisitor.

Definition at line 1766 of file tiny/tmp/OBTtinyxml.cpp.

01767 {
01768         return true;
01769 }

bool xmlTiny::TiXmlPrinter::VisitEnter ( const TiXmlElement ,
const TiXmlAttribute  
) [virtual]

Visit an element.

Reimplemented from xmlTiny::TiXmlVisitor.

Definition at line 1771 of file tiny/tmp/OBTtinyxml.cpp.

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

01772 {
01773         DoIndent();
01774         buffer += "<";
01775         buffer += element.Value();
01776 
01777         for( const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
01778         {
01779                 buffer += " ";
01780                 attrib->Print( 0, 0, &buffer );
01781         }
01782 
01783         if ( !element.FirstChild() ) 
01784         {
01785                 buffer += " />";
01786                 DoLineBreak();
01787         }
01788         else 
01789         {
01790                 buffer += ">";
01791                 if (    element.FirstChild()->ToText()
01792                           && element.LastChild() == element.FirstChild()
01793                           && element.FirstChild()->ToText()->CDATA() == false )
01794                 {
01795                         simpleTextPrint = true;
01796                         // no DoLineBreak()!
01797                 }
01798                 else
01799                 {
01800                         DoLineBreak();
01801                 }
01802         }
01803         ++depth;        
01804         return true;
01805 }

bool xmlTiny::TiXmlPrinter::VisitExit ( const TiXmlElement  )  [virtual]

Visit an element.

Reimplemented from xmlTiny::TiXmlVisitor.

Definition at line 1808 of file tiny/tmp/OBTtinyxml.cpp.

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

01809 {
01810         --depth;
01811         if ( !element.FirstChild() ) 
01812         {
01813                 // nothing.
01814         }
01815         else 
01816         {
01817                 if ( simpleTextPrint )
01818                 {
01819                         simpleTextPrint = false;
01820                 }
01821                 else
01822                 {
01823                         DoIndent();
01824                 }
01825                 buffer += "</";
01826                 buffer += element.Value();
01827                 buffer += ">";
01828                 DoLineBreak();
01829         }
01830         return true;
01831 }

bool xmlTiny::TiXmlPrinter::Visit ( const TiXmlDeclaration  )  [virtual]

Visit a declaration.

Reimplemented from xmlTiny::TiXmlVisitor.

Definition at line 1862 of file tiny/tmp/OBTtinyxml.cpp.

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

01863 {
01864         DoIndent();
01865         declaration.Print( 0, 0, &buffer );
01866         DoLineBreak();
01867         return true;
01868 }

bool xmlTiny::TiXmlPrinter::Visit ( const TiXmlText  )  [virtual]

Visit a text node.

Reimplemented from xmlTiny::TiXmlVisitor.

Definition at line 1834 of file tiny/tmp/OBTtinyxml.cpp.

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

01835 {
01836         if ( text.CDATA() )
01837         {
01838                 DoIndent();
01839                 buffer += "<![CDATA[";
01840                 buffer += text.Value();
01841                 buffer += "]]>";
01842                 DoLineBreak();
01843         }
01844         else if ( simpleTextPrint )
01845         {
01846                 TIXML_STRING str;
01847                 TiXmlBase::EncodeString( text.ValueTStr(), &str );
01848                 buffer += str;
01849         }
01850         else
01851         {
01852                 DoIndent();
01853                 TIXML_STRING str;
01854                 TiXmlBase::EncodeString( text.ValueTStr(), &str );
01855                 buffer += str;
01856                 DoLineBreak();
01857         }
01858         return true;
01859 }

bool xmlTiny::TiXmlPrinter::Visit ( const TiXmlComment  )  [virtual]

Visit a comment node.

Reimplemented from xmlTiny::TiXmlVisitor.

Definition at line 1871 of file tiny/tmp/OBTtinyxml.cpp.

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

01872 {
01873         DoIndent();
01874         buffer += "<!--";
01875         buffer += comment.Value();
01876         buffer += "-->";
01877         DoLineBreak();
01878         return true;
01879 }

bool xmlTiny::TiXmlPrinter::Visit ( const TiXmlUnknown  )  [virtual]

Visit an unknow node.

Reimplemented from xmlTiny::TiXmlVisitor.

Definition at line 1882 of file tiny/tmp/OBTtinyxml.cpp.

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

01883 {
01884         DoIndent();
01885         buffer += "<";
01886         buffer += unknown.Value();
01887         buffer += ">";
01888         DoLineBreak();
01889         return true;
01890 }

void xmlTiny::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 1758 of file tiny/tmp/OBTtinyxml.h.

References indent.

01758 { indent = _indent ? _indent : "" ; }

const char* xmlTiny::TiXmlPrinter::Indent (  )  [inline]

Query the indention string.

Definition at line 1760 of file tiny/tmp/OBTtinyxml.h.

References indent.

01760 { return indent.c_str(); }

void xmlTiny::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 1765 of file tiny/tmp/OBTtinyxml.h.

References lineBreak.

01765 { lineBreak = _lineBreak ? _lineBreak : ""; }

const char* xmlTiny::TiXmlPrinter::LineBreak (  )  [inline]

Query the current line breaking string.

Definition at line 1767 of file tiny/tmp/OBTtinyxml.h.

References lineBreak.

01767 { return lineBreak.c_str(); }

void xmlTiny::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 1772 of file tiny/tmp/OBTtinyxml.h.

References indent, and lineBreak.

01772                                                                                 { indent = "";
01773                                                                                                           lineBreak = "";
01774                                                                                                         }       

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

Return the result.

Definition at line 1776 of file tiny/tmp/OBTtinyxml.h.

References buffer.

01776 { return buffer.c_str(); }

size_t xmlTiny::TiXmlPrinter::Size (  )  [inline]

Return the length of the result string.

Definition at line 1778 of file tiny/tmp/OBTtinyxml.h.

References buffer.

01778 { return buffer.size(); }

const std::string& xmlTiny::TiXmlPrinter::Str (  )  [inline]

Return the result.

Definition at line 1782 of file tiny/tmp/OBTtinyxml.h.

01782 { return buffer; }

void xmlTiny::TiXmlPrinter::DoIndent (  )  [inline, private]

Definition at line 1786 of file tiny/tmp/OBTtinyxml.h.

References buffer, depth, and indent.

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

01786                         {
01787                 for( int i=0; i<depth; ++i )
01788                         buffer += indent;
01789         }

void xmlTiny::TiXmlPrinter::DoLineBreak (  )  [inline, private]

Definition at line 1790 of file tiny/tmp/OBTtinyxml.h.

References buffer, and lineBreak.

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

01790                            {
01791                 buffer += lineBreak;
01792         }


Member Data Documentation

int xmlTiny::TiXmlPrinter::depth [private]

Definition at line 1794 of file tiny/tmp/OBTtinyxml.h.

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

bool xmlTiny::TiXmlPrinter::simpleTextPrint [private]

Definition at line 1795 of file tiny/tmp/OBTtinyxml.h.

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

TIXML_STRING xmlTiny::TiXmlPrinter::buffer [private]

Definition at line 1796 of file tiny/tmp/OBTtinyxml.h.

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

TIXML_STRING xmlTiny::TiXmlPrinter::indent [private]

Definition at line 1797 of file tiny/tmp/OBTtinyxml.h.

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

TIXML_STRING xmlTiny::TiXmlPrinter::lineBreak [private]

Definition at line 1798 of file tiny/tmp/OBTtinyxml.h.

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


Generated on Wed Oct 1 11:34:26 2008 for OBT by  doxygen 1.5.3