Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
CommonHandler.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// Collection of functions used in handlers
19/****************************************************************************/
20#include <config.h>
21
24#include <utils/xml/XMLSubSys.h>
25
26#include "CommonHandler.h"
27
28
29// ===========================================================================
30// method definitions
31// ===========================================================================
32
35
36
38
39
40bool
44
45
46void
48 // declare Ok Flag
49 bool parsedOk = true;
50 // get key
51 const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, parsedOk);
52 // get SumoBaseObject parent
54 // check parent
55 if ((SumoBaseObjectParent == nullptr) || (SumoBaseObjectParent->getTag() == SUMO_TAG_ROOTFILE)) {
56 writeError(TL("Parameters must be defined within an object"));
57 } else if (SumoBaseObjectParent->getTag() == SUMO_TAG_PARAM) {
58 writeError(TL("Parameters cannot be defined within another parameter."));
59 } else if ((SumoBaseObjectParent->getTag() != SUMO_TAG_NOTHING) && parsedOk) {
60 // get tag str
61 const std::string parentTagStr = toString(SumoBaseObjectParent->getTag());
62 // circumventing empty string value
63 const std::string value = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
64 // show warnings if values are invalid
65 if (key.empty()) {
66 writeError(TLF("Error parsing key from % generic parameter. Key cannot be empty", parentTagStr));
68 writeError(TLF("Error parsing key from % generic parameter. Key contains invalid characters", parentTagStr));
69 } else {
70 WRITE_DEBUG("Inserting generic parameter '" + key + "|" + value + "' into " + parentTagStr);
71 // insert parameter in SumoBaseObjectParent
72 SumoBaseObjectParent->addParameter(key, value);
73 }
74 }
75}
76
77
78void
79CommonHandler::checkParsedParent(const SumoXMLTag currentTag, const std::vector<SumoXMLTag>& parentTags, bool& ok) {
80 if (parentTags.size() > 0) {
81 std::string tagsStr;
82 for (auto it = parentTags.begin(); it != parentTags.end(); it++) {
83 tagsStr.append(toString(*it));
84 if ((it+1) != parentTags.end()) {
85 if ((it+2) != parentTags.end()) {
86 tagsStr.append(", ");
87 } else {
88 tagsStr.append(" or ");
89 }
90 }
91 }
92 // obtain parent
94 if (parent == nullptr) {
95 ok = writeError(TLF("'%' must be defined within the definition of a %.", toString(currentTag), tagsStr));
96 } else if ((parent->getTag() != SUMO_TAG_NOTHING) && std::find(parentTags.begin(), parentTags.end(), parent->getTag()) == parentTags.end()) {
97 if (parent->hasStringAttribute(SUMO_ATTR_ID)) {
98 ok = writeError(TLF("'%' must be defined within the definition of a '%' (found % '%').", toString(currentTag), tagsStr,
99 toString(parent->getTag()), parent->getStringAttribute(SUMO_ATTR_ID)));
100 } else {
101 ok = writeError(TLF("'%' must be defined within the definition of a '%' (found %).", toString(currentTag), tagsStr,
102 toString(parent->getTag())));
103 }
104 }
105 }
106}
107
108
109bool
110CommonHandler::checkListOfVehicleTypes(const SumoXMLTag tag, const std::string& id, const std::vector<std::string>& vTypeIDs) {
111 for (const auto& vTypeID : vTypeIDs) {
112 if (!SUMOXMLDefinitions::isValidTypeID(vTypeID)) {
113 return writeError(TLF("Could not build % with ID '%' in netedit; '%' ist not a valid vType ID.", toString(tag), id, vTypeID));
114 }
115 }
116 return true;
117}
118
119
120bool
121CommonHandler::checkNegative(const SumoXMLTag tag, const std::string& id, const SumoXMLAttr attribute, const int value, const bool canBeZero) {
122 if (canBeZero) {
123 if (value < 0) {
124 return writeError(TLF("Could not build % with ID '%' in netedit; Attribute % cannot be negative.", toString(tag), id, toString(attribute)));
125 } else {
126 return true;
127 }
128 } else {
129 if (value <= 0) {
130 return writeError(TLF("Could not build % with ID '%' in netedit; Attribute % must be greather than zero.", toString(tag), id, toString(attribute)));
131 } else {
132 return true;
133 }
134 }
135}
136
137
138bool
139CommonHandler::checkNegative(const SumoXMLTag tag, const std::string& id, const SumoXMLAttr attribute, const double value, const bool canBeZero) {
140 if (canBeZero) {
141 if (value < 0) {
142 return writeError(TLF("Could not build % with ID '%' in netedit; Attribute % cannot be negative (%).", toString(tag), id, toString(attribute), toString(value)));
143 } else {
144 return true;
145 }
146 } else {
147 if (value <= 0) {
148 return writeError(TLF("Could not build % with ID '%' in netedit; Attribute % must be greather than zero (%).", toString(tag), id, toString(attribute), toString(value)));
149 } else {
150 return true;
151 }
152 }
153}
154
155
156bool
157CommonHandler::checkNegative(const SumoXMLTag tag, const std::string& id, const SumoXMLAttr attribute, const SUMOTime value, const bool canBeZero) {
158 if (canBeZero) {
159 if (value < 0) {
160 return writeError(TLF("Could not build % with ID '%' in netedit; Attribute % cannot be negative (%).", toString(tag), id, toString(attribute), time2string(value)));
161 } else {
162 return true;
163 }
164 } else {
165 if (value <= 0) {
166 return writeError(TLF("Could not build % with ID '%' in netedit; Attribute % must be greather than zero (%).", toString(tag), id, toString(attribute), time2string(value)));
167 } else {
168 return true;
169 }
170 }
171}
172
173
174bool
175CommonHandler::checkFileName(const SumoXMLTag tag, const std::string& id, const SumoXMLAttr attribute, const std::string &value) {
177 return true;
178 } else {
179 return writeError(TLF("Could not build % with ID '%' in netedit; % is invalid % ()", toString(tag), id, toString(attribute), value));
180 }
181}
182
183
184bool
185CommonHandler::checkValidAdditionalID(const SumoXMLTag tag, const std::string& value) {
186 if (value.empty()) {
187 return writeError(TLF("Could not build %; ID cannot be empty", toString(tag)));
188 } else if (!SUMOXMLDefinitions::isValidVehicleID(value)) {
189 return writeError(TLF("Could not build % with ID '%' in netedit; ID contains invalid characters.", toString(tag), value));
190 } else {
191 return true;
192 }
193}
194
195
196bool
197CommonHandler::checkValidDetectorID(const SumoXMLTag tag, const std::string& value) {
198 if (value.empty()) {
199 return writeError(TLF("Could not build %; ID cannot be empty", toString(tag)));
200 } else if (!SUMOXMLDefinitions::isValidDetectorID(value)) {
201 return writeError(TLF("Could not build % with ID '%' in netedit; detector ID contains invalid characters.", toString(tag), value));
202 } else {
203 return true;
204 }
205}
206
207
208bool
209CommonHandler::checkValidDemandElementID(const SumoXMLTag tag, const std::string& value) {
210 if (value.empty()) {
211 return writeError(TLF("Could not build %; ID cannot be empty", toString(tag)));
212 } else if (!SUMOXMLDefinitions::isValidVehicleID(value)) {
213 return writeError(TLF("Could not build % with ID '%' in netedit; ID contains invalid characters.", toString(tag), value));
214 } else {
215 return true;
216 }
217}
218
219
220bool
221CommonHandler::writeError(const std::string& error) {
222 WRITE_ERROR(error);
224 return false;
225}
226
227
228bool
229CommonHandler::writeErrorInvalidPosition(const SumoXMLTag tag, const std::string& id) {
230 return writeError(TLF("Could not build % with ID '%' in netedit; Invalid position over lane.", toString(tag), id));
231}
232
233
234bool
235CommonHandler::writeErrorDuplicated(const SumoXMLTag tag, const std::string& id, const SumoXMLTag checkedTag) {
236 return writeError(TLF("Could not build % with ID '%' in netedit; Found another % with the same ID.", toString(tag), id, toString(checkedTag)));
237}
238
239
240bool
241CommonHandler::writeErrorInvalidLanes(const SumoXMLTag tag, const std::string& id) {
242 return writeError(TLF("Could not build % with ID '%' in netedit; List of lanes isn't valid.", toString(tag), id));
243}
244
245
246bool
248 return writeError(TLF("Could not build % with ID '%' in netedit; Distinct number of distribution values and probabilities.", toString(tag), id));
249}
250
251
252bool
253CommonHandler::writeErrorInvalidParent(const SumoXMLTag tag, const std::string& id, const SumoXMLTag parentTag, const std::string& parentID) {
254 return writeError(TLF("Could not build % with ID '%' in netedit; % parent with ID '%' doesn't exist.", toString(tag), id, toString(parentTag), parentID));
255}
256
257
258bool
259CommonHandler::writeErrorInvalidParent(const SumoXMLTag tag, const SumoXMLTag parentTag, const std::string& parentID) {
260 return writeError(TLF("Could not build % in netedit; % parent with ID '%' doesn't exist.", toString(tag), toString(parentTag), parentID));
261}
262
263
264bool
266 return writeError(TLF("Could not build % in netedit; % parent doesn't exist.", toString(tag), toString(parentTag)));
267}
268
269/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_DEBUG(msg)
Definition MsgHandler.h:306
#define WRITE_ERROR(msg)
Definition MsgHandler.h:304
#define TL(string)
Definition MsgHandler.h:315
#define TLF(string,...)
Definition MsgHandler.h:317
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:69
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_NOTHING
invalid tag, must be the last one
@ SUMO_TAG_ROOTFILE
root file
@ SUMO_TAG_PARAM
parameter associated to a certain key
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_VALUE
@ SUMO_ATTR_ID
@ SUMO_ATTR_KEY
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
bool writeErrorInvalidDistribution(const SumoXMLTag tag, const std::string &id)
write error "invalid distribution"
virtual ~CommonHandler()
Destructor.
void checkParsedParent(const SumoXMLTag currentTag, const std::vector< SumoXMLTag > &parentTags, bool &ok)
bool writeError(const std::string &error)
write error and enable error creating element
bool myErrorCreatingElement
flag for mark if a element wasn't created
CommonHandler()
Constructor.
bool checkValidDetectorID(const SumoXMLTag tag, const std::string &value)
check if the given detector ID is valid
bool writeErrorInvalidParent(const SumoXMLTag tag, const std::string &id, const SumoXMLTag parentTag, const std::string &parentID)
write error "invalid parent element" giving ids of current and parent element
bool checkListOfVehicleTypes(const SumoXMLTag tag, const std::string &id, const std::vector< std::string > &vTypeIDs)
check list of IDs
void parseParameters(const SUMOSAXAttributes &attrs)
parse generic parameters
bool writeErrorDuplicated(const SumoXMLTag tag, const std::string &id, const SumoXMLTag checkedTag)
write error "duplicated additional"
bool checkValidAdditionalID(const SumoXMLTag tag, const std::string &value)
check if the given additional ID is valid
bool checkFileName(const SumoXMLTag tag, const std::string &id, const SumoXMLAttr attribute, const std::string &value)
check if the given filename is valid
bool writeErrorInvalidPosition(const SumoXMLTag tag, const std::string &id)
write error "invalid position"
CommonXMLStructure myCommonXMLStructure
common XML Structure
bool writeErrorInvalidLanes(const SumoXMLTag tag, const std::string &id)
write error "invalid list of lanes"
bool checkNegative(const SumoXMLTag tag, const std::string &id, const SumoXMLAttr attribute, const int value, const bool canBeZero)
check if the given int value is NOT negative
bool checkValidDemandElementID(const SumoXMLTag tag, const std::string &value)
check if the given demand elmement ID is valid
bool isErrorCreatingElement() const
get flag for mark if a element wasn't created
bool hasStringAttribute(const SumoXMLAttr attr) const
has function
SumoBaseObject * getParentSumoBaseObject() const
get pointer to mySumoBaseObjectParent SumoBaseObject (if is null, then is the root)
SumoXMLTag getTag() const
get XML myTag
const std::string & getStringAttribute(const SumoXMLAttr attr) const
get string attribute
CommonXMLStructure::SumoBaseObject * getCurrentSumoBaseObject() const
get current editedSumoBaseObject
Encapsulated SAX-Attributes.
virtual std::string getString(int id, bool *isPresent=nullptr) 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.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
static bool isValidTypeID(const std::string &value)
whether the given string is a valid id for an edge or vehicle type
static bool isValidVehicleID(const std::string &value)
whether the given string is a valid id for a vehicle or flow
static bool isValidFilename(const std::string &value)
whether the given string is a valid attribute for a filename (for example, a name)
static bool isValidDetectorID(const std::string &value)
whether the given string is a valid id for an detector
static bool isValidParameterKey(const std::string &value)
whether the given string is a valid key for a parameter