aboutsummaryrefslogtreecommitdiff
path: root/src/function/external_text_formatter.cc
diff options
context:
space:
mode:
authorAdrian Kummerländer2014-05-26 19:49:44 +0200
committerAdrian Kummerländer2014-05-26 19:49:44 +0200
commit86f8e73299e86b65affc1a71610dd061fa13bf5c (patch)
treee76856cd0f3b07638a4b73c7695014a39523f43a /src/function/external_text_formatter.cc
parentc5a11763985172f54d0da8a2a2778f882f3656e5 (diff)
downloadInputXSLT-86f8e73299e86b65affc1a71610dd061fa13bf5c.tar
InputXSLT-86f8e73299e86b65affc1a71610dd061fa13bf5c.tar.gz
InputXSLT-86f8e73299e86b65affc1a71610dd061fa13bf5c.tar.bz2
InputXSLT-86f8e73299e86b65affc1a71610dd061fa13bf5c.tar.lz
InputXSLT-86f8e73299e86b65affc1a71610dd061fa13bf5c.tar.xz
InputXSLT-86f8e73299e86b65affc1a71610dd061fa13bf5c.tar.zst
InputXSLT-86f8e73299e86b65affc1a71610dd061fa13bf5c.zip
Revamped external execute function into a external text formatter function
* importing XML output of a called executable into the result document required special logic which clashed with a general execute function * general execute function may be implemented in the future ** support for external text formatters with XML output has a higher priority * current implementation enables calling a markdown parser and including the XHTML output into the document
Diffstat (limited to 'src/function/external_text_formatter.cc')
-rw-r--r--src/function/external_text_formatter.cc103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/function/external_text_formatter.cc b/src/function/external_text_formatter.cc
new file mode 100644
index 0000000..d1b9e92
--- /dev/null
+++ b/src/function/external_text_formatter.cc
@@ -0,0 +1,103 @@
+#include "external_text_formatter.h"
+
+#include <xalanc/XSLT/XSLTInputSource.hpp>
+
+#include <xercesc/dom/DOMDocument.hpp>
+#include <xercesc/dom/DOMImplementation.hpp>
+#include <xercesc/dom/DOMElement.hpp>
+#include <xercesc/parsers/XercesDOMParser.hpp>
+
+#include <boost/process.hpp>
+
+#include <sstream>
+
+#include "support/xerces_string_guard.h"
+#include "support/dom/result_node_facade.h"
+
+namespace {
+
+using InputXSLT::XercesStringGuard;
+
+inline xercesc::DOMNode* importDocumentElement(
+ boost::process::pistream& outputStream,
+ xercesc::DOMDocument* const domDocument
+) {
+ std::stringstream xmlStream(
+ "<output>" + std::string(
+ (std::istreambuf_iterator<char>(outputStream)),
+ (std::istreambuf_iterator<char>())
+ ) + "</output>"
+ );
+
+ xercesc::XercesDOMParser parser;
+ parser.parse(xalan::XSLTInputSource(xmlStream));
+
+ return domDocument->importNode(
+ parser.getDocument()->getDocumentElement(),
+ true
+ );
+}
+
+}
+
+namespace InputXSLT {
+
+xercesc::DOMDocument* FunctionExternalTextFormatter::constructDocument(
+ const InputXSLT::FilesystemContext&,
+ const FunctionBase::parameter_tuple& parameters
+) {
+ const std::string& formatterPath(
+ 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 formatterProcess(
+ boost::process::launch(
+ formatterPath,
+ std::vector<std::string>{""},
+ context
+ )
+ );
+
+ boost::process::postream& inputStream = formatterProcess.get_stdin();
+ boost::process::pistream& outputStream = formatterProcess.get_stdout();
+
+ inputStream << stdinText;
+ inputStream.close();
+
+ boost::process::status status = formatterProcess.wait();
+
+ if ( status.exited() ) {
+ ResultNodeFacade result(domDocument, rootNode, "result");
+
+ result.setValueNode("code", std::to_string(status.exit_status()));
+ result.setContent(importDocumentElement(outputStream, domDocument));
+ } else {
+ ResultNodeFacade result(domDocument, rootNode, "error");
+
+ result.setValueNode("code", std::to_string(status.exit_status()));
+ }
+
+ return domDocument;
+}
+
+}