summaryrefslogtreecommitdiffstats
path: root/test/src/test-arabica-namespaces.cpp
blob: ae529c99efa24913af1e310b6dff864b7c84d3ac (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>

#include "uscxml/config.h"
#include "uscxml/Common.h"
#include <DOM/Document.hpp>
#include <XPath/XPath.hpp>
#include <DOM/SAX2DOM/SAX2DOM.hpp>
#include <DOM/io/Stream.hpp>
#include "uscxml/Interpreter.h"
#include "uscxml/DOMUtils.h"

using namespace Arabica::DOM;
using namespace Arabica::XPath;
using namespace uscxml;

#define VALIDATE \
std::pair<Document<std::string>, NameSpaceInfo> parsed = parse(xmlSS.str());\
Document<std::string> origDoc = parsed.first;\
NameSpaceInfo origNS = parsed.second;\
validateRootFoo(parsed);\
insertBar(parsed);\
std::cout << parsed.first << std::endl;\
validateRootFooBar(parsed);\
parsed = cloneDocument(parsed);\
insertBaz(parsed);\
std::cout << parsed.first << std::endl;\
validateRootFooBarBaz(parsed);\
assert(Interpreter::filterChildElements(origNS.xmlNSPrefix + "bar", origDoc.getDocumentElement()).size() == 3);\
assert(Interpreter::filterChildElements(origNS.xmlNSPrefix + "baz", origDoc.getDocumentElement()).size() == 0);


/**
 Test DOM manipulations and document cloning with different namespace scenarios
 */

static std::string testPath;
static Arabica::XPath::XPath<std::string> _xpath;

std::pair<Document<std::string>, NameSpaceInfo> parse(const std::string xmlString) {
	NameSpacingParser parser = NameSpacingParser::fromXML(xmlString);
	if (parser.errorsReported())
		assert(false);
	return std::make_pair(parser.getDocument(), parser.nameSpace);
}

std::pair<Document<std::string>, NameSpaceInfo> cloneDocument(std::pair<Document<std::string>, NameSpaceInfo>& parsed) {

	NameSpaceInfo nsInfo = parsed.second;
	Document<std::string> document = parsed.first;

	Document<std::string> clonedDocument;
	DOMImplementation<std::string> domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
	clonedDocument = domFactory.createDocument(document.getNamespaceURI(), "", 0);

	Node<std::string> child = document.getFirstChild();
	while (child) {
		Node<std::string> newNode = clonedDocument.importNode(child, true);
		clonedDocument.appendChild(newNode);
		child = child.getNextSibling();
	}

	return std::make_pair(clonedDocument, nsInfo);
}

void insertBar(std::pair<Document<std::string>, NameSpaceInfo>& parsed) {
	NameSpaceInfo nsInfo = parsed.second;
	Document<std::string> document = parsed.first;

	Node<std::string> root = document.getDocumentElement();
	for (int i = 0; i < 3; i++) {
		Element<std::string> bar = document.createElementNS(nsInfo.nsURL, "bar");
//		if (nsInfo.nsToPrefix.find(nsInfo.nsURL) != nsInfo.nsToPrefix.end())
		nsInfo.setPrefix(bar);
		root.appendChild(bar);
	}
}

void insertBaz(std::pair<Document<std::string>, NameSpaceInfo>& parsed) {
	NameSpaceInfo nsInfo = parsed.second;
	Document<std::string> document = parsed.first;

	Node<std::string> root = document.getDocumentElement();
	for (int i = 0; i < 3; i++) {
		Element<std::string> baz = document.createElementNS(nsInfo.nsURL, "baz");
		nsInfo.setPrefix(baz);
		root.appendChild(baz);
	}
}

static void validateRootFoo(std::pair<Document<std::string>, NameSpaceInfo>& parsed) {

	NameSpaceInfo nsInfo = parsed.second;
	Document<std::string> document = parsed.first;

	Node<std::string> root = document.getDocumentElement();
	_xpath.setNamespaceContext(*nsInfo.getNSContext());

	assert(TAGNAME(root) == nsInfo.xmlNSPrefix + "root");
	assert(LOCALNAME(root) == "root");
	NodeSet<std::string> foosFiltered = Interpreter::filterChildElements(nsInfo.xmlNSPrefix + "foo", root);
	assert(foosFiltered.size() == 3);
	NodeSet<std::string> foosXPath = _xpath.evaluate("//" + nsInfo.xpathPrefix + "foo", root).asNodeSet();
	assert(foosXPath.size() == 3);

	for (int i = 0; i < 3; i++) {
		assert(foosFiltered[i] == foosXPath[i]);
		assert(TAGNAME(foosFiltered[i]) == nsInfo.xmlNSPrefix + "foo");
		assert(LOCALNAME(foosFiltered[i]) == "foo");
	}

}

static void validateRootFooBar(std::pair<Document<std::string>, NameSpaceInfo>& parsed) {
	validateRootFoo(parsed);

	NameSpaceInfo nsInfo = parsed.second;
	Document<std::string> document = parsed.first;

	Node<std::string> root = document.getDocumentElement();
	_xpath.setNamespaceContext(*nsInfo.getNSContext());

	NodeSet<std::string> barsFiltered = Interpreter::filterChildElements(nsInfo.xmlNSPrefix + "bar", root);
	assert(barsFiltered.size() == 3);
	NodeSet<std::string> barsXPath = _xpath.evaluate("//" + nsInfo.xpathPrefix + "bar", root).asNodeSet();
	assert(barsXPath.size() == 3);

	for (int i = 0; i < 3; i++) {
		assert(barsFiltered[i] == barsXPath[i]);
		assert(TAGNAME(barsFiltered[i]) == nsInfo.xmlNSPrefix + "bar");
		assert(LOCALNAME(barsFiltered[i]) == "bar");
	}

}

static void validateRootFooBarBaz(std::pair<Document<std::string>, NameSpaceInfo>& parsed) {
	validateRootFooBar(parsed);

	NameSpaceInfo nsInfo = parsed.second;
	Document<std::string> document = parsed.first;

	Node<std::string> root = document.getDocumentElement();
	_xpath.setNamespaceContext(*nsInfo.getNSContext());

	assert(TAGNAME(root) == nsInfo.xmlNSPrefix + "root");
	assert(LOCALNAME(root) == "root");

	NodeSet<std::string> bazsFiltered = Interpreter::filterChildElements(nsInfo.xmlNSPrefix + "baz", root);
	assert(bazsFiltered.size() == 3);
	NodeSet<std::string> bazsXPath = _xpath.evaluate("//" + nsInfo.xpathPrefix + "baz", root).asNodeSet();
	assert(bazsXPath.size() == 3);

	for (int i = 0; i < 3; i++) {
		assert(bazsFiltered[i] == bazsXPath[i]);
		assert(TAGNAME(bazsFiltered[i]) == nsInfo.xmlNSPrefix + "baz");
		assert(LOCALNAME(bazsFiltered[i]) == "baz");
	}

}

int main(int argc, char** argv) {

	if (argc < 2) {
		std::cerr << "Expected path to the tests as first argument";
		exit(EXIT_FAILURE);
	}
	testPath = argv[1];

	// No namespaces at all
	{
		std::stringstream xmlSS;
		xmlSS << "<root><foo /><foo /><foo /></root>" << std::endl;
		VALIDATE
	}

	// default namespace
	{
		std::stringstream xmlSS;
		xmlSS << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
		<root xmlns=\"http://www.w3.org/2005/07/scxml\">\
			<foo /><foo /><foo />\
		</root>\
		" << std::endl;
		VALIDATE
	}

	// explicit namespaces
	{
		std::stringstream xmlSS;
		xmlSS << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
		<scxml:root xmlns:scxml=\"http://www.w3.org/2005/07/scxml\">\
			<scxml:foo /><scxml:foo /><scxml:foo />\
		</scxml:root>\
		" << std::endl;
		VALIDATE
	}

	// mixed namespaces
	{
		std::stringstream xmlSS;
		xmlSS << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
		<scxml:root xmlns:scxml=\"http://www.w3.org/2005/07/scxml\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\
		<xhtml:foo /><xhtml:foo /><xhtml:foo />\
		<scxml:foo /><scxml:foo /><scxml:foo />\
		</scxml:root>\
		" << std::endl;
		VALIDATE
	}

	// mixed namespaces with different default NS
	{
		std::stringstream xmlSS;
		xmlSS << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
		<scxml:root xmlns:scxml=\"http://www.w3.org/2005/07/scxml\" xmlns=\"http://www.w3.org/1999/xhtml\">\
		<foo /><foo /><foo />\
		<scxml:foo /><scxml:foo /><scxml:foo />\
		</scxml:root>\
		" << std::endl;
		VALIDATE
	}

}