aboutsummaryrefslogtreecommitdiff
path: root/src/function/read_xml_file.cc
blob: dc98fc7d43e24a3162149b9c1c57a04253c0e42c (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "read_xml_file.h"

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

#include "boost/filesystem/fstream.hpp"

#include "support/xerces_string_guard.h"
#include "support/filesystem_context.h"

namespace {

inline xercesc::DOMNode* importDocumentElement(
	const boost::filesystem::path& filePath,
	xercesc::DOMDocument* const domDocument
) {
	xercesc::XercesDOMParser parser;
	boost::filesystem::ifstream file(filePath);
	parser.parse(xalan::XSLTInputSource(file));

	return domDocument->importNode(
		parser.getDocument()->getDocumentElement(),
		true
	);
}

}

namespace InputXSLT {

FunctionReadXmlFile::FunctionReadXmlFile():
	document_cache_(std::make_shared<DomDocumentCache>()) { }

xalan::XObjectPtr FunctionReadXmlFile::execute(
	xalan::XPathExecutionContext& executionContext,
	xalan::XalanNode*,
	const xalan::XObjectPtr argument,
	const xalan::Locator* locator
) const {
	const FilesystemContext fs_context(
		boost::filesystem::path(
			*XercesStringGuard<char>(
				xercesc::XMLString::transcode(
					locator->getSystemId() + 7
				)
			)
		).parent_path().string()
	);

	const boost::filesystem::path filePath(
		fs_context.resolve(argument->str())
	);

	DomDocumentCache::optional_item optionalCachedDocument(
		this->document_cache_->get(filePath.string())
	);

	if ( !optionalCachedDocument.first ) {
		xercesc::DOMDocument* const domDocument(
			xercesc::DOMImplementation::getImplementation()->createDocument(
				nullptr,
				*XercesStringGuard<XMLCh>("content"),
				nullptr
			)
		);

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

		if ( boost::filesystem::is_regular_file(filePath) ) {
			xercesc::DOMElement* const resultNode(
				domDocument->createElement(*XercesStringGuard<XMLCh>("result"))
			);

			resultNode->setAttribute(
				*XercesStringGuard<XMLCh>("name"),
				*XercesStringGuard<XMLCh>(filePath.filename().string())
			);

			xercesc::DOMNode* const resultTreeNode(
				importDocumentElement(filePath, domDocument)
			);

			resultNode->appendChild(resultTreeNode);
			rootNode->appendChild(resultNode);
		} else {
			xercesc::DOMElement* const resultNode(
				domDocument->createElement(*XercesStringGuard<XMLCh>("error"))
			);

			rootNode->appendChild(resultNode);
		}

		optionalCachedDocument = this->document_cache_->create(
			filePath.string(),
			domDocument
		);
	}

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

	nodeList->addNodes(
		*optionalCachedDocument.second->getXalanDocument()
		                              ->getDocumentElement()
		                              ->getChildNodes()
	);

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

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

const xalan::XalanDOMString& FunctionReadXmlFile::getError(
	xalan::XalanDOMString& result) const {
	result.assign("The read-xml-file() function expects one argument of type string.");

	return result;
}

}