Browse Source

Provide finer control over indentation inside the XMLPrinter. You'll have to subclass it to override its standard behaviour by overwriting CompactMode().

Uli Kusterer 12 years ago
parent
commit
613410095c
2 changed files with 15 additions and 11 deletions
  1. 11 9
      tinyxml2.cpp
  2. 4 2
      tinyxml2.h

+ 11 - 9
tinyxml2.cpp

@@ -1940,17 +1940,17 @@ void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
 }
 }
 
 
 
 
-void XMLPrinter::OpenElement( const char* name )
+void XMLPrinter::OpenElement( const char* name, bool compactMode )
 {
 {
     if ( _elementJustOpened ) {
     if ( _elementJustOpened ) {
         SealElement();
         SealElement();
     }
     }
     _stack.Push( name );
     _stack.Push( name );
 
 
-    if ( _textDepth < 0 && !_firstElement && !_compactMode ) {
+    if ( _textDepth < 0 && !_firstElement && !compactMode ) {
         Print( "\n" );
         Print( "\n" );
     }
     }
-    if ( !_compactMode ) {
+    if ( !compactMode ) {
         PrintSpace( _depth );
         PrintSpace( _depth );
     }
     }
 
 
@@ -2002,7 +2002,7 @@ void XMLPrinter::PushAttribute( const char* name, double v )
 }
 }
 
 
 
 
-void XMLPrinter::CloseElement()
+void XMLPrinter::CloseElement( bool compactMode )
 {
 {
     --_depth;
     --_depth;
     const char* name = _stack.Pop();
     const char* name = _stack.Pop();
@@ -2011,7 +2011,7 @@ void XMLPrinter::CloseElement()
         Print( "/>" );
         Print( "/>" );
     }
     }
     else {
     else {
-        if ( _textDepth < 0 && !_compactMode) {
+        if ( _textDepth < 0 && !compactMode) {
             Print( "\n" );
             Print( "\n" );
             PrintSpace( _depth );
             PrintSpace( _depth );
         }
         }
@@ -2021,7 +2021,7 @@ void XMLPrinter::CloseElement()
     if ( _textDepth == _depth ) {
     if ( _textDepth == _depth ) {
         _textDepth = -1;
         _textDepth = -1;
     }
     }
-    if ( _depth == 0 && !_compactMode) {
+    if ( _depth == 0 && !compactMode) {
         Print( "\n" );
         Print( "\n" );
     }
     }
     _elementJustOpened = false;
     _elementJustOpened = false;
@@ -2146,7 +2146,9 @@ bool XMLPrinter::VisitEnter( const XMLDocument& doc )
 
 
 bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
 bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
 {
 {
-    OpenElement( element.Name() );
+	const XMLElement*	parentElem = element.Parent()->ToElement();
+	bool		compactMode = parentElem ? CompactMode(*parentElem) : _compactMode;
+    OpenElement( element.Name(), compactMode );
     while ( attribute ) {
     while ( attribute ) {
         PushAttribute( attribute->Name(), attribute->Value() );
         PushAttribute( attribute->Name(), attribute->Value() );
         attribute = attribute->Next();
         attribute = attribute->Next();
@@ -2155,9 +2157,9 @@ bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attr
 }
 }
 
 
 
 
-bool XMLPrinter::VisitExit( const XMLElement& )
+bool XMLPrinter::VisitExit( const XMLElement& element )
 {
 {
-    CloseElement();
+    CloseElement( CompactMode(element) );
     return true;
     return true;
 }
 }
 
 

+ 4 - 2
tinyxml2.h

@@ -1967,7 +1967,7 @@ public:
     /** If streaming, start writing an element.
     /** If streaming, start writing an element.
         The element must be closed with CloseElement()
         The element must be closed with CloseElement()
     */
     */
-    void OpenElement( const char* name );
+    void OpenElement( const char* name, bool compactMode );
     /// If streaming, add an attribute to an open element.
     /// If streaming, add an attribute to an open element.
     void PushAttribute( const char* name, const char* value );
     void PushAttribute( const char* name, const char* value );
     void PushAttribute( const char* name, int value );
     void PushAttribute( const char* name, int value );
@@ -1975,7 +1975,7 @@ public:
     void PushAttribute( const char* name, bool value );
     void PushAttribute( const char* name, bool value );
     void PushAttribute( const char* name, double value );
     void PushAttribute( const char* name, double value );
     /// If streaming, close the Element.
     /// If streaming, close the Element.
-    virtual void CloseElement();
+    virtual void CloseElement( bool compactMode );
 
 
     /// Add a text node.
     /// Add a text node.
     void PushText( const char* text, bool cdata=false );
     void PushText( const char* text, bool cdata=false );
@@ -2034,6 +2034,8 @@ public:
     }
     }
 
 
 protected:
 protected:
+	virtual bool CompactMode( const XMLElement& elem )	{ return _compactMode; };
+
 	/** Prints out the space before an element. You may override to change
 	/** Prints out the space before an element. You may override to change
 	    the space and tabs used. A PrintSpace() override should call Print().
 	    the space and tabs used. A PrintSpace() override should call Print().
 	*/
 	*/