From bbe16940e7a6f97128e99ad7642b964e48ecde2e Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Wed, 21 May 2014 21:16:43 +0200 Subject: Moved include path resolution into separate member method * enables access to include path resolution from external functions ** this will be useful to enable reading files from the include path using the external read-file or read-xml-file functions * moved file path extraction into a function local to the compilation unit --- src/support/include_entity_resolver.cc | 76 +++++++++++++++++++++------------- src/support/include_entity_resolver.h | 2 + 2 files changed, 50 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/support/include_entity_resolver.cc b/src/support/include_entity_resolver.cc index 79631a5..a00d9e6 100644 --- a/src/support/include_entity_resolver.cc +++ b/src/support/include_entity_resolver.cc @@ -6,6 +6,29 @@ #include "support/xerces_string_guard.h" +namespace { + +std::pair extractFilePath(const std::string& rawPath) { + const std::size_t leadingDelimiter = rawPath.find_first_of('['); + const std::size_t closingDelimiter = rawPath.find_last_of(']'); + + if ( leadingDelimiter != std::string::npos && + closingDelimiter != std::string::npos && + leadingDelimiter < closingDelimiter ) { + return std::make_pair( + true, + rawPath.substr( + leadingDelimiter + 1, + closingDelimiter - leadingDelimiter - 1 + ) + ); + } else { + return std::make_pair(false, std::string()); + } +} + +} + namespace InputXSLT { IncludeEntityResolver::IncludeEntityResolver( @@ -28,37 +51,18 @@ xercesc::InputSource* IncludeEntityResolver::resolveEntity( const XMLCh* const systemId ) { if ( systemId != nullptr ) { - const std::string rawPath( - *XercesStringGuard(systemId) - ); + auto filePath = extractFilePath(*XercesStringGuard(systemId)); - const std::size_t leadingDelimiter = rawPath.find_first_of('['); - const std::size_t closingDelimiter = rawPath.find_last_of(']'); - - if ( leadingDelimiter != std::string::npos && - closingDelimiter != std::string::npos && - leadingDelimiter < closingDelimiter ) { - const std::string filePath( - rawPath.substr( - leadingDelimiter + 1, - closingDelimiter - leadingDelimiter - 1 - ) - ); - - for ( auto&& context : this->path_ ) { - const boost::filesystem::path resolvedPath( - context.resolve(filePath) - ); + if ( filePath.first ) { + auto resolvedPath = this->resolve(filePath.second); - if ( boost::filesystem::exists(resolvedPath) && - boost::filesystem::is_regular_file(resolvedPath) ) { - return new xercesc::LocalFileInputSource( - *XercesStringGuard(resolvedPath.string()) - ); - } + if ( resolvedPath.first ) { + return new xercesc::LocalFileInputSource( + *XercesStringGuard(resolvedPath.second.string()) + ); + } else { + return nullptr; } - - return nullptr; } else { return nullptr; } @@ -67,4 +71,20 @@ xercesc::InputSource* IncludeEntityResolver::resolveEntity( } } +std::pair IncludeEntityResolver::resolve( + const std::string& filePath) { + for ( auto&& context : this->path_ ) { + const boost::filesystem::path resolvedPath( + context.resolve(filePath) + ); + + if ( boost::filesystem::exists(resolvedPath) && + boost::filesystem::is_regular_file(resolvedPath) ) { + return std::make_pair(true, resolvedPath); + } + } + + return std::make_pair(false, boost::filesystem::path()); +} + } diff --git a/src/support/include_entity_resolver.h b/src/support/include_entity_resolver.h index 390cfe1..b87e10f 100644 --- a/src/support/include_entity_resolver.h +++ b/src/support/include_entity_resolver.h @@ -20,6 +20,8 @@ class IncludeEntityResolver : public xercesc::EntityResolver { const XMLCh* const ); + std::pair resolve(const std::string&); + private: std::vector path_; -- cgit v1.2.3