#ifndef INPUTXSLT_SRC_FUNCTION_BASE_H_ #define INPUTXSLT_SRC_FUNCTION_BASE_H_ #include #include #include #include #include #include "common.h" #include "support/xalan_string.h" #include "support/filesystem_context.h" #include "support/include_entity_resolver.h" #include "support/dom/document_cache.h" #include "support/type/sequence.h" #include "support/type/xobject_value.h" namespace InputXSLT { template < typename Implementation, typename... Types > class FunctionBase : public xalan::Function { static const std::size_t parameter_count = sizeof...(Types); public: FunctionBase(IncludeEntityResolver* resolver): include_resolver_(resolver), document_cache_(std::make_shared()) { } virtual xalan::XObjectPtr execute( xalan::XPathExecutionContext& executionContext, xalan::XalanNode* context, const XObjectArgVectorType& parameters, const xalan::Locator* locator ) const { this->validateParameters( parameters, executionContext, context, locator ); xalan::XalanDocument* const domDocument( this->callConstructDocument( parameters, locator, typename IndexSequence::type() ) ); xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodes( executionContext ); nodes->addNodes( *domDocument->getDocumentElement()->getChildNodes() ); return executionContext.getXObjectFactory().createNodeSet(nodes); } 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: IncludeEntityResolver* const include_resolver_; private: std::shared_ptr document_cache_; const xalan::XalanDOMString& getError( xalan::XalanDOMString& result) const { result.assign(std::string( "The function expects " + std::to_string(parameter_count) + " parameter(s)" ).data()); return result; } template inline xalan::XalanDocument* callConstructDocument( const XObjectArgVectorType& parameters, const xalan::Locator* locator, Sequence ) const { XObjectValue valueGetter( IncludeEntityResolver::getPathFromSystemId( locator->getSystemId() ), this->include_resolver_ ); return this->document_cache_->create( static_cast(this)->constructDocument( valueGetter.get >::type>(parameters[Index])... ) ); } inline void validateParameters( const XObjectArgVectorType& parameters, xalan::XPathExecutionContext& executionContext, xalan::XalanNode* context, const xalan::Locator* locator ) const { const bool anyNull = std::any_of( parameters.begin(), parameters.end(), [](const xalan::XObjectPtr& ptr) -> bool { return ptr.null(); } ); if ( parameters.size() != parameter_count || anyNull ) { xalan::XPathExecutionContext::GetAndReleaseCachedString guard( executionContext ); this->generalError(executionContext, context, locator); } } }; } #endif // INPUTXSLT_SRC_FUNCTION_BASE_H_