SUMO - Simulation of Urban MObility
MSTriggeredXMLReader.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // The basic class for classes that read XML-triggers
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 // ===========================================================================
00024 // included modules
00025 // ===========================================================================
00026 #ifdef _MSC_VER
00027 #include <windows_config.h>
00028 #else
00029 #include <config.h>
00030 #endif
00031 
00032 #include <string>
00033 #include <xercesc/sax2/SAX2XMLReader.hpp>
00034 #include <xercesc/sax/SAXException.hpp>
00035 #include <xercesc/sax/SAXParseException.hpp>
00036 #include <utils/common/TplConvert.h>
00037 #include <utils/common/MsgHandler.h>
00038 #include <utils/common/UtilExceptions.h>
00039 #include <utils/xml/SUMOSAXHandler.h>
00040 #include <utils/xml/XMLSubSys.h>
00041 #include <microsim/MSEventControl.h>
00042 #include "MSTriggeredReader.h"
00043 #include "MSTriggeredXMLReader.h"
00044 #include <utils/common/WrappingCommand.h>
00045 
00046 #ifdef CHECK_MEMORY_LEAKS
00047 #include <foreign/nvwa/debug_new.h>
00048 #endif // CHECK_MEMORY_LEAKS
00049 
00050 
00051 // ===========================================================================
00052 // method definitions
00053 // ===========================================================================
00054 MSTriggeredXMLReader::MSTriggeredXMLReader(MSNet& net,
00055         const std::string& filename)
00056     : MSTriggeredReader(net),
00057       SUMOSAXHandler(filename),
00058       myParser(0), myHaveMore(true) {
00059     Command* c = new WrappingCommand< MSTriggeredReader >(this, &MSTriggeredReader::wrappedExecute);
00060     MSNet::getInstance()->getInsertionEvents().addEvent(c, net.getCurrentTimeStep(), MSEventControl::NO_CHANGE);
00061 }
00062 
00063 
00064 MSTriggeredXMLReader::~MSTriggeredXMLReader() {}
00065 
00066 
00067 bool
00068 MSTriggeredXMLReader::readNextTriggered() {
00069     while (myHaveMore && myParser->parseNext(myToken)) {
00070         if (nextRead()) {
00071             return true;
00072         }
00073     }
00074     myHaveMore = false;
00075     return false;
00076 }
00077 
00078 
00079 void
00080 MSTriggeredXMLReader::myInit() {
00081     try {
00082         myParser = XMLSubSys::getSAXReader(*this);
00083         if (!myParser->parseFirst(getFileName().c_str(), myToken)) {
00084             throw ProcessError("Can not read XML-file '" + getFileName() + "'.");
00085 
00086         }
00087     } catch (SAXException& e) {
00088         throw ProcessError(TplConvert<XMLCh>::_2str(e.getMessage()));
00089 
00090     } catch (XMLException& e) {
00091         throw ProcessError(TplConvert<XMLCh>::_2str(e.getMessage()));
00092 
00093     }
00094 
00095     if (readNextTriggered()) {
00096         if (myOffset < MSNet::getInstance()->getCurrentTimeStep()) {
00097             myOffset = MSNet::getInstance()->getCurrentTimeStep() + 1;
00098             // !!! Warning?
00099         }
00100     } else {
00101         myHaveMore = false;
00102     }
00103 }
00104 
00105 
00106 
00107 /****************************************************************************/
00108 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines