Xml Xml::parse( const char *src , int& len , bool& p_error ) { if( src == NULL ) return( Xml() ); // parse TiXmlDocument *doc = new TiXmlDocument(); Xml xml; try { const char *res = doc -> Parse( src ); if( res != NULL ) { p_error = false; len = res - src; xml.attach( doc , doc -> FirstChildElement() ); } } catch( RuntimeException& e ) { Logger log; log.attachRoot(); log.printStack( e ); p_error = true; len = 0; } return( xml ); }
Xml Xml::create( const char *contentName ) { TiXmlDocument *doc = new TiXmlDocument(); TiXmlElement *top = new TiXmlElement( contentName ); doc -> LinkEndChild( top ); Xml xml; xml.attach( doc , top ); return( xml ); }
Xml Xml::getNextChild( String name ) { ASSERTMSG( node != NULL , name + " node parent is null" ); TiXmlElement *xml = ( TiXmlElement * )node; TiXmlElement *xmlChild = xml -> NextSiblingElement( name ); Xml item; item.attach( doc , xmlChild ); return( item ); }
Xml Xml::addElement( String name ) { ASSERTMSG( node != NULL , name + " node parent is null" ); TiXmlNode *xml = ( TiXmlNode * )node; TiXmlElement *xmlProp = new TiXmlElement( name ); xml -> LinkEndChild( xmlProp ); Xml ret; ret.attach( doc , xmlProp ); return( ret ); }
Xml Xml::load( const char *path ) { TiXmlDocument *doc = new TiXmlDocument( path ); if( !doc -> LoadFile() ) { String err = String( "loadXml: cannot load root configuration from path=" ) + path; delete doc; throw RuntimeError( err ); } Xml xml; xml.attach( doc , doc -> FirstChildElement() ); return( xml ); }
Xml Xml::addTextElement( String name , String value ) { ASSERTMSG( node != NULL , name + " node parent is null" ); TiXmlElement *xml = ( TiXmlElement * )node; TiXmlElement *xmlElement = new TiXmlElement( name ); xml -> LinkEndChild( xmlElement ); TiXmlText *xmlValue = new TiXmlText( value ); xmlElement -> LinkEndChild( xmlValue ); Xml ret; ret.attach( doc , xmlValue ); return( ret ); }
Xml Xml::getChildNode( String s ) { ASSERT( node != NULL ); TiXmlElement *xml = ( TiXmlElement * )node; TiXmlElement *xmlChild = NULL; try { xmlChild = xml -> FirstChildElement( s ); } catch( ... ) { // ignore - handle by null value returned } Xml x; x.attach( doc , xmlChild ); return( x ); }
Xml Xml::getFirstChild( String name ) { ASSERTMSG( node != NULL , name + " node parent is null" ); TiXmlElement *xml = ( TiXmlElement * )node; TiXmlElement *xmlChild = NULL; try { xmlChild = xml -> FirstChildElement( name ); } catch( ... ) { // ignore - handle by null value returned } Xml item; item.attach( doc , xmlChild ); return( item ); }
Xml Xml::getParentNode() { ASSERT( node != NULL ); TiXmlNode *xml = ( TiXmlNode * )node; TiXmlNode *xmlParent = NULL; try { xmlParent = xml -> Parent(); if( xmlParent -> Type() == TiXmlNode::DOCUMENT ) xmlParent = NULL; } catch( ... ) { // ignore - handle by null value returned } Xml x; x.attach( doc , xmlParent ); return( x ); }
Xml Xml::read( const char *data , const char *contentName ) { TiXmlDocument *doc = new TiXmlDocument(); const char *p = doc -> Parse( data ); if( doc -> Error() ) { delete doc; String err = "XML message cannot be read"; throw RuntimeError( err ); } // verify trailing data are only spaces if( p != NULL ) { char c; while( c = *p++ ) if( !( c == ' ' || c == '\t' || c == '\v' || c == '\n' || c == '\r' ) ) break; if( c ) { delete doc; String err = "XML message is malformed - data found behind message: "; err += data; throw RuntimeError( err ); } } // verify message has given child element try { TiXmlElement *item = doc -> FirstChildElement( contentName ); if( item != NULL ) { Xml xml; xml.attach( doc , item ); return( xml ); } } catch( ... ) { // ignore - generate own exception instead } delete doc; String err = String( "XML message is malformed - cannot find required top item: " ) + contentName; throw RuntimeError( err ); }