SAXWeightsHandler Class Reference

#include <SAXWeightsHandler.h>

Inheritance diagram for SAXWeightsHandler:

SUMOSAXHandler GenericSAXHandler

Detailed Description

An XML-handler for network weights.

As network weights are used both in the simulation and the routers, a base class for loading them was built. Instances of this class should be supplied with at least one definition about what shall be retrieved (ToRetrieveDefinition, defined as inner class) which also contains the information about the retriever (EdgeFloatTimeLineRetriever, defined as inner class).

The ToRetrieveDefinition names the attribute which the SAXWeightsHandler shall parse and reporte. Within the parsed xml-file these attributes may be embedded in "lane" or "edge" elements, one for each edge or for each lane (see below). These elements should be embedded in interval-tags which specify the time the weight is valid at. The boolean "edgeBased" tells SAXWeightsHandler whether the weights are supplied on edge- or on lane-basis (whether it shall parse the "edge" or the "lane" elements).

Examples for files the SAXWeightsHandler can handle are the edgedump and the lanedump generated by the simulation.

The EdgeFloatTimeLineRetriever to which read values will be reported should have the method "addEdgeWeight" implemented. It wil be supplied with the current edge name, the interval the weight is valid for and the value.

Definition at line 73 of file SAXWeightsHandler.h.


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.
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.
 SAXWeightsHandler (ToRetrieveDefinition *def, const std::string &file)
 Constructor.
 SAXWeightsHandler (const std::vector< ToRetrieveDefinition * > &defs, const std::string &file)
 Constructor.
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.
 ~SAXWeightsHandler () throw ()
 Destructor.
SAX ErrorHandler callbacks
void error (const SAXParseException &exception) throw (ProcessError)
 Handler for XML-errors.
void fatalError (const SAXParseException &exception) throw (ProcessError)
 Handler for XML-errors.
void warning (const SAXParseException &exception) throw ()
 Handler for XML-warnings.

Protected Member Functions

std::string buildErrorMessage (const SAXParseException &exception) throw ()
 Builds an error message.
virtual void myCharacters (SumoXMLTag element, const std::string &chars) throw (ProcessError)
 Callback method for characters to implement by derived classes.
inherited from GenericSAXHandler
void myEndElement (SumoXMLTag elemente) throw ()
 Called when a closing tag occurs.
void myStartElement (SumoXMLTag element, const SUMOSAXAttributes &attrs) throw (ProcessError)
 Called on the opening of a tag;.

Private Member Functions

SAXWeightsHandleroperator= (const SAXWeightsHandler &src)
 we made the assignment operator invalid
 SAXWeightsHandler (const SAXWeightsHandler &src)
 we made the copy constructor invalid
void tryParse (const SUMOSAXAttributes &attrs, bool isEdge)
 Parses the efforts of a lane for the previously read times.

Private Attributes

std::string myCurrentEdgeID
 the edge which is currently being processed
SUMOReal myCurrentTimeBeg
 the begin of the time period that is currently processed
SUMOReal myCurrentTimeEnd
 the end of the time period that is currently processed
std::vector
< ToRetrieveDefinition * > 
myDefinitions
 List of definitions what shall be read and whereto stored while parsing the file.

Data Structures

class  EdgeFloatTimeLineRetriever
 Interface for a class which obtains read weights for named edges. More...
class  ToRetrieveDefinition
 Complete definition about what shall be retrieved and where to store it. More...

Constructor & Destructor Documentation

SAXWeightsHandler::SAXWeightsHandler ( const std::vector< ToRetrieveDefinition * > &  defs,
const std::string &  file 
)

Constructor.

Gets a list of retriever definitions. Please note that the retrievers are not deleted!

Definition at line 65 of file SAXWeightsHandler.cpp.

00067         : SUMOSAXHandler(file),
00068         myCurrentTimeBeg(-1), myCurrentTimeEnd(-1),
00069         myDefinitions(defs) {}

SAXWeightsHandler::SAXWeightsHandler ( ToRetrieveDefinition def,
const std::string &  file 
)

Constructor.

Gets a single definition. Please note that the retrievers are not deleted!

Definition at line 72 of file SAXWeightsHandler.cpp.

References myDefinitions.

00074         : SUMOSAXHandler(file),
00075         myCurrentTimeBeg(-1), myCurrentTimeEnd(-1) {
00076     myDefinitions.push_back(def);
00077 }

SAXWeightsHandler::~SAXWeightsHandler (  )  throw ()

Destructor.

Definition at line 80 of file SAXWeightsHandler.cpp.

References myDefinitions.

00080                                               {
00081     std::vector<ToRetrieveDefinition*>::iterator i;
00082     for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00083         delete *i;
00084     }
00085 }

SAXWeightsHandler::SAXWeightsHandler ( const SAXWeightsHandler src  )  [private]

we made the copy constructor invalid


Member Function Documentation

std::string SUMOSAXHandler::buildErrorMessage ( const SAXParseException &  exception  )  throw () [protected, inherited]

Builds an error message.

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

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

Definition at line 55 of file SUMOSAXHandler.cpp.

References GenericSAXHandler::getFileName().

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

00055                                                                             {
00056     std::ostringstream buf;
00057     char *pMsg = XMLString::transcode(exception.getMessage());
00058     buf << pMsg << std::endl;
00059     buf << " In file '" << getFileName() << "'" << std::endl;
00060     buf << " At line/column " << exception.getLineNumber()+1
00061     << '/' << exception.getColumnNumber() << "." << std::endl;
00062     XMLString::release(&pMsg);
00063     return buf.str();
00064 }

void GenericSAXHandler::characters ( const XMLCh *const   chars,
const XERCES3_SIZE_t  length 
) [inherited]

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.

Todo:
recheck/describe what happens with characters when a new element is opened
Todo:
describe characters processing in the class' head

Definition at line 168 of file GenericSAXHandler.cpp.

References GenericSAXHandler::myCharactersVector.

00169                                                            {
00170     myCharactersVector.push_back(TplConvert<XMLCh>::_2str(chars, length));
00171 }

void GenericSAXHandler::endElement ( const XMLCh *const  uri,
const XMLCh *const  localname,
const XMLCh *const   qname 
) [inherited]

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
Todo:
do not generate and report the string-representation

Definition at line 118 of file GenericSAXHandler.cpp.

References TplConvert< E >::_2str(), GenericSAXHandler::convertTag(), GenericSAXHandler::myCharacters(), GenericSAXHandler::myCharactersVector, GenericSAXHandler::myEndElement(), GenericSAXHandler::myParentHandler, GenericSAXHandler::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 }

void SUMOSAXHandler::error ( const SAXParseException &  exception  )  throw (ProcessError) [inherited]

Handler for XML-errors.

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

Parameters:
[in] exception The occured exception to process
Exceptions:
ProcessError On any call

Definition at line 74 of file SUMOSAXHandler.cpp.

References SUMOSAXHandler::buildErrorMessage().

00074                                                                             {
00075     throw ProcessError(buildErrorMessage(exception));
00076 }

void SUMOSAXHandler::fatalError ( const SAXParseException &  exception  )  throw (ProcessError) [inherited]

Handler for XML-errors.

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

Exceptions:
ProcessError On any call
Parameters:
[in] exception The occured exception to process

Definition at line 80 of file SUMOSAXHandler.cpp.

References SUMOSAXHandler::buildErrorMessage().

00080                                                                                  {
00081     throw ProcessError(buildErrorMessage(exception));
00082 }

const std::string & GenericSAXHandler::getFileName (  )  const throw () [inherited]

void GenericSAXHandler::myCharacters ( SumoXMLTag  element,
const std::string &  chars 
) throw (ProcessError) [protected, virtual, inherited]

Callback method for characters to implement by derived classes.

Called by "endElement" (see there).

Parameters:
[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 GenericSAXHandler::endElement().

00189 {}

void SAXWeightsHandler::myEndElement ( SumoXMLTag  elemente  )  throw () [protected, virtual]

Called when a closing tag occurs.

Parameters:
[in] element ID of the currently opened element
Exceptions:
ProcessError If something fails
See also:
GenericSAXHandler::myEndElement

Reimplemented from GenericSAXHandler.

Definition at line 153 of file SAXWeightsHandler.cpp.

References myCurrentEdgeID, myCurrentTimeBeg, myCurrentTimeEnd, myDefinitions, SUMO_TAG_EDGE, and SUMOReal.

00153                                                           {
00154     if (element==SUMO_TAG_EDGE) {
00155         std::vector<ToRetrieveDefinition*>::iterator i;
00156         for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00157             if ((*i)->myHadAttribute) {
00158                 (*i)->myDestination.addEdgeWeight(myCurrentEdgeID,
00159                                                   (*i)->myAggValue/(SUMOReal)(*i)->myNoLanes,
00160                                                   myCurrentTimeBeg, myCurrentTimeEnd);
00161             }
00162         }
00163     }
00164 }

void SAXWeightsHandler::myStartElement ( SumoXMLTag  element,
const SUMOSAXAttributes attrs 
) throw (ProcessError) [protected, virtual]

Called on the opening of a tag;.

Parameters:
[in] element ID of the currently opened element
[in] attrs Attributes within the currently opened element
Exceptions:
ProcessError If something fails
See also:
GenericSAXHandler::myStartElement

Reimplemented from GenericSAXHandler.

Definition at line 88 of file SAXWeightsHandler.cpp.

References myCurrentEdgeID, myCurrentTimeBeg, myCurrentTimeEnd, SUMO_ATTR_BEGIN, SUMO_ATTR_END, SUMO_ATTR_ID, SUMO_TAG_EDGE, SUMO_TAG_INTERVAL, SUMO_TAG_LANE, and tryParse().

00089                                                                                            {
00090     switch (element) {
00091     case SUMO_TAG_INTERVAL: {
00092         bool ok = true;
00093         myCurrentTimeBeg = attrs.getSUMORealReporting(SUMO_ATTR_BEGIN, "weights/interval", 0, ok);
00094         myCurrentTimeEnd = attrs.getSUMORealReporting(SUMO_ATTR_END, "weights/interval", 0, ok);
00095     }
00096     break;
00097     case SUMO_TAG_EDGE: {
00098         bool ok = true;
00099         myCurrentEdgeID = attrs.getOptStringReporting(SUMO_ATTR_ID, 0, 0, ok, "");
00100         tryParse(attrs, true);
00101     }
00102     break;
00103     case SUMO_TAG_LANE: {
00104         tryParse(attrs, false);
00105     }
00106     break;
00107     default:
00108         break;
00109     }
00110 }

SAXWeightsHandler& SAXWeightsHandler::operator= ( const SAXWeightsHandler src  )  [private]

we made the assignment operator invalid

void GenericSAXHandler::registerParent ( const SumoXMLTag  tag,
GenericSAXHandler handler 
) [inherited]

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

Definition at line 160 of file GenericSAXHandler.cpp.

References GenericSAXHandler::myParentHandler, GenericSAXHandler::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 () [inherited]

Sets the current file name.

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

Definition at line 72 of file GenericSAXHandler.cpp.

References GenericSAXHandler::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 
) [inherited]

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
Todo:
do not generate and report the string-representation

Definition at line 97 of file GenericSAXHandler.cpp.

References TplConvert< E >::_2str(), GenericSAXHandler::convertTag(), FileHelpers::getConfigurationRelative(), GenericSAXHandler::getFileName(), SUMOSAXAttributesImpl_Xerces::getString(), FileHelpers::isAbsolute(), GenericSAXHandler::myCharactersVector, GenericSAXHandler::myPredefinedTags, GenericSAXHandler::myPredefinedTagsMML, GenericSAXHandler::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 }

void SAXWeightsHandler::tryParse ( const SUMOSAXAttributes attrs,
bool  isEdge 
) [private]

Parses the efforts of a lane for the previously read times.

Definition at line 114 of file SAXWeightsHandler.cpp.

References MsgHandler::getErrorInstance(), SUMOSAXAttributes::getFloat(), SUMOSAXAttributes::hasAttribute(), MsgHandler::inform(), myCurrentEdgeID, myCurrentTimeBeg, myDefinitions, and toString().

Referenced by myStartElement().

00114                                                                        {
00115     // !!!! no error handling!
00116     std::vector<ToRetrieveDefinition*>::iterator i;
00117     if (isEdge) {
00118         // process all that want values directly from the edge
00119         for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00120             if ((*i)->myAmEdgeBased) {
00121                 if (attrs.hasAttribute((*i)->myAttributeName)) {
00122                     (*i)->myAggValue = attrs.getFloat((*i)->myAttributeName);
00123                     (*i)->myNoLanes = 1;
00124                     (*i)->myHadAttribute = true;
00125                 } else {
00126                     (*i)->myHadAttribute = false;
00127                 }
00128             } else {
00129                 (*i)->myAggValue = 0;
00130                 (*i)->myNoLanes = 0;
00131             }
00132         }
00133     } else {
00134         // process the current lane values
00135         for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00136             if (!(*i)->myAmEdgeBased) {
00137                 try {
00138                     (*i)->myAggValue += attrs.getFloat((*i)->myAttributeName);
00139                     ++((*i)->myNoLanes);
00140                     (*i)->myHadAttribute = true;
00141                 } catch (EmptyData &) {
00142                     MsgHandler::getErrorInstance()->inform("Missing value '" + (*i)->myAttributeName + "' in edge '" + myCurrentEdgeID + "'.");
00143                 } catch (NumberFormatException &) {
00144                     MsgHandler::getErrorInstance()->inform("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID + "' at time step " + toString(myCurrentTimeBeg) + ".");
00145                 }
00146             }
00147         }
00148     }
00149 }

void SUMOSAXHandler::warning ( const SAXParseException &  exception  )  throw () [inherited]

Handler for XML-warnings.

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

Parameters:
[in] exception The occured exception to process

Definition at line 68 of file SUMOSAXHandler.cpp.

References SUMOSAXHandler::buildErrorMessage(), MsgHandler::getWarningInstance(), and MsgHandler::inform().


Field Documentation

std::string SAXWeightsHandler::myCurrentEdgeID [private]

the edge which is currently being processed

Definition at line 204 of file SAXWeightsHandler.h.

Referenced by myEndElement(), myStartElement(), and tryParse().

the begin of the time period that is currently processed

Definition at line 198 of file SAXWeightsHandler.h.

Referenced by myEndElement(), myStartElement(), and tryParse().

the end of the time period that is currently processed

Definition at line 201 of file SAXWeightsHandler.h.

Referenced by myEndElement(), and myStartElement().

List of definitions what shall be read and whereto stored while parsing the file.

Definition at line 195 of file SAXWeightsHandler.h.

Referenced by myEndElement(), SAXWeightsHandler(), tryParse(), and ~SAXWeightsHandler().


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

Generated on Wed May 5 00:07:00 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6