diff options
author | Adrian Kummerländer | 2014-05-21 21:16:43 +0200 |
---|---|---|
committer | Adrian Kummerländer | 2014-05-21 21:16:43 +0200 |
commit | bbe16940e7a6f97128e99ad7642b964e48ecde2e (patch) | |
tree | 54b48400527fd4578984e332c8c6583fa735d298 /src/support | |
parent | 283101c02a7e6a71f221fe731168e9b0096e3766 (diff) | |
download | InputXSLT-bbe16940e7a6f97128e99ad7642b964e48ecde2e.tar InputXSLT-bbe16940e7a6f97128e99ad7642b964e48ecde2e.tar.gz InputXSLT-bbe16940e7a6f97128e99ad7642b964e48ecde2e.tar.bz2 InputXSLT-bbe16940e7a6f97128e99ad7642b964e48ecde2e.tar.lz InputXSLT-bbe16940e7a6f97128e99ad7642b964e48ecde2e.tar.xz InputXSLT-bbe16940e7a6f97128e99ad7642b964e48ecde2e.tar.zst InputXSLT-bbe16940e7a6f97128e99ad7642b964e48ecde2e.zip |
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
Diffstat (limited to 'src/support')
-rw-r--r-- | src/support/include_entity_resolver.cc | 76 | ||||
-rw-r--r-- | src/support/include_entity_resolver.h | 2 |
2 files changed, 50 insertions, 28 deletions
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<bool, std::string> 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<char>(systemId) - ); + auto filePath = extractFilePath(*XercesStringGuard<char>(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<XMLCh>(resolvedPath.string()) - ); - } + if ( resolvedPath.first ) { + return new xercesc::LocalFileInputSource( + *XercesStringGuard<XMLCh>(resolvedPath.second.string()) + ); + } else { + return nullptr; } - - return nullptr; } else { return nullptr; } @@ -67,4 +71,20 @@ xercesc::InputSource* IncludeEntityResolver::resolveEntity( } } +std::pair<bool, boost::filesystem::path> 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<bool, boost::filesystem::path> resolve(const std::string&); + private: std::vector<FilesystemContext> path_; |