22#include "libxml/encoding.h"
23#include "libxml/parser.h"
24#include "libxml/valid.h"
25#include "libxml/xmlIO.h"
26#include "libxml/xmlerror.h"
27#include "libxml/xmlschemas.h"
34 auto*
err_str =
static_cast<std::string*
>(
ctx);
40 std::array<char, 1024>
buf{};
54 if (
size >
static_cast<std::size_t
>(std::numeric_limits<int>::max()))
56 throw XmlException(
"XML input exceeds libxml2 length limit.");
58 return static_cast<int>(
size);
64 result.reserve(bytes.size());
65 for (
const unsigned char byte : bytes)
67 result.push_back(
static_cast<char>(
byte));
78 while (!
errors.empty() && (std::isspace(
static_cast<unsigned char>(
errors.back())) != 0))
85 while ((pos =
errors.find(
'\n', pos)) != std::string::npos)
87 errors.replace(pos, 1,
"\n│ ");
91 return std::format(
"\n"
98 "└────────────────────────────────────────────────────────────────────────────",
106 if ((
err !=
nullptr) && (
err->message !=
nullptr))
108 std::string
msg =
err->message;
109 while (!
msg.empty() && (std::isspace(
static_cast<unsigned char>(
msg.back())) != 0))
115 return std::format(
"Line {}: {}",
err->line,
msg);
119 return "Syntax error or malformed XML.";
141 throw XmlException(
"Failed to create validation context.");
157 "Check your scenario XML tags and attributes against 'fers-xml.dtd'.");
172 throw XmlException(
"Failed to create schema parser context.");
187 throw XmlException(
"Failed to parse schema from memory.");
194 throw XmlException(
"Failed to create schema validation context.");
206 "Check your scenario XML tags and attributes against 'fers-xml.xsd'.");
Class for managing XML documents.
bool loadFile(std::string_view filename)
Load an XML file into 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.
bool loadString(const std::string &content)
Load an XML document from a string in memory.
std::string dumpToString() const
Dumps the document to a string.
Class representing a node in an XML document.
Exception class for handling XML-related errors.
RealType length() const noexcept
Calculates the length (magnitude) of the vector.
void mergeXmlDocuments(const XmlDocument &mainDoc, const XmlDocument &includedDoc)
Merge two XML documents.
void removeIncludeElements(const XmlDocument &doc)
Remove "include" elements from the XML document.
Wrapper for managing XML documents and elements using libxml2.
@ FATAL
Fatal level for severe error events.
@ ERROR
Error level for error events.
std::string toString(const xmlChar *value)