aboutsummaryrefslogtreecommitdiff
path: root/src/function/write_file.cc
diff options
context:
space:
mode:
authorAdrian Kummerlaender2014-06-29 20:36:28 +0200
committerAdrian Kummerlaender2014-06-29 20:36:28 +0200
commitbd39d49464dbd17eb3d4cdbb5784431e43e56fbf (patch)
tree48013d44196637c6ac434c239be1b4950ba7056f /src/function/write_file.cc
parent299d0f6fa3fbd1bfd8d1d6d452e9d055973d0e0e (diff)
downloadInputXSLT-bd39d49464dbd17eb3d4cdbb5784431e43e56fbf.tar
InputXSLT-bd39d49464dbd17eb3d4cdbb5784431e43e56fbf.tar.gz
InputXSLT-bd39d49464dbd17eb3d4cdbb5784431e43e56fbf.tar.bz2
InputXSLT-bd39d49464dbd17eb3d4cdbb5784431e43e56fbf.tar.lz
InputXSLT-bd39d49464dbd17eb3d4cdbb5784431e43e56fbf.tar.xz
InputXSLT-bd39d49464dbd17eb3d4cdbb5784431e43e56fbf.tar.zst
InputXSLT-bd39d49464dbd17eb3d4cdbb5784431e43e56fbf.zip
Switched FunctionWriteFile content parameter to xalan::XalanNode pointer
* i.e. "write-file" now supports the serialization of given DOM structures ** this will be needed e.g. if FunctionTransform returns the result as a DOM tree instead of as a plain string ** enables the creation of multiple XML documents from within a single transformation *** i.e. backports the functionality provided in XSLT 2.0 by "xsl:result-document" to xalan's XSLT 1.0 * changed test cases accordingly
Diffstat (limited to 'src/function/write_file.cc')
-rw-r--r--src/function/write_file.cc53
1 files changed, 44 insertions, 9 deletions
diff --git a/src/function/write_file.cc b/src/function/write_file.cc
index 39495d8..024a87a 100644
--- a/src/function/write_file.cc
+++ b/src/function/write_file.cc
@@ -1,5 +1,10 @@
#include "write_file.h"
+#include <xalanc/PlatformSupport/XalanOutputStreamPrintWriter.hpp>
+#include <xalanc/PlatformSupport/XalanStdOutputStream.hpp>
+#include <xalanc/XMLSupport/FormatterToXML.hpp>
+#include <xalanc/XMLSupport/FormatterTreeWalker.hpp>
+
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMElement.hpp>
@@ -7,21 +12,47 @@
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
+#include "support/xalan_string.h"
#include "support/xerces_string_guard.h"
#include "support/dom/result_node_facade.h"
namespace {
-bool writeFile(
+bool serializeNodeToFile(
const boost::filesystem::path& filePath,
- const std::string& content
+ xalan::XalanNode* const contentNode
) {
- boost::filesystem::ofstream file(filePath);
+ const xalan::XalanNode::NodeType contentType(
+ contentNode->getNodeType()
+ );
+
+ if ( contentType != xalan::XalanNode::DOCUMENT_NODE &&
+ contentType != xalan::XalanNode::ATTRIBUTE_NODE ) {
+ boost::filesystem::ofstream file(filePath);
+
+ if ( file.is_open() ) {
+ if ( contentType == xalan::XalanNode::TEXT_NODE ) {
+ file << InputXSLT::toString(contentNode->getNodeValue());
+ } else {
+ xalan::XalanStdOutputStream outputStream(file);
+ xalan::XalanOutputStreamPrintWriter outputWriter(outputStream);
+
+ xalan::FormatterToXML formatter(outputWriter);
+ xalan::FormatterTreeWalker walker(formatter);
+
+ formatter.startDocument();
+
+ walker.traverseSubtree(contentNode);
+
+ formatter.endDocument();
+ }
- if ( file.is_open() ) {
- file << content << std::endl;
+ file << std::endl;
- return true;
+ return true;
+ } else {
+ return false;
+ }
} else {
return false;
}
@@ -33,7 +64,7 @@ namespace InputXSLT {
xercesc::DOMDocument* FunctionWriteFile::constructDocument(
boost::filesystem::path filePath,
- std::string content
+ xalan::XalanNode* const contentNode
) {
xercesc::DOMDocument* const domDocument(
xercesc::DOMImplementation::getImplementation()->createDocument(
@@ -50,8 +81,12 @@ xercesc::DOMDocument* FunctionWriteFile::constructDocument(
ResultNodeFacade result(domDocument, rootNode, "file");
result.setAttribute("path", filePath.string());
- if ( writeFile(filePath, content) ) {
- result.setAttribute("result", "success");
+ if ( contentNode != nullptr ) {
+ if ( serializeNodeToFile(filePath, contentNode) ) {
+ result.setAttribute("result", "success");
+ } else {
+ result.setAttribute("result", "error");
+ }
} else {
result.setAttribute("result", "error");
}