Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
DataHandler.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// The XML-Handler for data elements loading
19/****************************************************************************/
20#include <config.h>
21
24#include <utils/xml/XMLSubSys.h>
25
26#include "DataHandler.h"
27
28
29// ===========================================================================
30// method definitions
31// ===========================================================================
32
33DataHandler::DataHandler(const std::string& file) :
34 SUMOSAXHandler(file) {
35}
36
37
39
40
41bool
43 // run parser and return result
44 return XMLSubSys::runParser(*this, getFileName());
45}
46
47
48void
50 // switch tag
51 switch (obj->getTag()) {
52 // Stopping Places
58 break;
59 case SUMO_TAG_EDGE:
60 buildEdgeData(obj,
62 obj->getParameters());
63 break;
68 obj->getParameters());
69 break;
70 case SUMO_TAG_TAZREL:
74 obj->getParameters());
75 break;
76 default:
77 break;
78 }
79 // now iterate over childrens
80 for (const auto& child : obj->getSumoBaseObjectChildren()) {
81 // call this function recursively
83 }
84}
85
86
87void
89 // obtain tag
90 const SumoXMLTag tag = (element == 0) ? SUMO_TAG_ROOTFILE : static_cast<SumoXMLTag>(element);
91 // open SUMOBaseOBject
93 // check tag
94 try {
95 switch (tag) {
96 // interval
98 parseInterval(attrs);
99 break;
100 // datas
101 case SUMO_TAG_EDGE:
102 parseEdgeData(attrs);
103 break;
104 case SUMO_TAG_EDGEREL:
106 break;
107 case SUMO_TAG_TAZREL:
109 break;
110 case SUMO_TAG_PARAM:
111 WRITE_WARNING(TL("Data elements cannot load attributes as params"));
113 break;
114 default:
115 // tag cannot be parsed in routeHandler
117 break;
118 }
119 } catch (InvalidArgument& e) {
120 writeError(e.what());
121 }
122}
123
124
125void
127 // obtain tag
128 const SumoXMLTag tag = static_cast<SumoXMLTag>(element);
129 // get last inserted object
131 // close SUMOBaseOBject
133 // check tag
134 switch (tag) {
135 // only interval
137 // parse object and all their childrens
139 // delete object (and all of their childrens)
140 delete obj;
141 break;
142 default:
143 break;
144 }
145}
146
147
148bool
152
153
154void
155DataHandler::writeError(const std::string& error) {
158}
159
160
161void
163 // declare Ok Flag
164 bool parsedOk = true;
165 // needed attributes
166 const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
167 const double begin = attrs.get<double>(SUMO_ATTR_BEGIN, "", parsedOk);
168 const double end = attrs.get<double>(SUMO_ATTR_END, "", parsedOk);
169 // continue if flag is ok
170 if (parsedOk) {
171 // set tag
173 // add all attributes
177 }
178}
179
180
181void
183 // declare Ok Flag
184 bool parsedOk = true;
185 // needed attributes
186 const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
187 // fill attributes
188 getAttributes(attrs, {SUMO_ATTR_ID});
189 // continue if flag is ok
190 if (parsedOk) {
191 // set tag
193 // add all attributes
195 }
196}
197
198
199void
201 // declare Ok Flag
202 bool parsedOk = true;
203 // needed attributes
204 const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
205 const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
206 // fill attributes
208 // continue if flag is ok
209 if (parsedOk) {
210 // set tag
212 // add all attributes
215 }
216}
217
218
219void
221 // declare Ok Flag
222 bool parsedOk = true;
223 // needed attributes
224 const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
225 const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
226 // fill attributes
228 // continue if flag is ok
229 if (parsedOk) {
230 // set tag
232 // add all attributes
235 }
236}
237
238
239void
240DataHandler::getAttributes(const SUMOSAXAttributes& attrs, const std::vector<SumoXMLAttr> avoidAttributes) const {
241 // transform avoidAttributes to strings
242 std::vector<std::string> avoidAttributesStr;
243 for (const SumoXMLAttr& avoidAttribute : avoidAttributes) {
244 avoidAttributesStr.push_back(toString(avoidAttribute));
245 }
246 // iterate over attributes and fill parameters map
247 for (const std::string& attribute : attrs.getAttributeNames()) {
248 if (std::find(avoidAttributesStr.begin(), avoidAttributesStr.end(), attribute) == avoidAttributesStr.end()) {
250 }
251 }
252}
253
254
255void
256DataHandler::checkParent(const SumoXMLTag currentTag, const SumoXMLTag parentTag, bool& ok) {
257 // check that parent SUMOBaseObject's tag is the parentTag
260 writeError(toString(currentTag) + " must be defined within the definition of a " + toString(parentTag));
261 ok = false;
262 }
263}
264
265/****************************************************************************/
#define WRITE_ERROR(msg)
Definition MsgHandler.h:304
#define WRITE_WARNING(msg)
Definition MsgHandler.h:295
#define TL(string)
Definition MsgHandler.h:315
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_INTERVAL
an aggreagated-output interval
@ SUMO_TAG_EDGEREL
a relation between two edges
@ SUMO_TAG_ROOTFILE
root file
@ SUMO_TAG_PARAM
parameter associated to a certain key
@ SUMO_TAG_TAZREL
a relation between two TAZs
@ SUMO_TAG_EDGE
begin/end of the description of an edge
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_BEGIN
weights: time range begin
@ SUMO_ATTR_TO
@ SUMO_ATTR_FROM
@ SUMO_ATTR_END
weights: time range end
@ SUMO_ATTR_ID
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
void setTag(const SumoXMLTag tag)
set SumoBaseObject tag
SumoBaseObject * getParentSumoBaseObject() const
get pointer to mySumoBaseObjectParent SumoBaseObject (if is null, then is the root)
const std::map< std::string, std::string > & getParameters() const
get parameters
SumoXMLTag getTag() const
get XML myTag
void addParameter(const std::string &key, const std::string &value)
add parameter into current SumoBaseObject node
void addDoubleAttribute(const SumoXMLAttr attr, const double value)
add double attribute into current SumoBaseObject node
void addStringAttribute(const SumoXMLAttr attr, const std::string &value)
add string attribute into current SumoBaseObject node
double getDoubleAttribute(const SumoXMLAttr attr) const
get double attribute
const std::string & getStringAttribute(const SumoXMLAttr attr) const
get string attribute
const std::vector< SumoBaseObject * > & getSumoBaseObjectChildren() const
get SumoBaseObject children
void abortSUMOBaseOBject()
abort SUMOBaseOBject
CommonXMLStructure::SumoBaseObject * getCurrentSumoBaseObject() const
get current editedSumoBaseObject
void openSUMOBaseOBject()
open SUMOBaseOBject
void closeSUMOBaseOBject()
close SUMOBaseOBject
DataHandler(const std::string &file)
Constructor.
void parseTAZRelationData(const SUMOSAXAttributes &attrs)
parse TAZRelationData attributes
~DataHandler()
Destructor.
bool myErrorCreatingElement
flag for check if a element wasn't created
void parseSumoBaseObject(CommonXMLStructure::SumoBaseObject *obj)
parse SumoBaseObject (it's called recursivelly)
bool parse()
parse
void writeError(const std::string &error)
write error and enable error creating element
void parseEdgeRelationData(const SUMOSAXAttributes &attrs)
parse edgeRelationData attributes
virtual void myEndElement(int element)
Called when a closing tag occurs.
void checkParent(const SumoXMLTag currentTag, const SumoXMLTag parentTag, bool &ok)
check parents
virtual void buildTAZRelationData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &fromTAZID, const std::string &toTAZID, const Parameterised::Map &parameters)=0
Builds TAZRelationData.
void parseEdgeData(const SUMOSAXAttributes &attrs)
parse edgeData attributes
virtual void buildDataInterval(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &dataSetID, const double begin, const double end)=0
Builds DataInterval.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
virtual void buildEdgeData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &edgeID, const Parameterised::Map &parameters)=0
Builds edgeData.
void getAttributes(const SUMOSAXAttributes &attrs, const std::vector< SumoXMLAttr > avoidAttributes) const
parse attributes as parameters
CommonXMLStructure myCommonXMLStructure
common XML Structure
void parseInterval(const SUMOSAXAttributes &attrs)
virtual void buildEdgeRelationData(const CommonXMLStructure::SumoBaseObject *sumoBaseObject, const std::string &fromEdgeID, const std::string &toEdgeID, const Parameterised::Map &parameters)=0
Builds edgeRelationData.
bool isErrorCreatingElement() const
get flag for check if a element wasn't created
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
const std::string & getFileName() const
returns the current file name
Encapsulated SAX-Attributes.
virtual std::vector< std::string > getAttributeNames() const =0
Retrieves all attribute names.
virtual std::string getStringSecure(int id, const std::string &def) const =0
Returns the string-value of the named (by its enum-value) attribute.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
SAX-handler base for SUMO-files.
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.