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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // A handler which converts occuring elements and attributes into enums
17 /****************************************************************************/
18 #ifndef GenericSAXHandler_h
19 #define GenericSAXHandler_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
28 #include <map>
29 #include <stack>
30 #include <sstream>
31 #include <vector>
32 #include <xercesc/sax2/Attributes.hpp>
33 #include <xercesc/sax2/DefaultHandler.hpp>
36 #include "SUMOSAXAttributes.h"
37 
38 
39 // ===========================================================================
40 // class definitions
41 // ===========================================================================
67 class GenericSAXHandler : public XERCES_CPP_NAMESPACE::DefaultHandler {
68 
69 public:
90  StringBijection<int>::Entry* tags, int terminatorTag,
91  StringBijection<int>::Entry* attrs, int terminatorAttr,
92  const std::string& file, const std::string& expectedRoot = "");
93 
94 
96  virtual ~GenericSAXHandler();
97 
98 
111  void startElement(const XMLCh* const uri, const XMLCh* const localname,
112  const XMLCh* const qname, const XERCES_CPP_NAMESPACE::Attributes& attrs);
113 
114 
124  void characters(const XMLCh* const chars, const XERCES3_SIZE_t length);
125 
126 
139  void endElement(const XMLCh* const uri, const XMLCh* const localname,
140  const XMLCh* const qname);
141 
142 
146  void registerParent(const int tag, GenericSAXHandler* handler);
147 
148 
156  void setFileName(const std::string& name);
157 
158 
164  const std::string& getFileName() const;
165 
166 
168 
169 
178  void warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
179 
180 
189  void error(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
190 
191 
200  void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
202 
203 
204  // Reader needs access to myStartElement, myEndElement
205  friend class SUMOSAXReader;
206 
207 
208 protected:
218  std::string buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
219 
220 
229  virtual void myStartElement(int element,
230  const SUMOSAXAttributes& attrs);
231 
232 
241  virtual void myCharacters(int element,
242  const std::string& chars);
243 
244 
251  virtual void myEndElement(int element);
252 
253 
254  void setSchemaSeen(const bool schemaSeen = true) {
255  mySchemaSeen = schemaSeen;
256  }
257 
258 private:
266  XMLCh* convert(const std::string& name) const;
267 
268 
277  int convertTag(const std::string& tag) const;
278 
279 
280 private:
282 
283 
284  // the type of the map from ids to their unicode-string representation
285  typedef std::map<int, XMLCh*> AttrMap;
286 
287  // the map from ids to their unicode-string representation
289 
291  std::map<int, std::string> myPredefinedTagsMML;
293 
294 
296 
297 
298  // the type of the map that maps tag names to ints
299  typedef std::map<std::string, int> TagMap;
300 
301  // the map of tag names to their internal numerical representation
304 
306  std::vector<std::string> myCharactersVector;
307 
310 
313 
315  std::string myFileName;
316 
318  std::string myExpectedRoot;
319 
322 
323 private:
326 
329 
330 };
331 
332 #endif
333 
334 /****************************************************************************/
335 
GenericSAXHandler::registerParent
void registerParent(const int tag, GenericSAXHandler *handler)
Assigning a parent handler which is enabled when the specified tag is closed.
Definition: GenericSAXHandler.cpp:161
GenericSAXHandler::warning
void warning(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-warnings.
Definition: GenericSAXHandler.cpp:199
GenericSAXHandler::myCharacters
virtual void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
Definition: GenericSAXHandler.cpp:221
GenericSAXHandler::AttrMap
std::map< int, XMLCh * > AttrMap
Definition: GenericSAXHandler.h:285
StringBijection.h
GenericSAXHandler::fatalError
void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
Definition: GenericSAXHandler.cpp:211
SUMOSAXReader
SAX-reader encapsulation containing binary reader.
Definition: SUMOSAXReader.h:55
GenericSAXHandler::TagMap
std::map< std::string, int > TagMap
Definition: GenericSAXHandler.h:299
XERCES3_SIZE_t
#define XERCES3_SIZE_t
Definition: config.h:216
GenericSAXHandler::myStartElement
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Callback method for an opening tag to implement by derived classes.
Definition: GenericSAXHandler.cpp:217
GenericSAXHandler::mySchemaSeen
bool mySchemaSeen
whether the reader has already seen a schema
Definition: GenericSAXHandler.h:321
GenericSAXHandler::myPredefinedTagsMML
std::map< int, std::string > myPredefinedTagsMML
the map from ids to their string representation
Definition: GenericSAXHandler.h:291
GenericSAXHandler::myTagMap
TagMap myTagMap
Definition: GenericSAXHandler.h:302
GenericSAXHandler::myParentIndicator
int myParentIndicator
The tag indicating that control should be given back.
Definition: GenericSAXHandler.h:312
GenericSAXHandler::GenericSAXHandler
GenericSAXHandler(StringBijection< int >::Entry *tags, int terminatorTag, StringBijection< int >::Entry *attrs, int terminatorAttr, const std::string &file, const std::string &expectedRoot="")
Constructor.
Definition: GenericSAXHandler.cpp:40
GenericSAXHandler::startElement
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.
Definition: GenericSAXHandler.cpp:93
StringBijection
Definition: StringBijection.h:43
GenericSAXHandler::endElement
void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname)
The inherited method called when a tag is being closed.
Definition: GenericSAXHandler.cpp:120
GenericSAXHandler::myEndElement
virtual void myEndElement(int element)
Callback method for a closing tag to implement by derived classes.
Definition: GenericSAXHandler.cpp:225
UtilExceptions.h
GenericSAXHandler::myPredefinedTags
AttrMap myPredefinedTags
Definition: GenericSAXHandler.h:288
GenericSAXHandler::buildErrorMessage
std::string buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Builds an error message.
Definition: GenericSAXHandler.cpp:186
GenericSAXHandler::convertTag
int convertTag(const std::string &tag) const
Converts a tag from its string into its numerical representation.
Definition: GenericSAXHandler.cpp:176
GenericSAXHandler::getFileName
const std::string & getFileName() const
returns the current file name
Definition: GenericSAXHandler.cpp:74
GenericSAXHandler::myFileName
std::string myFileName
The name of the currently parsed file.
Definition: GenericSAXHandler.h:315
GenericSAXHandler::myCharactersVector
std::vector< std::string > myCharactersVector
A list of character strings obtained so far to build the complete characters string at the end.
Definition: GenericSAXHandler.h:306
SUMOSAXAttributes.h
GenericSAXHandler::error
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
Definition: GenericSAXHandler.cpp:205
config.h
GenericSAXHandler::myExpectedRoot
std::string myExpectedRoot
The root element to expect, empty string disables the check.
Definition: GenericSAXHandler.h:318
GenericSAXHandler::~GenericSAXHandler
virtual ~GenericSAXHandler()
Destructor.
Definition: GenericSAXHandler.cpp:60
GenericSAXHandler::setSchemaSeen
void setSchemaSeen(const bool schemaSeen=true)
Definition: GenericSAXHandler.h:254
GenericSAXHandler::operator=
const GenericSAXHandler & operator=(const GenericSAXHandler &s)
invalidated assignment operator
GenericSAXHandler
A handler which converts occuring elements and attributes into enums.
Definition: GenericSAXHandler.h:67
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
GenericSAXHandler::characters
void characters(const XMLCh *const chars, const XERCES3_SIZE_t length)
The inherited method called when characters occurred.
Definition: GenericSAXHandler.cpp:169
GenericSAXHandler::convert
XMLCh * convert(const std::string &name) const
converts from c++-string into unicode
Definition: GenericSAXHandler.cpp:80
GenericSAXHandler::setFileName
void setFileName(const std::string &name)
Sets the current file name.
Definition: GenericSAXHandler.cpp:68
GenericSAXHandler::myParentHandler
GenericSAXHandler * myParentHandler
The handler to give control back to.
Definition: GenericSAXHandler.h:309