aboutsummaryrefslogtreecommitdiff
path: root/src/function
diff options
context:
space:
mode:
Diffstat (limited to 'src/function')
-rw-r--r--src/function/base.h66
-rw-r--r--src/function/read_directory.cc4
-rw-r--r--src/function/read_directory.h4
-rw-r--r--src/function/read_file.cc4
-rw-r--r--src/function/read_file.h4
-rw-r--r--src/function/read_xml_file.cc4
-rw-r--r--src/function/read_xml_file.h4
-rw-r--r--src/function/transform.cc64
-rw-r--r--src/function/transform.h37
9 files changed, 90 insertions, 101 deletions
diff --git a/src/function/base.h b/src/function/base.h
index fe9f6ff..5604186 100644
--- a/src/function/base.h
+++ b/src/function/base.h
@@ -7,6 +7,8 @@
#include <xalanc/XPath/XObject.hpp>
#include <memory>
+#include <algorithm>
+#include <array>
#include "common.h"
#include "support/dom/document_cache.h"
@@ -14,19 +16,44 @@
namespace InputXSLT {
-template <class Implementation>
+template <
+ class Implementation,
+ std::size_t ArgumentCount
+>
class FunctionBase : public xalan::Function {
public:
+ typedef std::array<
+ boost::filesystem::path,
+ ArgumentCount
+ > argument_array;
+
FunctionBase():
document_cache_(std::make_shared<DomDocumentCache>()) { }
virtual xalan::XObjectPtr execute(
xalan::XPathExecutionContext& executionContext,
- xalan::XalanNode*,
- const xalan::XObjectPtr argument,
+ xalan::XalanNode* context,
+ const XObjectArgVectorType& rawArguments,
const xalan::Locator* locator
) const {
+ this->validateArguments(
+ rawArguments,
+ executionContext,
+ context,
+ locator
+ );
+
const FilesystemContext fsContext(locator);
+ argument_array pathArguments;
+
+ std::transform(
+ rawArguments.begin(),
+ rawArguments.end(),
+ pathArguments.begin(),
+ [&fsContext](const xalan::XObjectPtr& ptr) -> boost::filesystem::path {
+ return fsContext.resolve(ptr->str());
+ }
+ );
xalan::XalanDocument* const domDocument(
this->document_cache_->create(
@@ -34,7 +61,7 @@ class FunctionBase : public xalan::Function {
const_cast<FunctionBase*>(this)
)->constructDocument(
fsContext,
- fsContext.resolve(argument->str())
+ pathArguments
)
)
);
@@ -61,16 +88,43 @@ class FunctionBase : public xalan::Function {
FunctionBase& operator=(const FunctionBase&) = delete;
bool operator==(const FunctionBase&) const = delete;
- protected:
+ private:
std::shared_ptr<DomDocumentCache> document_cache_;
const xalan::XalanDOMString& getError(
xalan::XalanDOMString& result) const {
- result.assign("The function expects one argument of type string.");
+ result.assign(std::string(
+ "The function expects " +
+ std::to_string(ArgumentCount) +
+ " argument(s) of type string."
+ ).data());
return result;
}
+ inline void validateArguments(
+ const XObjectArgVectorType& rawArguments,
+ xalan::XPathExecutionContext& executionContext,
+ xalan::XalanNode* context,
+ const xalan::Locator* locator
+ ) const {
+ const bool notNull = std::none_of(
+ rawArguments.begin(),
+ rawArguments.end(),
+ [](const xalan::XObjectPtr& ptr) -> bool {
+ return ptr.null();
+ }
+ );
+
+ if ( rawArguments.size() != ArgumentCount && notNull ) {
+ xalan::XPathExecutionContext::GetAndReleaseCachedString guard(
+ executionContext
+ );
+
+ this->generalError(executionContext, context, locator);
+ }
+ }
+
};
}
diff --git a/src/function/read_directory.cc b/src/function/read_directory.cc
index 8b41dae..e030e33 100644
--- a/src/function/read_directory.cc
+++ b/src/function/read_directory.cc
@@ -11,8 +11,10 @@ namespace InputXSLT {
xercesc::DOMDocument* FunctionReadDirectory::constructDocument(
const InputXSLT::FilesystemContext& fsContext,
- const boost::filesystem::path& directoryPath
+ const FunctionBase::argument_array& arguments
) {
+ const boost::filesystem::path& directoryPath = arguments[0];
+
xercesc::DOMDocument* const domDocument(
xercesc::DOMImplementation::getImplementation()->createDocument(
nullptr,
diff --git a/src/function/read_directory.h b/src/function/read_directory.h
index b050ff8..9dc3869 100644
--- a/src/function/read_directory.h
+++ b/src/function/read_directory.h
@@ -5,7 +5,7 @@
namespace InputXSLT {
-class FunctionReadDirectory : public FunctionBase<FunctionReadDirectory> {
+class FunctionReadDirectory : public FunctionBase<FunctionReadDirectory, 1> {
public:
using FunctionBase::FunctionBase;
@@ -14,7 +14,7 @@ class FunctionReadDirectory : public FunctionBase<FunctionReadDirectory> {
xercesc::DOMDocument* constructDocument(
const FilesystemContext&,
- const boost::filesystem::path&
+ const FunctionBase::argument_array&
);
};
diff --git a/src/function/read_file.cc b/src/function/read_file.cc
index 439d905..da0e988 100644
--- a/src/function/read_file.cc
+++ b/src/function/read_file.cc
@@ -26,8 +26,10 @@ namespace InputXSLT {
xercesc::DOMDocument* FunctionReadFile::constructDocument(
const FilesystemContext&,
- const boost::filesystem::path& filePath
+ const FunctionBase::argument_array& arguments
) {
+ const boost::filesystem::path& filePath = arguments[0];
+
xercesc::DOMDocument* const domDocument(
xercesc::DOMImplementation::getImplementation()->createDocument(
nullptr,
diff --git a/src/function/read_file.h b/src/function/read_file.h
index 90139f4..e7b746c 100644
--- a/src/function/read_file.h
+++ b/src/function/read_file.h
@@ -5,7 +5,7 @@
namespace InputXSLT {
-class FunctionReadFile : public FunctionBase<FunctionReadFile> {
+class FunctionReadFile : public FunctionBase<FunctionReadFile, 1> {
public:
using FunctionBase::FunctionBase;
@@ -14,7 +14,7 @@ class FunctionReadFile : public FunctionBase<FunctionReadFile> {
xercesc::DOMDocument* constructDocument(
const FilesystemContext&,
- const boost::filesystem::path&
+ const FunctionBase::argument_array&
);
};
diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc
index 71ba819..b3bf3de 100644
--- a/src/function/read_xml_file.cc
+++ b/src/function/read_xml_file.cc
@@ -34,8 +34,10 @@ namespace InputXSLT {
xercesc::DOMDocument* FunctionReadXmlFile::constructDocument(
const FilesystemContext&,
- const boost::filesystem::path& filePath
+ const FunctionBase::argument_array& arguments
) {
+ const boost::filesystem::path& filePath = arguments[0];
+
xercesc::DOMDocument* const domDocument(
xercesc::DOMImplementation::getImplementation()->createDocument(
nullptr,
diff --git a/src/function/read_xml_file.h b/src/function/read_xml_file.h
index 3636d18..423f0a2 100644
--- a/src/function/read_xml_file.h
+++ b/src/function/read_xml_file.h
@@ -5,7 +5,7 @@
namespace InputXSLT {
-class FunctionReadXmlFile : public FunctionBase<FunctionReadXmlFile> {
+class FunctionReadXmlFile : public FunctionBase<FunctionReadXmlFile, 1> {
public:
using FunctionBase::FunctionBase;
@@ -14,7 +14,7 @@ class FunctionReadXmlFile : public FunctionBase<FunctionReadXmlFile> {
xercesc::DOMDocument* constructDocument(
const FilesystemContext&,
- const boost::filesystem::path&
+ const FunctionBase::argument_array&
);
};
diff --git a/src/function/transform.cc b/src/function/transform.cc
index 9b600d9..360b61d 100644
--- a/src/function/transform.cc
+++ b/src/function/transform.cc
@@ -7,15 +7,15 @@
#include "transformation_facade.h"
#include "support/xerces_string_guard.h"
-namespace {
-
-using InputXSLT::XercesStringGuard;
+namespace InputXSLT {
-xercesc::DOMDocument* constructDocument(
+xercesc::DOMDocument* FunctionTransform::constructDocument(
const InputXSLT::FilesystemContext&,
- const boost::filesystem::path& transformationPath,
- const boost::filesystem::path& targetPath
+ const FunctionBase::argument_array& arguments
) {
+ const boost::filesystem::path& transformationPath = arguments[0];
+ const boost::filesystem::path& targetPath = arguments[1];
+
xercesc::DOMDocument* const domDocument(
xercesc::DOMImplementation::getImplementation()->createDocument(
nullptr,
@@ -59,55 +59,3 @@ xercesc::DOMDocument* constructDocument(
}
}
-
-namespace InputXSLT {
-
-FunctionTransform::FunctionTransform():
- document_cache_(std::make_shared<DomDocumentCache>()) { }
-
-xalan::XObjectPtr FunctionTransform::execute(
- xalan::XPathExecutionContext& executionContext,
- xalan::XalanNode*,
- const xalan::XObjectPtr argument1,
- const xalan::XObjectPtr argument2,
- const xalan::Locator* locator
-) const {
- const FilesystemContext fsContext(locator);
-
- xalan::XalanDocument* const domDocument(
- this->document_cache_->create(
- constructDocument(
- fsContext,
- fsContext.resolve(argument1->str()),
- fsContext.resolve(argument2->str())
- )
- )
- );
-
- xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList(
- executionContext
- );
-
- nodeList->addNodes(
- *domDocument->getDocumentElement()->getChildNodes()
- );
-
- return executionContext.getXObjectFactory().createNodeSet(nodeList);
-}
-
-FunctionTransform* FunctionTransform::clone(
- xalan::MemoryManager& manager) const {
- return xalan::XalanCopyConstruct(
- manager,
- *this
- );
-}
-
-const xalan::XalanDOMString& FunctionTransform::getError(
- xalan::XalanDOMString& result) const {
- result.assign("The function expects two arguments of type string.");
-
- return result;
-}
-
-}
diff --git a/src/function/transform.h b/src/function/transform.h
index 41bba05..fc735f0 100644
--- a/src/function/transform.h
+++ b/src/function/transform.h
@@ -1,40 +1,21 @@
#ifndef INPUTXSLT_SRC_FUNCTION_TRANSFORM_H_
#define INPUTXSLT_SRC_FUNCTION_TRANSFORM_H_
-#include <xalanc/XalanTransformer/XalanTransformer.hpp>
-#include <xalanc/XPath/XObjectFactory.hpp>
-#include <xalanc/XPath/Function.hpp>
-#include <xalanc/XPath/XObject.hpp>
-
-#include <memory>
-
-#include "common.h"
-#include "support/dom/document_cache.h"
-#include "support/filesystem_context.h"
+#include "base.h"
namespace InputXSLT {
-class FunctionTransform : public xalan::Function {
+class FunctionTransform : public FunctionBase<FunctionTransform, 2> {
public:
- FunctionTransform();
-
- virtual xalan::XObjectPtr execute(
- xalan::XPathExecutionContext&,
- xalan::XalanNode*,
- const xalan::XObjectPtr,
- const xalan::XObjectPtr,
- const xalan::Locator*
- ) const;
-
- virtual FunctionTransform* clone(xalan::MemoryManager&) const;
-
- FunctionTransform& operator=(const FunctionTransform&) = delete;
- bool operator==(const FunctionTransform&) const = delete;
+ using FunctionBase::FunctionBase;
- private:
- std::shared_ptr<DomDocumentCache> document_cache_;
+ protected:
+ friend FunctionBase;
- const xalan::XalanDOMString& getError(xalan::XalanDOMString&) const;
+ xercesc::DOMDocument* constructDocument(
+ const FilesystemContext&,
+ const FunctionBase::argument_array&
+ );
};