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 /****************************************************************************/
17 // A handler which converts occuring elements and attributes into enums
18 /****************************************************************************/
19 #ifndef GenericSAXHandler_h
20 #define GenericSAXHandler_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <string>
29 #include <map>
30 #include <stack>
31 #include <sstream>
32 #include <vector>
33 #include <xercesc/sax2/Attributes.hpp>
34 #include <xercesc/sax2/DefaultHandler.hpp>
37 #include "SUMOSAXAttributes.h"
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
68 class GenericSAXHandler : public XERCES_CPP_NAMESPACE::DefaultHandler {
69 
70 public:
91  StringBijection<int>::Entry* tags, int terminatorTag,
92  StringBijection<int>::Entry* attrs, int terminatorAttr,
93  const std::string& file, const std::string& expectedRoot = "");
94 
95 
97  virtual ~GenericSAXHandler();
98 
99 
112  void startElement(const XMLCh* const uri, const XMLCh* const localname,
113  const XMLCh* const qname, const XERCES_CPP_NAMESPACE::Attributes& attrs);
114 
115 
125  void characters(const XMLCh* const chars, const XERCES3_SIZE_t length);
126 
127 
140  void endElement(const XMLCh* const uri, const XMLCh* const localname,
141  const XMLCh* const qname);
142 
143 
147  void registerParent(const int tag, GenericSAXHandler* handler);
148 
149 
157  void setFileName(const std::string& name);
158 
159 
165  const std::string& getFileName() const;
166 
167 
169 
170 
179  void warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
180 
181 
190  void error(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
191 
192 
201  void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
203 
204 
205  // Reader needs access to myStartElement, myEndElement
206  friend class SUMOSAXReader;
207 
208 
209 protected:
219  std::string buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException& exception);
220 
221 
230  virtual void myStartElement(int element,
231  const SUMOSAXAttributes& attrs);
232 
233 
242  virtual void myCharacters(int element,
243  const std::string& chars);
244 
245 
252  virtual void myEndElement(int element);
253 
254 
255  void setSchemaSeen(const bool schemaSeen = true) {
256  mySchemaSeen = schemaSeen;
257  }
258 
259 private:
267  XMLCh* convert(const std::string& name) const;
268 
269 
278  int convertTag(const std::string& tag) const;
279 
280 
281 private:
283 
284 
285  // the type of the map from ids to their unicode-string representation
286  typedef std::map<int, XMLCh*> AttrMap;
287 
288  // the map from ids to their unicode-string representation
290 
292  std::map<int, std::string> myPredefinedTagsMML;
294 
295 
297 
298 
299  // the type of the map that maps tag names to ints
300  typedef std::map<std::string, int> TagMap;
301 
302  // the map of tag names to their internal numerical representation
305 
307  std::vector<std::string> myCharactersVector;
308 
311 
314 
316  std::string myFileName;
317 
319  std::string myExpectedRoot;
320 
323 
324 private:
327 
330 
331 };
332 
333 #endif
334 
335 /****************************************************************************/
336 
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:162
GenericSAXHandler::warning
void warning(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-warnings.
Definition: GenericSAXHandler.cpp:200
GenericSAXHandler::myCharacters
virtual void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
Definition: GenericSAXHandler.cpp:222
GenericSAXHandler::AttrMap
std::map< int, XMLCh * > AttrMap
Definition: GenericSAXHandler.h:286
StringBijection.h
GenericSAXHandler::fatalError
void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
Definition: GenericSAXHandler.cpp:212
SUMOSAXReader
SAX-reader encapsulation containing binary reader.
Definition: SUMOSAXReader.h:56
GenericSAXHandler::TagMap
std::map< std::string, int > TagMap
Definition: GenericSAXHandler.h:300
XERCES3_SIZE_t
#define XERCES3_SIZE_t
Definition: config.h:213
GenericSAXHandler::myStartElement
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Callback method for an opening tag to implement by derived classes.
Definition: GenericSAXHandler.cpp:218
GenericSAXHandler::mySchemaSeen
bool mySchemaSeen
whether the reader has already seen a schema
Definition: GenericSAXHandler.h:322
GenericSAXHandler::myPredefinedTagsMML
std::map< int, std::string > myPredefinedTagsMML
the map from ids to their string representation
Definition: GenericSAXHandler.h:292
GenericSAXHandler::myTagMap
TagMap myTagMap
Definition: GenericSAXHandler.h:303
GenericSAXHandler::myParentIndicator
int myParentIndicator
The tag indicating that control should be given back.
Definition: GenericSAXHandler.h:313
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:41
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:94
StringBijection
Definition: StringBijection.h:44
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:121
GenericSAXHandler::myEndElement
virtual void myEndElement(int element)
Callback method for a closing tag to implement by derived classes.
Definition: GenericSAXHandler.cpp:226
UtilExceptions.h
GenericSAXHandler::myPredefinedTags
AttrMap myPredefinedTags
Definition: GenericSAXHandler.h:289
GenericSAXHandler::buildErrorMessage
std::string buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Builds an error message.
Definition: GenericSAXHandler.cpp:187
GenericSAXHandler::convertTag
int convertTag(const std::string &tag) const
Converts a tag from its string into its numerical representation.
Definition: GenericSAXHandler.cpp:177
GenericSAXHandler::getFileName
const std::string & getFileName() const
returns the current file name
Definition: GenericSAXHandler.cpp:75
GenericSAXHandler::myFileName
std::string myFileName
The name of the currently parsed file.
Definition: GenericSAXHandler.h:316
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:307
SUMOSAXAttributes.h
GenericSAXHandler::error
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
Definition: GenericSAXHandler.cpp:206
config.h
GenericSAXHandler::myExpectedRoot
std::string myExpectedRoot
The root element to expect, empty string disables the check.
Definition: GenericSAXHandler.h:319
GenericSAXHandler::~GenericSAXHandler
virtual ~GenericSAXHandler()
Destructor.
Definition: GenericSAXHandler.cpp:61
GenericSAXHandler::setSchemaSeen
void setSchemaSeen(const bool schemaSeen=true)
Definition: GenericSAXHandler.h:255
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:68
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:57
GenericSAXHandler::characters
void characters(const XMLCh *const chars, const XERCES3_SIZE_t length)
The inherited method called when characters occurred.
Definition: GenericSAXHandler.cpp:170
GenericSAXHandler::convert
XMLCh * convert(const std::string &name) const
converts from c++-string into unicode
Definition: GenericSAXHandler.cpp:81
GenericSAXHandler::setFileName
void setFileName(const std::string &name)
Sets the current file name.
Definition: GenericSAXHandler.cpp:69
GenericSAXHandler::myParentHandler
GenericSAXHandler * myParentHandler
The handler to give control back to.
Definition: GenericSAXHandler.h:310