FEDRA emulsion software from the OPERA Collaboration
TiXmlPrinter Class Reference

#include <tinyxml.h>

Inheritance diagram for TiXmlPrinter:
Collaboration diagram for TiXmlPrinter:

Public Member Functions

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

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() );

Constructor & Destructor Documentation

◆ TiXmlPrinter()

TiXmlPrinter::TiXmlPrinter ( )
inline
1740  : depth( 0 ), simpleTextPrint( false ),
1741  buffer(), indent( " " ), lineBreak( "\n" ) {}
TIXML_STRING lineBreak
Definition: tinyxml.h:1797
bool simpleTextPrint
Definition: tinyxml.h:1794
TIXML_STRING indent
Definition: tinyxml.h:1796
int depth
Definition: tinyxml.h:1793
TIXML_STRING buffer
Definition: tinyxml.h:1795

Member Function Documentation

◆ CStr()

const char* TiXmlPrinter::CStr ( )
inline

Return the result.

1775 { return buffer.c_str(); }

◆ DoIndent()

void TiXmlPrinter::DoIndent ( )
inlineprivate
1785  {
1786  for( int i=0; i<depth; ++i )
1787  buffer += indent;
1788  }

◆ DoLineBreak()

void TiXmlPrinter::DoLineBreak ( )
inlineprivate
1789  {
1790  buffer += lineBreak;
1791  }

◆ Indent()

const char* TiXmlPrinter::Indent ( )
inline

Query the indention string.

1759 { return indent.c_str(); }

◆ LineBreak()

const char* TiXmlPrinter::LineBreak ( )
inline

Query the current line breaking string.

1766 { return lineBreak.c_str(); }

◆ SetIndent()

void TiXmlPrinter::SetIndent ( const char *  _indent)
inline

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

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

◆ SetLineBreak()

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.

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

◆ SetStreamPrinting()

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.

1771  { indent = "";
1772  lineBreak = "";
1773  }

◆ Size()

size_t TiXmlPrinter::Size ( )
inline

Return the length of the result string.

1777 { return buffer.size(); }

◆ Visit() [1/4]

bool TiXmlPrinter::Visit ( const TiXmlComment )
virtual

Visit a comment node.

Reimplemented from TiXmlVisitor.

1867 {
1868  DoIndent();
1869  buffer += "<!--";
1870  buffer += comment.Value();
1871  buffer += "-->";
1872  DoLineBreak();
1873  return true;
1874 }
void DoLineBreak()
Definition: tinyxml.h:1789
void DoIndent()
Definition: tinyxml.h:1785

◆ Visit() [2/4]

bool TiXmlPrinter::Visit ( const TiXmlDeclaration )
virtual

Visit a declaration.

Reimplemented from TiXmlVisitor.

1858 {
1859  DoIndent();
1860  declaration.Print( 0, 0, &buffer );
1861  DoLineBreak();
1862  return true;
1863 }

◆ Visit() [3/4]

bool TiXmlPrinter::Visit ( const TiXmlText )
virtual

Visit a text node.

Reimplemented from TiXmlVisitor.

1830 {
1831  if ( text.CDATA() )
1832  {
1833  DoIndent();
1834  buffer += "<![CDATA[";
1835  buffer += text.Value();
1836  buffer += "]]>";
1837  DoLineBreak();
1838  }
1839  else if ( simpleTextPrint )
1840  {
1841  TIXML_STRING str;
1842  TiXmlBase::EncodeString( text.ValueTStr(), &str );
1843  buffer += str;
1844  }
1845  else
1846  {
1847  DoIndent();
1848  TIXML_STRING str;
1849  TiXmlBase::EncodeString( text.ValueTStr(), &str );
1850  buffer += str;
1851  DoLineBreak();
1852  }
1853  return true;
1854 }
TText * text
Definition: Canv_SYSTEMATICS_ALLCOMBINED__RMSEnergy__vs__Energy__ELECTRON.C:164
static void EncodeString(const TIXML_STRING &str, TIXML_STRING *out)
Definition: tinyxml.cpp:52
#define TIXML_STRING
Definition: tinyxml.h:53

◆ Visit() [4/4]

bool TiXmlPrinter::Visit ( const TiXmlUnknown )
virtual

Visit an unknown node.

Reimplemented from TiXmlVisitor.

1878 {
1879  DoIndent();
1880  buffer += "<";
1881  buffer += unknown.Value();
1882  buffer += ">";
1883  DoLineBreak();
1884  return true;
1885 }

◆ VisitEnter() [1/2]

bool TiXmlPrinter::VisitEnter ( const TiXmlDocument )
virtual

Visit a document.

Reimplemented from TiXmlVisitor.

1757 {
1758  return true;
1759 }

◆ VisitEnter() [2/2]

bool TiXmlPrinter::VisitEnter ( const TiXmlElement ,
const TiXmlAttribute  
)
virtual

Visit an element.

Reimplemented from TiXmlVisitor.

1767 {
1768  DoIndent();
1769  buffer += "<";
1770  buffer += element.Value();
1771 
1772  for( const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
1773  {
1774  buffer += " ";
1775  attrib->Print( 0, 0, &buffer );
1776  }
1777 
1778  if ( !element.FirstChild() )
1779  {
1780  buffer += " />";
1781  DoLineBreak();
1782  }
1783  else
1784  {
1785  buffer += ">";
1786  if ( element.FirstChild()->ToText()
1787  && element.LastChild() == element.FirstChild()
1788  && element.FirstChild()->ToText()->CDATA() == false )
1789  {
1790  simpleTextPrint = true;
1791  // no DoLineBreak()!
1792  }
1793  else
1794  {
1795  DoLineBreak();
1796  }
1797  }
1798  ++depth;
1799  return true;
1800 }
Definition: tinyxml.h:780
const TiXmlAttribute * Next() const
Get the next sibling attribute in the DOM. Returns null at end.
Definition: tinyxml.cpp:1170

◆ VisitExit() [1/2]

bool TiXmlPrinter::VisitExit ( const TiXmlDocument )
virtual

Visit a document.

Reimplemented from TiXmlVisitor.

1762 {
1763  return true;
1764 }

◆ VisitExit() [2/2]

bool TiXmlPrinter::VisitExit ( const TiXmlElement )
virtual

Visit an element.

Reimplemented from TiXmlVisitor.

1804 {
1805  --depth;
1806  if ( !element.FirstChild() )
1807  {
1808  // nothing.
1809  }
1810  else
1811  {
1812  if ( simpleTextPrint )
1813  {
1814  simpleTextPrint = false;
1815  }
1816  else
1817  {
1818  DoIndent();
1819  }
1820  buffer += "</";
1821  buffer += element.Value();
1822  buffer += ">";
1823  DoLineBreak();
1824  }
1825  return true;
1826 }

Member Data Documentation

◆ buffer

TIXML_STRING TiXmlPrinter::buffer
private

◆ depth

int TiXmlPrinter::depth
private

◆ indent

TIXML_STRING TiXmlPrinter::indent
private

◆ lineBreak

TIXML_STRING TiXmlPrinter::lineBreak
private

◆ simpleTextPrint

bool TiXmlPrinter::simpleTextPrint
private

The documentation for this class was generated from the following files: