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.org/sumo
3 // Copyright (C) 2002-2020 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 occuring 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  // Reader needs access to myStartElement, myEndElement
203  friend class SUMOSAXReader;
204 
205 
206 protected:
216  std::string buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
217 
218 
227  virtual void myStartElement(int element,
228  const SUMOSAXAttributes& attrs);
229 
230 
239  virtual void myCharacters(int element,
240  const std::string& chars);
241 
242 
249  virtual void myEndElement(int element);
250 
251 
252 private:
260  XMLCh* convert(const std::string& name) const;
261 
262 
271  int convertTag(const std::string& tag) const;
272 
273 
274 private:
276 
277 
278  // the type of the map from ids to their unicode-string representation
279  typedef std::vector<XMLCh*> AttrMap;
280 
281  // the map from ids to their unicode-string representation
283 
285  std::vector<std::string> myPredefinedTagsMML;
287 
288 
290 
291 
292  // the type of the map that maps tag names to ints
293  typedef std::map<std::string, int> TagMap;
294 
295  // the map of tag names to their internal numerical representation
298 
300  std::vector<std::string> myCharactersVector;
301 
304 
307 
309  std::string myFileName;
310 
312  std::string myExpectedRoot;
313 
316 
317 private:
320 
323 
324 };
A handler which converts occuring 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.
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.
XMLCh * convert(const std::string &name) const
converts from c++-string into unicode
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
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
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
GenericSAXHandler * myParentHandler
The handler to give control back to.
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.
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
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:52