From 4d0569669e01434b3bb9f689cd176f590344bea8 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Thu, 22 May 2014 19:58:15 +0200 Subject: Added include path resolution to external file access functions * if a given file can not be located relative to the transformation's location both "read-file" and "read-xml-file" will now try to resolve the path using the IncludeEntityResolver * all external functions based on the FunctionBase template are now provided with a pointer to a IncludeEntityResolver instance * it is determined by the external function implementation whether include path resolution will be used --- src/function/base.h | 9 +++++++-- src/function/read_file.cc | 13 ++++++++++++- src/function/read_xml_file.cc | 13 ++++++++++++- src/function/transform.cc | 4 ---- src/function/transform.h | 7 +------ src/plattform_guard.cc | 6 +++--- 6 files changed, 35 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/function/base.h b/src/function/base.h index cc207c3..3609201 100644 --- a/src/function/base.h +++ b/src/function/base.h @@ -10,9 +10,10 @@ #include "common.h" #include "support/xalan_string.h" +#include "support/filesystem_context.h" +#include "support/include_entity_resolver.h" #include "support/tuple/mapper.h" #include "support/dom/document_cache.h" -#include "support/filesystem_context.h" namespace InputXSLT { @@ -24,7 +25,8 @@ class FunctionBase : public xalan::Function { public: typedef std::tuple parameter_tuple; - FunctionBase(): + FunctionBase(IncludeEntityResolver* resolver): + include_resolver_(resolver), document_cache_(std::make_shared()) { } virtual xalan::XObjectPtr execute( @@ -73,6 +75,9 @@ class FunctionBase : public xalan::Function { FunctionBase& operator=(const FunctionBase&) = delete; bool operator==(const FunctionBase&) const = delete; + protected: + IncludeEntityResolver* const include_resolver_; + private: std::shared_ptr document_cache_; diff --git a/src/function/read_file.cc b/src/function/read_file.cc index 5ded1ac..85f834d 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -28,10 +28,21 @@ xercesc::DOMDocument* FunctionReadFile::constructDocument( const FilesystemContext& fsContext, const FunctionBase::parameter_tuple& parameters ) { - const boost::filesystem::path filePath( + boost::filesystem::path filePath( fsContext.resolve(std::get<0>(parameters)) ); + if ( !(boost::filesystem::exists(filePath) && + boost::filesystem::is_regular_file(filePath)) ) { + auto resolvedPath = this->include_resolver_->resolve( + std::get<0>(parameters) + ); + + if ( resolvedPath.first ) { + filePath = resolvedPath.second; + } + } + xercesc::DOMDocument* const domDocument( xercesc::DOMImplementation::getImplementation()->createDocument( nullptr, diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc index 5f1d47d..b9246e6 100644 --- a/src/function/read_xml_file.cc +++ b/src/function/read_xml_file.cc @@ -38,10 +38,21 @@ xercesc::DOMDocument* FunctionReadXmlFile::constructDocument( const FilesystemContext& fsContext, const FunctionBase::parameter_tuple& parameters ) { - const boost::filesystem::path filePath( + boost::filesystem::path filePath( fsContext.resolve(std::get<0>(parameters)) ); + if ( !(boost::filesystem::exists(filePath) && + boost::filesystem::is_regular_file(filePath)) ) { + auto resolvedPath = this->include_resolver_->resolve( + std::get<0>(parameters) + ); + + if ( resolvedPath.first ) { + filePath = resolvedPath.second; + } + } + xercesc::DOMDocument* const domDocument( xercesc::DOMImplementation::getImplementation()->createDocument( nullptr, diff --git a/src/function/transform.cc b/src/function/transform.cc index 8823745..cf6ff09 100644 --- a/src/function/transform.cc +++ b/src/function/transform.cc @@ -9,10 +9,6 @@ namespace InputXSLT { -FunctionTransform::FunctionTransform(IncludeEntityResolver* resolver): - FunctionBase::FunctionBase(), - include_resolver_(resolver) { } - xercesc::DOMDocument* FunctionTransform::constructDocument( const InputXSLT::FilesystemContext& fsContext, const FunctionBase::parameter_tuple& parameters diff --git a/src/function/transform.h b/src/function/transform.h index 94fae53..810738e 100644 --- a/src/function/transform.h +++ b/src/function/transform.h @@ -3,8 +3,6 @@ #include "base.h" -#include "support/include_entity_resolver.h" - namespace InputXSLT { class FunctionTransform : public FunctionBase< @@ -14,7 +12,7 @@ class FunctionTransform : public FunctionBase< xalan::XObjectPtr > { public: - FunctionTransform(IncludeEntityResolver*); + using FunctionBase::FunctionBase; protected: friend FunctionBase; @@ -24,9 +22,6 @@ class FunctionTransform : public FunctionBase< const FunctionBase::parameter_tuple& ); - private: - IncludeEntityResolver* const include_resolver_; - }; } diff --git a/src/plattform_guard.cc b/src/plattform_guard.cc index 8ffbbb9..d66c8b5 100644 --- a/src/plattform_guard.cc +++ b/src/plattform_guard.cc @@ -25,19 +25,19 @@ PlattformGuard::PlattformGuard(const std::vector& path): xalan::XalanTransformer::installExternalFunctionGlobal( customNamespace, xalan::XalanDOMString("read-file"), - InputXSLT::FunctionReadFile() + InputXSLT::FunctionReadFile(&this->include_resolver_) ); xalan::XalanTransformer::installExternalFunctionGlobal( customNamespace, xalan::XalanDOMString("read-xml-file"), - InputXSLT::FunctionReadXmlFile() + InputXSLT::FunctionReadXmlFile(&this->include_resolver_) ); xalan::XalanTransformer::installExternalFunctionGlobal( customNamespace, xalan::XalanDOMString("read-directory"), - InputXSLT::FunctionReadDirectory() + InputXSLT::FunctionReadDirectory(&this->include_resolver_) ); xalan::XalanTransformer::installExternalFunctionGlobal( -- cgit v1.2.3