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

This commit is contained in:
Uli Kusterer 2014-02-01 12:48:51 +01:00
parent 5bb2d8079b
commit d5c9e8b81d
3 changed files with 34 additions and 7 deletions

View File

@ -580,7 +580,8 @@ XMLNode::XMLNode( XMLDocument* doc ) :
_parent( 0 ), _parent( 0 ),
_firstChild( 0 ), _lastChild( 0 ), _firstChild( 0 ), _lastChild( 0 ),
_prev( 0 ), _next( 0 ), _prev( 0 ), _next( 0 ),
_memPool( 0 ) _memPool( 0 ),
_forceCompactMode( false )
{ {
} }
@ -1945,17 +1946,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 );
} }
@ -2151,7 +2152,7 @@ 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() ); OpenElement( element.Name(), _compactMode ? true : element.Parent()->GetForceCompactMode() );
while ( attribute ) { while ( attribute ) {
PushAttribute( attribute->Name(), attribute->Value() ); PushAttribute( attribute->Name(), attribute->Value() );
attribute = attribute->Next(); attribute = attribute->Next();

View File

@ -822,6 +822,9 @@ public:
// internal // internal
virtual char* ParseDeep( char*, StrPair* ); virtual char* ParseDeep( char*, StrPair* );
bool GetForceCompactMode() const { if( _forceCompactMode || !Parent() ) return _forceCompactMode; return Parent()->GetForceCompactMode(); };
void SetForceCompactMode( bool b ) { _forceCompactMode = b; };
protected: protected:
XMLNode( XMLDocument* ); XMLNode( XMLDocument* );
virtual ~XMLNode(); virtual ~XMLNode();
@ -838,6 +841,8 @@ protected:
XMLNode* _prev; XMLNode* _prev;
XMLNode* _next; XMLNode* _next;
bool _forceCompactMode;
private: private:
MemPool* _memPool; MemPool* _memPool;
void Unlink( XMLNode* child ); void Unlink( XMLNode* child );
@ -1961,7 +1966,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 );

View File

@ -1013,6 +1013,27 @@ int main( int argc, const char ** argv )
XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false ); 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. // Make sure an attribute with a space in it succeeds.
static const char* xml0 = "<element attribute1= \"Test Attribute\"/>"; static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";