NIXMLNodesHandler Class Reference

#include <NIXMLNodesHandler.h>

Inheritance diagram for NIXMLNodesHandler:

SUMOSAXHandler GenericSAXHandler

Detailed Description

Importer for network nodes stored in XML.

This SAX-handler parses node information and stores it in the given node container. Additionally, the given tls-container may be filled with additional information.

Definition at line 56 of file NIXMLNodesHandler.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
 NIXMLNodesHandler (NBNodeCont &nc, NBTrafficLightLogicCont &tlc, OptionsCont &options)
 Constructor.
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.
 ~NIXMLNodesHandler () 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.
virtual void myEndElement (SumoXMLTag element) throw (ProcessError)
 Callback method for a closing tag to implement by derived classes.
inherited from GenericSAXHandler
void myStartElement (SumoXMLTag element, const SUMOSAXAttributes &attrs) throw (ProcessError)
 Called on the opening of a tag; Parses node information.

Private Member Functions

 NIXMLNodesHandler (const NIXMLNodesHandler &s)
 invalid copy constructor
NIXMLNodesHandleroperator= (const NIXMLNodesHandler &s)
 invalid assignment operator
void processTrafficLightDefinitions (const SUMOSAXAttributes &attrs, NBNode *currentNode)
 Builds the defined traffic light or adds a node to it.

Private Attributes

std::string myID
 The id of the currently parsed node.
NBNodeContmyNodeCont
 The node container to add built nodes to.
OptionsContmyOptions
 A reference to the program's options.
Position2D myPosition
 The position of the currently parsed node.
NBTrafficLightLogicContmyTLLogicCont
 The traffic lights container to add built tls to.

Constructor & Destructor Documentation

NIXMLNodesHandler::NIXMLNodesHandler ( NBNodeCont nc,
NBTrafficLightLogicCont tlc,
OptionsCont options 
)

Constructor.

Parameters:
in,filled] nc The node container to fill
in,filled] tlc The traffic lights container to fill
[in] options The options to use
Todo:
Options are only given to determine whether "flip-y" is set; maybe this should be done by giving a bool
Todo:
Why are options not const?

Definition at line 57 of file NIXMLNodesHandler.cpp.

00060         : SUMOSAXHandler("xml-nodes - file"),
00061         myOptions(options),
00062         myNodeCont(nc), myTLLogicCont(tlc) {}

NIXMLNodesHandler::~NIXMLNodesHandler (  )  throw ()

Destructor.

Definition at line 65 of file NIXMLNodesHandler.cpp.

00065 {}

NIXMLNodesHandler::NIXMLNodesHandler ( const NIXMLNodesHandler s  )  [private]

invalid copy constructor


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 GenericSAXHandler::myEndElement ( SumoXMLTag  element  )  throw (ProcessError) [protected, virtual, inherited]

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

Called by "endElement" (see there).

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

00193 {}

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

Called on the opening of a tag; Parses node information.

Tries to parse a node. If the node can be parsed, it is stored within "myNodeCont". Otherwise an error is generated. Then, if givenm the tls information is parsed and inserted into "myTLLogicCont".

Parameters:
[in] element ID of the currently opened element
[in] attrs Attributes within the currently opened element
Exceptions:
ProcessError If something fails (not used herein) -policy No exception in order to allow further processing
Todo:
ProcessErrors are thrown when parsing traffic lights!?

Reimplemented from GenericSAXHandler.

Definition at line 69 of file NIXMLNodesHandler.cpp.

References OptionsCont::getBool(), NBNode::getControllingTLS(), MsgHandler::getErrorInstance(), NBNode::getPosition(), NBNode::getType(), MsgHandler::inform(), NBNodeCont::insert(), Position2D::mul(), myID, myNodeCont, myOptions, myPosition, myTLLogicCont, NBNode::NODETYPE_PRIORITY_JUNCTION, NBNode::NODETYPE_RIGHT_BEFORE_LEFT, NBNode::NODETYPE_TRAFFIC_LIGHT, NBNode::NODETYPE_UNKNOWN, processTrafficLightDefinitions(), NBNode::reinit(), NBTrafficLightLogicCont::remove(), NBNode::removeTrafficLights(), NBNodeCont::retrieve(), Position2D::set(), SUMO_ATTR_TYPE, SUMO_ATTR_X, SUMO_ATTR_Y, SUMO_TAG_NODE, WRITE_WARNING, Position2D::x(), GeoConvHelper::x2cartesian(), and Position2D::y().

00070                                                                                       {
00071     if (element!=SUMO_TAG_NODE) {
00072         return;
00073     }
00074     // get the id, report a warning if not given or empty...
00075     if (!attrs.setIDFromAttributes("node", myID), false) {
00076         WRITE_WARNING("No node id given... Skipping.");
00077         return;
00078     }
00079     NBNode *node = myNodeCont.retrieve(myID);
00080     // retrieve the position of the node
00081     bool ok = true;
00082     bool xOk = false;
00083     bool yOk = false;
00084     if (node!=0) {
00085         myPosition = node->getPosition();
00086         xOk = yOk = true;
00087     }
00088     if (attrs.hasAttribute(SUMO_ATTR_X)) {
00089         myPosition.set(attrs.getSUMORealReporting(SUMO_ATTR_X, "node", myID.c_str(), ok), myPosition.y());
00090         xOk = true;
00091     }
00092     if (attrs.hasAttribute(SUMO_ATTR_Y)) {
00093         myPosition.set(myPosition.x(), attrs.getSUMORealReporting(SUMO_ATTR_Y, "node", myID.c_str(), ok));
00094         yOk = true;
00095     }
00096     if (xOk&&yOk) {
00097         if (!GeoConvHelper::x2cartesian(myPosition)) {
00098             MsgHandler::getErrorInstance()->inform("Unable to project coordinates for node '" + myID + "'.");
00099         }
00100     } else {
00101         MsgHandler::getErrorInstance()->inform("Missing position (at node ID='" + myID + "').");
00102     }
00103     // check whether the y-axis shall be flipped
00104     if (myOptions.getBool("flip-y")) {
00105         myPosition.mul(1.0, -1.0);
00106     }
00107     // get the type
00108     NBNode::BasicNodeType type = NBNode::NODETYPE_UNKNOWN;
00109     if (node!=0) {
00110         type = node->getType();
00111     }
00112     if (attrs.hasAttribute(SUMO_ATTR_TYPE)) {
00113         std::string typeS = attrs.getOptStringReporting(SUMO_ATTR_TYPE, "node", myID.c_str(), ok, "");
00114         if (typeS=="priority") {
00115             type = NBNode::NODETYPE_PRIORITY_JUNCTION;
00116         } else if (typeS=="right_before_left") {
00117             type = NBNode::NODETYPE_RIGHT_BEFORE_LEFT;
00118         } else if (typeS=="traffic_light") {
00119             type = NBNode::NODETYPE_TRAFFIC_LIGHT;
00120         }
00121     }
00122     // check whether a prior node shall be modified
00123     if (node==0) {
00124         node = new NBNode(myID, myPosition, type);
00125         if (!myNodeCont.insert(node)) {
00126             throw ProcessError("Could not insert node though checked this before (id='" + myID + "').");
00127         }
00128     } else {
00129         // remove previously set tls if this node is not controlled by a tls
00130         std::set<NBTrafficLightDefinition*> tls = node->getControllingTLS();
00131         node->removeTrafficLights();
00132         for (std::set<NBTrafficLightDefinition*>::iterator i=tls.begin(); i!=tls.end(); ++i) {
00133             if ((*i)->getNodes().size()==0) {
00134                 myTLLogicCont.remove((*i)->getID());
00135             }
00136         }
00137         // patch information
00138         node->reinit(myPosition, type);
00139     }
00140     // process traffic light definition
00141     if (type==NBNode::NODETYPE_TRAFFIC_LIGHT) {
00142         processTrafficLightDefinitions(attrs, node);
00143     }
00144 }

NIXMLNodesHandler& NIXMLNodesHandler::operator= ( const NIXMLNodesHandler s  )  [private]

invalid assignment operator

void NIXMLNodesHandler::processTrafficLightDefinitions ( const SUMOSAXAttributes attrs,
NBNode currentNode 
) [private]

Builds the defined traffic light or adds a node to it.

Parameters:
[in] attrs Attributes within the currently opened node
[in] currentNode The built node to add the tls information to

Definition at line 148 of file NIXMLNodesHandler.cpp.

References NBTrafficLightDefinition::addControlledInnerEdges(), NBTrafficLightDefinition::addNode(), NBTrafficLightLogicCont::getDefinition(), SUMOSAXAttributes::getOptStringReporting(), NBTrafficLightLogicCont::insert(), myID, myTLLogicCont, SUMOSAXAttributes::parseStringVector(), SUMO_ATTR_CONTROLLED_INNER, and SUMO_ATTR_TLID.

Referenced by myStartElement().

00149                              {
00150     // try to get the tl-id
00151     // if a tl-id is given, we will look whether this tl already exists
00152     //  if so, we will add the node to it, otherwise allocate a new one with this id
00153     // if no tl-id exists, we will build a tl with the node's id
00154     NBTrafficLightDefinition *tlDef = 0;
00155     bool ok = true;
00156     std::string tlID = attrs.getOptStringReporting(SUMO_ATTR_TLID, "node", 0, ok, "");
00157     if (tlID!="") {
00158         // ok, the traffic light has a name
00159         tlDef = myTLLogicCont.getDefinition(tlID);
00160         if (tlDef==0) {
00161             // this traffic light is visited the first time
00162             tlDef = new NBOwnTLDef(tlID, currentNode);
00163             if (!myTLLogicCont.insert(tlDef)) {
00164                 // actually, nothing should fail here
00165                 delete tlDef;
00166                 throw ProcessError("Could not allocate tls '" + tlID + "'.");
00167             }
00168         } else {
00169             tlDef->addNode(currentNode);
00170         }
00171     } else {
00172         // ok, this node is a traffic light node where no other nodes
00173         //  participate
00174         tlDef = new NBOwnTLDef(myID, currentNode);
00175         if (!myTLLogicCont.insert(tlDef)) {
00176             // actually, nothing should fail here
00177             delete tlDef;
00178             throw ProcessError("Could not allocate tls '" + myID + "'.");
00179         }
00180     }
00181     // process inner edges which shall be controlled
00182     std::vector<std::string> controlledInner;
00183     SUMOSAXAttributes::parseStringVector(attrs.getOptStringReporting(SUMO_ATTR_CONTROLLED_INNER, "node", 0, ok, ""), controlledInner);
00184     if (controlledInner.size()!=0) {
00185         tlDef->addControlledInnerEdges(controlledInner);
00186     }
00187 }

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 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 NIXMLNodesHandler::myID [private]

The id of the currently parsed node.

Definition at line 111 of file NIXMLNodesHandler.h.

Referenced by myStartElement(), and processTrafficLightDefinitions().

The node container to add built nodes to.

Definition at line 117 of file NIXMLNodesHandler.h.

Referenced by myStartElement().

A reference to the program's options.

Definition at line 108 of file NIXMLNodesHandler.h.

Referenced by myStartElement().

The position of the currently parsed node.

Definition at line 114 of file NIXMLNodesHandler.h.

Referenced by myStartElement().

The traffic lights container to add built tls to.

Definition at line 120 of file NIXMLNodesHandler.h.

Referenced by myStartElement(), and processTrafficLightDefinitions().


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

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