From 35334241ce4b76b1b1a66219ce938f27fdf39031 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Tue, 20 May 2014 21:17:10 +0200 Subject: Replaced FunctionResolveInclude with IncludeEntityResolver * xalan / xerces offers the possibility of implementing custom entity resolvers which are called upon by "" had to be implemented --- src/support/include_entity_resolver.cc | 70 ++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/support/include_entity_resolver.cc (limited to 'src/support/include_entity_resolver.cc') diff --git a/src/support/include_entity_resolver.cc b/src/support/include_entity_resolver.cc new file mode 100644 index 0000000..79631a5 --- /dev/null +++ b/src/support/include_entity_resolver.cc @@ -0,0 +1,70 @@ +#include "include_entity_resolver.h" + +#include + +#include "boost/filesystem.hpp" + +#include "support/xerces_string_guard.h" + +namespace InputXSLT { + +IncludeEntityResolver::IncludeEntityResolver( + const std::vector& path): + path_() { + this->path_.reserve(path.size()); + + std::transform( + path.begin(), + path.end(), + std::back_inserter(this->path_), + [](const std::string& path) -> FilesystemContext { + return FilesystemContext(path); + } + ); +} + +xercesc::InputSource* IncludeEntityResolver::resolveEntity( + const XMLCh* const, + const XMLCh* const systemId +) { + if ( systemId != nullptr ) { + const std::string rawPath( + *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 ( boost::filesystem::exists(resolvedPath) && + boost::filesystem::is_regular_file(resolvedPath) ) { + return new xercesc::LocalFileInputSource( + *XercesStringGuard(resolvedPath.string()) + ); + } + } + + return nullptr; + } else { + return nullptr; + } + } else { + return nullptr; + } +} + +} -- cgit v1.2.3