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

#include <xalanc/PlatformSupport/XalanOutputStreamPrintWriter.hpp>
#include <xalanc/PlatformSupport/XalanStdOutputStream.hpp>
#include <xalanc/XMLSupport/FormatterToXML.hpp>

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>

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

namespace InputXSLT {

DomDocumentCache::document_ptr FunctionGenerate::constructDocument(
	xalan::XSLTInputSource  inputSource,
	xalan::XSLTInputSource  transformationSource,
	boost::filesystem::path targetPath
) const {
	DomDocumentCache::document_ptr domDocument(
		DomDocumentCache::createDocument("content")
	);

	ResultNodeFacade result(domDocument.get(), "generation");
	result.setAttribute("path", targetPath.string());

	boost::filesystem::create_directories(targetPath.parent_path());
	boost::filesystem::ofstream file(targetPath);

	if ( file.is_open() ) {
		TransformerFacade transformer(this->include_resolver_);

		try {
			xalan::XalanStdOutputStream         output(file);
			xalan::XalanOutputStreamPrintWriter writer(output);
			xalan::FormatterToXML               targetFormatter(writer);

			targetFormatter.setDoIndent(true);

			transformer.generate(
				inputSource,
				transformationSource,
				targetFormatter
			);

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

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

		WarningCapacitor::warning_cache_ptr warnings(
			transformer.getCachedWarnings()
		);

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

	return domDocument;
}

}