FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
XmlElement Class Reference

Class representing a node in an XML document. More...

#include "libxml_wrapper.h"

Public Member Functions

 XmlElement (const xmlNode *node)
 Constructor for XmlElement.
 
 XmlElement (const XmlElement &)=default
 
 XmlElement (XmlElement &&) noexcept=default
 
XmlElementoperator= (const XmlElement &)=default
 
XmlElementoperator= (XmlElement &&) noexcept=default
 
 ~XmlElement ()=default
 
std::string_view name () const noexcept
 Get the name of the XML element.
 
std::string getText () const
 Get the text content of the XML element.
 
void setText (const std::string_view text) const
 Set the text content of the XML element.
 
void setAttribute (const std::string_view name, const std::string_view value) const
 Set an attribute on the XML element.
 
XmlElement addChild (const std::string_view name) const noexcept
 Add a child element to the current node.
 
XmlElement childElement (const std::string_view name="", const unsigned index=0) const noexcept
 Retrieve a child element by name and index.
 
bool isValid () const noexcept
 Check if the XML element is valid.
 
xmlNodePtr getNode () const noexcept
 Get the underlying XML node pointer.
 

Static Public Member Functions

static std::string getSafeAttribute (const XmlElement &element, const std::string_view name)
 Get the value of an attribute safely.
 
static std::optional< std::string > getOptionalAttribute (const XmlElement &element, const std::string_view name)
 Get the value of an optional attribute.
 

Detailed Description

Class representing a node in an XML document.

This class encapsulates an XML element, allowing users to access and manipulate element names, attributes, content, and children. It uses libxml2 for all operations and provides simplified methods to interact with XML nodes.

Definition at line 54 of file libxml_wrapper.h.

Constructor & Destructor Documentation

◆ XmlElement() [1/3]

XmlElement::XmlElement ( const xmlNode *  node)
explicit

Constructor for XmlElement.

Parameters
nodeThe xmlNode pointer representing the XML element.

Definition at line 64 of file libxml_wrapper.h.

64: _node(const_cast<xmlNode*>(node)) {}

◆ XmlElement() [2/3]

XmlElement::XmlElement ( const XmlElement )
default

◆ XmlElement() [3/3]

XmlElement::XmlElement ( XmlElement &&  )
defaultnoexcept

◆ ~XmlElement()

XmlElement::~XmlElement ( )
default

Member Function Documentation

◆ addChild()

XmlElement XmlElement::addChild ( const std::string_view  name) const
noexcept

Add a child element to the current node.

Parameters
nameThe name of the new child element.
Returns
The newly created XmlElement.

Definition at line 173 of file libxml_wrapper.h.

174 {
175 const xmlNode* child = xmlNewNode(nullptr, reinterpret_cast<const xmlChar*>(name.data()));
176 xmlAddChild(_node, const_cast<xmlNode*>(child));
177 return XmlElement(child);
178 }
Class representing a node in an XML document.
std::string_view name() const noexcept
Get the name of the XML element.

References name().

Referenced by serial::xml_serializer_utils::addChildWithText(), serial::xml_serializer_utils::serializeMonostatic(), serial::xml_serializer_utils::serializeMotionPath(), serial::xml_serializer_utils::serializeParameters(), serial::xml_serializer_utils::serializePlatform(), serial::xml_serializer_utils::serializeReceiver(), serial::xml_serializer_utils::serializeRotation(), serial::xml_serializer_utils::serializeSchedule(), serial::xml_serializer_utils::serializeTarget(), serial::xml_serializer_utils::serializeTiming(), serial::xml_serializer_utils::serializeTransmitter(), serial::xml_serializer_utils::serializeWaveform(), and serial::world_to_xml_string().

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

◆ childElement()

XmlElement XmlElement::childElement ( const std::string_view  name = "",
const unsigned  index = 0 
) const
noexcept

Retrieve a child element by name and index.

Parameters
nameThe name of the child element (optional).
indexThe index of the child to retrieve.
Returns
The child element or an invalid XmlElement if not found.

Definition at line 187 of file libxml_wrapper.h.

188 {
189 if (_node == nullptr)
190 {
191 return XmlElement(nullptr);
192 }
193 unsigned count = 0;
194 for (auto* child = _node->children; child != nullptr; child = child->next)
195 {
196 if (child->type == XML_ELEMENT_NODE && (name.empty() || name == reinterpret_cast<const char*>(child->name)))
197 {
198 if (count == index)
199 {
200 return XmlElement(child);
201 }
202 ++count;
203 }
204 }
205 return XmlElement(nullptr);
206 }

References name().

Referenced by serial::xml_parser_utils::collectIncludeElements(), radar::FileTarget::FileTarget(), serial::xml_parser_utils::get_child_real_type(), serial::xml_parser_utils::parseMotionPath(), serial::xml_parser_utils::parseParameters(), serial::xml_parser_utils::parsePlatform(), serial::xml_parser_utils::parsePlatformElements(), serial::xml_parser_utils::parseReceiver(), serial::xml_parser_utils::parseRotationPath(), serial::xml_parser_utils::parseSchedule(), serial::xml_parser_utils::parseTarget(), serial::xml_parser_utils::parseTransmitter(), serial::xml_parser_utils::parseWaveform(), serial::xml_parser_utils::processParsedDocument(), and removeIncludeElements().

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

◆ getNode()

xmlNodePtr XmlElement::getNode ( ) const
noexcept

Get the underlying XML node pointer.

Returns
The underlying XML node pointer.

Definition at line 220 of file libxml_wrapper.h.

220{ return _node; }

Referenced by getOptionalAttribute(), getSafeAttribute(), mergeXmlDocuments(), and XmlDocument::setRootElement().

+ Here is the caller graph for this function:

◆ getOptionalAttribute()

static std::optional< std::string > XmlElement::getOptionalAttribute ( const XmlElement element,
const std::string_view  name 
)
static

Get the value of an optional attribute.

Parameters
elementThe XmlElement to retrieve the attribute from.
nameThe name of the attribute.
Returns
The attribute value if present.

Definition at line 140 of file libxml_wrapper.h.

141 {
142 if (!element.isValid())
143 {
144 return std::nullopt;
145 }
146 if (xmlChar* attr = xmlGetProp(element.getNode(), reinterpret_cast<const xmlChar*>(name.data())))
147 {
148 std::string value = reinterpret_cast<const char*>(attr);
149 xmlFree(attr);
150 return value;
151 }
152 return std::nullopt;
153 }
bool isValid() const noexcept
Check if the XML element is valid.
xmlNodePtr getNode() const noexcept
Get the underlying XML node pointer.

References getNode(), isValid(), and name().

+ Here is the call graph for this function:

◆ getSafeAttribute()

static std::string XmlElement::getSafeAttribute ( const XmlElement element,
const std::string_view  name 
)
static

Get the value of an attribute safely.

Parameters
elementThe XmlElement to retrieve the attribute from.
nameThe name of the attribute.
Returns
The value of the attribute.
Exceptions
XmlExceptionif the attribute is not found.

Definition at line 118 of file libxml_wrapper.h.

119 {
120 std::string value;
121 if (xmlChar* attr = xmlGetProp(element.getNode(), reinterpret_cast<const xmlChar*>(name.data())))
122 {
123 value = reinterpret_cast<const char*>(attr);
124 xmlFree(attr);
125 }
126 else
127 {
128 throw XmlException("Attribute not found: " + std::string(name));
129 }
130 return value;
131 }
Exception class for handling XML-related errors.

References getNode(), and name().

Referenced by serial::xml_parser_utils::get_attribute_bool(), serial::xml_parser_utils::parseAntenna(), serial::xml_parser_utils::parseMotionPath(), serial::xml_parser_utils::parseParameters(), serial::xml_parser_utils::parsePlatform(), serial::xml_parser_utils::parseReceiver(), serial::xml_parser_utils::parseRotationPath(), serial::xml_parser_utils::parseSchedule(), serial::xml_parser_utils::parseTarget(), serial::xml_parser_utils::parseTiming(), serial::xml_parser_utils::parseTransmitter(), serial::xml_parser_utils::parseWaveform(), serial::xml_parser_utils::processParsedDocument(), and serial::xml_parser_utils::resolve_reference_id().

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

◆ getText()

std::string XmlElement::getText ( ) const

Get the text content of the XML element.

Returns
The text content as a string.

Definition at line 88 of file libxml_wrapper.h.

89 {
90 if (_node == nullptr)
91 {
92 return "";
93 }
94 xmlChar* text = xmlNodeGetContent(_node);
95 std::string result = reinterpret_cast<const char*>(text);
96 xmlFree(text);
97 return result;
98 }

Referenced by serial::xml_parser_utils::collectIncludeElements(), serial::xml_parser_utils::get_child_real_type(), and serial::xml_parser_utils::parseParameters().

+ Here is the caller graph for this function:

◆ isValid()

◆ name()

std::string_view XmlElement::name ( ) const
noexcept

Get the name of the XML element.

Returns
The name of the XML element.

Definition at line 81 of file libxml_wrapper.h.

81{ return reinterpret_cast<const char*>(_node->name); }

Referenced by addChild(), childElement(), getOptionalAttribute(), getSafeAttribute(), serial::xml_parser_utils::processParsedDocument(), and setAttribute().

+ Here is the caller graph for this function:

◆ operator=() [1/2]

XmlElement & XmlElement::operator= ( const XmlElement )
default

◆ operator=() [2/2]

XmlElement & XmlElement::operator= ( XmlElement &&  )
defaultnoexcept

◆ setAttribute()

void XmlElement::setAttribute ( const std::string_view  name,
const std::string_view  value 
) const

Set an attribute on the XML element.

Parameters
nameThe name of the attribute.
valueThe value to set for the attribute.

Definition at line 161 of file libxml_wrapper.h.

162 {
163 xmlSetProp(_node, reinterpret_cast<const xmlChar*>(name.data()),
164 reinterpret_cast<const xmlChar*>(value.data()));
165 }

References name().

Referenced by serial::xml_serializer_utils::serializeAntenna(), serial::xml_serializer_utils::serializeMonostatic(), serial::xml_serializer_utils::serializeMotionPath(), serial::xml_serializer_utils::serializeParameters(), serial::xml_serializer_utils::serializePlatform(), serial::xml_serializer_utils::serializeReceiver(), serial::xml_serializer_utils::serializeRotation(), serial::xml_serializer_utils::serializeSchedule(), serial::xml_serializer_utils::serializeTarget(), serial::xml_serializer_utils::serializeTiming(), serial::xml_serializer_utils::serializeTransmitter(), serial::xml_serializer_utils::serializeWaveform(), serial::xml_serializer_utils::setAttributeFromBool(), and serial::world_to_xml_string().

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

◆ setText()

void XmlElement::setText ( const std::string_view  text) const

Set the text content of the XML element.

Parameters
textThe text to set as the content of the node.

Definition at line 105 of file libxml_wrapper.h.

106 {
107 xmlNodeSetContent(_node, reinterpret_cast<const xmlChar*>(text.data()));
108 }

Referenced by serial::xml_serializer_utils::addChildWithText().

+ Here is the caller graph for this function:

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