Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/xml/XMLSubSys.h
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2002-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file XMLSubSys.h
15
/// @author Daniel Krajzewicz
16
/// @author Michael Behrisch
17
/// @date Mon, 1 Jul 2002
18
///
19
// Utility methods for initialising, closing and using the XML-subsystem
20
/****************************************************************************/
21
#pragma once
22
#include <config.h>
23
24
#include <string>
25
#include <vector>
26
#include <xercesc/sax2/SAX2XMLReader.hpp>
27
28
29
// ===========================================================================
30
// class declarations
31
// ===========================================================================
32
class GenericSAXHandler;
33
class SUMOSAXHandler;
34
class SUMOSAXReader;
35
36
37
// ===========================================================================
38
// class definitions
39
// ===========================================================================
40
/**
41
* @class XMLSubSys
42
* @brief Utility methods for initialising, closing and using the XML-subsystem
43
*
44
* The Xerces-parsers need an initialisation and should also be closed.
45
*
46
* As we use xerces for both the input files and the configuration we
47
* would have to check whether the system was initialised before. Instead,
48
* we call XMLSubSys::init(bool) once at the beginning of our application and
49
* XMLSubSys::close() at the end.
50
*
51
* Closing and initialising the XML subsystem is necessary. Still, we never
52
* encountered any problems with it. Once, after some modifications, SUMO
53
* crashed when closing the XML sub system. The reason was a memory leak
54
* within the microsim-module. On initialisation, a SAX2XMLReader is built
55
* which can be used during later process. It is destroyed when the subsystem
56
* is closed.
57
*
58
* In addition to initialisation and shutdown, this module allows to build
59
* SAXReaders and/or running a given handler on a given file without
60
* dealing with the reader at all.
61
*/
62
class XMLSubSys {
63
public:
64
/**
65
* @brief Initialises the xml-subsystem.
66
*
67
* Calls XMLPlatformUtils::Initialize(). If this fails, the exception is
68
* caught and its content is reported using a ProcessError.
69
*
70
* @exception ProcessError If the initialisation fails
71
*/
72
static void init();
73
74
75
/**
76
* @brief Enables or disables validation.
77
*
78
* The setting is only valid for parsers created after the call. Existing parsers are not adapted.
79
*
80
* @param[in] validationScheme Whether validation of XML-documents against schemata shall be enabled
81
* @param[in] netValidationScheme Whether validation of SUMO networks against schemata shall be enabled
82
*/
83
static void setValidation(const std::string& validationScheme, const std::string& netValidationScheme, const std::string& routeValidationScheme);
84
85
86
/**
87
* @brief Closes the xml-subsystem
88
*
89
* Deletes the built reader and calls XMLPlatformUtils::Terminate();
90
*/
91
static void close();
92
93
94
/**
95
* @brief Builds a reader and assigns the handler to it
96
*
97
* Tries to build a SAX2XMLReader using "getSAXReader()". If this
98
* fails, 0 is returned. Otherwise, the given handler is assigned
99
* to the reader as the current DefaultHandler and ErrorHandler.
100
*
101
* @param[in] handler The handler to assign to the built reader
102
* @param[in] isNet whether a network gets loaded
103
* @param[in] isRoute whether routes get loaded
104
* @return The built Xerces-SAX-reader, 0 if something failed
105
* @see getSAXReader()
106
*/
107
static SUMOSAXReader* getSAXReader(SUMOSAXHandler& handler,
108
const bool isNet = false, const bool isRoute = false);
109
110
111
/**
112
* @brief Sets the given handler for the default reader
113
*
114
* Uses the reader built on init() which is stored in myReader.
115
*
116
* @param[in] handler The handler to assign to the built reader
117
*/
118
static void setHandler(GenericSAXHandler& handler);
119
120
121
/**
122
* @brief Runs the given handler on the given file; returns if everything's ok
123
*
124
* Uses the reader built on init() which is stored in myReader to parse the given
125
* file.
126
*
127
* All exceptions are catched and reported to the error-instance of the MsgHandler.
128
* Also, if the reader could not be built, this is reported.
129
*
130
* The method returns true if everything went ok. This means, that the reader could be
131
* built, no exception was caught, and nothing was reported to the error-instance
132
* of the MsgHandler.
133
*
134
* @param[in] handler The handler to assign to the built reader
135
* @param[in] file The file to run the parser at
136
* @param[in] isNet whether a network gets loaded
137
* @param[in] isRoute whether routes get loaded
138
* @param[in] isExternal whether it is an external file like matsim or opendrive
139
* @param[in] catchExceptions whether exceptions on parsing should be caught or transferred into a ProcessError
140
* @return true if the parsing was done without errors, false otherwise (error was printed)
141
*/
142
static bool runParser(GenericSAXHandler& handler, const std::string& file,
143
const bool isNet = false, const bool isRoute = false,
144
const bool isExternal = false, const bool catchExceptions = true);
145
146
147
private:
148
static std::string warnLocalScheme(const std::string& newScheme, const bool haveSUMO_HOME);
149
150
private:
151
/// @brief The XML Readers used for repeated parsing
152
static std::vector<SUMOSAXReader*> myReaders;
153
154
/// @brief Information whether the reader is parsing
155
static int myNextFreeReader;
156
157
/// @brief Information whether built reader/parser shall validate XML-documents against schemata
158
static std::string myValidationScheme;
159
160
/// @brief Information whether built reader/parser shall validate SUMO networks against schemata
161
static std::string myNetValidationScheme;
162
163
/// @brief Information whether built reader/parser shall validate SUMO routes against schemata
164
static std::string myRouteValidationScheme;
165
166
/// @brief Schema cache to be used for grammars which are not declared
167
static XERCES_CPP_NAMESPACE::XMLGrammarPool* myGrammarPool;
168
169
/// @brief Whether a warning about missing SUMO_HOME should be emitted
170
static bool myNeedsValidationWarning;
171
172
};
173
174