aboutsummaryrefslogtreecommitdiff
path: root/src/function/read_directory.cc
blob: 3f503b3f07463712676838e6bcc73e406a9482bc (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
91
92
93
94
95
96
97
98
99
100
#include "read_directory.h"

#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 "support/utility.h"
#include "support/xerces_string_guard.h"

namespace InputXSLT {

FunctionReadDirectory::FunctionReadDirectory(const FilesystemContext& context):
	fs_context_(context),
	document_cache_(std::make_shared<DomDocumentCache>()) { }

xalan::XObjectPtr FunctionReadDirectory::execute(
	xalan::XPathExecutionContext& executionContext,
	xalan::XalanNode*,
	const xalan::XObjectPtr argument,
	const xalan::Locator*
) const {
	DomDocumentCache::item* const cachedDocument(
		this->document_cache_->get(xalanToString(argument->str()))
	);

	if ( !cachedDocument->isFinalized() ) {
		xercesc::DOMDocument* const domDocument(
			cachedDocument->getXercesDocument()
		);

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

		this->fs_context_.iterate(
			argument->str(),
			[&domDocument, &rootNode](const boost::filesystem::path& p) {
			xercesc::DOMElement* const itemNode(
				domDocument->createElement(*XercesStringGuard("item"))
			);

			switch ( boost::filesystem::status(p).type() ) {
				case boost::filesystem::regular_file: {
					itemNode->setAttribute(
						*XercesStringGuard("type"),
						*XercesStringGuard("file")
					);

					break;
				};
				case boost::filesystem::directory_file: {
					itemNode->setAttribute(
						*XercesStringGuard("type"),
						*XercesStringGuard("directory")
					);

					break;
				};
				default: {
					break;
				};
			}

			xercesc::DOMText* const textNode(
				domDocument->createTextNode(
					*XercesStringGuard(p.filename().string())
				)
			);

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

	xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList(
		executionContext
	);

	nodeList->addNodes(
		*cachedDocument->getXalanDocument()->getDocumentElement()->getChildNodes()
	);

	return executionContext.getXObjectFactory().createNodeSet(nodeList);
}

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 of type string.");

	return result;
}

}