aboutsummaryrefslogtreecommitdiff
path: root/src/function
diff options
context:
space:
mode:
Diffstat (limited to 'src/function')
-rw-r--r--src/function/base.h27
-rw-r--r--src/function/external_command.cc (renamed from src/function/external_text_formatter.cc)30
-rw-r--r--src/function/external_command.h31
-rw-r--r--src/function/external_text_formatter.h30
4 files changed, 66 insertions, 52 deletions
diff --git a/src/function/base.h b/src/function/base.h
index 2d1e4c4..a8f93b9 100644
--- a/src/function/base.h
+++ b/src/function/base.h
@@ -5,6 +5,8 @@
#include <xalanc/XPath/Function.hpp>
#include <xalanc/XPath/XObject.hpp>
+#include <boost/optional.hpp>
+
#include <memory>
#include <tuple>
@@ -14,6 +16,7 @@
#include "support/include_entity_resolver.h"
#include "support/dom/document_cache.h"
#include "support/type/sequence.h"
+#include "support/type/filter.h"
#include "support/type/xobject_value.h"
namespace InputXSLT {
@@ -23,7 +26,13 @@ template <
typename... Types
>
class FunctionBase : public xalan::Function {
- static const std::size_t parameter_count = sizeof...(Types);
+ static const std::size_t parameter_count = sizeof...(Types);
+ static const std::size_t optional_parameter_count = std::tuple_size<
+ typename filter_derived<
+ boost::optional_detail::optional_tag,
+ Types...
+ >::type
+ >::value;
public:
FunctionBase(IncludeEntityResolver* resolver):
@@ -108,16 +117,20 @@ class FunctionBase : public xalan::Function {
valueGetter.get<typename std::tuple_element<
Index,
std::tuple<Types...>
- >::type>(parameters[Index])...
+ >::type>(
+ Index < parameters.size()
+ ? parameters[Index]
+ : xalan::XObjectPtr()
+ )...
)
);
}
inline void validateParameters(
- const XObjectArgVectorType& parameters,
+ const XObjectArgVectorType& parameters,
xalan::XPathExecutionContext& executionContext,
- xalan::XalanNode* context,
- const xalan::Locator* locator
+ xalan::XalanNode* context,
+ const xalan::Locator* locator
) const {
const bool anyNull = std::any_of(
parameters.begin(),
@@ -127,7 +140,9 @@ class FunctionBase : public xalan::Function {
}
);
- if ( parameters.size() != parameter_count || anyNull ) {
+ if ( parameters.size() > parameter_count ||
+ parameters.size() < optional_parameter_count ||
+ anyNull ) {
xalan::XPathExecutionContext::GetAndReleaseCachedString guard(
executionContext
);
diff --git a/src/function/external_text_formatter.cc b/src/function/external_command.cc
index 03902e9..bd2bf84 100644
--- a/src/function/external_text_formatter.cc
+++ b/src/function/external_command.cc
@@ -1,4 +1,4 @@
-#include "external_text_formatter.h"
+#include "external_command.h"
#include <xalanc/XSLT/XSLTInputSource.hpp>
@@ -39,9 +39,9 @@ inline xercesc::DOMNode* importDocumentElement(
namespace InputXSLT {
-DomDocumentCache::document_ptr FunctionExternalTextFormatter::constructDocument(
- std::string formatterCommand,
- std::string stdinText
+DomDocumentCache::document_ptr FunctionExternalCommand::constructDocument(
+ std::string command,
+ boost::optional<std::string> stdinText
) const {
DomDocumentCache::document_ptr domDocument(
DomDocumentCache::createDocument("content")
@@ -52,23 +52,21 @@ DomDocumentCache::document_ptr FunctionExternalTextFormatter::constructDocument(
context.stdout_behavior = boost::process::capture_stream();
context.stdin_behavior = boost::process::capture_stream();
- boost::process::child formatterProcess(
- boost::process::launch_shell(
- formatterCommand,
- context
- )
+ boost::process::child commandProcess(
+ boost::process::launch_shell(command, context)
);
- boost::process::postream& inputStream = formatterProcess.get_stdin();
- boost::process::pistream& outputStream = formatterProcess.get_stdout();
-
- inputStream << stdinText;
- inputStream.close();
+ if ( stdinText ) {
+ boost::process::postream& inputStream = commandProcess.get_stdin();
+ inputStream << *stdinText;
+ inputStream.close();
+ }
- boost::process::status status = formatterProcess.wait();
+ boost::process::pistream& outputStream = commandProcess.get_stdout();
+ boost::process::status status = commandProcess.wait();
ResultNodeFacade result(domDocument.get(), "output");
- result.setAttribute("formatter", formatterCommand);
+ result.setAttribute("command", command);
result.setAttribute("code", std::to_string(status.exit_status()));
if ( status.exited() ) {
diff --git a/src/function/external_command.h b/src/function/external_command.h
new file mode 100644
index 0000000..8af5079
--- /dev/null
+++ b/src/function/external_command.h
@@ -0,0 +1,31 @@
+#ifndef INPUTXSLT_SRC_FUNCTION_EXTERNAL_COMMAND_H_
+#define INPUTXSLT_SRC_FUNCTION_EXTERNAL_COMMAND_H_
+
+#include <boost/filesystem.hpp>
+#include <boost/optional.hpp>
+
+#include "base.h"
+
+namespace InputXSLT {
+
+class FunctionExternalCommand : public FunctionBase<
+ FunctionExternalCommand,
+ std::string,
+ boost::optional<std::string>
+> {
+ public:
+ using FunctionBase::FunctionBase;
+
+ protected:
+ friend FunctionBase;
+
+ DomDocumentCache::document_ptr constructDocument(
+ std::string,
+ boost::optional<std::string>
+ ) const;
+
+};
+
+}
+
+#endif // INPUTXSLT_SRC_FUNCTION_EXTERNAL_COMMAND_H_
diff --git a/src/function/external_text_formatter.h b/src/function/external_text_formatter.h
deleted file mode 100644
index 4788784..0000000
--- a/src/function/external_text_formatter.h
+++ /dev/null
@@ -1,30 +0,0 @@
-#ifndef INPUTXSLT_SRC_FUNCTION_EXTERNAL_TEXT_FORMATTER_H_
-#define INPUTXSLT_SRC_FUNCTION_EXTERNAL_TEXT_FORMATTER_H_
-
-#include <boost/filesystem.hpp>
-
-#include "base.h"
-
-namespace InputXSLT {
-
-class FunctionExternalTextFormatter : public FunctionBase<
- FunctionExternalTextFormatter,
- std::string,
- std::string
-> {
- public:
- using FunctionBase::FunctionBase;
-
- protected:
- friend FunctionBase;
-
- DomDocumentCache::document_ptr constructDocument(
- std::string,
- std::string
- ) const;
-
-};
-
-}
-
-#endif // INPUTXSLT_SRC_FUNCTION_EXTERNAL_TEXT_FORMATTER_H_