/
XmlIo.cpp
350 lines (309 loc) · 12.4 KB
/
XmlIo.cpp
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//
#include <MaterialXTest/External/Catch/catch.hpp>
#include <MaterialXFormat/Environ.h>
#include <MaterialXFormat/Util.h>
#include <MaterialXFormat/XmlIo.h>
namespace mx = MaterialX;
TEST_CASE("Load content", "[xmlio]")
{
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::FilePath libraryPath = searchPath.find("libraries/stdlib");
mx::FilePath examplesPath = searchPath.find("resources/Materials/Examples/StandardSurface");
searchPath.append(libraryPath);
searchPath.append(examplesPath);
// Read the standard library.
std::vector<mx::DocumentPtr> libs;
for (const mx::FilePath& filename : libraryPath.getFilesInDirectory(mx::MTLX_EXTENSION))
{
mx::DocumentPtr lib = mx::createDocument();
mx::readFromXmlFile(lib, filename, searchPath);
libs.push_back(lib);
}
// Read and validate each example document.
for (const mx::FilePath& filename : examplesPath.getFilesInDirectory(mx::MTLX_EXTENSION))
{
mx::DocumentPtr doc = mx::createDocument();
mx::readFromXmlFile(doc, filename, searchPath);
for (mx::DocumentPtr lib : libs)
{
doc->importLibrary(lib);
}
std::string message;
bool docValid = doc->validate(&message);
if (!docValid)
{
WARN("[" + filename.asString() + "] " + message);
}
REQUIRE(docValid);
// Traverse the document tree
int valueElementCount = 0;
for (mx::ElementPtr elem : doc->traverseTree())
{
if (elem->isA<mx::ValueElement>())
{
valueElementCount++;
}
}
REQUIRE(valueElementCount > 0);
// Serialize to XML.
mx::XmlWriteOptions writeOptions;
writeOptions.writeXIncludeEnable = false;
std::string xmlString = mx::writeToXmlString(doc, &writeOptions);
// Verify that the serialized document is identical.
mx::DocumentPtr writtenDoc = mx::createDocument();
mx::readFromXmlString(writtenDoc, xmlString);
REQUIRE(*writtenDoc == *doc);
mx::readFromXmlBuffer(writtenDoc, xmlString.c_str());
REQUIRE(*writtenDoc == *doc);
// Flatten all subgraphs.
doc->flattenSubgraphs();
for (mx::NodeGraphPtr graph : doc->getNodeGraphs())
{
if (graph->getActiveSourceUri() == doc->getSourceUri())
{
graph->flattenSubgraphs();
}
}
REQUIRE(doc->validate());
// Verify that all referenced types and nodes are declared.
bool referencesValid = true;
for (mx::ElementPtr elem : doc->traverseTree())
{
if (elem->getActiveSourceUri() != doc->getSourceUri())
{
continue;
}
mx::TypedElementPtr typedElem = elem->asA<mx::TypedElement>();
if (typedElem && typedElem->hasType() && !typedElem->isMultiOutputType())
{
if (!typedElem->getTypeDef())
{
WARN("[" + typedElem->getActiveSourceUri() + "] TypedElement " + typedElem->getName() + " has no matching TypeDef");
referencesValid = false;
}
}
mx::NodePtr node = elem->asA<mx::Node>();
if (node)
{
if (!node->getNodeDefString().empty() && !node->getNodeDef())
{
WARN("[" + node->getActiveSourceUri() + "] Node " + node->getName() + " has no matching NodeDef for " + node->getNodeDefString());
referencesValid = false;
}
}
}
REQUIRE(referencesValid);
}
// Read the same document twice and verify that duplicate elements
// are skipped.
mx::DocumentPtr doc = mx::createDocument();
std::string filename = "standard_surface_look_brass_tiled.mtlx";
mx::readFromXmlFile(doc, filename, searchPath);
mx::readFromXmlFile(doc, filename, searchPath);
REQUIRE(doc->validate());
// Import libraries twice and verify that duplicate elements are
// skipped.
mx::DocumentPtr libDoc = doc->copy();
for (mx::DocumentPtr lib : libs)
{
libDoc->importLibrary(lib);
libDoc->importLibrary(lib);
}
REQUIRE(libDoc->validate());
// Read document with conflicting elements.
mx::DocumentPtr conflictDoc = doc->copy();
for (mx::ElementPtr elem : conflictDoc->traverseTree())
{
if (elem->isA<mx::Node>("image"))
{
elem->setFilePrefix("differentFolder/");
}
}
mx::readFromXmlFile(conflictDoc, filename, searchPath);
REQUIRE(conflictDoc->validate());
// Reread in clean document
doc = mx::createDocument();
mx::readFromXmlFile(doc, filename, searchPath);
// Read document without XIncludes.
mx::DocumentPtr flatDoc = mx::createDocument();
mx::XmlReadOptions readOptions;
readOptions.readXIncludeFunction = nullptr;
mx::readFromXmlFile(flatDoc, filename, searchPath, &readOptions);
readOptions.readXIncludeFunction = mx::readFromXmlFile;
REQUIRE(*flatDoc != *doc);
// Read document using environment search path.
mx::setEnviron(mx::MATERIALX_SEARCH_PATH_ENV_VAR, searchPath.asString());
mx::DocumentPtr envDoc = mx::createDocument();
mx::readFromXmlFile(envDoc, filename, mx::FileSearchPath(), &readOptions);
REQUIRE(*doc == *envDoc);
mx::removeEnviron(mx::MATERIALX_SEARCH_PATH_ENV_VAR);
REQUIRE_THROWS_AS(mx::readFromXmlFile(envDoc, filename, mx::FileSearchPath(), &readOptions), mx::ExceptionFileMissing);
// Serialize to XML with a custom predicate that skips images.
auto skipImages = [](mx::ConstElementPtr elem)
{
return !elem->isA<mx::Node>("image");
};
mx::XmlWriteOptions writeOptions;
writeOptions.writeXIncludeEnable = false;
writeOptions.elementPredicate = skipImages;
std::string xmlString = mx::writeToXmlString(doc, &writeOptions);
// Reconstruct and verify that the document contains no images.
mx::DocumentPtr writtenDoc = mx::createDocument();
mx::readFromXmlString(writtenDoc, xmlString, mx::FileSearchPath(), &readOptions);
REQUIRE(*writtenDoc != *doc);
unsigned imageElementCount = 0;
for (mx::ElementPtr elem : writtenDoc->traverseTree())
{
if (elem->isA<mx::Node>("image"))
{
imageElementCount++;
}
}
REQUIRE(imageElementCount == 0);
// Serialize to XML with a custom predicate to remove XIncludes.
auto skipLibIncludes = [libs](mx::ConstElementPtr elem)
{
if (elem->hasSourceUri())
{
for (auto lib : libs)
{
if (lib->getSourceUri() == elem->getSourceUri())
{
return false;
}
}
}
return true;
};
writeOptions.writeXIncludeEnable = true;
writeOptions.elementPredicate = skipLibIncludes;
xmlString = mx::writeToXmlString(writtenDoc, &writeOptions);
// Verify that the document contains no XIncludes.
writtenDoc = mx::createDocument();
mx::readFromXmlString(writtenDoc, xmlString, mx::FileSearchPath(), &readOptions);
bool hasSourceUri = false;
for (mx::ElementPtr elem : writtenDoc->traverseTree())
{
if (elem->hasSourceUri())
{
hasSourceUri = true;
break;
}
}
REQUIRE(!hasSourceUri);
// Read a document with a relative XInclude.
mx::DocumentPtr parentDoc = mx::createDocument();
mx::readFromXmlFile(parentDoc, "resources/Materials/TestSuite/libraries/metal/brass_wire_mesh.mtlx", searchPath);
REQUIRE(parentDoc->getNodeDef("ND_TestMetal") != nullptr);
// Read a string with a relative Xinclude
parentDoc = mx::createDocument();
std::string includeTest =
"<?xml version=\"1.0\"?> "
"<materialx version=\"1.38\">"
"<xi:include href=\"standard_surface_brass_tiled.mtlx\" />"
"<xi:include href=\"standard_surface_greysphere_calibration.mtlx\" />"
"</materialx>";
mx::readFromXmlString(parentDoc, includeTest, searchPath);
REQUIRE(parentDoc->getNodeGraph("NG_brass1") != nullptr);
REQUIRE(parentDoc->getNodeGraph("NG_Greysphere_Calibration") != nullptr);
// Read a non-existent document.
mx::DocumentPtr nonExistentDoc = mx::createDocument();
REQUIRE_THROWS_AS(mx::readFromXmlFile(nonExistentDoc, "NonExistent.mtlx", mx::FileSearchPath(), &readOptions), mx::ExceptionFileMissing);
}
TEST_CASE("Comments and newlines", "[xmlio]")
{
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::FilePath testPath = searchPath.find("resources/Materials/Examples/StandardSurface/standard_surface_chess_set.mtlx");
// Read the example file into an XML string buffer.
std::string origXml = mx::readFile(testPath);
// Convert the string to a document with comments and newlines preserved.
mx::DocumentPtr doc = mx::createDocument();
mx::XmlReadOptions readOptions;
readOptions.readComments = true;
readOptions.readNewlines = true;
mx::readFromXmlString(doc, origXml, mx::FileSearchPath(), &readOptions);
// Write the document to a new XML string buffer.
std::string newXml = mx::writeToXmlString(doc);
// Verify that the XML string buffers are identical.
REQUIRE(origXml == newXml);
}
TEST_CASE("Fuzz testing", "[xmlio]")
{
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::FilePath examplesPath = searchPath.find("resources/Materials/Examples/StandardSurface");
std::mt19937 rng(0);
std::uniform_int_distribution<size_t> randChar(0, 255);
for (const mx::FilePath& filename : examplesPath.getFilesInDirectory(mx::MTLX_EXTENSION))
{
// Read the example file into an XML string buffer.
const std::string origString = mx::readFile(examplesPath / filename);
REQUIRE(origString.size() > 0);
std::uniform_int_distribution<size_t> randPos(0, origString.size() - 1);
// Iterate over test runs.
for (size_t testRun = 0; testRun < 256; testRun++)
{
std::string editString = origString;
// Iterate over string edits.
for (size_t editIndex = 0; editIndex < 32; editIndex++)
{
// Randomly alter one character in the document.
size_t charIndex = randPos(rng);
size_t newChar = randChar(rng);
editString[charIndex] = (char) newChar;
// Attempt to interpret the edited string as a document, allowing only MaterialX exceptions.
mx::DocumentPtr doc = mx::createDocument();
try
{
mx::readFromXmlString(doc, editString, searchPath);
doc->validate();
}
catch (const mx::Exception&)
{
// On a MaterialX exception, proceed to the next test run.
break;
}
}
}
}
}
TEST_CASE("Locale region testing", "[xmlio]")
{
// In the United States, the thousands separator is a comma, while in Germany it is a period.
// Thus, one thousand twenty five is displayed as 1,025 in the United States and 1.025 in Germany.
//
// In a MaterialX document, a vector3 value of "1,1.5,2.0" should be interpreted as (1.0f, 1.5f, 2.0f).
// Set the locale to Germany, if supported on this platform, and note the original locale.
std::locale origLocale;
try
{
origLocale = std::locale::global(std::locale("de_DE"));
}
catch (const std::runtime_error& e)
{
WARN("Unable to change locale " << e.what());
return;
}
// Read and validate each example document.
mx::FilePath testPath("resources/Materials/TestSuite/locale");
for (const mx::FilePath& filename : testPath.getFilesInDirectory(mx::MTLX_EXTENSION))
{
mx::DocumentPtr doc = mx::createDocument();
mx::readFromXmlFile(doc, testPath / filename);
REQUIRE(doc->validate());
for (mx::ElementPtr elem : doc->traverseTree())
{
if (elem->isA<mx::ValueElement>())
{
if (elem->hasAttribute("uiname"))
{
REQUIRE(!elem->getAttribute("uiname").empty());
}
}
}
}
// Restore the original locale.
std::locale::global(origLocale);
}