From 72d12932fc3cd0f2766730ad3d5cdca4b8ca616a Mon Sep 17 00:00:00 2001 From: Bohdan Dudar Date: Tue, 1 Oct 2024 00:30:16 +0200 Subject: [PATCH 1/4] fix xercesc parsing of envarionment variables in the path --- DDCore/src/XML/DocumentHandler.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/DDCore/src/XML/DocumentHandler.cpp b/DDCore/src/XML/DocumentHandler.cpp index 21c15af0c..73a8db737 100644 --- a/DDCore/src/XML/DocumentHandler.cpp +++ b/DDCore/src/XML/DocumentHandler.cpp @@ -328,13 +328,20 @@ Document DocumentHandler::load(Handle_t base, const XMLCh* fname, UriReader* rea /// Load XML file and parse it using URI resolver to read data. Document DocumentHandler::load(const std::string& fname, UriReader* reader) const { + auto _clean_fname = [](const std::string& s) -> std::string { + const std::string& temp = getEnviron(s); + std::string temp2 = undressed_file_name(temp.empty() ? s : temp); + if ( strncmp(temp2.c_str(),"file:",5) == 0 ) return temp2.substr(5); + return temp2; + }; + auto fname_clean = _clean_fname(fname); std::string path; - printout(DEBUG,"DocumentHandler","+++ Loading document URI: %s",fname.c_str()); + printout(DEBUG,"DocumentHandler","+++ Loading document URI: %s",fname_clean.c_str()); try { - size_t idx = fname.find(':'); - size_t idq = fname.find('/'); + size_t idx = fname_clean.find(':'); + size_t idq = fname_clean.find('/'); if ( idq == std::string::npos ) idq = 0; - XMLURL xerurl = (const XMLCh*) Strng_t(idx==std::string::npos || idx>idq ? "file:"+fname : fname); + XMLURL xerurl = (const XMLCh*) Strng_t(idx==std::string::npos || idx>idq ? "file:"+fname_clean : fname_clean); std::string proto = _toString(xerurl.getProtocolName()); path = _toString(xerurl.getPath()); printout(DEBUG,"DocumentHandler","+++ protocol:%s path:%s",proto.c_str(), path.c_str()); @@ -348,8 +355,8 @@ Document DocumentHandler::load(const std::string& fname, UriReader* reader) cons if ( reader ) reader->parserLoaded(path); } else { - if ( reader && reader->load(fname, path) ) { - MemBufInputSource src((const XMLByte*)path.c_str(), path.length(), fname.c_str(), false); + if ( reader && reader->load(fname_clean, path) ) { + MemBufInputSource src((const XMLByte*)path.c_str(), path.length(), fname_clean.c_str(), false); parser->parse(src); return (XmlDocument*)parser->adoptDocument(); } @@ -359,7 +366,7 @@ Document DocumentHandler::load(const std::string& fname, UriReader* reader) cons catch (const std::exception& e) { printout(ERROR,"DocumentHandler","+++ Exception(XercesC): parse(path):%s",e.what()); try { - parser->parse(fname.c_str()); + parser->parse(fname_clean.c_str()); if ( reader ) reader->parserLoaded(path); } catch (const std::exception& ex) { From 7c0b52bdefdc211052c8973cbe5ccaa36e168bd9 Mon Sep 17 00:00:00 2001 From: Bohdan Dudar Date: Tue, 1 Oct 2024 20:29:16 +0200 Subject: [PATCH 2/4] Use only one function at the top --- DDCore/src/XML/DocumentHandler.cpp | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/DDCore/src/XML/DocumentHandler.cpp b/DDCore/src/XML/DocumentHandler.cpp index 73a8db737..38846f57a 100644 --- a/DDCore/src/XML/DocumentHandler.cpp +++ b/DDCore/src/XML/DocumentHandler.cpp @@ -39,6 +39,14 @@ namespace { return fn; } int s_minPrintLevel = dd4hep::INFO; + + static std::string _clean_fname(const std::string& s) { + std::string const& temp = getEnviron(s); + std::string temp2 = undressed_file_name(temp.empty() ? s : temp); + if ( strncmp(temp2.c_str(),"file:",5)==0 ) return temp2.substr(5); + return temp2; + } + } #ifndef __TIXML__ @@ -328,12 +336,6 @@ Document DocumentHandler::load(Handle_t base, const XMLCh* fname, UriReader* rea /// Load XML file and parse it using URI resolver to read data. Document DocumentHandler::load(const std::string& fname, UriReader* reader) const { - auto _clean_fname = [](const std::string& s) -> std::string { - const std::string& temp = getEnviron(s); - std::string temp2 = undressed_file_name(temp.empty() ? s : temp); - if ( strncmp(temp2.c_str(),"file:",5) == 0 ) return temp2.substr(5); - return temp2; - }; auto fname_clean = _clean_fname(fname); std::string path; printout(DEBUG,"DocumentHandler","+++ Loading document URI: %s",fname_clean.c_str()); @@ -356,7 +358,7 @@ Document DocumentHandler::load(const std::string& fname, UriReader* reader) cons } else { if ( reader && reader->load(fname_clean, path) ) { - MemBufInputSource src((const XMLByte*)path.c_str(), path.length(), fname_clean.c_str(), false); + MemBufInputSource src((const XMLByte*)path.c_str(), path.length(), fname.c_str(), false); parser->parse(src); return (XmlDocument*)parser->adoptDocument(); } @@ -366,7 +368,7 @@ Document DocumentHandler::load(const std::string& fname, UriReader* reader) cons catch (const std::exception& e) { printout(ERROR,"DocumentHandler","+++ Exception(XercesC): parse(path):%s",e.what()); try { - parser->parse(fname_clean.c_str()); + parser->parse(fname.c_str()); if ( reader ) reader->parserLoaded(path); } catch (const std::exception& ex) { @@ -438,15 +440,6 @@ namespace dd4hep { }; }} -namespace { - static std::string _clean_fname(const std::string& s) { - std::string const& temp = getEnviron(s); - std::string temp2 = undressed_file_name(temp.empty() ? s : temp); - if ( strncmp(temp2.c_str(),"file:",5)==0 ) return temp2.substr(5); - return temp2; - } -} - /// System ID of a given XML entity std::string DocumentHandler::system_path(Handle_t base, const std::string& fname) { std::string fn, clean = _clean_fname(fname); From 10a36d6406da22b8fdf7826418f64783ed69407d Mon Sep 17 00:00:00 2001 From: Bohdan Dudar Date: Tue, 1 Oct 2024 20:31:24 +0200 Subject: [PATCH 3/4] Remove extra static. Unnamed namespace already makes it visible only to the translation unit. --- DDCore/src/XML/DocumentHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DDCore/src/XML/DocumentHandler.cpp b/DDCore/src/XML/DocumentHandler.cpp index 38846f57a..a8841f219 100644 --- a/DDCore/src/XML/DocumentHandler.cpp +++ b/DDCore/src/XML/DocumentHandler.cpp @@ -40,7 +40,7 @@ namespace { } int s_minPrintLevel = dd4hep::INFO; - static std::string _clean_fname(const std::string& s) { + std::string _clean_fname(const std::string& s) { std::string const& temp = getEnviron(s); std::string temp2 = undressed_file_name(temp.empty() ? s : temp); if ( strncmp(temp2.c_str(),"file:",5)==0 ) return temp2.substr(5); From 6c7019910f6d9337b50e6b70ac3b36782136e902 Mon Sep 17 00:00:00 2001 From: Bohdan Dudar Date: Tue, 1 Oct 2024 20:37:47 +0200 Subject: [PATCH 4/4] Add explanation --- DDCore/src/XML/DocumentHandler.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/DDCore/src/XML/DocumentHandler.cpp b/DDCore/src/XML/DocumentHandler.cpp index a8841f219..be750e315 100644 --- a/DDCore/src/XML/DocumentHandler.cpp +++ b/DDCore/src/XML/DocumentHandler.cpp @@ -40,9 +40,10 @@ namespace { } int s_minPrintLevel = dd4hep::INFO; - std::string _clean_fname(const std::string& s) { - std::string const& temp = getEnviron(s); - std::string temp2 = undressed_file_name(temp.empty() ? s : temp); + std::string _clean_fname(const std::string& filepath) { + // This function seems to resolve environment variables inside the filepath string and return resolved string + std::string const& temp = getEnviron(filepath); + std::string temp2 = undressed_file_name( temp.empty() ? filepath : temp ); if ( strncmp(temp2.c_str(),"file:",5)==0 ) return temp2.substr(5); return temp2; }