FERS 0.1.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 (xmlNodePtr node)
 Constructor for XmlElement.
 
 XmlElement (const XmlElement &)=default
 
 XmlElement (XmlElement &&) noexcept=default
 
XmlElementoperator= (const XmlElement &)=default
 
XmlElementoperator= (XmlElement &&) noexcept=default
 
 ~XmlElement ()=default
 
std::string name () const
 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
 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 XmlElement create (const std::string_view name)
 Create a new XML element by name.
 
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 152 of file libxml_wrapper.h.

Constructor & Destructor Documentation

◆ XmlElement() [1/3]

XmlElement::XmlElement ( xmlNodePtr  node)
explicit

Constructor for XmlElement.

Parameters
nodeThe xmlNode pointer representing the XML element.

Definition at line 162 of file libxml_wrapper.h.

162: _node(node) {}
math::Vec3 max

◆ 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

Add a child element to the current node.

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

Definition at line 286 of file libxml_wrapper.h.

287 {
289 xmlAddChild(_node, child);
290 return XmlElement(child);
291 }
Class representing a node in an XML document.
std::string name() const
Get the name of the XML element.
xmlNodePtr createNode(const std::string_view name)

References xml_detail::createNode(), max, and name().

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

301 {
302 if (_node == nullptr)
303 {
304 return XmlElement(nullptr);
305 }
306 unsigned count = 0;
307 for (auto* child = _node->children; child != nullptr; child = child->next)
308 {
309 if (child->type == XML_ELEMENT_NODE && (name.empty() || xml_detail::equals(child->name, name)))
310 {
311 if (count == index)
312 {
313 return XmlElement(child);
314 }
315 ++count;
316 }
317 }
318 return XmlElement(nullptr);
319 }
bool equals(const xmlChar *xml_value, const std::string_view value)

References xml_detail::equals(), max, and name().

Referenced by serial::xml_parser_utils::parseCoordinateSystemParameter(), serial::xml_parser_utils::parseOptionalNumericParameters(), serial::xml_parser_utils::parseOriginParameter(), serial::xml_parser_utils::parseRotationAngleUnit(), serial::xml_parser_utils::parseWaveform(), serial::xml_parser_utils::setOptionalRealParameter(), and serial::xml_parser_utils::setOptionalUnsignedParameter().

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

◆ create()

static XmlElement XmlElement::create ( const std::string_view  name)
static

Create a new XML element by name.

Parameters
nameThe name of the XML element.
Returns
The newly created XML element.

Definition at line 187 of file libxml_wrapper.h.

188 {
190 }

References xml_detail::createNode(), and name().

Referenced by serial::world_to_xml_string().

+ 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 333 of file libxml_wrapper.h.

333{ return _node; }

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

252 {
253 if (!element.isValid())
254 {
255 return std::nullopt;
256 }
258 if (xmlChar* attr = xmlGetProp(element.getNode(), xml_name.c_str()))
259 {
260 std::string value = xml_detail::toString(attr);
261 xmlFree(attr);
262 return value;
263 }
264 return std::nullopt;
265 }
std::string toString(const xmlChar *value)

References max, name(), and xml_detail::toString().

Referenced by serial::xml_parser_utils::get_attribute_bool(), serial::xml_parser_utils::parseMotionPath(), and serial::xml_parser_utils::parseOriginParameter().

+ Here is the call graph for this function:
+ Here is the caller 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 228 of file libxml_wrapper.h.

229 {
230 std::string value;
232 if (xmlChar* attr = xmlGetProp(element.getNode(), xml_name.c_str()))
233 {
234 value = xml_detail::toString(attr);
235 xmlFree(attr);
236 }
237 else
238 {
239 throw XmlException("Attribute not found: " + std::string(name));
240 }
241 return value;
242 }
Exception class for handling XML-related errors.

References max, name(), and xml_detail::toString().

Referenced by serial::xml_parser_utils::parseAntenna(), serial::xml_parser_utils::parseCoordinateSystemParameter(), serial::xml_parser_utils::parseMonostatic(), serial::xml_parser_utils::parseOriginParameter(), serial::xml_parser_utils::parsePlatform(), serial::xml_parser_utils::parseReceiver(), serial::xml_parser_utils::parseReceiverWithMode(), 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::parseTransmitterWithMode(), serial::xml_parser_utils::parseUtmCoordinateSystem(), 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 197 of file libxml_wrapper.h.

198 {
199 if (_node == nullptr)
200 {
201 return "";
202 }
204 std::string result = xml_detail::toString(text);
205 xmlFree(text);
206 return result;
207 }

References max, and xml_detail::toString().

Referenced by serial::xml_parser_utils::parseRotationAngleUnit().

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

◆ isValid()

bool XmlElement::isValid ( ) const
noexcept

Check if the XML element is valid.

Returns
True if the node is valid, otherwise false.

Definition at line 326 of file libxml_wrapper.h.

326{ return _node != nullptr; }

Referenced by serial::xml_parser_utils::parseOptionalNumericParameters(), serial::xml_parser_utils::parseWaveform(), serial::xml_parser_utils::setOptionalRealParameter(), and serial::xml_parser_utils::setOptionalUnsignedParameter().

+ Here is the caller graph for this function:

◆ name()

std::string XmlElement::name ( ) const

Get the name of the XML element.

Returns
The name of the XML element.

Definition at line 179 of file libxml_wrapper.h.

179{ return _node == nullptr ? "" : xml_detail::toString(_node->name); }

References xml_detail::toString().

Referenced by addChild(), childElement(), create(), getOptionalAttribute(), getSafeAttribute(), and setAttribute().

+ Here is the call graph for this function:
+ 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 273 of file libxml_wrapper.h.

274 {
277 xmlSetProp(_node, xml_name.c_str(), xml_value.c_str());
278 }

References max, and name().

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

+ 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 214 of file libxml_wrapper.h.

215 {
217 xmlNodeSetContentLen(_node, xml_text.c_str(), xml_text.contentLength());
218 }

References max.


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