From 0d670478b51c55e44f57995fe3ca8a4585723a6c Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Tue, 17 Jun 2014 21:28:04 +0200 Subject: Added context awareness to XObjectValue casting logic * added support for defining boost::filesystem::path as a external function parameter ** boost::filesystem::path parameters are resolved against the appropriate FilesystemContext and IncludeEntityResolver instances * xalan::XSLTInputSource parameter source paths are also resolved * removed need for passing a reference FilesystemContext to "constructDocument" methods ** they now only accept the parameters of the external function implemented by them ** all path resolution logic is wrapped by the newly created XObjectValue class * converted XObjectValue namespace into class ** the "get" template method is now a template member method ** this was needed to enable value casting logic to access the appropriate FilesystemContext and IncludeEntityResolver instances * this commit marks the next step towards the goals defined in 741a70f --- src/function/base.h | 14 ++++---- src/function/external_text_formatter.cc | 1 - src/function/external_text_formatter.h | 1 - src/function/read_directory.cc | 10 ++---- src/function/read_directory.h | 9 +++-- src/function/read_file.cc | 13 +------ src/function/read_file.h | 9 +++-- src/function/transform.cc | 11 +++--- src/function/transform.h | 7 ++-- src/support/filesystem_context.cc | 63 ++++++++++++--------------------- src/support/filesystem_context.h | 22 ++++-------- src/support/include_entity_resolver.cc | 2 +- src/support/include_entity_resolver.h | 2 +- src/support/type/xobject_value.cc | 55 ++++++++++++++++++++++------ src/support/type/xobject_value.h | 21 ++++++++--- 15 files changed, 121 insertions(+), 119 deletions(-) (limited to 'src') diff --git a/src/function/base.h b/src/function/base.h index 5212d65..5aada0e 100644 --- a/src/function/base.h +++ b/src/function/base.h @@ -96,16 +96,18 @@ class FunctionBase : public xalan::Function { const xalan::Locator* locator, Sequence ) const { + XObjectValue valueGetter( + this->include_resolver_->resolve( + locator->getSystemId() + ), + this->include_resolver_ + ); + return this->document_cache_->create( static_cast( const_cast(this) )->constructDocument( - FilesystemContext( - this->include_resolver_->resolve( - locator->getSystemId() - ) - ), - XObjectValue::get >::type>(parameters[Index])... diff --git a/src/function/external_text_formatter.cc b/src/function/external_text_formatter.cc index 4171ded..b2f9d33 100644 --- a/src/function/external_text_formatter.cc +++ b/src/function/external_text_formatter.cc @@ -43,7 +43,6 @@ inline xercesc::DOMNode* importDocumentElement( namespace InputXSLT { xercesc::DOMDocument* FunctionExternalTextFormatter::constructDocument( - const InputXSLT::FilesystemContext&, std::string formatterPath, std::string stdinText ) { diff --git a/src/function/external_text_formatter.h b/src/function/external_text_formatter.h index 95ff127..3af1d52 100644 --- a/src/function/external_text_formatter.h +++ b/src/function/external_text_formatter.h @@ -17,7 +17,6 @@ class FunctionExternalTextFormatter : public FunctionBase< friend FunctionBase; xercesc::DOMDocument* constructDocument( - const FilesystemContext&, std::string, std::string ); diff --git a/src/function/read_directory.cc b/src/function/read_directory.cc index cfbe302..884963e 100644 --- a/src/function/read_directory.cc +++ b/src/function/read_directory.cc @@ -10,13 +10,7 @@ namespace InputXSLT { xercesc::DOMDocument* FunctionReadDirectory::constructDocument( - const InputXSLT::FilesystemContext& fsContext, - std::string path -) { - const boost::filesystem::path directoryPath( - fsContext.resolve(path) - ); - + boost::filesystem::path directoryPath) { xercesc::DOMDocument* const domDocument( xercesc::DOMImplementation::getImplementation()->createDocument( nullptr, @@ -37,7 +31,7 @@ xercesc::DOMDocument* FunctionReadDirectory::constructDocument( xercesc::DOMNode* const resultNode = result.getNode(); - fsContext.iterate( + FilesystemContext::iterate( directoryPath, [&domDocument, &resultNode](const boost::filesystem::path& p) { ResultNodeFacade result(domDocument, resultNode, "entry"); diff --git a/src/function/read_directory.h b/src/function/read_directory.h index 6366a09..41976d5 100644 --- a/src/function/read_directory.h +++ b/src/function/read_directory.h @@ -1,13 +1,15 @@ #ifndef INPUTXSLT_SRC_FUNCTION_READ_DIRECTORY_H_ #define INPUTXSLT_SRC_FUNCTION_READ_DIRECTORY_H_ +#include "boost/filesystem.hpp" + #include "base.h" namespace InputXSLT { class FunctionReadDirectory : public FunctionBase< FunctionReadDirectory, - std::string + boost::filesystem::path > { public: using FunctionBase::FunctionBase; @@ -15,10 +17,7 @@ class FunctionReadDirectory : public FunctionBase< protected: friend FunctionBase; - xercesc::DOMDocument* constructDocument( - const FilesystemContext&, - std::string - ); + xercesc::DOMDocument* constructDocument(boost::filesystem::path); }; diff --git a/src/function/read_file.cc b/src/function/read_file.cc index 1ea76c7..96e677f 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -49,18 +49,7 @@ inline std::string readPlainFile(const boost::filesystem::path& filePath) { namespace InputXSLT { xercesc::DOMDocument* FunctionReadFile::constructDocument( - const FilesystemContext& fsContext, - std::string rawPath -) { - boost::filesystem::path filePath(fsContext.resolve(rawPath)); - - if ( !(boost::filesystem::exists(filePath) && - boost::filesystem::is_regular_file(filePath)) ) { - if ( auto resolvedPath = this->include_resolver_->resolve(rawPath) ) { - filePath = *resolvedPath; - } - } - + boost::filesystem::path filePath) { xercesc::DOMDocument* const domDocument( xercesc::DOMImplementation::getImplementation()->createDocument( nullptr, diff --git a/src/function/read_file.h b/src/function/read_file.h index fec8fe2..9c2185a 100644 --- a/src/function/read_file.h +++ b/src/function/read_file.h @@ -1,13 +1,15 @@ #ifndef INPUTXSLT_SRC_FUNCTION_READ_FILE_H_ #define INPUTXSLT_SRC_FUNCTION_READ_FILE_H_ +#include "boost/filesystem.hpp" + #include "base.h" namespace InputXSLT { class FunctionReadFile : public FunctionBase< FunctionReadFile, - std::string + boost::filesystem::path > { public: using FunctionBase::FunctionBase; @@ -15,10 +17,7 @@ class FunctionReadFile : public FunctionBase< protected: friend FunctionBase; - xercesc::DOMDocument* constructDocument( - const FilesystemContext&, - std::string - ); + xercesc::DOMDocument* constructDocument(boost::filesystem::path); }; diff --git a/src/function/transform.cc b/src/function/transform.cc index 62b89ba..3ab6475 100644 --- a/src/function/transform.cc +++ b/src/function/transform.cc @@ -29,10 +29,9 @@ inline std::function handleErrors( namespace InputXSLT { xercesc::DOMDocument* FunctionTransform::constructDocument( - const InputXSLT::FilesystemContext& fsContext, - xalan::XSLTInputSource transformationSource, - std::string targetPath, - xalan::XObjectPtr parameterObject + xalan::XSLTInputSource transformationSource, + boost::filesystem::path targetPath, + xalan::XObjectPtr parameterObject ) { xercesc::DOMDocument* const domDocument( xercesc::DOMImplementation::getImplementation()->createDocument( @@ -50,7 +49,7 @@ xercesc::DOMDocument* FunctionTransform::constructDocument( result.setAttribute( "target", - boost::filesystem::path(targetPath).filename().string() + targetPath.filename().string() ); if ( auto transformation = TransformationFacade::try_create( @@ -60,7 +59,7 @@ xercesc::DOMDocument* FunctionTransform::constructDocument( ) ) { try { transformation->generate( - fsContext.resolve(targetPath).string(), + targetPath.string(), parameterObject ); diff --git a/src/function/transform.h b/src/function/transform.h index 081fe90..516233f 100644 --- a/src/function/transform.h +++ b/src/function/transform.h @@ -3,6 +3,8 @@ #include +#include "boost/filesystem.hpp" + #include "base.h" namespace InputXSLT { @@ -10,7 +12,7 @@ namespace InputXSLT { class FunctionTransform : public FunctionBase< FunctionTransform, xalan::XSLTInputSource, - std::string, + boost::filesystem::path, xalan::XObjectPtr > { public: @@ -20,9 +22,8 @@ class FunctionTransform : public FunctionBase< friend FunctionBase; xercesc::DOMDocument* constructDocument( - const FilesystemContext&, xalan::XSLTInputSource, - std::string, + boost::filesystem::path, xalan::XObjectPtr ); diff --git a/src/support/filesystem_context.cc b/src/support/filesystem_context.cc index a18ec43..21548a5 100644 --- a/src/support/filesystem_context.cc +++ b/src/support/filesystem_context.cc @@ -4,52 +4,13 @@ #include #include "support/xalan_string.h" -#include "support/xerces_string_guard.h" namespace InputXSLT { -FilesystemContext::FilesystemContext(const boost::filesystem::path& path): - path_(boost::filesystem::canonical( - path.parent_path() - )) { } - -FilesystemContext::FilesystemContext(const std::string& path): - path_(boost::filesystem::canonical(path)) { } - -boost::filesystem::path FilesystemContext::resolve( - const std::string& path) const { - const boost::filesystem::path targetPath(path); - - if ( targetPath.is_absolute() ) { - return targetPath; - } else { - return absolute(this->path_ / targetPath); - } -} - -boost::filesystem::path FilesystemContext::resolve( - const xalan::XalanDOMString& path) const { - return this->resolve(toString(path)); -} - -void FilesystemContext::iterate( - const std::string& path, - const std::function& func -) const { - this->iterate(this->resolve(path), func); -} - -void FilesystemContext::iterate( - const xalan::XalanDOMString& path, - const std::function& func -) const { - this->iterate(toString(path), func); -} - void FilesystemContext::iterate( const boost::filesystem::path& directory, const std::function& func -) const { +) { std::vector directoryItems; std::copy_if( @@ -74,4 +35,26 @@ void FilesystemContext::iterate( ); } +FilesystemContext::FilesystemContext(const boost::filesystem::path& path): + path_(boost::filesystem::canonical( + path.parent_path() + )) { } + +FilesystemContext::FilesystemContext(const std::string& path): + path_(boost::filesystem::canonical(path)) { } + +boost::filesystem::path FilesystemContext::resolve( + const xalan::XalanDOMString& path) const { + return this->resolve(toString(path)); +} + +boost::filesystem::path FilesystemContext::resolve( + const boost::filesystem::path& path) const { + if ( path.is_absolute() ) { + return path; + } else { + return absolute(this->path_ / path); + } +} + } diff --git a/src/support/filesystem_context.h b/src/support/filesystem_context.h index 9741039..2088c6b 100644 --- a/src/support/filesystem_context.h +++ b/src/support/filesystem_context.h @@ -15,26 +15,16 @@ namespace InputXSLT { class FilesystemContext { public: + static void iterate( + const boost::filesystem::path&, + const std::function& + ); + explicit FilesystemContext(const boost::filesystem::path&); explicit FilesystemContext(const std::string&); - boost::filesystem::path resolve(const std::string&) const; boost::filesystem::path resolve(const xalan::XalanDOMString&) const; - - void iterate( - const std::string&, - const std::function& - ) const; - - void iterate( - const xalan::XalanDOMString&, - const std::function& - ) const; - - void iterate( - const boost::filesystem::path&, - const std::function& - ) const; + boost::filesystem::path resolve(const boost::filesystem::path&) const; private: const boost::filesystem::path path_; diff --git a/src/support/include_entity_resolver.cc b/src/support/include_entity_resolver.cc index 86535c8..e1c9f96 100644 --- a/src/support/include_entity_resolver.cc +++ b/src/support/include_entity_resolver.cc @@ -71,7 +71,7 @@ boost::filesystem::path IncludeEntityResolver::resolve( } boost::optional IncludeEntityResolver::resolve( - const std::string& filePath) const { + const boost::filesystem::path& filePath) const { for ( auto&& context : this->path_ ) { const boost::filesystem::path resolvedPath( context.resolve(filePath) diff --git a/src/support/include_entity_resolver.h b/src/support/include_entity_resolver.h index c8f2867..32d9001 100644 --- a/src/support/include_entity_resolver.h +++ b/src/support/include_entity_resolver.h @@ -25,7 +25,7 @@ class IncludeEntityResolver : public xercesc::EntityResolver { boost::filesystem::path resolve( const XMLCh* const) const; boost::optional resolve( - const std::string&) const; + const boost::filesystem::path&) const; private: const std::vector path_; diff --git a/src/support/type/xobject_value.cc b/src/support/type/xobject_value.cc index 812be52..0236597 100644 --- a/src/support/type/xobject_value.cc +++ b/src/support/type/xobject_value.cc @@ -4,6 +4,7 @@ #include #include +#include "boost/filesystem.hpp" #include @@ -11,34 +12,68 @@ namespace InputXSLT { -namespace XObjectValue { +XObjectValue::XObjectValue( + const boost::filesystem::path& path, + const IncludeEntityResolver* resolver +): + filesystem_context_(path), + include_resolver_(resolver) { } template <> -std::string get(const xalan::XObjectPtr& ptr) { +std::string XObjectValue::get( + const xalan::XObjectPtr& ptr) const { return boost::trim_copy(toString(ptr->str())); } template <> -xalan::XObjectPtr get(const xalan::XObjectPtr& ptr) { +boost::filesystem::path XObjectValue::get( + const xalan::XObjectPtr& ptr) const { + const boost::filesystem::path rawPath( + toString(ptr->str()) + ); + + const boost::filesystem::path filePath( + this->filesystem_context_.resolve(rawPath) + ); + + if ( !(boost::filesystem::exists(filePath) && + boost::filesystem::is_regular_file(filePath)) ) { + if ( auto resolvedPath = this->include_resolver_->resolve(rawPath) ) { + return *resolvedPath; + } else { + return filePath; + } + } else { + return filePath; + } +} + +template <> +xalan::XObjectPtr XObjectValue::get( + const xalan::XObjectPtr& ptr) const { return ptr; } template <> -xalan::XSLTInputSource get( - const xalan::XObjectPtr& ptr) { +xalan::XSLTInputSource XObjectValue::get( + const xalan::XObjectPtr& ptr) const { switch ( ptr->getType() ) { case xalan::XObject::eObjectType::eTypeNodeSet: { - return xalan::XSLTInputSource(ptr->nodeset().item(0)); + return xalan::XSLTInputSource( + ptr->nodeset().item(0) + ); } case xalan::XObject::eObjectType::eTypeResultTreeFrag: { - return xalan::XSLTInputSource(ptr->rtree().getFirstChild()); + return xalan::XSLTInputSource( + ptr->rtree().getFirstChild() + ); } default: { - return xalan::XSLTInputSource(ptr->str()); + return xalan::XSLTInputSource( + this->get(ptr).string().data() + ); } } } } - -} diff --git a/src/support/type/xobject_value.h b/src/support/type/xobject_value.h index bb602a4..cfc259d 100644 --- a/src/support/type/xobject_value.h +++ b/src/support/type/xobject_value.h @@ -4,13 +4,26 @@ #include #include "common.h" +#include "support/filesystem_context.h" +#include "support/include_entity_resolver.h" namespace InputXSLT { -namespace XObjectValue { - template - Type get(const xalan::XObjectPtr&); -} +class XObjectValue { + public: + XObjectValue( + const boost::filesystem::path&, + const IncludeEntityResolver* + ); + + template + Type get(const xalan::XObjectPtr&) const; + + private: + const FilesystemContext filesystem_context_; + const IncludeEntityResolver* const include_resolver_; + +}; } -- cgit v1.2.3