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 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/function/base.h (limited to 'src/function/base.h') 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_ -- cgit v1.2.3