From 86f8e73299e86b65affc1a71610dd061fa13bf5c Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Mon, 26 May 2014 19:49:44 +0200 Subject: 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 --- src/function/execute.cc | 77 ------------------------ src/function/execute.h | 28 --------- src/function/external_text_formatter.cc | 103 ++++++++++++++++++++++++++++++++ src/function/external_text_formatter.h | 28 +++++++++ 4 files changed, 131 insertions(+), 105 deletions(-) delete mode 100644 src/function/execute.cc delete mode 100644 src/function/execute.h create mode 100644 src/function/external_text_formatter.cc create mode 100644 src/function/external_text_formatter.h (limited to 'src/function') diff --git a/src/function/execute.cc b/src/function/execute.cc deleted file mode 100644 index f19ba75..0000000 --- a/src/function/execute.cc +++ /dev/null @@ -1,77 +0,0 @@ -#include "execute.h" - -#include -#include -#include - -#include - -#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("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{""}, - 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(outputStream)), - (std::istreambuf_iterator()) - ); - - 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 deleted file mode 100644 index 12167da..0000000 --- a/src/function/execute.h +++ /dev/null @@ -1,28 +0,0 @@ -#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/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 + +#include +#include +#include +#include + +#include + +#include + +#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( + "" + std::string( + (std::istreambuf_iterator(outputStream)), + (std::istreambuf_iterator()) + ) + "" + ); + + 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("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{""}, + 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; +} + +} diff --git a/src/function/external_text_formatter.h b/src/function/external_text_formatter.h new file mode 100644 index 0000000..877bac5 --- /dev/null +++ b/src/function/external_text_formatter.h @@ -0,0 +1,28 @@ +#ifndef INPUTXSLT_SRC_FUNCTION_EXTERNAL_TEXT_FORMATTER_H_ +#define INPUTXSLT_SRC_FUNCTION_EXTERNAL_TEXT_FORMATTER_H_ + +#include "base.h" + +namespace InputXSLT { + +class FunctionExternalTextFormatter : public FunctionBase< + FunctionExternalTextFormatter, + std::string, + std::string +> { + public: + using FunctionBase::FunctionBase; + + protected: + friend FunctionBase; + + xercesc::DOMDocument* constructDocument( + const FilesystemContext&, + const FunctionBase::parameter_tuple& + ); + +}; + +} + +#endif // INPUTXSLT_SRC_FUNCTION_EXTERNAL_TEXT_FORMATTER_H_ -- cgit v1.2.3