FERS 0.1.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
XmlDocument Class Reference

Class for managing XML documents. More...

#include "libxml_wrapper.h"

Public Member Functions

 XmlDocument ()
 Constructor for XmlDocument.
 
 ~XmlDocument ()=default
 
 XmlDocument (const XmlDocument &)=delete
 
 XmlDocument (XmlDocument &&) noexcept=default
 
XmlDocumentoperator= (const XmlDocument &)=delete
 
XmlDocumentoperator= (XmlDocument &&) noexcept=default
 
bool loadFile (std::string_view filename)
 Load an XML file into the document.
 
bool loadString (const std::string &content)
 Load an XML document from a string in memory.
 
bool saveFile (const std::string_view filename) const
 Save the document to a file.
 
std::string dumpToString () const
 Dumps the document to a string.
 
void setRootElement (const XmlElement &root) const
 Set the root element of the document.
 
XmlElement getRootElement () const
 Get the root element of the document.
 
bool validateWithDtd (std::span< const unsigned char > dtdData) const
 Validate the document using a DTD.
 
bool validateWithXsd (std::span< const unsigned char > xsdData) const
 Validate the document using an XSD schema.
 

Detailed Description

Class for managing XML documents.

Definition at line 340 of file libxml_wrapper.h.

Constructor & Destructor Documentation

◆ XmlDocument() [1/3]

XmlDocument::XmlDocument ( )

Constructor for XmlDocument.

Exceptions
std::runtime_errorif the document creation fails.

Definition at line 350 of file libxml_wrapper.h.

351 {
352 if (!_doc)
353 {
354 throw XmlException("Failed to create XML document.");
355 }
356 }
Exception class for handling XML-related errors.
xmlDocPtr createDocument()
math::Vec3 max

◆ ~XmlDocument()

XmlDocument::~XmlDocument ( )
default

◆ XmlDocument() [2/3]

XmlDocument::XmlDocument ( const XmlDocument )
delete

◆ XmlDocument() [3/3]

XmlDocument::XmlDocument ( XmlDocument &&  )
defaultnoexcept

Member Function Documentation

◆ dumpToString()

std::string XmlDocument::dumpToString ( ) const

Dumps the document to a string.

Returns
A string containing the XML document.

Definition at line 277 of file libxml_wrapper.cpp.

278{
279 if (!_doc)
280 {
281 LOG(logging::Level::ERROR, "Document is null; Cannot dump to string");
282 return "";
283 }
284 xmlChar* buffer = nullptr;
285 int size = 0;
286 xmlDocDumpFormatMemory(_doc.get(), &buffer, &size, 1);
287 if (buffer == nullptr)
288 {
289 LOG(logging::Level::ERROR, "Failed to dump XML document to memory buffer.");
290 return "";
291 }
292 const std::string result = xml_detail::toString(buffer, size);
294 return result;
295}
#define LOG(level,...)
Definition logging.h:19
@ ERROR
Error level for error events.
std::string toString(const xmlChar *value)

References logging::ERROR, LOG, max, and xml_detail::toString().

+ Here is the call graph for this function:

◆ getRootElement()

XmlElement XmlDocument::getRootElement ( ) const

Get the root element of the document.

Returns
The root element.
Exceptions
std::runtime_errorif the document is not loaded or the root element is missing.

Definition at line 428 of file libxml_wrapper.h.

429 {
430 if (!_doc)
431 {
432 throw std::runtime_error("Document not loaded");
433 }
435 if (root == nullptr)
436 {
437 throw std::runtime_error("Root element not found");
438 }
439 return XmlElement(root);
440 }
Class representing a node in an XML document.

References max.

◆ loadFile()

bool XmlDocument::loadFile ( std::string_view  filename)

Load an XML file into the document.

Parameters
filenameThe name of the file to load.
Returns
True if the file was successfully loaded, otherwise false.

Definition at line 247 of file libxml_wrapper.cpp.

248{
250 // Pass NOERROR and NOWARNING to prevent default terminal spam, so we handle it cleanly
251 _doc.reset(xmlReadFile(filename.data(), nullptr, XML_PARSE_NOERROR | XML_PARSE_NOWARNING));
252 if (!_doc)
253 {
254 const std::string fancyError =
255 formatError("XML Parsing Failed", getXmlLastErrorFormatted(), "Ensure the XML file is well-formed.");
257 return false;
258 }
259 return true;
260}

References logging::ERROR, LOG, and max.

◆ loadString()

bool XmlDocument::loadString ( const std::string &  content)

Load an XML document from a string in memory.

Parameters
contentThe string containing the XML document.
Returns
True if the string was successfully parsed, otherwise false.

Definition at line 262 of file libxml_wrapper.cpp.

263{
265 _doc.reset(xmlReadMemory(content.c_str(), static_cast<int>(content.length()), "in_memory.xml", nullptr,
267 if (!_doc)
268 {
269 const std::string fancyError =
270 formatError("XML Parsing Failed", getXmlLastErrorFormatted(), "Ensure the XML string is well-formed.");
272 return false;
273 }
274 return true;
275}
RealType length() const noexcept
Calculates the length (magnitude) of the vector.

References logging::ERROR, math::Vec3::length(), LOG, and max.

+ Here is the call graph for this function:

◆ operator=() [1/2]

XmlDocument & XmlDocument::operator= ( const XmlDocument )
delete

◆ operator=() [2/2]

XmlDocument & XmlDocument::operator= ( XmlDocument &&  )
defaultnoexcept

◆ saveFile()

bool XmlDocument::saveFile ( const std::string_view  filename) const

Save the document to a file.

Parameters
filenameThe name of the file to save to.
Returns
True if the file was successfully saved, otherwise false.

Definition at line 390 of file libxml_wrapper.h.

391 {
392 if (!_doc)
393 {
394 LOG(logging::Level::ERROR, "Document is null; Cannot save to file");
395 return false;
396 }
397 return xmlSaveFormatFileEnc(filename.data(), _doc.get(), "UTF-8", 1) != -1;
398 }

References logging::ERROR, LOG, and max.

◆ setRootElement()

void XmlDocument::setRootElement ( const XmlElement root) const

Set the root element of the document.

Parameters
rootThe root element to set.
Exceptions
std::runtime_errorif the document is not created.

Definition at line 413 of file libxml_wrapper.h.

414 {
415 if (!_doc)
416 {
417 throw std::runtime_error("Document not created");
418 }
419 xmlDocSetRootElement(_doc.get(), root.getNode());
420 }

References max.

◆ validateWithDtd()

bool XmlDocument::validateWithDtd ( std::span< const unsigned char dtdData) const

Validate the document using a DTD.

Parameters
dtdDataThe DTD data used for validation.
Returns
True if the document is valid according to the DTD.
Exceptions
XmlExceptionif the DTD is invalid or the validation fails.

Definition at line 123 of file libxml_wrapper.cpp.

124{
125 const std::string dtd_string = bytesToString(dtdData);
126 xmlDtdPtr dtd =
127 xmlIOParseDTD(nullptr,
131 if (dtd == nullptr)
132 {
133 throw XmlException("Failed to parse DTD from memory.");
134 }
135
136 const std::unique_ptr<xmlValidCtxt, decltype(&xmlFreeValidCtxt)> validation_ctxt(xmlNewValidCtxt(),
138 if (!validation_ctxt)
139 {
141 throw XmlException("Failed to create validation context.");
142 }
143
144 // Bind our custom error handler into the DTD Validation Context
145 std::string dtdErrors;
146 validation_ctxt->userData = &dtdErrors;
149
150 const bool is_valid = xmlValidateDtd(validation_ctxt.get(), _doc.get(), dtd) != 0;
152
153 if (!is_valid)
154 {
155 const std::string fancyError =
156 formatError("XML DTD Validation Failed", dtdErrors,
157 "Check your scenario XML tags and attributes against 'fers-xml.dtd'.");
159 throw XmlException("XML failed DTD validation.");
160 }
161
162 return true;
163}

References logging::ERROR, LOG, and max.

◆ validateWithXsd()

bool XmlDocument::validateWithXsd ( std::span< const unsigned char xsdData) const

Validate the document using an XSD schema.

Parameters
xsdDataThe XSD data used for validation.
Returns
True if the document is valid according to the XSD schema.
Exceptions
XmlExceptionif the XSD is invalid or the validation fails.

Definition at line 165 of file libxml_wrapper.cpp.

166{
167 const std::string xsd_string = bytesToString(xsdData);
168 const std::unique_ptr<xmlSchemaParserCtxt, decltype(&xmlSchemaFreeParserCtxt)> schema_parser_ctxt(
171 {
172 throw XmlException("Failed to create schema parser context.");
173 }
174
175 // Bind custom error handler into the Schema Parse Context
176 std::string xsdParseErrors;
179
180 const std::unique_ptr<xmlSchema, decltype(&xmlSchemaFree)> schema(xmlSchemaParse(schema_parser_ctxt.get()),
182 if (!schema)
183 {
184 const std::string fancyError =
185 formatError("XSD Schema Parse Failed", xsdParseErrors, "The internal XSD schema is invalid.");
187 throw XmlException("Failed to parse schema from memory.");
188 }
189
190 const std::unique_ptr<xmlSchemaValidCtxt, decltype(&xmlSchemaFreeValidCtxt)> schema_valid_ctxt(
193 {
194 throw XmlException("Failed to create schema validation context.");
195 }
196
197 // Bind custom error handler into the Schema Validation Context
198 std::string xsdErrors;
200 &xsdErrors);
201
202 if (const bool is_valid = xmlSchemaValidateDoc(schema_valid_ctxt.get(), _doc.get()) == 0; !is_valid)
203 {
204 const std::string fancyError =
205 formatError("XML XSD Validation Failed", xsdErrors,
206 "Check your scenario XML tags and attributes against 'fers-xml.xsd'.");
208 throw XmlException("XML failed XSD validation.");
209 }
210
211 return true;
212}
@ FATAL
Fatal level for severe error events.

References logging::ERROR, logging::FATAL, LOG, and max.


The documentation for this class was generated from the following files: