aboutsummaryrefslogtreecommitdiff
path: root/src/function/read_file.cc
blob: ac6225a6310f4dde67c213982a5be4794cee9dd3 (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
#include "read_file.h"

#include "boost/filesystem/fstream.hpp"

#include <fstream>

namespace InputXSLT {

FunctionReadFile::FunctionReadFile(const FilesystemContext& context):
	fs_context_(context) { }

xalan::XObjectPtr FunctionReadFile::execute(
	xalan::XPathExecutionContext& executionContext,
	xalan::XalanNode*,
	const xalan::XObjectPtr argument,
	const xalan::Locator*
) const {
	const boost::filesystem::path filePath(
		this->fs_context_.resolve(argument->str())
	);

	if ( boost::filesystem::is_regular_file(filePath) ) {
		boost::filesystem::ifstream file(filePath);

		const std::string fileContent(
			(std::istreambuf_iterator<char>(file)),
			(std::istreambuf_iterator<char>())
		);

		return executionContext.getXObjectFactory().createString(
			xalan::XalanDOMString(fileContent.data())
		);
	} else {
		return executionContext.getXObjectFactory().createString(
			xalan::XalanDOMString("io error")
		);
	}
}

FunctionReadFile* FunctionReadFile::clone(
	xalan::MemoryManager& manager) const {
	return xalan::XalanCopyConstruct(manager, *this);
}

const xalan::XalanDOMString& FunctionReadFile::getError(
	xalan::XalanDOMString& result) const {
	result.assign("The read-file() function expects one argument of type string.");

	return result;
}

}