From 9aad4dffd9c8b0fde534b3abc3c27875547423fa Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Mon, 5 May 2014 20:42:36 +0200 Subject: Encapsulated common parts of external function inplementations * common parts were moved into CRTP template base class _FunctionBase_ * all external functions are derived from that class ** only have to implement _constructDocument_ member method ** currently only supports a single string input parameter which is enough for now * this change condenses external funtion implementations to the essential and should increase readability as well as maintainability --- src/function/base.h | 89 ++++++++++++++++++++++++++++++++++++++++++ src/function/read_directory.cc | 62 +---------------------------- src/function/read_directory.h | 35 +++++------------ src/function/read_file.cc | 65 +++--------------------------- src/function/read_file.h | 35 +++++------------ src/function/read_xml_file.cc | 63 +++--------------------------- src/function/read_xml_file.h | 36 +++++------------ 7 files changed, 130 insertions(+), 255 deletions(-) create mode 100644 src/function/base.h (limited to 'src') diff --git a/src/function/base.h b/src/function/base.h new file mode 100644 index 0000000..231800d --- /dev/null +++ b/src/function/base.h @@ -0,0 +1,89 @@ +#ifndef INPUTXSLT_SRC_FUNCTION_BASE_H_ +#define INPUTXSLT_SRC_FUNCTION_BASE_H_ + +#include +#include +#include +#include + +#include + +#include "common.h" +#include "support/dom/document_cache.h" +#include "support/filesystem_context.h" + +namespace InputXSLT { + +template +class FunctionBase : public xalan::Function { + public: + FunctionBase(): + document_cache_(std::make_shared()) { } + + virtual xalan::XObjectPtr execute( + xalan::XPathExecutionContext& executionContext, + xalan::XalanNode*, + const xalan::XObjectPtr argument, + const xalan::Locator* locator + ) const { + const FilesystemContext fsContext(locator); + + const boost::filesystem::path argumentPath( + fsContext.resolve(argument->str()) + ); + + DomDocumentCache::optional_item optionalCachedDocument( + this->document_cache_->get(argumentPath.string()) + ); + + if ( !optionalCachedDocument.first ) { + optionalCachedDocument = this->document_cache_->create( + argumentPath.string(), + static_cast( + const_cast(this) + )->constructDocument( + fsContext, + argumentPath + ) + ); + } + + xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList( + executionContext + ); + + nodeList->addNodes( + *optionalCachedDocument.second->getXalanDocument() + ->getDocumentElement() + ->getChildNodes() + ); + + return executionContext.getXObjectFactory().createNodeSet(nodeList); + } + + virtual FunctionBase* clone( + xalan::MemoryManager& manager) const { + return xalan::XalanCopyConstruct( + manager, + static_cast(*this) + ); + } + + FunctionBase& operator=(const FunctionBase&) = delete; + bool operator==(const FunctionBase&) const = delete; + + protected: + std::shared_ptr document_cache_; + + const xalan::XalanDOMString& getError( + xalan::XalanDOMString& result) const { + result.assign("The function expects one argument of type string."); + + return result; + } + +}; + +} + +#endif // INPUTXSLT_SRC_FUNCTION_BASE_H_ diff --git a/src/function/read_directory.cc b/src/function/read_directory.cc index 48ffa51..8b41dae 100644 --- a/src/function/read_directory.cc +++ b/src/function/read_directory.cc @@ -6,13 +6,10 @@ #include #include "support/xerces_string_guard.h" -#include "support/filesystem_context.h" -namespace { - -using InputXSLT::XercesStringGuard; +namespace InputXSLT { -xercesc::DOMDocument* constructDocument( +xercesc::DOMDocument* FunctionReadDirectory::constructDocument( const InputXSLT::FilesystemContext& fsContext, const boost::filesystem::path& directoryPath ) { @@ -84,58 +81,3 @@ xercesc::DOMDocument* constructDocument( } } - -namespace InputXSLT { - -FunctionReadDirectory::FunctionReadDirectory(): - document_cache_(std::make_shared()) { } - -xalan::XObjectPtr FunctionReadDirectory::execute( - xalan::XPathExecutionContext& executionContext, - xalan::XalanNode*, - const xalan::XObjectPtr argument, - const xalan::Locator* locator -) const { - const FilesystemContext fsContext(locator); - - const boost::filesystem::path directoryPath( - fsContext.resolve(argument->str()) - ); - - DomDocumentCache::optional_item optionalCachedDocument( - this->document_cache_->get(directoryPath.string()) - ); - - if ( !optionalCachedDocument.first ) { - optionalCachedDocument = this->document_cache_->create( - directoryPath.string(), - constructDocument(fsContext, directoryPath) - ); - } - - xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList( - executionContext - ); - - nodeList->addNodes( - *optionalCachedDocument.second->getXalanDocument() - ->getDocumentElement() - ->getChildNodes() - ); - - return executionContext.getXObjectFactory().createNodeSet(nodeList); -} - -FunctionReadDirectory* FunctionReadDirectory::clone( - xalan::MemoryManager& manager) const { - return xalan::XalanCopyConstruct(manager, *this); -} - -const xalan::XalanDOMString& FunctionReadDirectory::getError( - xalan::XalanDOMString& result) const { - result.assign("The read-directory() function expects one argument of type string."); - - return result; -} - -} diff --git a/src/function/read_directory.h b/src/function/read_directory.h index 02a0305..2c28642 100644 --- a/src/function/read_directory.h +++ b/src/function/read_directory.h @@ -1,38 +1,21 @@ #ifndef INPUTXSLT_SRC_FUNCTION_READ_DIRECTORY_H_ #define INPUTXSLT_SRC_FUNCTION_READ_DIRECTORY_H_ -#include -#include -#include -#include - -#include - -#include "common.h" -#include "support/dom/document_cache.h" +#include "base.h" namespace InputXSLT { -class FunctionReadDirectory : public xalan::Function { +class FunctionReadDirectory : public FunctionBase { public: - FunctionReadDirectory(); - - virtual xalan::XObjectPtr execute( - xalan::XPathExecutionContext&, - xalan::XalanNode*, - const xalan::XObjectPtr, - const xalan::Locator* - ) const; - - virtual FunctionReadDirectory* clone(xalan::MemoryManager&) const; - - FunctionReadDirectory& operator=(const FunctionReadDirectory&) = delete; - bool operator==(const FunctionReadDirectory&) const = delete; + using FunctionBase::FunctionBase; - private: - std::shared_ptr document_cache_; + protected: + friend FunctionBase; - const xalan::XalanDOMString& getError(xalan::XalanDOMString&) const; + xercesc::DOMDocument* constructDocument( + const FilesystemContext&, + const boost::filesystem::path& + ); }; diff --git a/src/function/read_file.cc b/src/function/read_file.cc index ba94909..439d905 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -8,12 +8,9 @@ #include "boost/filesystem/fstream.hpp" #include "support/xerces_string_guard.h" -#include "support/filesystem_context.h" namespace { -using InputXSLT::XercesStringGuard; - inline std::string readFile(const boost::filesystem::path& filePath) { boost::filesystem::ifstream file(filePath); @@ -23,7 +20,12 @@ inline std::string readFile(const boost::filesystem::path& filePath) { ); } -xercesc::DOMDocument* constructDocument( +} + +namespace InputXSLT { + +xercesc::DOMDocument* FunctionReadFile::constructDocument( + const FilesystemContext&, const boost::filesystem::path& filePath ) { xercesc::DOMDocument* const domDocument( @@ -68,58 +70,3 @@ xercesc::DOMDocument* constructDocument( } } - -namespace InputXSLT { - -FunctionReadFile::FunctionReadFile(): - document_cache_(std::make_shared()) { } - -xalan::XObjectPtr FunctionReadFile::execute( - xalan::XPathExecutionContext& executionContext, - xalan::XalanNode*, - const xalan::XObjectPtr argument, - const xalan::Locator* locator -) const { - const FilesystemContext fsContext(locator); - - const boost::filesystem::path filePath( - fsContext.resolve(argument->str()) - ); - - DomDocumentCache::optional_item optionalCachedDocument( - this->document_cache_->get(filePath.string()) - ); - - if ( !optionalCachedDocument.first ) { - optionalCachedDocument = this->document_cache_->create( - filePath.string(), - constructDocument(filePath) - ); - } - - xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList( - executionContext - ); - - nodeList->addNodes( - *optionalCachedDocument.second->getXalanDocument() - ->getDocumentElement() - ->getChildNodes() - ); - - return executionContext.getXObjectFactory().createNodeSet(nodeList); -} - -FunctionReadFile* FunctionReadFile::clone( - xalan::MemoryManager& manager) const { - return xalan::XalanCopyConstruct(manager, *this); -} - -const xalan::XalanDOMString& FunctionReadFile::getError( - xalan::XalanDOMString& result) const { - result.assign("The read-file() function expects one argument of type string."); - - return result; -} - -} diff --git a/src/function/read_file.h b/src/function/read_file.h index 65746a5..fd36847 100644 --- a/src/function/read_file.h +++ b/src/function/read_file.h @@ -1,38 +1,21 @@ #ifndef INPUTXSLT_SRC_FUNCTION_READ_FILE_H_ #define INPUTXSLT_SRC_FUNCTION_READ_FILE_H_ -#include -#include -#include -#include - -#include - -#include "common.h" -#include "support/dom/document_cache.h" +#include "base.h" namespace InputXSLT { -class FunctionReadFile : public xalan::Function { +class FunctionReadFile : public FunctionBase { public: - FunctionReadFile(); - - virtual xalan::XObjectPtr execute( - xalan::XPathExecutionContext&, - xalan::XalanNode*, - const xalan::XObjectPtr, - const xalan::Locator* - ) const; - - virtual FunctionReadFile* clone(xalan::MemoryManager&) const; - - FunctionReadFile& operator=(const FunctionReadFile&) = delete; - bool operator==(const FunctionReadFile&) const = delete; + using FunctionBase::FunctionBase; - private: - std::shared_ptr document_cache_; + protected: + friend FunctionBase; - const xalan::XalanDOMString& getError(xalan::XalanDOMString&) const; + xercesc::DOMDocument* constructDocument( + const FilesystemContext&, + const boost::filesystem::path& + ); }; diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc index a4e3e3c..71ba819 100644 --- a/src/function/read_xml_file.cc +++ b/src/function/read_xml_file.cc @@ -9,7 +9,6 @@ #include "boost/filesystem/fstream.hpp" #include "support/xerces_string_guard.h" -#include "support/filesystem_context.h" namespace { @@ -29,7 +28,12 @@ inline xercesc::DOMNode* importDocumentElement( ); } -xercesc::DOMDocument* constructDocument( +} + +namespace InputXSLT { + +xercesc::DOMDocument* FunctionReadXmlFile::constructDocument( + const FilesystemContext&, const boost::filesystem::path& filePath ) { xercesc::DOMDocument* const domDocument( @@ -72,58 +76,3 @@ xercesc::DOMDocument* constructDocument( } } - -namespace InputXSLT { - -FunctionReadXmlFile::FunctionReadXmlFile(): - document_cache_(std::make_shared()) { } - -xalan::XObjectPtr FunctionReadXmlFile::execute( - xalan::XPathExecutionContext& executionContext, - xalan::XalanNode*, - const xalan::XObjectPtr argument, - const xalan::Locator* locator -) const { - const FilesystemContext fsContext(locator); - - const boost::filesystem::path filePath( - fsContext.resolve(argument->str()) - ); - - DomDocumentCache::optional_item optionalCachedDocument( - this->document_cache_->get(filePath.string()) - ); - - if ( !optionalCachedDocument.first ) { - optionalCachedDocument = this->document_cache_->create( - filePath.string(), - constructDocument(filePath) - ); - } - - xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList( - executionContext - ); - - nodeList->addNodes( - *optionalCachedDocument.second->getXalanDocument() - ->getDocumentElement() - ->getChildNodes() - ); - - return executionContext.getXObjectFactory().createNodeSet(nodeList); -} - -FunctionReadXmlFile* FunctionReadXmlFile::clone( - xalan::MemoryManager& manager) const { - return xalan::XalanCopyConstruct(manager, *this); -} - -const xalan::XalanDOMString& FunctionReadXmlFile::getError( - xalan::XalanDOMString& result) const { - result.assign("The read-xml-file() function expects one argument of type string."); - - return result; -} - -} diff --git a/src/function/read_xml_file.h b/src/function/read_xml_file.h index d7ef440..5314b4a 100644 --- a/src/function/read_xml_file.h +++ b/src/function/read_xml_file.h @@ -1,39 +1,21 @@ #ifndef INPUTXSLT_SRC_FUNCTION_READ_XML_FILE_H_ #define INPUTXSLT_SRC_FUNCTION_READ_XML_FILE_H_ -#include -#include -#include -#include - -#include -#include - -#include "common.h" -#include "support/dom/document_cache.h" +#include "base.h" namespace InputXSLT { -class FunctionReadXmlFile : public xalan::Function { +class FunctionReadXmlFile : public FunctionBase { public: - FunctionReadXmlFile(); - - virtual xalan::XObjectPtr execute( - xalan::XPathExecutionContext&, - xalan::XalanNode*, - const xalan::XObjectPtr, - const xalan::Locator* - ) const; - - virtual FunctionReadXmlFile* clone(xalan::MemoryManager&) const; - - FunctionReadXmlFile& operator=(const FunctionReadXmlFile&) = delete; - bool operator==(const FunctionReadXmlFile&) const = delete; + using FunctionBase::FunctionBase; - private: - std::shared_ptr document_cache_; + protected: + friend FunctionBase; - const xalan::XalanDOMString& getError(xalan::XalanDOMString& result) const; + xercesc::DOMDocument* constructDocument( + const FilesystemContext&, + const boost::filesystem::path& + ); }; -- cgit v1.2.3