#include <GenericSAXHandler.h>

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 emelents and attributes within "SUMOXMLDefinition". 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 (SumoXMLTag).
Also, this class allows to retrieve attributes using enums (SumoXMLAttr) within the implemented "myStartElement" method.
Basically, GenericSAXHandler 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 80 of file GenericSAXHandler.h.
attributes parsing | |
| typedef std::map< SumoXMLAttr, XMLCh * > | AttrMap |
| AttrMap | myPredefinedTags |
| std::map< SumoXMLAttr, std::string > | myPredefinedTagsMML |
| the map from ids to their string representation | |
elements parsing | |
| typedef std::map< std::string, SumoXMLTag > | TagMap |
| TagMap | myTagMap |
Public Member Functions | |
| void | characters (const XMLCh *const chars, const XERCES3_SIZE_t length) |
| The inherited method called when characters occured. | |
| void | endElement (const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname) |
| The inherited method called when a tag is being closed. | |
| GenericSAXHandler (Tag *tags, Attr *attrs, const std::string &file) throw () | |
| Constructor. | |
| const std::string & | getFileName () const throw () |
| returns the current file name | |
| void | registerParent (const SumoXMLTag tag, GenericSAXHandler *handler) |
| Assigning a parent handler which is enabled when the specified tag is closed. | |
| void | setFileName (const std::string &name) throw () |
| Sets the current file name. | |
| void | startElement (const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attrs) |
| The inherited method called when a new tag opens. | |
| virtual | ~GenericSAXHandler () throw () |
| Destructor. | |
Protected Member Functions | |
| virtual void | myCharacters (SumoXMLTag element, const std::string &chars) throw (ProcessError) |
| Callback method for characters to implement by derived classes. | |
| virtual void | myEndElement (SumoXMLTag element) throw (ProcessError) |
| Callback method for a closing tag to implement by derived classes. | |
| virtual void | myStartElement (SumoXMLTag element, const SUMOSAXAttributes &attrs) throw (ProcessError) |
| Callback method for an opening tag to implement by derived classes. | |
Private Member Functions | |
| XMLCh * | convert (const std::string &name) const throw () |
| converts from c++-string into unicode | |
| SumoXMLTag | convertTag (const std::string &tag) const throw () |
| Converts a tag from its string into its numerical representation. | |
Private Attributes | |
| std::vector< std::string > | myCharactersVector |
| A list of character strings obtained so far to build the complete characters string at the end. | |
| std::string | myFileName |
| The name of the currently parsed file. | |
| GenericSAXHandler * | myParentHandler |
| SumoXMLTag | myParentIndicator |
Data Structures | |
| struct | Attr |
| An attribute name and its numerical representation. More... | |
| struct | Tag |
| A tag (element) name with its numerical representation. More... | |
typedef std::map<SumoXMLAttr, XMLCh*> GenericSAXHandler::AttrMap [private] |
Definition at line 259 of file GenericSAXHandler.h.
typedef std::map<std::string, SumoXMLTag> GenericSAXHandler::TagMap [private] |
Definition at line 273 of file GenericSAXHandler.h.
| GenericSAXHandler::GenericSAXHandler | ( | GenericSAXHandler::Tag * | tags, | |
| GenericSAXHandler::Attr * | attrs, | |||
| const std::string & | file | |||
| ) | throw () |
Constructor.
This constructor gets the lists of known tag and attribute names with their enums (sumotags and sumoattrs in most cases). The list is closed by a tag/attribute with the enum-value SUMO_TAG_NOTHING/SUMO_ATTR_NOTHING, respectively.
The attribute names are converted into XMLCh* and stored within an internal container. This container is cleared within the destructor.
| [in] | tags | The list of known tags |
| [in] | attrs | The list of known attributes |
| [in] | file | The name of the processed file |
Definition at line 46 of file GenericSAXHandler.cpp.
References convert(), SUMO_ATTR_NOTHING, and SUMO_TAG_NOTHING.
00048 : myParentHandler(0), myParentIndicator(SUMO_TAG_NOTHING), myFileName(file) { 00049 int i = 0; 00050 while (tags[i].key != SUMO_TAG_NOTHING) { 00051 myTagMap.insert(TagMap::value_type(tags[i].name, tags[i].key)); 00052 i++; 00053 } 00054 i = 0; 00055 while (attrs[i].key != SUMO_ATTR_NOTHING) { 00056 assert(myPredefinedTags.find(attrs[i].key)==myPredefinedTags.end()); 00057 myPredefinedTags[attrs[i].key] = convert(attrs[i].name); 00058 myPredefinedTagsMML[attrs[i].key] = attrs[i].name; 00059 i++; 00060 } 00061 }
| GenericSAXHandler::~GenericSAXHandler | ( | ) | throw () [virtual] |
Destructor.
Definition at line 64 of file GenericSAXHandler.cpp.
References myPredefinedTags.
00064 { 00065 for (AttrMap::iterator i1=myPredefinedTags.begin(); i1!=myPredefinedTags.end(); i1++) { 00066 delete[](*i1).second; 00067 } 00068 }
| void GenericSAXHandler::characters | ( | const XMLCh *const | chars, | |
| const XERCES3_SIZE_t | length | |||
| ) |
The inherited method called when characters occured.
The retrieved characters are converted into a string and appended into a private buffer. They are reported as soon as the element ends.
Definition at line 168 of file GenericSAXHandler.cpp.
References myCharactersVector.
00169 { 00170 myCharactersVector.push_back(TplConvert<XMLCh>::_2str(chars, length)); 00171 }
| XMLCh * GenericSAXHandler::convert | ( | const std::string & | name | ) | const throw () [private] |
converts from c++-string into unicode
| [in] | name | The string to convert |
Definition at line 84 of file GenericSAXHandler.cpp.
00084 { 00085 size_t len = name.length(); 00086 XMLCh *ret = new XMLCh[len+1]; 00087 size_t i=0; 00088 for (; i<len; i++) { 00089 ret[i] = (XMLCh) name[i]; 00090 } 00091 ret[i] = 0; 00092 return ret; 00093 }
| SumoXMLTag GenericSAXHandler::convertTag | ( | const std::string & | tag | ) | const throw () [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.
| [in] | tag | The string to convert |
Definition at line 175 of file GenericSAXHandler.cpp.
References myTagMap, and SUMO_TAG_NOTHING.
Referenced by endElement(), and startElement().
00175 { 00176 TagMap::const_iterator i=myTagMap.find(tag); 00177 if (i==myTagMap.end()) { 00178 return SUMO_TAG_NOTHING; 00179 } 00180 return (*i).second; 00181 }
| void GenericSAXHandler::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.
Definition at line 118 of file GenericSAXHandler.cpp.
References TplConvert< E >::_2str(), convertTag(), myCharacters(), myCharactersVector, myEndElement(), myParentHandler, myParentIndicator, XMLSubSys::setHandler(), SUMO_TAG_INCLUDE, and SUMO_TAG_NOTHING.
00120 { 00121 std::string name = TplConvert<XMLCh>::_2str(qname); 00122 SumoXMLTag element = convertTag(name); 00123 // collect characters 00124 if (myCharactersVector.size()!=0) { 00125 size_t len = 0; 00126 unsigned i; 00127 for (i=0; i<myCharactersVector.size(); ++i) { 00128 len += myCharactersVector[i].length(); 00129 } 00130 char *buf = new char[len+1]; 00131 size_t pos = 0; 00132 for (i=0; i<myCharactersVector.size(); ++i) { 00133 memcpy((unsigned char*) buf+pos, (unsigned char*) myCharactersVector[i].c_str(), 00134 sizeof(char)*myCharactersVector[i].length()); 00135 pos += myCharactersVector[i].length(); 00136 } 00137 buf[pos] = 0; 00138 00139 // call user handler 00140 try { 00141 myCharacters(element, buf); 00142 } catch (std::runtime_error &) { 00143 delete[] buf; 00144 throw; 00145 } 00146 delete[] buf; 00147 } 00148 if (element != SUMO_TAG_INCLUDE) { 00149 myEndElement(element); 00150 if (myParentHandler && myParentIndicator == element) { 00151 XMLSubSys::setHandler(*myParentHandler); 00152 myParentIndicator = SUMO_TAG_NOTHING; 00153 myParentHandler = 0; 00154 } 00155 } 00156 }
| const std::string & GenericSAXHandler::getFileName | ( | ) | const throw () |
returns the current file name
Definition at line 78 of file GenericSAXHandler.cpp.
References myFileName.
Referenced by NLHandler::addE1Detector(), NLHandler::addE2Detector(), NLHandler::addEdgeLaneMeanData(), NLHandler::addRouteProbeDetector(), NLHandler::addSource(), NLHandler::addTrigger(), NLHandler::addVTypeProbeDetector(), NLHandler::beginE3Detector(), SUMOSAXHandler::buildErrorMessage(), MSRouteLoader::init(), PCNetProjectionLoader::loadIfSet(), NILoader::loadXMLType(), MSTriggeredXMLReader::myInit(), RODFDetectorHandler::myStartElement(), NLHandler::myStartElement(), NIImporter_OpenDrive::myStartElement(), MSLaneSpeedTrigger::myStartElement(), GUISettingsHandler::myStartElement(), RORDGenerator_ODAmounts::RORDGenerator_ODAmounts(), and startElement().
00078 { 00079 return myFileName; 00080 }
| void GenericSAXHandler::myCharacters | ( | SumoXMLTag | element, | |
| const std::string & | chars | |||
| ) | throw (ProcessError) [protected, virtual] |
Callback method for characters to implement by derived classes.
Called by "endElement" (see there).
| [in] | element | The opened element, given as a SumoXMLTag |
| [in] | chars | The complete embedded character string ProcessError These method may throw a ProcessError if something fails |
Reimplemented in ROJTRTurnDefLoader, MSRouteHandler, NIImporter_OpenDrive, NIImporter_SUMO, NLHandler, PCLoaderXML, PCNetProjectionLoader, and RORDLoader_SUMOBase.
Definition at line 189 of file GenericSAXHandler.cpp.
Referenced by endElement().
| void GenericSAXHandler::myEndElement | ( | SumoXMLTag | element | ) | throw (ProcessError) [protected, virtual] |
Callback method for a closing tag to implement by derived classes.
Called by "endElement" (see there).
| [in] | element | The closed element, given as a SumoXMLTag ProcessError These method may throw a ProcessError if something fails |
Reimplemented in MSRouteHandler, MSLaneSpeedTrigger, MSTriggeredRerouter, NIImporter_OpenDrive, NIImporter_OpenStreetMap::NodesHandler, NIImporter_OpenStreetMap::EdgesHandler, NIImporter_SUMO, NIXMLEdgesHandler, NLHandler, ODDistrictHandler, PCLoaderOSM::NodesHandler, PCLoaderOSM::EdgesHandler, RORDGenerator_ODAmounts, RORDLoader_SUMOBase, RORDLoader_TripDefs, traci::TraCIHandler, and SAXWeightsHandler.
Definition at line 193 of file GenericSAXHandler.cpp.
Referenced by endElement().
| void GenericSAXHandler::myStartElement | ( | SumoXMLTag | element, | |
| const SUMOSAXAttributes & | attrs | |||
| ) | throw (ProcessError) [protected, virtual] |
Callback method for an opening tag to implement by derived classes.
Called by "startElement" (see there).
| [in] | element | The element that contains the characters, given as a SumoXMLTag |
| [in] | attrs | The SAX-attributes, wrapped as SUMOSAXAttributes ProcessError These method may throw a ProcessError if something fails |
Reimplemented in RODFDetectorHandler, ROJTRTurnDefLoader, MSRouteHandler, MSCalibrator::MSCalibrator_FileTriggeredChild, MSEmitter::MSEmitter_FileTriggeredChild, MSLaneSpeedTrigger, MSTriggeredRerouter, NIImporter_OpenDrive, NIImporter_OpenStreetMap::NodesHandler, NIImporter_OpenStreetMap::EdgesHandler, NIImporter_SUMO, NIXMLConnectionsHandler, NIXMLEdgesHandler, NIXMLNodesHandler, NIXMLTypesHandler, NLHandler, ODDistrictHandler, PCLoaderOSM::NodesHandler, PCLoaderOSM::EdgesHandler, PCLoaderXML, PCNetProjectionLoader, PCTypeDefHandler, RONetHandler, RORDGenerator_ODAmounts, RORDLoader_SUMOBase, RORDLoader_TripDefs, traci::TraCIHandler, GUISettingsHandler, and SAXWeightsHandler.
Definition at line 185 of file GenericSAXHandler.cpp.
Referenced by startElement().
| void GenericSAXHandler::registerParent | ( | const SumoXMLTag | tag, | |
| GenericSAXHandler * | handler | |||
| ) |
Assigning a parent handler which is enabled when the specified tag is closed.
Definition at line 160 of file GenericSAXHandler.cpp.
References myParentHandler, myParentIndicator, and XMLSubSys::setHandler().
Referenced by NLTriggerBuilder::parseAndBuildLaneSpeedTrigger().
00160 { 00161 myParentHandler = handler; 00162 myParentIndicator = tag; 00163 XMLSubSys::setHandler(*this); 00164 }
| void GenericSAXHandler::setFileName | ( | const std::string & | name | ) | throw () |
Sets the current file name.
| [in] | name | The name of the currently processed file |
Definition at line 72 of file GenericSAXHandler.cpp.
References myFileName.
Referenced by PCNetProjectionLoader::loadIfSet(), ROLoader::loadNet(), NIImporter_OpenStreetMap::loadNetwork(), NILoader::loadXMLType(), and traci::TraCIServer::TraCIServer().
00072 { 00073 myFileName = name; 00074 }
| void GenericSAXHandler::startElement | ( | const XMLCh *const | uri, | |
| const XMLCh *const | localname, | |||
| const XMLCh *const | qname, | |||
| const 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.
Definition at line 97 of file GenericSAXHandler.cpp.
References TplConvert< E >::_2str(), convertTag(), FileHelpers::getConfigurationRelative(), getFileName(), SUMOSAXAttributesImpl_Xerces::getString(), FileHelpers::isAbsolute(), myCharactersVector, myPredefinedTags, myPredefinedTagsMML, myStartElement(), XMLSubSys::runParser(), SUMO_ATTR_HREF, and SUMO_TAG_INCLUDE.
00100 { 00101 std::string name = TplConvert<XMLCh>::_2str(qname); 00102 SumoXMLTag element = convertTag(name); 00103 myCharactersVector.clear(); 00104 SUMOSAXAttributesImpl_Xerces na(attrs, myPredefinedTags, myPredefinedTagsMML); 00105 if (element == SUMO_TAG_INCLUDE) { 00106 std::string file = na.getString(SUMO_ATTR_HREF); 00107 if (!FileHelpers::isAbsolute(file)) { 00108 file = FileHelpers::getConfigurationRelative(getFileName(), file); 00109 } 00110 XMLSubSys::runParser(*this, file); 00111 } else { 00112 myStartElement(element, na); 00113 } 00114 }
std::vector<std::string> GenericSAXHandler::myCharactersVector [private] |
A list of character strings obtained so far to build the complete characters string at the end.
Definition at line 280 of file GenericSAXHandler.h.
Referenced by characters(), endElement(), and startElement().
std::string GenericSAXHandler::myFileName [private] |
The name of the currently parsed file.
Definition at line 283 of file GenericSAXHandler.h.
Referenced by getFileName(), and setFileName().
Definition at line 285 of file GenericSAXHandler.h.
Referenced by endElement(), and registerParent().
Definition at line 286 of file GenericSAXHandler.h.
Referenced by endElement(), and registerParent().
AttrMap GenericSAXHandler::myPredefinedTags [private] |
Definition at line 262 of file GenericSAXHandler.h.
Referenced by startElement(), and ~GenericSAXHandler().
std::map<SumoXMLAttr, std::string> GenericSAXHandler::myPredefinedTagsMML [private] |
the map from ids to their string representation
Definition at line 265 of file GenericSAXHandler.h.
Referenced by startElement().
TagMap GenericSAXHandler::myTagMap [private] |
1.5.6