Eclipse SUMO - Simulation of Urban MObility
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-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 /****************************************************************************/
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 // ===========================================================================
65 class GenericSAXHandler : public XERCES_CPP_NAMESPACE::DefaultHandler {
66 
67 public:
88  StringBijection<int>::Entry* tags, int terminatorTag,
89  StringBijection<int>::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 
202  void setSection(const int element, const bool seen) {
203  mySection = element;
204  mySectionSeen = seen;
205  mySectionOpen = seen;
206  mySectionEnded = false;
207  }
208 
209  bool sectionFinished() const {
210  return mySectionEnded;
211  }
212 
213  std::pair<int, SUMOSAXAttributes*> retrieveNextSectionStart() {
214  std::pair<int, SUMOSAXAttributes*> ret = myNextSectionStart;
215  myNextSectionStart.first = -1;
216  myNextSectionStart.second = nullptr;
217  return ret;
218  }
219 
220  void needsCharacterData(const bool value = true) {
221  myCollectCharacterData = value;
222  }
223 
224  // Reader needs access to myStartElement, myEndElement
225  friend class SUMOSAXReader;
226 
227 
228 protected:
238  std::string buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
239 
240 
249  virtual void myStartElement(int element,
250  const SUMOSAXAttributes& attrs);
251 
252 
261  virtual void myCharacters(int element,
262  const std::string& chars);
263 
264 
271  virtual void myEndElement(int element);
272 
274  void callParentEnd(int element);
275 
276 private:
284  XMLCh* convert(const std::string& name) const;
285 
286 
295  int convertTag(const std::string& tag) const;
296 
297 
298 private:
300 
301 
302  // the type of the map from ids to their unicode-string representation
303  typedef std::vector<XMLCh*> AttrMap;
304 
305  // the map from ids to their unicode-string representation
307 
309  std::vector<std::string> myPredefinedTagsMML;
311 
312 
314 
315 
316  // the type of the map that maps tag names to ints
317  typedef std::map<std::string, int> TagMap;
318 
319  // the map of tag names to their internal numerical representation
322 
324  std::vector<std::string> myCharactersVector;
325 
328 
331 
333  std::string myFileName;
334 
336  std::string myExpectedRoot;
337 
340 
342  bool myRootSeen = false;
343 
345  int mySection = -1;
346 
348  bool mySectionSeen = false;
349 
351  bool mySectionEnded = false;
352 
354  bool mySectionOpen = false;
355 
356  std::pair<int, SUMOSAXAttributes*> myNextSectionStart;
357 
358 private:
361 
364 
365 };
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.
const GenericSAXHandler & operator=(const GenericSAXHandler &s)
invalidated assignment operator
std::vector< XMLCh * > AttrMap
bool mySectionEnded
whether the reader has already seen the end of the section
std::pair< int, SUMOSAXAttributes * > retrieveNextSectionStart()
GenericSAXHandler(StringBijection< int >::Entry *tags, int terminatorTag, StringBijection< int >::Entry *attrs, int terminatorAttr, const std::string &file, const std::string &expectedRoot="")
Constructor.
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
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
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.
Definition: SUMOSAXReader.h:53