aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/function/execute.cc77
-rw-r--r--src/function/execute.h28
-rw-r--r--src/plattform_guard.cc7
3 files changed, 112 insertions, 0 deletions
diff --git a/src/function/execute.cc b/src/function/execute.cc
new file mode 100644
index 0000000..f19ba75
--- /dev/null
+++ b/src/function/execute.cc
@@ -0,0 +1,77 @@
+#include "execute.h"
+
+#include <xercesc/dom/DOMDocument.hpp>
+#include <xercesc/dom/DOMImplementation.hpp>
+#include <xercesc/dom/DOMElement.hpp>
+
+#include <boost/process.hpp>
+
+#include "support/xerces_string_guard.h"
+#include "support/dom/result_node_facade.h"
+
+namespace InputXSLT {
+
+xercesc::DOMDocument* FunctionExecute::constructDocument(
+ const InputXSLT::FilesystemContext&,
+ const FunctionBase::parameter_tuple& parameters
+) {
+ const std::string& executablePath(
+ std::get<0>(parameters)
+ );
+
+ const std::string& stdinText(
+ std::get<1>(parameters)
+ );
+
+ xercesc::DOMDocument* const domDocument(
+ xercesc::DOMImplementation::getImplementation()->createDocument(
+ nullptr,
+ *XercesStringGuard<XMLCh>("content"),
+ nullptr
+ )
+ );
+
+ xercesc::DOMNode* const rootNode(
+ domDocument->getDocumentElement()
+ );
+
+ boost::process::context context;
+ context.stdout_behavior = boost::process::capture_stream();
+ context.stdin_behavior = boost::process::capture_stream();
+
+ boost::process::child process(
+ boost::process::launch(
+ executablePath,
+ std::vector<std::string>{""},
+ context
+ )
+ );
+
+ boost::process::postream& inputStream = process.get_stdin();
+ boost::process::pistream& outputStream = process.get_stdout();
+
+ inputStream << stdinText;
+ inputStream.close();
+
+ const std::string resultText(
+ (std::istreambuf_iterator<char>(outputStream)),
+ (std::istreambuf_iterator<char>())
+ );
+
+ boost::process::status status = process.wait();
+
+ if ( status.exited() ) {
+ ResultNodeFacade result(domDocument, rootNode, "result");
+
+ result.setValueNode("code", std::to_string(status.exit_status()));
+ result.setValueNode("output", resultText);
+ } else {
+ ResultNodeFacade result(domDocument, rootNode, "error");
+
+ result.setValueNode("code", std::to_string(status.exit_status()));
+ }
+
+ return domDocument;
+}
+
+}
diff --git a/src/function/execute.h b/src/function/execute.h
new file mode 100644
index 0000000..12167da
--- /dev/null
+++ b/src/function/execute.h
@@ -0,0 +1,28 @@
+#ifndef INPUTXSLT_SRC_FUNCTION_EXECUTE_H_
+#define INPUTXSLT_SRC_FUNCTION_EXECUTE_H_
+
+#include "base.h"
+
+namespace InputXSLT {
+
+class FunctionExecute : public FunctionBase<
+ FunctionExecute,
+ std::string,
+ std::string
+> {
+ public:
+ using FunctionBase::FunctionBase;
+
+ protected:
+ friend FunctionBase;
+
+ xercesc::DOMDocument* constructDocument(
+ const FilesystemContext&,
+ const FunctionBase::parameter_tuple&
+ );
+
+};
+
+}
+
+#endif // INPUTXSLT_SRC_FUNCTION_EXECUTE_H_
diff --git a/src/plattform_guard.cc b/src/plattform_guard.cc
index d66c8b5..ff4de08 100644
--- a/src/plattform_guard.cc
+++ b/src/plattform_guard.cc
@@ -10,6 +10,7 @@
#include "function/read_xml_file.h"
#include "function/read_directory.h"
#include "function/transform.h"
+#include "function/execute.h"
namespace InputXSLT {
@@ -45,6 +46,12 @@ PlattformGuard::PlattformGuard(const std::vector<std::string>& path):
xalan::XalanDOMString("transform"),
InputXSLT::FunctionTransform(&this->include_resolver_)
);
+
+ xalan::XalanTransformer::installExternalFunctionGlobal(
+ customNamespace,
+ xalan::XalanDOMString("execute"),
+ InputXSLT::FunctionExecute(&this->include_resolver_)
+ );
}
PlattformGuard::~PlattformGuard() {