From 423b2109e76aaab996b62d594c78890c7c86af17 Mon Sep 17 00:00:00 2001 From: Markus Frank <Markus.Frank@cern.ch> Date: Mon, 22 Jan 2018 18:05:41 +0100 Subject: [PATCH] Add possibility to parse XML from string. Imporve DDCond manual. Start to adapt DDAlign manual --- DDCore/src/XML/DocumentHandler.cpp | 46 +++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/DDCore/src/XML/DocumentHandler.cpp b/DDCore/src/XML/DocumentHandler.cpp index 35bd6829f..3cd2264ae 100644 --- a/DDCore/src/XML/DocumentHandler.cpp +++ b/DDCore/src/XML/DocumentHandler.cpp @@ -527,20 +527,40 @@ Document DocumentHandler::parse(const char* bytes, size_t /* length */, const ch "[URI Resolution is not supported by TiXML]"); } TiXmlDocument* doc = new TiXmlDocument(); - try { - if ( 0 == doc->Parse(bytes) ) { - return (XmlDocument*)doc; - } - if ( doc->Error() ) { - printout(FATAL,"DocumentHandler", - "+++ Error (TinyXML) while parsing XML string [%s]", - doc->ErrorDesc()); - printout(FATAL,"DocumentHandler", - "+++ XML Document error: %s Location Line:%d Column:%d", - doc->Value(), doc->ErrorRow(), doc->ErrorCol()); - throw runtime_error(string("dd4hep: ")+doc->ErrorDesc()); + try { + if ( bytes ) { + size_t len = ::strlen(bytes); + // TiXml does not support white spaces at the end. Check and remove. + if ( bytes[len-1] != 0 || ::isspace(bytes[len-2]) ) { + char* buff = new char[len+1]; + try { + ::memcpy(buff, bytes, len+1); + buff[len] = 0; + for(size_t i=len-1; ::isspace(buff[i]); --i) buff[i] = 0; + if ( 0 == doc->Parse(buff) ) { + delete [] buff; + return (XmlDocument*)doc; + } + } + catch(...) { + } + delete [] buff; + } + if ( 0 == doc->Parse(bytes) ) { + return (XmlDocument*)doc; + } + if ( doc->Error() ) { + printout(FATAL,"DocumentHandler", + "+++ Error (TinyXML) while parsing XML string [%s]", + doc->ErrorDesc()); + printout(FATAL,"DocumentHandler", + "+++ XML Document error: %s Location Line:%d Column:%d", + doc->Value(), doc->ErrorRow(), doc->ErrorCol()); + throw runtime_error(string("dd4hep: ")+doc->ErrorDesc()); + } + throw runtime_error("dd4hep: Unknown error while parsing XML document string with TiXml."); } - throw runtime_error("dd4hep: Unknown error while parsing XML document with TiXml."); + throw runtime_error("dd4hep: FAILED to parse invalid document string [NULL] with TiXml."); } catch(exception& e) { printout(ERROR,"DocumentHandler","+++ Exception (TinyXML): parse(string):%s",e.what()); -- GitLab