Line data Source code
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 : /****************************************************************************/
14 : /// @file CommonHandler.cpp
15 : /// @author Pablo Alvarez Lopez
16 : /// @date Dec 2024
17 : ///
18 : // Collection of functions used in handlers
19 : /****************************************************************************/
20 : #include <config.h>
21 :
22 : #include <utils/common/MsgHandler.h>
23 : #include <utils/common/StringUtils.h>
24 : #include <utils/xml/XMLSubSys.h>
25 :
26 : #include "CommonHandler.h"
27 :
28 :
29 : // ===========================================================================
30 : // method definitions
31 : // ===========================================================================
32 :
33 0 : CommonHandler::CommonHandler() {
34 0 : }
35 :
36 :
37 0 : CommonHandler::~CommonHandler() {}
38 :
39 :
40 : bool
41 0 : CommonHandler::isErrorCreatingElement() const {
42 0 : return myErrorCreatingElement;
43 : }
44 :
45 :
46 : void
47 0 : CommonHandler::parseParameters(const SUMOSAXAttributes& attrs) {
48 : // declare Ok Flag
49 0 : bool parsedOk = true;
50 : // get key
51 0 : const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, parsedOk);
52 : // get SumoBaseObject parent
53 0 : CommonXMLStructure::SumoBaseObject* SumoBaseObjectParent = myCommonXMLStructure.getCurrentSumoBaseObject()->getParentSumoBaseObject();
54 : // check parent
55 0 : if ((SumoBaseObjectParent == nullptr) || (SumoBaseObjectParent->getTag() == SUMO_TAG_ROOTFILE)) {
56 0 : writeError(TL("Parameters must be defined within an object"));
57 0 : } else if (SumoBaseObjectParent->getTag() == SUMO_TAG_PARAM) {
58 0 : writeError(TL("Parameters cannot be defined within another parameter."));
59 0 : } else if ((SumoBaseObjectParent->getTag() != SUMO_TAG_NOTHING) && parsedOk) {
60 : // get tag str
61 0 : const std::string parentTagStr = toString(SumoBaseObjectParent->getTag());
62 : // circumventing empty string value
63 0 : const std::string value = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
64 : // show warnings if values are invalid
65 0 : if (key.empty()) {
66 0 : writeError(TLF("Error parsing key from % generic parameter. Key cannot be empty", parentTagStr));
67 0 : } else if (!SUMOXMLDefinitions::isValidParameterKey(key)) {
68 0 : writeError(TLF("Error parsing key from % generic parameter. Key contains invalid characters", parentTagStr));
69 : } else {
70 0 : WRITE_DEBUG("Inserting generic parameter '" + key + "|" + value + "' into " + parentTagStr);
71 : // insert parameter in SumoBaseObjectParent
72 0 : SumoBaseObjectParent->addParameter(key, value);
73 : }
74 : }
75 0 : }
76 :
77 :
78 : void
79 0 : CommonHandler::checkParsedParent(const SumoXMLTag currentTag, const std::vector<SumoXMLTag>& parentTags, bool& ok) {
80 0 : if (parentTags.size() > 0) {
81 : std::string tagsStr;
82 0 : for (auto it = parentTags.begin(); it != parentTags.end(); it++) {
83 0 : tagsStr.append(toString(*it));
84 0 : if ((it+1) != parentTags.end()) {
85 0 : if ((it+2) != parentTags.end()) {
86 0 : tagsStr.append(", ");
87 : } else {
88 0 : tagsStr.append(" or ");
89 : }
90 : }
91 : }
92 : // obtain parent
93 0 : CommonXMLStructure::SumoBaseObject* const parent = myCommonXMLStructure.getCurrentSumoBaseObject()->getParentSumoBaseObject();
94 0 : if (parent == nullptr) {
95 0 : ok = writeError(TLF("'%' must be defined within the definition of a %.", toString(currentTag), tagsStr));
96 0 : } else if ((parent->getTag() != SUMO_TAG_NOTHING) && std::find(parentTags.begin(), parentTags.end(), parent->getTag()) == parentTags.end()) {
97 0 : if (parent->hasStringAttribute(SUMO_ATTR_ID)) {
98 0 : 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 0 : ok = writeError(TLF("'%' must be defined within the definition of a '%' (found %).", toString(currentTag), tagsStr,
102 : toString(parent->getTag())));
103 : }
104 : }
105 : }
106 0 : }
107 :
108 :
109 : bool
110 0 : CommonHandler::checkListOfVehicleTypes(const SumoXMLTag tag, const std::string& id, const std::vector<std::string>& vTypeIDs) {
111 0 : for (const auto& vTypeID : vTypeIDs) {
112 0 : if (!SUMOXMLDefinitions::isValidTypeID(vTypeID)) {
113 0 : 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 :
120 : bool
121 0 : CommonHandler::checkNegative(const SumoXMLTag tag, const std::string& id, const SumoXMLAttr attribute, const int value, const bool canBeZero) {
122 0 : if (canBeZero) {
123 0 : if (value < 0) {
124 0 : 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 0 : if (value <= 0) {
130 0 : 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 :
138 : bool
139 0 : CommonHandler::checkNegative(const SumoXMLTag tag, const std::string& id, const SumoXMLAttr attribute, const double value, const bool canBeZero) {
140 0 : if (canBeZero) {
141 0 : if (value < 0) {
142 0 : 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 0 : if (value <= 0) {
148 0 : 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 :
156 : bool
157 0 : CommonHandler::checkNegative(const SumoXMLTag tag, const std::string& id, const SumoXMLAttr attribute, const SUMOTime value, const bool canBeZero) {
158 0 : if (canBeZero) {
159 0 : if (value < 0) {
160 0 : 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 0 : if (value <= 0) {
166 0 : 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 :
174 : bool
175 0 : CommonHandler::checkFileName(const SumoXMLTag tag, const std::string& id, const SumoXMLAttr attribute, const std::string &value) {
176 0 : if (SUMOXMLDefinitions::isValidFilename(value)) {
177 : return true;
178 : } else {
179 0 : return writeError(TLF("Could not build % with ID '%' in netedit; % is invalid % ()", toString(tag), id, toString(attribute), value));
180 : }
181 : }
182 :
183 :
184 : bool
185 0 : CommonHandler::checkValidAdditionalID(const SumoXMLTag tag, const std::string& value) {
186 0 : if (value.empty()) {
187 0 : return writeError(TLF("Could not build %; ID cannot be empty", toString(tag)));
188 0 : } else if (!SUMOXMLDefinitions::isValidVehicleID(value)) {
189 0 : 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 :
196 : bool
197 0 : CommonHandler::checkValidDetectorID(const SumoXMLTag tag, const std::string& value) {
198 0 : if (value.empty()) {
199 0 : return writeError(TLF("Could not build %; ID cannot be empty", toString(tag)));
200 0 : } else if (!SUMOXMLDefinitions::isValidDetectorID(value)) {
201 0 : 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 :
208 : bool
209 0 : CommonHandler::checkValidDemandElementID(const SumoXMLTag tag, const std::string& value) {
210 0 : if (value.empty()) {
211 0 : return writeError(TLF("Could not build %; ID cannot be empty", toString(tag)));
212 0 : } else if (!SUMOXMLDefinitions::isValidVehicleID(value)) {
213 0 : 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 :
220 : bool
221 0 : CommonHandler::writeError(const std::string& error) {
222 0 : WRITE_ERROR(error);
223 0 : myErrorCreatingElement = true;
224 0 : return false;
225 : }
226 :
227 :
228 : bool
229 0 : CommonHandler::writeErrorInvalidPosition(const SumoXMLTag tag, const std::string& id) {
230 0 : return writeError(TLF("Could not build % with ID '%' in netedit; Invalid position over lane.", toString(tag), id));
231 : }
232 :
233 :
234 : bool
235 0 : CommonHandler::writeErrorDuplicated(const SumoXMLTag tag, const std::string& id, const SumoXMLTag checkedTag) {
236 0 : return writeError(TLF("Could not build % with ID '%' in netedit; Found another % with the same ID.", toString(tag), id, toString(checkedTag)));
237 : }
238 :
239 :
240 : bool
241 0 : CommonHandler::writeErrorInvalidLanes(const SumoXMLTag tag, const std::string& id) {
242 0 : return writeError(TLF("Could not build % with ID '%' in netedit; List of lanes isn't valid.", toString(tag), id));
243 : }
244 :
245 :
246 : bool
247 0 : CommonHandler::writeErrorInvalidDistribution(const SumoXMLTag tag, const std::string& id) {
248 0 : return writeError(TLF("Could not build % with ID '%' in netedit; Distinct number of distribution values and probabilities.", toString(tag), id));
249 : }
250 :
251 :
252 : bool
253 0 : CommonHandler::writeErrorInvalidParent(const SumoXMLTag tag, const std::string& id, const SumoXMLTag parentTag, const std::string& parentID) {
254 0 : 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 :
258 : bool
259 0 : CommonHandler::writeErrorInvalidParent(const SumoXMLTag tag, const SumoXMLTag parentTag, const std::string& parentID) {
260 0 : return writeError(TLF("Could not build % in netedit; % parent with ID '%' doesn't exist.", toString(tag), toString(parentTag), parentID));
261 : }
262 :
263 :
264 : bool
265 0 : CommonHandler::writeErrorInvalidParent(const SumoXMLTag tag, const SumoXMLTag parentTag) {
266 0 : return writeError(TLF("Could not build % in netedit; % parent doesn't exist.", toString(tag), toString(parentTag)));
267 : }
268 :
269 : /****************************************************************************/
|