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

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

#include "support/xerces_string_guard.h"
#include "support/dom/result_node_facade.h"

namespace InputXSLT {

xercesc::DOMDocument* FunctionReadDirectory::constructDocument(
	boost::filesystem::path directoryPath) {
	xercesc::DOMDocument* const domDocument(
		xercesc::DOMImplementation::getImplementation()->createDocument(
			nullptr,
			*XercesStringGuard<XMLCh>("content"),
			nullptr
		)
	);

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

	ResultNodeFacade result(domDocument, rootNode, "directory");
	result.setAttribute("path", directoryPath.string());

	if ( boost::filesystem::is_directory(directoryPath) ) {
		result.setAttribute("result", "success");

		xercesc::DOMNode* const resultNode = result.getResultElement();

		FilesystemContext::iterate(
			directoryPath,
			[&domDocument, &resultNode](const boost::filesystem::path& p) {
			ResultNodeFacade result(domDocument, resultNode, "entry");

			switch ( boost::filesystem::status(p).type() ) {
				case boost::filesystem::regular_file: {
					result.setAttribute("type",      "file");
					result.setValueNode("name",      p.stem().string());
					result.setValueNode("extension", p.extension().string());

					break;
				};
				case boost::filesystem::directory_file: {
					result.setAttribute("type", "directory");
					result.setValueNode("name", p.filename().string());

					break;
				};
				default: {
					result.setAttribute("type", "misc");
					result.setValueNode("name", p.filename().string());

					break;
				};
			}

			result.setValueNode("full", boost::filesystem::canonical(p).string());
		});
	} else {
		result.setAttribute("result", "error");
	}

	return domDocument;
}

}