From cdf4ad3486debe75fe89b2f04bb62541f3ac8405 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Mon, 21 Apr 2014 13:30:23 +0200 Subject: Extracted filesystem path resolution into InputXSLT::FilesystemContext * this class provides methods for resolving paths relative to the contained base path * other filesystem interaction methods will also be implemented in this class ** for instance directory traversal --- CMakeLists.txt | 1 + src/function/read_file.cc | 16 +++++----------- src/function/read_file.h | 7 +++---- src/function/read_xml_file.cc | 18 ++++++------------ src/function/read_xml_file.h | 7 +++---- src/support/filesystem_context.cc | 24 ++++++++++++++++++++++++ src/support/filesystem_context.h | 28 ++++++++++++++++++++++++++++ src/transformer_facade.cc | 6 +++--- src/transformer_facade.h | 2 ++ 9 files changed, 75 insertions(+), 34 deletions(-) create mode 100644 src/support/filesystem_context.cc create mode 100644 src/support/filesystem_context.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 095480a..43b8e98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable( src/transformer_facade.cc src/function/read_file.cc src/function/read_xml_file.cc + src/support/filesystem_context.cc ) target_link_libraries( diff --git a/src/function/read_file.cc b/src/function/read_file.cc index 19ecfc8..9e32f04 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -6,8 +6,8 @@ namespace InputXSLT { -FunctionReadFile::FunctionReadFile(const std::string& path): - path_(path) { } +FunctionReadFile::FunctionReadFile(const FilesystemContext& context): + fs_context_(context) { } xalan::XObjectPtr FunctionReadFile::execute( xalan::XPathExecutionContext& executionContext, @@ -20,19 +20,13 @@ xalan::XObjectPtr FunctionReadFile::execute( executionContext ); - generalError(executionContext, context, locator); + this->generalError(executionContext, context, locator); } - xalan::CharVectorType castHelper; - arguments[0]->str().transcode(castHelper); - - const std::string fileName( - castHelper.begin(), - castHelper.end() + boost::filesystem::ifstream file( + this->fs_context_.resolve(arguments[0]->str()) ); - boost::filesystem::ifstream file(this->path_ / fileName); - const std::string fileContent( (std::istreambuf_iterator(file)), (std::istreambuf_iterator()) diff --git a/src/function/read_file.h b/src/function/read_file.h index f5bdb9d..b88336a 100644 --- a/src/function/read_file.h +++ b/src/function/read_file.h @@ -6,17 +6,16 @@ #include #include -#include "boost/filesystem.hpp" - #include #include "common.h" +#include "support/filesystem_context.h" namespace InputXSLT { class FunctionReadFile : public xalan::Function { public: - FunctionReadFile(const std::string&); + FunctionReadFile(const FilesystemContext&); virtual xalan::XObjectPtr execute( xalan::XPathExecutionContext&, @@ -31,7 +30,7 @@ class FunctionReadFile : public xalan::Function { bool operator==(const FunctionReadFile&) const = delete; private: - const boost::filesystem::path path_; + const FilesystemContext& fs_context_; const xalan::XalanDOMString& getError(xalan::XalanDOMString&) const; diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc index 78f81c4..ac42288 100644 --- a/src/function/read_xml_file.cc +++ b/src/function/read_xml_file.cc @@ -4,12 +4,12 @@ namespace InputXSLT { -FunctionReadXmlFile::FunctionReadXmlFile(const std::string& path): - path_(path), +FunctionReadXmlFile::FunctionReadXmlFile(const FilesystemContext& context): + fs_context_(context), parser_() { } FunctionReadXmlFile::FunctionReadXmlFile(const FunctionReadXmlFile& src): - path_(src.path_), + fs_context_(src.fs_context_), parser_() { } xalan::XObjectPtr FunctionReadXmlFile::execute( @@ -23,19 +23,13 @@ xalan::XObjectPtr FunctionReadXmlFile::execute( executionContext ); - generalError(executionContext, context, locator); + this->generalError(executionContext, context, locator); } - xalan::CharVectorType castHelper; - arguments[0]->str().transcode(castHelper); - - const std::string fileName( - castHelper.begin(), - castHelper.end() + boost::filesystem::ifstream file( + this->fs_context_.resolve(arguments[0]->str()) ); - boost::filesystem::ifstream file(this->path_ / fileName); - return executionContext.getXObjectFactory().createNodeSet( this->parser_.parseXMLStream( xalan::XSLTInputSource(file) diff --git a/src/function/read_xml_file.h b/src/function/read_xml_file.h index 6877feb..aa8590a 100644 --- a/src/function/read_xml_file.h +++ b/src/function/read_xml_file.h @@ -7,17 +7,16 @@ #include #include -#include "boost/filesystem.hpp" - #include #include "common.h" +#include "support/filesystem_context.h" namespace InputXSLT { class FunctionReadXmlFile : public xalan::Function { public: - FunctionReadXmlFile(const std::string&); + FunctionReadXmlFile(const FilesystemContext&); FunctionReadXmlFile(const FunctionReadXmlFile&); virtual xalan::XObjectPtr execute( @@ -33,7 +32,7 @@ class FunctionReadXmlFile : public xalan::Function { bool operator==(const FunctionReadXmlFile&) const = delete; private: - const boost::filesystem::path path_; + const FilesystemContext& fs_context_; mutable xalan::XercesParserLiaison parser_; const xalan::XalanDOMString& getError(xalan::XalanDOMString& result) const; diff --git a/src/support/filesystem_context.cc b/src/support/filesystem_context.cc new file mode 100644 index 0000000..d3f9614 --- /dev/null +++ b/src/support/filesystem_context.cc @@ -0,0 +1,24 @@ +#include "filesystem_context.h" + +namespace InputXSLT { + +FilesystemContext::FilesystemContext(const std::string& path): + path_(path) { } + +boost::filesystem::path FilesystemContext::resolve( + const std::string& path) const { + return boost::filesystem::path(this->path_ / path); +} + +boost::filesystem::path FilesystemContext::resolve( + const xalan::XalanDOMString& path) const { + xalan::CharVectorType castHelper; + path.transcode(castHelper); + + return this->resolve(std::string( + castHelper.begin(), + castHelper.end() + )); +} + +} diff --git a/src/support/filesystem_context.h b/src/support/filesystem_context.h new file mode 100644 index 0000000..d5836f1 --- /dev/null +++ b/src/support/filesystem_context.h @@ -0,0 +1,28 @@ +#ifndef INPUTXSLT_SRC_SUPPORT_FILESYSTEM_CONTEXT_H_ +#define INPUTXSLT_SRC_SUPPORT_FILESYSTEM_CONTEXT_H_ + +#include + +#include "boost/filesystem.hpp" + +#include + +#include "common.h" + +namespace InputXSLT { + +class FilesystemContext { + public: + explicit FilesystemContext(const std::string&); + + boost::filesystem::path resolve(const std::string&) const; + boost::filesystem::path resolve(const xalan::XalanDOMString&) const; + + private: + const boost::filesystem::path path_; + +}; + +} + +#endif // INPUTXSLT_SRC_SUPPORT_FILESYSTEM_CONTEXT_H_ diff --git a/src/transformer_facade.cc b/src/transformer_facade.cc index be2a6a2..dfd51ef 100644 --- a/src/transformer_facade.cc +++ b/src/transformer_facade.cc @@ -17,6 +17,7 @@ namespace InputXSLT { TransformerFacade::TransformerFacade(const std::string& path): + fs_context_(path), parser_(), transformer_() { const xalan::XalanDOMString customNamespace( @@ -26,15 +27,14 @@ TransformerFacade::TransformerFacade(const std::string& path): this->transformer_.installExternalFunction( customNamespace, xalan::XalanDOMString("read-file"), - InputXSLT::FunctionReadFile(path) + InputXSLT::FunctionReadFile(this->fs_context_) ); this->transformer_.installExternalFunction( customNamespace, xalan::XalanDOMString("read-xml-file"), - InputXSLT::FunctionReadXmlFile(path) + InputXSLT::FunctionReadXmlFile(this->fs_context_) ); - } int TransformerFacade::execute( diff --git a/src/transformer_facade.h b/src/transformer_facade.h index c11fa78..ed25edc 100644 --- a/src/transformer_facade.h +++ b/src/transformer_facade.h @@ -7,6 +7,7 @@ #include #include "common.h" +#include "support/filesystem_context.h" namespace InputXSLT { @@ -17,6 +18,7 @@ class TransformerFacade { int execute(const std::string&, const std::string&); private: + const FilesystemContext fs_context_; mutable xalan::XercesParserLiaison parser_; xalan::XalanTransformer transformer_; -- cgit v1.2.3