Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GenericSAXHandler.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2002-2026 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/****************************************************************************/
20// A handler which converts occurring elements and attributes into enums
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <string>
26#include <map>
27#include <stack>
28#include <sstream>
29#include <vector>
30#include <xercesc/sax2/Attributes.hpp>
31#include <xercesc/sax2/DefaultHandler.hpp>
34#include "SUMOSAXAttributes.h"
35
36
37// ===========================================================================
38// class definitions
39// ===========================================================================
65class GenericSAXHandler : public XERCES_CPP_NAMESPACE::DefaultHandler {
66
67public:
88 SequentialStringBijection::Entry* tags, int terminatorTag,
89 SequentialStringBijection::Entry* attrs, int terminatorAttr,
90 const std::string& file, const std::string& expectedRoot = "");
91
92
94 virtual ~GenericSAXHandler();
95
96
109 void startElement(const XMLCh* const uri, const XMLCh* const localname,
110 const XMLCh* const qname, const XERCES_CPP_NAMESPACE::Attributes& attrs);
111
112
122 void characters(const XMLCh* const chars, const XERCES3_SIZE_t length);
123
124
137 void endElement(const XMLCh* const uri, const XMLCh* const localname,
138 const XMLCh* const qname);
139
140
144 void registerParent(const int tag, GenericSAXHandler* handler);
145
146
154 void setFileName(const std::string& name);
155
156
162 const std::string& getFileName() const;
163
164
166
167
176 void warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
177
178
187 void error(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
188
189
198 void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
200
201 void setSection(const int element, const bool seen) {
202 mySection = element;
203 mySectionSeen = seen;
204 mySectionOpen = seen;
205 mySectionEnded = false;
206 }
207
208 bool sectionFinished() const {
209 return mySectionEnded;
210 }
211
212 std::pair<int, SUMOSAXAttributes*> retrieveNextSectionStart() {
213 std::pair<int, SUMOSAXAttributes*> ret = myNextSectionStart;
214 myNextSectionStart.first = -1;
215 myNextSectionStart.second = nullptr;
216 return ret;
217 }
218
219 void needsCharacterData(const bool value = true) {
221 }
222
223 // Reader needs access to myStartElement, myEndElement
224 friend class SUMOSAXReader;
225
226protected:
236 std::string buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
237
246 virtual void myStartElement(int element,
247 const SUMOSAXAttributes& attrs);
248
257 virtual void myCharacters(int element,
258 const std::string& chars);
259
266 virtual void myEndElement(int element);
267
269 void callParentEnd(int element);
270
271private:
279 XMLCh* convert(const std::string& name) const;
280
289 int convertTag(const std::string& tag) const;
290
291private:
293
294
295 // the type of the map from ids to their unicode-string representation
296 typedef std::vector<XMLCh*> AttrMap;
297
298 // the map from ids to their unicode-string representation
300
302 std::vector<std::string> myPredefinedTagsMML;
304
305
307
308
309 // the type of the map that maps tag names to ints
310 typedef std::map<std::string, int> TagMap;
311
312 // the map of tag names to their internal numerical representation
315
317 std::vector<std::string> myCharactersVector;
318
321
324
326 std::string myFileName;
327
329 std::string myExpectedRoot;
330
333
335 bool myRootSeen = false;
336
338 int mySection = -1;
339
341 bool mySectionSeen = false;
342
344 bool mySectionEnded = false;
345
347 bool mySectionOpen = false;
348
349 std::pair<int, SUMOSAXAttributes*> myNextSectionStart;
350
351
352private:
355
358
359};
A handler which converts occurring elements and attributes into enums.
void registerParent(const int tag, GenericSAXHandler *handler)
Assigning a parent handler which is enabled when the specified tag is closed.
virtual void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
void needsCharacterData(const bool value=true)
std::string buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Builds an error message.
int convertTag(const std::string &tag) const
Converts a tag from its string into its numerical representation.
bool sectionFinished() const
XMLCh * convert(const std::string &name) const
converts from c++-string into unicode
void setSection(const int element, const bool seen)
void startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const XERCES_CPP_NAMESPACE::Attributes &attrs)
The inherited method called when a new tag opens.
std::vector< XMLCh * > AttrMap
bool mySectionEnded
whether the reader has already seen the end of the section
std::string myFileName
The name of the currently parsed file.
GenericSAXHandler(const GenericSAXHandler &s)
invalidated copy constructor
bool mySectionOpen
whether an element of the current section is open
std::pair< int, SUMOSAXAttributes * > retrieveNextSectionStart()
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
bool mySectionSeen
whether the reader has already seen the begin of the section
GenericSAXHandler * myParentHandler
The handler to give control back to.
int mySection
The tag indicating the current section to parse.
void setFileName(const std::string &name)
Sets the current file name.
std::map< std::string, int > TagMap
int myParentIndicator
The tag indicating that control should be given back.
void characters(const XMLCh *const chars, const XERCES3_SIZE_t length)
The inherited method called when characters occurred.
virtual ~GenericSAXHandler()
Destructor.
virtual void myEndElement(int element)
Callback method for a closing tag to implement by derived classes.
void warning(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-warnings.
std::string myExpectedRoot
The root element to expect, empty string disables the check.
std::pair< int, SUMOSAXAttributes * > myNextSectionStart
void callParentEnd(int element)
signal endElement to the parent handler (special case for MSCalibrator)
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Callback method for an opening tag to implement by derived classes.
std::vector< std::string > myCharactersVector
A list of character strings obtained so far to build the complete characters string at the end.
void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
bool myRootSeen
whether the reader has already seen the root element
bool myCollectCharacterData
whether the reader should collect character data
const std::string & getFileName() const
returns the current file name
std::vector< std::string > myPredefinedTagsMML
the map from ids to their string representation
const GenericSAXHandler & operator=(const GenericSAXHandler &s)
invalidated assignment operator
void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname)
The inherited method called when a tag is being closed.
Encapsulated SAX-Attributes.
SAX-reader encapsulation containing binary reader.