PCLoaderXML Class Reference

#include <PCLoaderXML.h>

Inheritance diagram for PCLoaderXML:

SUMOSAXHandler GenericSAXHandler

Detailed Description

A reader for polygons and pois stored in XML-format.

Reads pois stored as XML definition. The definitions must match the format POLYCONVERT generates.

Definition at line 55 of file PCLoaderXML.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.
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.
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.

Static Public Member Functions

static void loadIfSet (OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm) throw (ProcessError)
 Loads pois/polygons assumed to be stored as XML.

Protected Member Functions

std::string buildErrorMessage (const SAXParseException &exception) throw ()
 Builds an error message.
virtual void myEndElement (SumoXMLTag element) throw (ProcessError)
 Callback method for a closing tag to implement by derived classes.
 PCLoaderXML (PCPolyContainer &toFill, PCTypeMap &tm, OptionsCont &oc) throw ()
 Constructor.
 ~PCLoaderXML () throw ()
 Destructor.
inherited from GenericSAXHandler
void myCharacters (SumoXMLTag element, const std::string &chars) throw (ProcessError)
 Called when characters occure.
virtual void myStartElement (SumoXMLTag element, const SUMOSAXAttributes &attrs) throw (ProcessError)
 Called on the opening of a tag;.

Private Attributes

PCPolyContainermyCont
 The container to store the converted polygons/pois into.
OptionsContmyOptions
 Settings to use.
PCTypeMapmyTypeMap
 The type map to use.
Temporary storages used when parsing polygons
RGBColor myCurrentColor
 The color of the currently parsed polygon.
std::string myCurrentID
 The id of the currently parsed polygon.
bool myCurrentIgnorePrunning
 Whether the current polygon must not be prunned.
int myCurrentLayer
 The layer of the currently parsed polygon.
std::string myCurrentType
 The type of the currently parsed polygon.

Constructor & Destructor Documentation

PCLoaderXML::PCLoaderXML ( PCPolyContainer toFill,
PCTypeMap tm,
OptionsCont oc 
) throw () [protected]

Constructor.

Parameters:
[in] toFill The poly/pois container to add loaded polys/pois to
[in] tm The type map to use for setting values of loaded polys/pois
[in] oc The options container to get further options from

Definition at line 85 of file PCLoaderXML.cpp.

00087         : SUMOSAXHandler("xml-poi-definition"),
00088         myCont(toFill), myTypeMap(tm), myOptions(oc) {}

PCLoaderXML::~PCLoaderXML (  )  throw () [protected]

Destructor.

Definition at line 91 of file PCLoaderXML.cpp.

00091 {}


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 PCLoaderXML::loadIfSet ( OptionsCont oc,
PCPolyContainer toFill,
PCTypeMap tm 
) throw (ProcessError) [static]

Loads pois/polygons assumed to be stored as XML.

If the option "xml" is set within the given options container, an instance of PCLoaderXML is built and used as a handler for the files given in this option.

Parameters:
[in] oc The options container to get further options from
[in] toFill The poly/pois container to add loaded polys/pois to
[in] tm The type map to use for setting values of loaded polys/pois
Exceptions:
ProcessError if something fails

Definition at line 60 of file PCLoaderXML.cpp.

References MsgHandler::beginProcessMsg(), MsgHandler::endProcessMsg(), FileHelpers::exists(), MsgHandler::getMessageInstance(), and XMLSubSys::runParser().

Referenced by main().

00061                                                           {
00062     if (!oc.isSet("xml")) {
00063         return;
00064     }
00065     PCLoaderXML handler(toFill, tm, oc);
00066     // parse file(s)
00067     std::vector<std::string> files = oc.getStringVector("xml");
00068     for (std::vector<std::string>::const_iterator file=files.begin(); file!=files.end(); ++file) {
00069         if (!FileHelpers::exists(*file)) {
00070             throw ProcessError("Could not open xml-file '" + *file + "'.");
00071         }
00072         MsgHandler::getMessageInstance()->beginProcessMsg("Parsing XML from '" + *file + "'...");
00073         if (!XMLSubSys::runParser(handler, *file)) {
00074             throw ProcessError();
00075         }
00076         MsgHandler::getMessageInstance()->endProcessMsg("done.");
00077     }
00078 }

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

Called when characters occure.

Parameters:
[in] element ID of the last opened element
[in] chars The read characters (complete)
Exceptions:
ProcessError If something fails
See also:
GenericSAXHandler::myCharacters

Reimplemented from GenericSAXHandler.

Definition at line 187 of file PCLoaderXML.cpp.

References Position2DVector::getCont(), MsgHandler::getErrorInstance(), MsgHandler::getWarningInstance(), MsgHandler::inform(), PCPolyContainer::insert(), myCont, myCurrentColor, myCurrentID, myCurrentIgnorePrunning, myCurrentLayer, myCurrentType, GeomConvHelper::parseShapeReporting(), Position2DVector::push_back(), SUMO_TAG_POLY, and GeoConvHelper::x2cartesian().

Referenced by myStartElement().

00188                                                                       {
00189     if (element==SUMO_TAG_POLY) {
00190         bool ok = true;
00191         Position2DVector pshape = GeomConvHelper::parseShapeReporting(chars, "poly", myCurrentID.c_str(), ok, false);
00192         if (!ok) {
00193             return;
00194         }
00195         const Position2DVector::ContType &cont = pshape.getCont();
00196         Position2DVector shape;
00197         for (Position2DVector::ContType::const_iterator i=cont.begin(); i!=cont.end(); ++i) {
00198             Position2D pos((*i));
00199             if (!GeoConvHelper::x2cartesian(pos)) {
00200                 MsgHandler::getWarningInstance()->inform("Unable to project coordinates for polygon '" + myCurrentID + "'.");
00201             }
00202             shape.push_back(pos);
00203         }
00204         Polygon2D *poly = new Polygon2D(myCurrentID, myCurrentType, myCurrentColor, shape, false);
00205         if (!myCont.insert(myCurrentID, poly, myCurrentLayer, myCurrentIgnorePrunning)) {
00206             MsgHandler::getErrorInstance()->inform("Polygon '" + myCurrentID + "' could not been added.");
00207             delete poly;
00208         }
00209     }
00210 }

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 PCLoaderXML::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 95 of file PCLoaderXML.cpp.

References PCTypeMap::TypeDef::color, PCTypeMap::TypeDef::discard, PCTypeMap::get(), MsgHandler::getErrorInstance(), OptionsCont::getInt(), OptionsCont::getOptions(), OptionsCont::getString(), MsgHandler::getWarningInstance(), PCTypeMap::has(), PCTypeMap::TypeDef::id, MsgHandler::inform(), PCPolyContainer::insert(), OptionsCont::isInStringVector(), PCTypeMap::TypeDef::layer, myCharacters(), myCont, myCurrentColor, myCurrentID, myCurrentIgnorePrunning, myCurrentLayer, myCurrentType, myOptions, myTypeMap, RGBColor::parseColor(), PCTypeMap::TypeDef::prefix, SUMO_ATTR_ID, SUMO_ATTR_SHAPE, SUMO_ATTR_TYPE, SUMO_ATTR_X, SUMO_ATTR_Y, SUMO_TAG_POI, SUMO_TAG_POLY, SUMOReal, and GeoConvHelper::x2cartesian().

00096                                                                                 {
00097     if (element!=SUMO_TAG_POI && element!=SUMO_TAG_POLY) {
00098         return;
00099     }
00100     if (element==SUMO_TAG_POI) {
00101         // get the id, report an error if not given or empty...
00102         std::string id;
00103         if (!attrs.setIDFromAttributes("poi", id)) {
00104             return;
00105         }
00106         bool ok = true;
00107         SUMOReal x = attrs.getSUMORealReporting(SUMO_ATTR_X, "poi", id.c_str(), ok);
00108         SUMOReal y = attrs.getSUMORealReporting(SUMO_ATTR_Y, "poi", id.c_str(), ok);
00109         std::string type = attrs.getOptStringReporting(SUMO_ATTR_TYPE, "poi", id.c_str(), ok, "");
00110         if (!ok) {
00111             return;
00112         }
00113         Position2D pos(x, y);
00114         if (!GeoConvHelper::x2cartesian(pos)) {
00115             MsgHandler::getWarningInstance()->inform("Unable to project coordinates for POI '" + id + "'.");
00116         }
00117         // patch the values
00118         bool discard = false;
00119         int layer = myOptions.getInt("layer");
00120         RGBColor color;
00121         if (myTypeMap.has(type)) {
00122             const PCTypeMap::TypeDef &def = myTypeMap.get(type);
00123             id = def.prefix + id;
00124             type = def.id;
00125             color = RGBColor::parseColor(def.color);
00126             discard = def.discard;
00127             layer = def.layer;
00128         } else {
00129             id = myOptions.getString("prefix") + id;
00130             type = myOptions.getString("type");
00131             color = RGBColor::parseColor(myOptions.getString("color"));
00132         }
00133         if (!discard) {
00134             bool ignorePrunning = false;
00135             if (OptionsCont::getOptions().isInStringVector("prune.ignore", id)) {
00136                 ignorePrunning = true;
00137             }
00138             PointOfInterest *poi = new PointOfInterest(id, type, pos, color);
00139             if (!myCont.insert(id, poi, layer, ignorePrunning)) {
00140                 MsgHandler::getErrorInstance()->inform("POI '" + id + "' could not been added.");
00141                 delete poi;
00142             }
00143         }
00144     }
00145     if (element==SUMO_TAG_POLY) {
00146         bool discard = false;
00147         int layer = myOptions.getInt("layer");
00148         bool ok = true;
00149         std::string id = attrs.getOptStringReporting(SUMO_ATTR_ID, "poly", myCurrentID.c_str(), ok, "");
00150         std::string type = attrs.getOptStringReporting(SUMO_ATTR_TYPE, "poly", myCurrentID.c_str(), ok, "");
00151         if (!ok) {
00152             return;
00153         }
00154         RGBColor color;
00155         if (myTypeMap.has(type)) {
00156             const PCTypeMap::TypeDef &def = myTypeMap.get(type);
00157             id = def.prefix + id;
00158             type = def.id;
00159             color = RGBColor::parseColor(def.color);
00160             discard = def.discard;
00161             layer = def.layer;
00162         } else {
00163             id = myOptions.getString("prefix") + id;
00164             type = myOptions.getString("type");
00165             color = RGBColor::parseColor(myOptions.getString("color"));
00166         }
00167         if (!discard) {
00168             bool ignorePrunning = false;
00169             if (OptionsCont::getOptions().isInStringVector("prune.ignore", id)) {
00170                 ignorePrunning = true;
00171             }
00172             myCurrentID = id;
00173             myCurrentType = type;
00174             myCurrentColor = color;
00175             myCurrentIgnorePrunning = ignorePrunning;
00176             myCurrentLayer = layer;
00177             if (attrs.hasAttribute(SUMO_ATTR_SHAPE)) {
00178                 // @deprecated At some time, no shape definition using characters will be allowed
00179                 myCharacters(element, attrs.getStringReporting(SUMO_ATTR_SHAPE, "poly", myCurrentID.c_str(), ok));
00180             }
00181         }
00182     }
00183 }

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

The container to store the converted polygons/pois into.

Definition at line 115 of file PCLoaderXML.h.

Referenced by myCharacters(), and myStartElement().

The color of the currently parsed polygon.

Definition at line 134 of file PCLoaderXML.h.

Referenced by myCharacters(), and myStartElement().

std::string PCLoaderXML::myCurrentID [private]

The id of the currently parsed polygon.

Definition at line 128 of file PCLoaderXML.h.

Referenced by myCharacters(), and myStartElement().

Whether the current polygon must not be prunned.

Definition at line 137 of file PCLoaderXML.h.

Referenced by myCharacters(), and myStartElement().

The layer of the currently parsed polygon.

Definition at line 140 of file PCLoaderXML.h.

Referenced by myCharacters(), and myStartElement().

std::string PCLoaderXML::myCurrentType [private]

The type of the currently parsed polygon.

Definition at line 131 of file PCLoaderXML.h.

Referenced by myCharacters(), and myStartElement().

Settings to use.

Definition at line 121 of file PCLoaderXML.h.

Referenced by myStartElement().

The type map to use.

Definition at line 118 of file PCLoaderXML.h.

Referenced by myStartElement().


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

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