FERS 1.0.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 204 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 214 of file libxml_wrapper.h.

214 : _doc(xmlNewDoc(reinterpret_cast<const xmlChar*>("1.0")), &xmlFreeDoc)
215 {
216 if (!_doc)
217 {
218 throw XmlException("Failed to create XML document.");
219 }
220 }
Exception class for handling XML-related errors.

◆ ~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 128 of file libxml_wrapper.cpp.

129{
130 if (!_doc)
131 {
132 LOG(logging::Level::ERROR, "Document is null; Cannot dump to string");
133 return "";
134 }
135 xmlChar* buffer = nullptr;
136 int size = 0;
137 xmlDocDumpFormatMemory(_doc.get(), &buffer, &size, 1);
138 if (!buffer)
139 {
140 LOG(logging::Level::ERROR, "Failed to dump XML document to memory buffer.");
141 return "";
142 }
143 const std::string result(reinterpret_cast<const char*>(buffer), static_cast<size_t>(size));
144 xmlFree(buffer);
145 return result;
146}
#define LOG(level,...)
Definition logging.h:19
@ ERROR
Error level for error events.

References logging::ERROR, and LOG.

Referenced by serial::world_to_xml_string().

+ Here is the caller 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 292 of file libxml_wrapper.h.

293 {
294 if (!_doc)
295 {
296 throw std::runtime_error("Document not loaded");
297 }
298 const xmlNode* root = xmlDocGetRootElement(_doc.get());
299 if (!root)
300 {
301 throw std::runtime_error("Root element not found");
302 }
303 return XmlElement(root);
304 }
Class representing a node in an XML document.

Referenced by radar::FileTarget::FileTarget(), mergeXmlDocuments(), and removeIncludeElements().

+ Here is the caller graph for this function:

◆ 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 116 of file libxml_wrapper.cpp.

117{
118 _doc.reset(xmlReadFile(filename.data(), nullptr, 0));
119 return _doc != nullptr;
120}

Referenced by radar::FileTarget::FileTarget(), and serial::parseSimulation().

+ Here is the caller graph for this function:

◆ 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 122 of file libxml_wrapper.cpp.

123{
124 _doc.reset(xmlReadMemory(content.c_str(), static_cast<int>(content.length()), "in_memory.xml", nullptr, 0));
125 return _doc != nullptr;
126}

Referenced by serial::parseSimulationFromString().

+ Here is the caller 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 254 of file libxml_wrapper.h.

255 {
256 if (!_doc)
257 {
258 LOG(logging::Level::ERROR, "Document is null; Cannot save to file");
259 return false;
260 }
261 return xmlSaveFormatFileEnc(filename.data(), _doc.get(), "UTF-8", 1) != -1;
262 }

References logging::ERROR, and LOG.

◆ 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 277 of file libxml_wrapper.h.

278 {
279 if (!_doc)
280 {
281 throw std::runtime_error("Document not created");
282 }
283 xmlDocSetRootElement(_doc.get(), root.getNode());
284 }
xmlNodePtr getNode() const noexcept
Get the underlying XML node pointer.

References XmlElement::getNode().

Referenced by serial::world_to_xml_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 20 of file libxml_wrapper.cpp.

21{
22 xmlDtdPtr dtd =
23 xmlIOParseDTD(nullptr,
24 xmlParserInputBufferCreateMem(reinterpret_cast<const char*>(dtdData.data()),
25 static_cast<int>(dtdData.size()), XML_CHAR_ENCODING_UTF8),
26 XML_CHAR_ENCODING_UTF8);
27 if (!dtd)
28 {
29 throw XmlException("Failed to parse DTD from memory.");
30 }
31
32 const std::unique_ptr<xmlValidCtxt, decltype(&xmlFreeValidCtxt)> validation_ctxt(xmlNewValidCtxt(),
33 xmlFreeValidCtxt);
34 if (!validation_ctxt)
35 {
36 xmlFreeDtd(dtd);
37 throw XmlException("Failed to create validation context.");
38 }
39
40 const bool is_valid = xmlValidateDtd(validation_ctxt.get(), _doc.get(), dtd);
41 xmlFreeDtd(dtd);
42
43 if (!is_valid)
44 {
45 throw XmlException("XML failed DTD validation.");
46 }
47
48 return true;
49}

◆ 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 51 of file libxml_wrapper.cpp.

52{
53 const std::unique_ptr<xmlSchemaParserCtxt, decltype(&xmlSchemaFreeParserCtxt)> schema_parser_ctxt(
54 xmlSchemaNewMemParserCtxt(reinterpret_cast<const char*>(xsdData.data()), static_cast<int>(xsdData.size())),
55 xmlSchemaFreeParserCtxt);
56 if (!schema_parser_ctxt)
57 {
58 throw XmlException("Failed to create schema parser context.");
59 }
60
61 const std::unique_ptr<xmlSchema, decltype(&xmlSchemaFree)> schema(xmlSchemaParse(schema_parser_ctxt.get()),
62 xmlSchemaFree);
63 if (!schema)
64 {
65 throw XmlException("Failed to parse schema from memory.");
66 }
67
68 const std::unique_ptr<xmlSchemaValidCtxt, decltype(&xmlSchemaFreeValidCtxt)> schema_valid_ctxt(
69 xmlSchemaNewValidCtxt(schema.get()), xmlSchemaFreeValidCtxt);
70 if (!schema_valid_ctxt)
71 {
72 throw XmlException("Failed to create schema validation context.");
73 }
74
75 if (const bool is_valid = xmlSchemaValidateDoc(schema_valid_ctxt.get(), _doc.get()) == 0; !is_valid)
76 {
77 throw XmlException("XML failed XSD validation.");
78 }
79
80 return true;
81}

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