/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2002-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file XMLSubSys.h14/// @author Daniel Krajzewicz15/// @author Michael Behrisch16/// @date Mon, 1 Jul 200217///18// Utility methods for initialising, closing and using the XML-subsystem19/****************************************************************************/20#pragma once21#include <config.h>2223#include <string>24#include <vector>25#include <xercesc/sax2/SAX2XMLReader.hpp>262728// ===========================================================================29// class declarations30// ===========================================================================31class GenericSAXHandler;32class SUMOSAXHandler;33class SUMOSAXReader;343536// ===========================================================================37// class definitions38// ===========================================================================39/**40* @class XMLSubSys41* @brief Utility methods for initialising, closing and using the XML-subsystem42*43* The Xerces-parsers need an initialisation and should also be closed.44*45* As we use xerces for both the input files and the configuration we46* would have to check whether the system was initialised before. Instead,47* we call XMLSubSys::init(bool) once at the beginning of our application and48* XMLSubSys::close() at the end.49*50* Closing and initialising the XML subsystem is necessary. Still, we never51* encountered any problems with it. Once, after some modifications, SUMO52* crashed when closing the XML sub system. The reason was a memory leak53* within the microsim-module. On initialisation, a SAX2XMLReader is built54* which can be used during later process. It is destroyed when the subsystem55* is closed.56*57* In addition to initialisation and shutdown, this module allows to build58* SAXReaders and/or running a given handler on a given file without59* dealing with the reader at all.60*/61class XMLSubSys {62public:63/**64* @brief Initialises the xml-subsystem.65*66* Calls XMLPlatformUtils::Initialize(). If this fails, the exception is67* caught and its content is reported using a ProcessError.68*69* @exception ProcessError If the initialisation fails70*/71static void init();727374/**75* @brief Enables or disables validation.76*77* The setting is only valid for parsers created after the call. Existing parsers are not adapted.78*79* @param[in] validationScheme Whether validation of XML-documents against schemata shall be enabled80* @param[in] netValidationScheme Whether validation of SUMO networks against schemata shall be enabled81*/82static void setValidation(const std::string& validationScheme, const std::string& netValidationScheme, const std::string& routeValidationScheme);838485/**86* @brief Closes the xml-subsystem87*88* Deletes the built reader and calls XMLPlatformUtils::Terminate();89*/90static void close();919293/**94* @brief Builds a reader and assigns the handler to it95*96* Tries to build a SAX2XMLReader using "getSAXReader()". If this97* fails, 0 is returned. Otherwise, the given handler is assigned98* to the reader as the current DefaultHandler and ErrorHandler.99*100* @param[in] handler The handler to assign to the built reader101* @param[in] isNet whether a network gets loaded102* @param[in] isRoute whether routes get loaded103* @return The built Xerces-SAX-reader, 0 if something failed104* @see getSAXReader()105*/106static SUMOSAXReader* getSAXReader(SUMOSAXHandler& handler,107const bool isNet = false, const bool isRoute = false);108109110/**111* @brief Sets the given handler for the default reader112*113* Uses the reader built on init() which is stored in myReader.114*115* @param[in] handler The handler to assign to the built reader116*/117static void setHandler(GenericSAXHandler& handler);118119120/**121* @brief Runs the given handler on the given file; returns if everything's ok122*123* Uses the reader built on init() which is stored in myReader to parse the given124* file.125*126* All exceptions are catched and reported to the error-instance of the MsgHandler.127* Also, if the reader could not be built, this is reported.128*129* The method returns true if everything went ok. This means, that the reader could be130* built, no exception was caught, and nothing was reported to the error-instance131* of the MsgHandler.132*133* @param[in] handler The handler to assign to the built reader134* @param[in] file The file to run the parser at135* @param[in] isNet whether a network gets loaded136* @param[in] isRoute whether routes get loaded137* @param[in] isExternal whether it is an external file like matsim or opendrive138* @param[in] catchExceptions whether exceptions on parsing should be caught or transferred into a ProcessError139* @return true if the parsing was done without errors, false otherwise (error was printed)140*/141static bool runParser(GenericSAXHandler& handler, const std::string& file,142const bool isNet = false, const bool isRoute = false,143const bool isExternal = false, const bool catchExceptions = true);144145146private:147static std::string warnLocalScheme(const std::string& newScheme, const bool haveSUMO_HOME);148149private:150/// @brief The XML Readers used for repeated parsing151static std::vector<SUMOSAXReader*> myReaders;152153/// @brief Information whether the reader is parsing154static int myNextFreeReader;155156/// @brief Information whether built reader/parser shall validate XML-documents against schemata157static std::string myValidationScheme;158159/// @brief Information whether built reader/parser shall validate SUMO networks against schemata160static std::string myNetValidationScheme;161162/// @brief Information whether built reader/parser shall validate SUMO routes against schemata163static std::string myRouteValidationScheme;164165/// @brief Schema cache to be used for grammars which are not declared166static XERCES_CPP_NAMESPACE::XMLGrammarPool* myGrammarPool;167168/// @brief Whether a warning about missing SUMO_HOME should be emitted169static bool myNeedsValidationWarning;170171};172173174