From 426265b91d4533b7aa16d53124ad9b5d0a6862d6 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Mon, 19 May 2014 22:14:31 +0200 Subject: Implemented external "resolve-include" function * resolves paths provided as strings against include paths provided through the newly created "--include" argument of the test executable * this was implemented to enable central collections of XSLT libraries simmilar to how there are C headers in "/usr/include" --- src/function/resolve_include.cc | 62 +++++++++++++++++++++++++++++++++++++++++ src/function/resolve_include.h | 44 +++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/function/resolve_include.cc create mode 100644 src/function/resolve_include.h (limited to 'src/function') diff --git a/src/function/resolve_include.cc b/src/function/resolve_include.cc new file mode 100644 index 0000000..ac4adf5 --- /dev/null +++ b/src/function/resolve_include.cc @@ -0,0 +1,62 @@ +#include "resolve_include.h" + +#include + +#include "support/xalan_string.h" + +namespace InputXSLT { + +FunctionResolveInclude::FunctionResolveInclude( + const std::vector& path): + path_(new std::vector()) { + this->path_->reserve(path.size()); + + std::transform( + path.begin(), + path.end(), + std::back_inserter(*this->path_), + [](const std::string& path) -> FilesystemContext { + return FilesystemContext(path); + } + ); +} + +xalan::XObjectPtr FunctionResolveInclude::execute( + xalan::XPathExecutionContext& executionContext, + xalan::XalanNode*, + const xalan::XObjectPtr parameter, + const xalan::Locator* +) const { + const std::string filePath(toString(parameter->str())); + + 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 executionContext.getXObjectFactory().createString( + toString(resolvedPath.string()) + ); + } + } + + return executionContext.getXObjectFactory().createString( + toString("error") + ); +} + +FunctionResolveInclude* FunctionResolveInclude::clone( + xalan::MemoryManager& manager) const { + return xalan::XalanCopyConstruct(manager, *this); +} + +const xalan::XalanDOMString& FunctionResolveInclude::getError( + xalan::XalanDOMString& result) const { + result.assign("The function expects one parameter of type string."); + + return result; +} + +} diff --git a/src/function/resolve_include.h b/src/function/resolve_include.h new file mode 100644 index 0000000..2d6e62f --- /dev/null +++ b/src/function/resolve_include.h @@ -0,0 +1,44 @@ +#ifndef INPUTXSLT_SRC_FUNCTION_RESOLVE_INCLUDE_H_ +#define INPUTXSLT_SRC_FUNCTION_RESOLVE_INCLUDE_H_ + +#include +#include +#include + +#include "boost/filesystem.hpp" + +#include +#include +#include + +#include "common.h" +#include "support/filesystem_context.h" + +namespace InputXSLT { + +class FunctionResolveInclude : public xalan::Function { + public: + FunctionResolveInclude(const std::vector&); + + virtual xalan::XObjectPtr execute( + xalan::XPathExecutionContext&, + xalan::XalanNode*, + const xalan::XObjectPtr, + const xalan::Locator* + ) const; + + virtual FunctionResolveInclude* clone(xalan::MemoryManager&) const; + + FunctionResolveInclude& operator=(const FunctionResolveInclude&) = delete; + bool operator==(const FunctionResolveInclude&) const = delete; + + private: + const std::shared_ptr> path_; + + const xalan::XalanDOMString& getError(xalan::XalanDOMString&) const; + +}; + +} + +#endif // INPUTXSLT_SRC_FUNCTION_RESOLVE_INCLUDE_H_ -- cgit v1.2.3