diff options
author | Adrian Kummerländer | 2014-05-26 19:49:44 +0200 |
---|---|---|
committer | Adrian Kummerländer | 2014-05-26 19:49:44 +0200 |
commit | 86f8e73299e86b65affc1a71610dd061fa13bf5c (patch) | |
tree | e76856cd0f3b07638a4b73c7695014a39523f43a /src/function | |
parent | c5a11763985172f54d0da8a2a2778f882f3656e5 (diff) | |
download | InputXSLT-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')
-rw-r--r-- | src/function/external_text_formatter.cc (renamed from src/function/execute.cc) | 56 | ||||
-rw-r--r-- | src/function/external_text_formatter.h (renamed from src/function/execute.h) | 10 |
2 files changed, 46 insertions, 20 deletions
diff --git a/src/function/execute.cc b/src/function/external_text_formatter.cc index f19ba75..d1b9e92 100644 --- a/src/function/execute.cc +++ b/src/function/external_text_formatter.cc @@ -1,21 +1,52 @@ -#include "execute.h" +#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* FunctionExecute::constructDocument( +xercesc::DOMDocument* FunctionExternalTextFormatter::constructDocument( const InputXSLT::FilesystemContext&, const FunctionBase::parameter_tuple& parameters ) { - const std::string& executablePath( + const std::string& formatterPath( std::get<0>(parameters) ); @@ -39,32 +70,27 @@ xercesc::DOMDocument* FunctionExecute::constructDocument( context.stdout_behavior = boost::process::capture_stream(); context.stdin_behavior = boost::process::capture_stream(); - boost::process::child process( + boost::process::child formatterProcess( boost::process::launch( - executablePath, + formatterPath, std::vector<std::string>{""}, context ) ); - boost::process::postream& inputStream = process.get_stdin(); - boost::process::pistream& outputStream = process.get_stdout(); + boost::process::postream& inputStream = formatterProcess.get_stdin(); + boost::process::pistream& outputStream = formatterProcess.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(); + boost::process::status status = formatterProcess.wait(); if ( status.exited() ) { ResultNodeFacade result(domDocument, rootNode, "result"); - result.setValueNode("code", std::to_string(status.exit_status())); - result.setValueNode("output", resultText); + result.setValueNode("code", std::to_string(status.exit_status())); + result.setContent(importDocumentElement(outputStream, domDocument)); } else { ResultNodeFacade result(domDocument, rootNode, "error"); diff --git a/src/function/execute.h b/src/function/external_text_formatter.h index 12167da..877bac5 100644 --- a/src/function/execute.h +++ b/src/function/external_text_formatter.h @@ -1,12 +1,12 @@ -#ifndef INPUTXSLT_SRC_FUNCTION_EXECUTE_H_ -#define INPUTXSLT_SRC_FUNCTION_EXECUTE_H_ +#ifndef INPUTXSLT_SRC_FUNCTION_EXTERNAL_TEXT_FORMATTER_H_ +#define INPUTXSLT_SRC_FUNCTION_EXTERNAL_TEXT_FORMATTER_H_ #include "base.h" namespace InputXSLT { -class FunctionExecute : public FunctionBase< - FunctionExecute, +class FunctionExternalTextFormatter : public FunctionBase< + FunctionExternalTextFormatter, std::string, std::string > { @@ -25,4 +25,4 @@ class FunctionExecute : public FunctionBase< } -#endif // INPUTXSLT_SRC_FUNCTION_EXECUTE_H_ +#endif // INPUTXSLT_SRC_FUNCTION_EXTERNAL_TEXT_FORMATTER_H_ |