Преглед изворни кода

Added SetForceCompactMode() for overriding the compact setting on a per-node level. All sub-nodes will be printed compact as well.

Uli Kusterer пре 12 година
родитељ
комит
593a33d061
3 измењених фајлова са 34 додато и 7 уклоњено
  1. 6 5
      tinyxml2.cpp
  2. 7 2
      tinyxml2.h
  3. 21 0
      xmltest.cpp

+ 6 - 5
tinyxml2.cpp

@@ -581,7 +581,8 @@ XMLNode::XMLNode( XMLDocument* doc ) :
     _parent( 0 ),
     _firstChild( 0 ), _lastChild( 0 ),
     _prev( 0 ), _next( 0 ),
-    _memPool( 0 )
+    _memPool( 0 ),
+	_forceCompactMode( false )
 {
 }
 
@@ -1981,17 +1982,17 @@ void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
 }
 
 
-void XMLPrinter::OpenElement( const char* name )
+void XMLPrinter::OpenElement( const char* name, bool compactMode )
 {
     if ( _elementJustOpened ) {
         SealElement();
     }
     _stack.Push( name );
 
-    if ( _textDepth < 0 && !_firstElement && !_compactMode ) {
+    if ( _textDepth < 0 && !_firstElement && !compactMode ) {
         Print( "\n" );
     }
-    if ( !_compactMode ) {
+    if ( !compactMode ) {
         PrintSpace( _depth );
     }
 
@@ -2187,7 +2188,7 @@ bool XMLPrinter::VisitEnter( const XMLDocument& doc )
 
 bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
 {
-    OpenElement( element.Name() );
+    OpenElement( element.Name(), _compactMode ? true : element.Parent()->GetForceCompactMode() );
     while ( attribute ) {
         PushAttribute( attribute->Name(), attribute->Value() );
         attribute = attribute->Next();

+ 7 - 2
tinyxml2.h

@@ -821,6 +821,9 @@ public:
 
     // internal
     virtual char* ParseDeep( char*, StrPair* );
+	
+	bool	GetForceCompactMode() const		{ if( _forceCompactMode || !Parent() ) return _forceCompactMode; return Parent()->GetForceCompactMode(); };
+	void	SetForceCompactMode( bool b )	{ _forceCompactMode = b; };
 
 protected:
     XMLNode( XMLDocument* );
@@ -837,6 +840,8 @@ protected:
 
     XMLNode*		_prev;
     XMLNode*		_next;
+	
+	bool			_forceCompactMode;
 
 private:
     MemPool*		_memPool;
@@ -1509,7 +1514,7 @@ public:
     char* ParseDeep( char* p, StrPair* endTag );
     virtual XMLNode* ShallowClone( XMLDocument* document ) const;
     virtual bool ShallowEqual( const XMLNode* compare ) const;
-
+	
 private:
     XMLElement( XMLDocument* doc );
     virtual ~XMLElement();
@@ -2007,7 +2012,7 @@ public:
     /** If streaming, start writing an element.
         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.
     void PushAttribute( const char* name, const char* value );
     void PushAttribute( const char* name, int value );

+ 21 - 0
xmltest.cpp

@@ -1012,6 +1012,27 @@ int main( int argc, const char ** argv )
 		ele->DeleteAttribute( "attrib3" );
 		XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
 	}
+	
+	{
+		XMLDocument	doc0;
+		XMLElement*	root = doc0.NewElement("root");
+		doc0.InsertEndChild(root);
+		XMLElement*	text = doc0.NewElement("text");
+		text->SetForceCompactMode(true);
+		root->InsertEndChild(text);
+		XMLText*	befText = doc0.NewText("Before ");
+		text->InsertEndChild(befText);
+		XMLElement*	tag = doc0.NewElement("tag");
+		text->InsertEndChild(tag);
+		XMLText*	tagText = doc0.NewText("Tag");
+		tag->InsertEndChild(tagText);
+		XMLText*	aftText = doc0.NewText(" After");
+		text->InsertEndChild(aftText);
+		
+		XMLPrinter printer;
+    	doc0.Print( &printer );
+		XMLTest( "Selective text wrapping", "<root>\n    <text>Before <tag>Tag</tag> After</text>\n</root>\n", printer.CStr() );
+	}
 
 	{
 		// Make sure an attribute with a space in it succeeds.