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

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

#include "transformation_facade.h"
#include "support/xerces_string_guard.h"
#include "support/dom/result_node_facade.h"
#include "support/error/error_capacitor.h"

namespace {

using InputXSLT::ErrorCapacitor;

inline std::function<void(const ErrorCapacitor::error_cache&)> handleErrors(
	InputXSLT::ResultNodeFacade& result) {
	return [&result](const ErrorCapacitor::error_cache& errors) {
		result.setAttribute("result", "error");

		for ( auto&& error : errors ) {
			result.setValueNode("error", error);
		}
	};
}

}

namespace InputXSLT {

xercesc::DOMDocument* FunctionTransform::constructDocument(
	const InputXSLT::FilesystemContext& fsContext,
	std::string                         transformationPath,
	std::string                         targetPath,
	xalan::XObjectPtr                   parameterObject
) {
	xercesc::DOMDocument* const domDocument(
		xercesc::DOMImplementation::getImplementation()->createDocument(
			nullptr,
			*XercesStringGuard<XMLCh>("content"),
			nullptr
		)
	);

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

	ResultNodeFacade result(domDocument, rootNode, "transformation");

	result.setAttribute(
		"target",
		boost::filesystem::path(targetPath).filename().string()
	);

	if ( auto transformation = TransformationFacade::try_create(
		handleErrors(result),
		fsContext.resolve(transformationPath).string(),
		this->include_resolver_
	) ) {
		try {
			transformation->generate(
				fsContext.resolve(targetPath).string(),
				parameterObject
			);

			result.setAttribute("result", "success");
		}
		catch (const ErrorCapacitor::exception& exception) {
			handleErrors(result)(*exception);
		}

		WarningCapacitor::warning_cache_ptr warnings(
			transformation->getCachedWarnings()
		);

		for ( auto&& warning : *warnings ) {
			result.setValueNode("warning", warning);
		}
	}

	return domDocument;
}

}