diff options
author | Adrian Kummerlaender | 2014-06-26 20:26:32 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2014-06-26 20:26:32 +0200 |
commit | 266b408ee53b9bc7db2b1a5c92e62adfd2af6def (patch) | |
tree | b84551406742ba892824a5770ae8fb8ec3a6a510 /src/function/write_file.cc | |
parent | 7b872121000d4db4026d0c90fcb95a10f1e43694 (diff) | |
download | InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar.gz InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar.bz2 InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar.lz InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar.xz InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.tar.zst InputXSLT-266b408ee53b9bc7db2b1a5c92e62adfd2af6def.zip |
Implemented basic external "write-file" function
* accepts a path parameter and the content to be written
* removed target parameter form FunctionTransform
** transformation result is now returned as a string
*** nodeset return value is planned
** e.g. writing the result to the fs is optional and has to be achieved using FunctionWriteFile
* changed "transform" test case accordingly
* this marks a paradigm shift and is the continuation of the changes described in 741a70f
** InputXSLT now also implements a output function
* added basic io error handling to FunctionReadFile
Diffstat (limited to 'src/function/write_file.cc')
-rw-r--r-- | src/function/write_file.cc | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/function/write_file.cc b/src/function/write_file.cc new file mode 100644 index 0000000..39495d8 --- /dev/null +++ b/src/function/write_file.cc @@ -0,0 +1,62 @@ +#include "write_file.h" + +#include <xercesc/dom/DOMDocument.hpp> +#include <xercesc/dom/DOMImplementation.hpp> +#include <xercesc/dom/DOMElement.hpp> + +#include <boost/filesystem.hpp> +#include <boost/filesystem/fstream.hpp> + +#include "support/xerces_string_guard.h" +#include "support/dom/result_node_facade.h" + +namespace { + +bool writeFile( + const boost::filesystem::path& filePath, + const std::string& content +) { + boost::filesystem::ofstream file(filePath); + + if ( file.is_open() ) { + file << content << std::endl; + + return true; + } else { + return false; + } +} + +} + +namespace InputXSLT { + +xercesc::DOMDocument* FunctionWriteFile::constructDocument( + boost::filesystem::path filePath, + std::string content +) { + xercesc::DOMDocument* const domDocument( + xercesc::DOMImplementation::getImplementation()->createDocument( + nullptr, + *XercesStringGuard<XMLCh>("content"), + nullptr + ) + ); + + xercesc::DOMNode* const rootNode( + domDocument->getDocumentElement() + ); + + ResultNodeFacade result(domDocument, rootNode, "file"); + result.setAttribute("path", filePath.string()); + + if ( writeFile(filePath, content) ) { + result.setAttribute("result", "success"); + } else { + result.setAttribute("result", "error"); + } + + return domDocument; +} + +} |