Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GenericHandler Class Reference

A handler which converts occurring elements and attributes into enums. More...

#include <GenericHandler.h>

Inheritance diagram for GenericHandler:
[legend]
Collaboration diagram for GenericHandler:
[legend]

Public Member Functions

void characters (const XMLCh *const chars, const XERCES3_SIZE_t length)
 The inherited method called when characters occurred.
 
void endElement (const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname)
 The inherited method called when a tag is being closed.
 
 GenericHandler (const std::string &file, const std::string &expectedRoot="")
 Constructor.
 
const std::string & getFileName () const
 returns the current file name
 
void registerParent (const int tag, GenericHandler *handler)
 Assigning a parent handler which is enabled when the specified tag is closed.
 
void setFileName (const std::string &name)
 Sets the current file name.
 
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.
 
virtual ~GenericHandler ()
 Destructor.
 

Friends

class SUMOSAXReader
 

elements parsing

typedef std::map< std::string, int > TagMap
 
TagMap myTagMap
 
std::vector< std::string > myCharactersVector
 A list of character strings obtained so far to build the complete characters string at the end.
 
GenericHandlermyParentHandler
 The handler to give control back to.
 
int myParentIndicator
 The tag indicating that control should be given back.
 
std::string myFileName
 The name of the currently parsed file.
 
std::string myExpectedRoot
 The root element to expect, empty string disables the check.
 
bool myCollectCharacterData = false
 whether the reader should collect character data
 
bool myRootSeen = false
 whether the reader has already seen the root element
 
int mySection = -1
 The tag indicating the current section to parse.
 
bool mySectionSeen = false
 whether the reader has already seen the begin of the section
 
bool mySectionEnded = false
 whether the reader has already seen the end of the section
 
bool mySectionOpen = false
 whether an element of the current section is open
 
std::pair< int, SUMOSAXAttributes * > myNextSectionStart
 
 GenericHandler (const GenericHandler &s)
 invalidated copy constructor
 
const GenericHandleroperator= (const GenericHandler &s)
 invalidated assignment operator
 

SAX ErrorHandler callbacks

void warning (const XERCES_CPP_NAMESPACE::SAXParseException &exception)
 Handler for XML-warnings.
 
void error (const XERCES_CPP_NAMESPACE::SAXParseException &exception)
 Handler for XML-errors.
 
void fatalError (const XERCES_CPP_NAMESPACE::SAXParseException &exception)
 Handler for XML-errors.
 
void setSection (const int element, const bool seen)
 
bool sectionFinished () const
 
std::pair< int, SUMOSAXAttributes * > retrieveNextSectionStart ()
 
void needsCharacterData (const bool value=true)
 
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.
 
virtual void myCharacters (int element, const std::string &chars)
 Callback method for characters to implement by derived classes.
 
virtual void myEndElement (int element)
 Callback method for a closing tag to implement by derived classes.
 
void callParentEnd (int element)
 signal endElement to the parent handler (special case for MSCalibrator)
 
XMLCh * convert (const std::string &name) const
 converts from c++-string into unicode
 
int convertTag (const std::string &tag) const
 Converts a tag from its string into its numerical representation.
 

Detailed Description

A handler which converts occurring elements and attributes into enums.

Normally, when using a standard SAX-handler, we would have to compare the incoming XMLCh*-element names with the ones we can parse. The same applies to parsing the attributes. This was assumed to be very time consuming, that's why we derive our handlers from this class.

The idea behind this second handler layer was avoid repeated conversion from strings/whatever to XMLCh* and back again. The usage is quite straight forward, the only overhead is the need to define the enums - both elements and attributes within "SUMOXMLDefinitions". Still, it maybe helps to avoid typos.

This class implements the SAX-callback and offers a new set of callbacks which must be implemented by derived classes. Instead of XMLCh*-values, element names are supplied to the derived classes as enums (int).

Also, this class allows to retrieve attributes using enums (int) within the implemented "myStartElement" method.

Basically, GenericHandler is not derived within SUMO directly, but via SUMOSAXHandler which knows all tags/attributes used by SUMO. It is still kept separate for an easier maintainability and later extensions.

Definition at line 63 of file GenericHandler.h.

Member Typedef Documentation

◆ TagMap

typedef std::map<std::string, int> GenericHandler::TagMap
private

Definition at line 280 of file GenericHandler.h.

Constructor & Destructor Documentation

◆ GenericHandler() [1/2]

GenericHandler::GenericHandler ( const std::string &  file,
const std::string &  expectedRoot = "" 
)

Constructor.

This constructor gets the lists of known tag and attribute names with their enums (sumotags and sumoattrs in most cases). The end of the list is signaled by terminatorTag/terminatorAttr respectively.

The attribute names are converted into XMLCh* and stored within an internal container. This container is cleared within the destructor.

Parameters
[in]tagsThe list of known tags
[in]terminatorTagThe tag which indicates the end of tags (usually the last entry)
[in]attrsThe list of known attributes
[in]terminatorAttrThe attr which indicates the end of attrs (usually the last entry)
[in]fileThe name of the processed file
[in]expectedRootThe expected root element, empty string disables the check
Todo:
Why are both lists non-const and given as pointers?

Definition at line 38 of file GenericHandler.cpp.

◆ ~GenericHandler()

GenericHandler::~GenericHandler ( )
virtual

Destructor.

Definition at line 46 of file GenericHandler.cpp.

References myNextSectionStart.

◆ GenericHandler() [2/2]

GenericHandler::GenericHandler ( const GenericHandler s)
private

invalidated copy constructor

Member Function Documentation

◆ buildErrorMessage()

std::string GenericHandler::buildErrorMessage ( const XERCES_CPP_NAMESPACE::SAXParseException &  exception)
protected

Builds an error message.

The error message includes the file name and the line/column information as supported by the given SAXParseException

Parameters
[in]exceptionThe name of the currently processed file
Returns
A string describing the given exception

Definition at line 182 of file GenericHandler.cpp.

References getFileName().

Referenced by error(), fatalError(), and warning().

Here is the caller graph for this function:

◆ callParentEnd()

void GenericHandler::callParentEnd ( int  element)
protected

signal endElement to the parent handler (special case for MSCalibrator)

Definition at line 224 of file GenericHandler.cpp.

References myEndElement(), and myParentHandler.

◆ characters()

void GenericHandler::characters ( const XMLCh *const  chars,
const XERCES3_SIZE_t  length 
)

The inherited method called when characters occurred.

The retrieved characters are converted into a string and appended into a private buffer. They are reported as soon as the element ends.

Todo:

recheck/describe what happens with characters when a new element is opened

describe characters processing in the class' head

Definition at line 164 of file GenericHandler.cpp.

References myCharactersVector, myCollectCharacterData, and StringUtils::transcode().

◆ convert()

XMLCh * GenericHandler::convert ( const std::string &  name) const
private

converts from c++-string into unicode

Todo:
recheck encoding
Parameters
[in]nameThe string to convert
Returns
The string converted into a XMLCh-string

Definition at line 64 of file GenericHandler.cpp.

◆ convertTag()

int GenericHandler::convertTag ( const std::string &  tag) const
private

Converts a tag from its string into its numerical representation.

Returns the enum-representation stored for the given tag. If the tag is not known, SUMO_TAG_NOTHING is returned.

Parameters
[in]tagThe string to convert
Returns
The int-value that represents the string, SUMO_TAG_NOTHING if the named attribute is not known

Definition at line 172 of file GenericHandler.cpp.

References myTagMap, and SUMO_TAG_NOTHING.

Referenced by endElement(), and startElement().

Here is the caller graph for this function:

◆ endElement()

void GenericHandler::endElement ( const XMLCh *const  uri,
const XMLCh *const  localname,
const XMLCh *const  qname 
)

The inherited method called when a tag is being closed.

This method calls the user-implemented methods myCharacters with the previously collected and converted characters.

Then, myEndElement is called, supplying it the qname converted to its enum- and string-representations.

Todo:

recheck/describe encoding of the string-representation

do not generate and report the string-representation

Definition at line 112 of file GenericHandler.cpp.

References convertTag(), myCharacters(), myCharactersVector, myEndElement(), myParentHandler, myParentIndicator, mySection, mySectionOpen, SUMO_TAG_INCLUDE, SUMO_TAG_NOTHING, and StringUtils::transcode().

◆ error()

void GenericHandler::error ( const XERCES_CPP_NAMESPACE::SAXParseException &  exception)

Handler for XML-errors.

The message is built using buildErrorMessage and thrown within a ProcessError.

Parameters
[in]exceptionThe occurred exception to process
Exceptions
ProcessErrorOn any call

Definition at line 201 of file GenericHandler.cpp.

References buildErrorMessage().

◆ fatalError()

void GenericHandler::fatalError ( const XERCES_CPP_NAMESPACE::SAXParseException &  exception)

Handler for XML-errors.

The message is built using buildErrorMessage and thrown within a ProcessError.

Exceptions
ProcessErrorOn any call
Parameters
[in]exceptionThe occurred exception to process

Definition at line 207 of file GenericHandler.cpp.

References buildErrorMessage().

◆ getFileName()

const std::string & GenericHandler::getFileName ( ) const

returns the current file name

Returns
The name of the currently processed file

Definition at line 58 of file GenericHandler.cpp.

References myFileName.

Referenced by buildErrorMessage(), and startElement().

Here is the caller graph for this function:

◆ myCharacters()

void GenericHandler::myCharacters ( int  element,
const std::string &  chars 
)
protectedvirtual

Callback method for characters to implement by derived classes.

Called by "endElement" (see there).

Parameters
[in]elementThe opened element, given as a int
[in]charsThe complete embedded character string @exceptions ProcessError These method may throw a ProcessError if something fails

Definition at line 217 of file GenericHandler.cpp.

Referenced by endElement().

Here is the caller graph for this function:

◆ myEndElement()

void GenericHandler::myEndElement ( int  element)
protectedvirtual

Callback method for a closing tag to implement by derived classes.

Called by "endElement" (see there).

Parameters
[in]elementThe closed element, given as a int @exceptions ProcessError These method may throw a ProcessError if something fails

Definition at line 221 of file GenericHandler.cpp.

Referenced by callParentEnd(), and endElement().

Here is the caller graph for this function:

◆ myStartElement()

void GenericHandler::myStartElement ( int  element,
const SUMOSAXAttributes attrs 
)
protectedvirtual

Callback method for an opening tag to implement by derived classes.

Called by "startElement" (see there).

Parameters
[in]elementThe element that contains the characters, given as a int
[in]attrsThe SAX-attributes, wrapped as SUMOSAXAttributes @exceptions ProcessError These method may throw a ProcessError if something fails

Definition at line 213 of file GenericHandler.cpp.

Referenced by startElement().

Here is the caller graph for this function:

◆ needsCharacterData()

void GenericHandler::needsCharacterData ( const bool  value = true)
inline

Definition at line 206 of file GenericHandler.h.

References myCollectCharacterData.

◆ operator=()

const GenericHandler & GenericHandler::operator= ( const GenericHandler s)
private

invalidated assignment operator

◆ registerParent()

void GenericHandler::registerParent ( const int  tag,
GenericHandler handler 
)

Assigning a parent handler which is enabled when the specified tag is closed.

Definition at line 156 of file GenericHandler.cpp.

References myParentHandler, and myParentIndicator.

◆ retrieveNextSectionStart()

std::pair< int, SUMOSAXAttributes * > GenericHandler::retrieveNextSectionStart ( )
inline

Definition at line 199 of file GenericHandler.h.

References myNextSectionStart.

◆ sectionFinished()

bool GenericHandler::sectionFinished ( ) const
inline

Definition at line 195 of file GenericHandler.h.

References mySectionEnded.

◆ setFileName()

void GenericHandler::setFileName ( const std::string &  name)

Sets the current file name.

Parameters
[in]nameThe name of the currently processed file
Todo:
Hmmm - this is as unsafe as having a direct access to the variable; recheck

Definition at line 52 of file GenericHandler.cpp.

References myFileName.

◆ setSection()

void GenericHandler::setSection ( const int  element,
const bool  seen 
)
inline

Definition at line 188 of file GenericHandler.h.

References mySection, mySectionEnded, mySectionOpen, and mySectionSeen.

◆ startElement()

void GenericHandler::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.

The method parses the supplied XMLCh*-qname using the internal name/enum-map to obtain the enum representation of the attribute name.

Then, "myStartElement" is called supplying the enumeration value, the string-representation of the name and the attributes.

Todo:

recheck/describe encoding of the string-representation

do not generate and report the string-representation

Definition at line 77 of file GenericHandler.cpp.

References convertTag(), FileHelpers::getConfigurationRelative(), getFileName(), SUMOSAXAttributesImpl_Xerces::getString(), FileHelpers::isAbsolute(), myCharactersVector, myExpectedRoot, myNextSectionStart, myRootSeen, mySection, mySectionEnded, mySectionOpen, mySectionSeen, myStartElement(), SUMO_ATTR_HREF, SUMO_TAG_INCLUDE, TL, StringUtils::transcode(), and WRITE_WARNINGF.

◆ warning()

void GenericHandler::warning ( const XERCES_CPP_NAMESPACE::SAXParseException &  exception)

Handler for XML-warnings.

The message is built using buildErrorMessage and reported to the warning-instance of the MsgHandler.

Parameters
[in]exceptionThe occurred exception to process

Definition at line 195 of file GenericHandler.cpp.

References buildErrorMessage(), and WRITE_WARNING.

Friends And Related Symbol Documentation

◆ SUMOSAXReader

friend class SUMOSAXReader
friend

Definition at line 67 of file GenericHandler.h.

Field Documentation

◆ myCharactersVector

std::vector<std::string> GenericHandler::myCharactersVector
private

A list of character strings obtained so far to build the complete characters string at the end.

Definition at line 288 of file GenericHandler.h.

Referenced by characters(), endElement(), and startElement().

◆ myCollectCharacterData

bool GenericHandler::myCollectCharacterData = false
private

whether the reader should collect character data

Definition at line 303 of file GenericHandler.h.

Referenced by characters(), and needsCharacterData().

◆ myExpectedRoot

std::string GenericHandler::myExpectedRoot
private

The root element to expect, empty string disables the check.

Definition at line 300 of file GenericHandler.h.

Referenced by startElement().

◆ myFileName

std::string GenericHandler::myFileName
private

The name of the currently parsed file.

Definition at line 297 of file GenericHandler.h.

Referenced by getFileName(), and setFileName().

◆ myNextSectionStart

std::pair<int, SUMOSAXAttributes*> GenericHandler::myNextSectionStart
private

Definition at line 320 of file GenericHandler.h.

Referenced by retrieveNextSectionStart(), startElement(), and ~GenericHandler().

◆ myParentHandler

GenericHandler* GenericHandler::myParentHandler
private

The handler to give control back to.

Definition at line 291 of file GenericHandler.h.

Referenced by callParentEnd(), endElement(), and registerParent().

◆ myParentIndicator

int GenericHandler::myParentIndicator
private

The tag indicating that control should be given back.

Definition at line 294 of file GenericHandler.h.

Referenced by endElement(), and registerParent().

◆ myRootSeen

bool GenericHandler::myRootSeen = false
private

whether the reader has already seen the root element

Definition at line 306 of file GenericHandler.h.

Referenced by startElement().

◆ mySection

int GenericHandler::mySection = -1
private

The tag indicating the current section to parse.

Definition at line 309 of file GenericHandler.h.

Referenced by endElement(), setSection(), and startElement().

◆ mySectionEnded

bool GenericHandler::mySectionEnded = false
private

whether the reader has already seen the end of the section

Definition at line 315 of file GenericHandler.h.

Referenced by sectionFinished(), setSection(), and startElement().

◆ mySectionOpen

bool GenericHandler::mySectionOpen = false
private

whether an element of the current section is open

Definition at line 318 of file GenericHandler.h.

Referenced by endElement(), setSection(), and startElement().

◆ mySectionSeen

bool GenericHandler::mySectionSeen = false
private

whether the reader has already seen the begin of the section

Definition at line 312 of file GenericHandler.h.

Referenced by setSection(), and startElement().

◆ myTagMap

TagMap GenericHandler::myTagMap
private

Definition at line 283 of file GenericHandler.h.

Referenced by convertTag().


The documentation for this class was generated from the following files: