aboutsummaryrefslogtreecommitdiff
path: root/src/function/read_directory.cc
blob: a4284c09e14bd017129ef8b126ec50532a173e51 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "read_directory.h"

#include <xalanc/XercesParserLiaison/XercesDOMSupport.hpp>
#include <xalanc/XalanTransformer/XercesDOMWrapperParsedSource.hpp>

#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/dom/DOMText.hpp>
#include <xercesc/util/XMLString.hpp>

#include <sstream>

namespace InputXSLT {

FunctionReadDirectory::FunctionReadDirectory(const FilesystemContext& context):
	fs_context_(context),
	parser_() { }

FunctionReadDirectory::FunctionReadDirectory(const FunctionReadDirectory& src):
	fs_context_(src.fs_context_),
	parser_() { }

xalan::XObjectPtr FunctionReadDirectory::execute(
	xalan::XPathExecutionContext&                executionContext,
	xalan::XalanNode*                            context,
	const xalan::Function::XObjectArgVectorType& arguments,
	const xalan::Locator*                        locator
) const {
	if ( arguments.size() != 1 ) {
		xalan::XPathExecutionContext::GetAndReleaseCachedString guard(
			executionContext
		);

		this->generalError(executionContext, context, locator);
	}

	xercesc::DOMDocument* const inputDom(
		xercesc::DOMImplementation::getImplementation()->createDocument(
			nullptr, xercesc::XMLString::transcode("content"), nullptr
		)
	);

	xercesc::DOMNode* const rootNode = inputDom->getDocumentElement();

	this->fs_context_.iterate(
		arguments[0]->str(),
		[&inputDom, &rootNode](const boost::filesystem::path& p) {
		xercesc::DOMNode* const fileNode = inputDom->createElement(
			xercesc::XMLString::transcode("file")
		);

		xercesc::DOMText* const textNode = inputDom->createTextNode(
			xercesc::XMLString::transcode(p.filename().string().data())
		);

		fileNode->appendChild(textNode);
		rootNode->appendChild(fileNode);
	});


	xalan::XercesDOMSupport domSupport(this->parser_);

	xalan::XercesDOMWrapperParsedSource* const parsedSource(
		new xalan::XercesDOMWrapperParsedSource(
			inputDom,
			this->parser_,
			domSupport
		)
	);

	return executionContext.getXObjectFactory().createNodeSet(
		parsedSource->getDocument()
	);

}

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

const xalan::XalanDOMString& FunctionReadDirectory::getError(
	xalan::XalanDOMString& result) const {
	result.assign("The read-directory() function expects one argument.");

	return result;
}

}