FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
xml_parser.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright (c) 2006-2008 Marc Brooker and Michael Inggs
4// Copyright (c) 2008-present FERS Contributors (see AUTHORS.md).
5//
6// See the GNU GPLv2 LICENSE file in the FERS project root for more information.
7
8/**
9 * @file xml_parser.cpp
10 * @brief Implementation file for parsing XML configuration files for simulation.
11 */
12
13#include "xml_parser.h"
14
15#include <filesystem>
16
17#include "core/logging.h"
18#include "core/parameters.h"
19#include "core/world.h"
20#include "libxml_wrapper.h"
21#include "xml_parser_utils.h"
22
23namespace serial
24{
25 void parseSimulation(const std::string& filename, core::World* world, const bool validate,
26 std::mt19937& masterSeeder)
27 {
28 world->clear();
30
31 XmlDocument main_doc;
32 if (!main_doc.loadFile(filename))
33 {
34 throw XmlException("Failed to load main XML file: " + filename);
35 }
36
37 const std::filesystem::path main_dir = std::filesystem::path(filename).parent_path();
38 const bool did_combine = xml_parser_utils::addIncludeFilesToMainDocument(main_doc, main_dir);
39
40 if (validate)
41 {
42 xml_parser_utils::validateXml(did_combine, main_doc);
43 }
44 else
45 {
46 LOG(logging::Level::DEBUG, "Skipping XML validation.");
47 }
48
50 ctx.world = world;
51 ctx.master_seeder = &masterSeeder;
52 ctx.base_dir = main_dir;
54
56
57 // Push the isolated context parameters into global application parameters
59 }
60
61 void parseSimulationFromString(const std::string& xmlContent, core::World* world, const bool validate,
62 std::mt19937& masterSeeder)
63 {
64 world->clear();
66
67 XmlDocument doc;
68 if (!doc.loadString(xmlContent))
69 {
70 throw XmlException("Failed to parse XML from memory string.");
71 }
72
73 if (validate)
74 {
75 // Note: <include> tags are not processed when loading from a string.
77 }
78 else
79 {
80 LOG(logging::Level::DEBUG, "Skipping XML validation.");
81 }
82
83 // When loading from a string, there's no base directory for relative asset paths.
84 // The UI/caller is responsible for ensuring any paths in the XML are absolute or resolvable.
85 const std::filesystem::path base_dir = ".";
86
88 ctx.world = world;
89 ctx.master_seeder = &masterSeeder;
90 ctx.base_dir = base_dir;
92
94
95 // Push the isolated context parameters into global application parameters
97 }
98}
Class for managing XML documents.
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.
Exception class for handling XML-related errors.
The World class manages the simulator environment.
Definition world.h:39
void clear() noexcept
Clears all objects and assets from the simulation world.
Definition world.cpp:243
Wrapper for managing XML documents and elements using libxml2.
Header file for the logging system.
#define LOG(level,...)
Definition logging.h:19
@ DEBUG
Debug level for general debugging information.
Parameters params
Definition parameters.h:85
bool addIncludeFilesToMainDocument(const XmlDocument &mainDoc, const fs::path &currentDir)
void processParsedDocument(const XmlDocument &doc, ParserContext &ctx)
Coordinates the full parsing of a validated XML document tree.
void validateXml(const bool didCombine, const XmlDocument &mainDoc)
Validates an XML document against the embedded DTD and XSD schemas.
AssetLoaders createDefaultAssetLoaders()
Creates an AssetLoaders struct populated with standard file-I/O implementations.
void parseSimulation(const std::string &filename, core::World *world, const bool validate, std::mt19937 &masterSeeder)
Parses a simulation configuration from an XML file.
void parseSimulationFromString(const std::string &xmlContent, core::World *world, const bool validate, std::mt19937 &masterSeeder)
Parses a simulation configuration directly from an XML string in memory.
Defines the Parameters struct and provides methods for managing simulation parameters.
void reset() noexcept
Resets the parameters to their default-constructed state.
Definition parameters.h:82
Encapsulates the state required during the XML parsing process.
core::World * world
Pointer to the World where parsed objects are inserted.
std::mt19937 * master_seeder
RNG used to generate independent seeds for simulated objects.
std::filesystem::path base_dir
The directory of the main XML file (used to resolve relative asset paths).
params::Parameters parameters
An isolated copy of the simulation parameters being built.
AssetLoaders loaders
The injected asset loaders for external files.
Header file for the World class in the simulator.
High-level facade for parsing XML configuration files into the FERS simulation environment.
Core utility layer for parsing FERS XML scenario files.