Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GenericHandler.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-2024 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/****************************************************************************/
18// A handler which converts occurring elements and attributes into strings
19/****************************************************************************/
20#pragma once
21#include <config.h>
22
23#include <string>
24#include <map>
25#include <stack>
26#include <sstream>
27#include <vector>
28#include <xercesc/sax2/Attributes.hpp>
29#include <xercesc/sax2/DefaultHandler.hpp>
32#include "SUMOSAXAttributes.h"
33
34
35// ===========================================================================
36// class definitions
37// ===========================================================================
63class GenericHandler : public XERCES_CPP_NAMESPACE::DefaultHandler {
64
65public:
66 // Reader needs access to myStartElement, myEndElement
67 friend class SUMOSAXReader;
68
88 GenericHandler(const std::string& file, const std::string& expectedRoot = "");
89
91 virtual ~GenericHandler();
92
105 void startElement(const XMLCh* const uri, const XMLCh* const localname,
106 const XMLCh* const qname, const XERCES_CPP_NAMESPACE::Attributes& attrs);
107
117 void characters(const XMLCh* const chars, const XERCES3_SIZE_t length);
118
131 void endElement(const XMLCh* const uri, const XMLCh* const localname,
132 const XMLCh* const qname);
133
135 void registerParent(const int tag, GenericHandler* handler);
136
144 void setFileName(const std::string& name);
145
151 const std::string& getFileName() const;
152
154
155
164 void warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
165
174 void error(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
175
184 void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
185
187
188 void setSection(const int element, const bool seen) {
189 mySection = element;
190 mySectionSeen = seen;
191 mySectionOpen = seen;
192 mySectionEnded = false;
193 }
194
195 bool sectionFinished() const {
196 return mySectionEnded;
197 }
198
199 std::pair<int, SUMOSAXAttributes*> retrieveNextSectionStart() {
200 std::pair<int, SUMOSAXAttributes*> ret = myNextSectionStart;
201 myNextSectionStart.first = -1;
202 myNextSectionStart.second = nullptr;
203 return ret;
204 }
205
206 void needsCharacterData(const bool value = true) {
208 }
209
210protected:
220 std::string buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
221
230 virtual void myStartElement(int element,
231 const SUMOSAXAttributes& attrs);
232
241 virtual void myCharacters(int element,
242 const std::string& chars);
243
250 virtual void myEndElement(int element);
251
253 void callParentEnd(int element);
254
255private:
263 XMLCh* convert(const std::string& name) const;
264
273 int convertTag(const std::string& tag) const;
274
275private:
277
278
279 // the type of the map that maps tag names to ints
280 typedef std::map<std::string, int> TagMap;
281
282 // the map of tag names to their internal numerical representation
284
286
288 std::vector<std::string> myCharactersVector;
289
292
295
297 std::string myFileName;
298
300 std::string myExpectedRoot;
301
304
306 bool myRootSeen = false;
307
309 int mySection = -1;
310
312 bool mySectionSeen = false;
313
315 bool mySectionEnded = false;
316
318 bool mySectionOpen = false;
319
320 std::pair<int, SUMOSAXAttributes*> myNextSectionStart;
321
322private:
325
328
329};
A handler which converts occurring elements and attributes into enums.
void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname)
The inherited method called when a tag is being closed.
std::pair< int, SUMOSAXAttributes * > retrieveNextSectionStart()
bool myCollectCharacterData
whether the reader should collect character data
bool mySectionEnded
whether the reader has already seen the end of the section
void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
bool mySectionOpen
whether an element of the current section is open
void registerParent(const int tag, GenericHandler *handler)
Assigning a parent handler which is enabled when the specified tag is closed.
virtual ~GenericHandler()
Destructor.
bool myRootSeen
whether the reader has already seen the root element
std::map< std::string, int > TagMap
virtual void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
void callParentEnd(int element)
signal endElement to the parent handler (special case for MSCalibrator)
std::string myFileName
The name of the currently parsed file.
int mySection
The tag indicating the current section to parse.
const std::string & getFileName() const
returns the current file name
std::vector< std::string > myCharactersVector
A list of character strings obtained so far to build the complete characters string at the end.
int myParentIndicator
The tag indicating that control should be given back.
virtual void myEndElement(int element)
Callback method for a closing tag to implement by derived classes.
std::string buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Builds an error message.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Callback method for an opening tag to implement by derived classes.
std::pair< int, SUMOSAXAttributes * > myNextSectionStart
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
int convertTag(const std::string &tag) const
Converts a tag from its string into its numerical representation.
XMLCh * convert(const std::string &name) const
converts from c++-string into unicode
void setSection(const int element, const bool seen)
void warning(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-warnings.
GenericHandler(const GenericHandler &s)
invalidated copy constructor
GenericHandler * myParentHandler
The handler to give control back to.
bool mySectionSeen
whether the reader has already seen the begin of the section
void setFileName(const std::string &name)
Sets the current file name.
std::string myExpectedRoot
The root element to expect, empty string disables the check.
bool sectionFinished() const
void needsCharacterData(const bool value=true)
void characters(const XMLCh *const chars, const XERCES3_SIZE_t length)
The inherited method called when characters occurred.
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.
const GenericHandler & operator=(const GenericHandler &s)
invalidated assignment operator
Encapsulated SAX-Attributes.
SAX-reader encapsulation containing binary reader.