aboutsummaryrefslogtreecommitdiff
path: root/src/function/write_file.cc
blob: 39495d8b956412aa8c69d529daee829ca0797a31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;
}

}